2 # A little wrapper around pycco, to add Rust support.
3 import pycco, pycco_resources
4 from pygments import lexers, formatters
8 def patch_html(source, marker, new_text):
9 '''Find the [marker] in [source], and insert [new_text] after it.'''
10 assert source.count(marker) == 1
11 return source.replace(marker, marker + new_text, 1)
13 # now, monkey-patch pycco for Rust support
14 pycco.main.supported_languages[".rs"] = { "name": "rust", "comment_symbol": "//"}
15 for ext, l in pycco.main.supported_languages.items():
16 # Does the line begin with a comment?
17 l["comment_matcher"] = re.compile(r"^\s*" + l["comment_symbol"] + "\s?")
18 # The dividing token we feed into Pygments, to delimit the boundaries between
20 l["divider_text"] = "\n" + l["comment_symbol"] + "DIVIDER\n"
21 # The mirror of `divider_text` that we expect Pygments to return. We can split
22 # on this to recover the original sections.
23 l["divider_html"] = re.compile(r'\n*<span class="c[1]?">' + l["comment_symbol"] + 'DIVIDER</span>\n*')
24 # Get the Pygments Lexer for this language.
25 l["lexer"] = lexers.get_lexer_by_name(l["name"])
27 # and monkey-patch the function generating the output to do some post-processing
28 generate_documentation_orig = pycco.main.generate_documentation
29 generate_documentation_called = False
30 def generate_documentation(*args, **kwargs):
31 global generate_documentation_called
32 generate_documentation_called = True
33 result = generate_documentation_orig(*args, **kwargs)
35 result = patch_html(result, b'<link rel="stylesheet" href="pycco.css">',
36 b'<link rel="stylesheet" href="pycco_custom.css"><meta name="viewport" content="width=device-width">')
37 result = patch_html(result, b'<title>', b'Rust-101: ')
38 ## remove empty code blocks
39 result = re.sub(b'''<div class='code'>
40 *<div class="highlight"><pre>(<span></span>)?</pre></div>
41 *</div>''', b'<!-- empty code block -->', result)
44 pycco.main.generate_documentation = generate_documentation
47 assert len(sys.argv) == 2
49 assert generate_documentation_called