X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/e48d2870f91b769680b0cd12895066a94c4131aa..ff51d51af5fe1df973d276c12ba9bdac84d94382:/pycco-rs?ds=sidebyside diff --git a/pycco-rs b/pycco-rs index e6199d9..d250b20 100755 --- a/pycco-rs +++ b/pycco-rs @@ -1,11 +1,17 @@ -#!/usr/bin/python +#!/usr/bin/env python # A little wrapper around pycco, to add Rust support. -import pycco +import pycco, pycco_resources from pygments import lexers, formatters -import re -pycco.main.languages[".rs"] = { "name": "rust", "symbol": "//"} +import sys, re + +# helper functions +def patch_html(source, marker, new_text): + '''Find the [marker] in [source], and insert [new_text] after it.''' + assert source.count(marker) == 1 + return source.replace(marker, marker + new_text, 1) -# need to re-build this stuff... +# now, monkey-patch pycco for Rust support +pycco.main.languages[".rs"] = { "name": "rust", "symbol": "//"} for ext, l in pycco.main.languages.items(): # Does the line begin with a comment? l["comment_matcher"] = re.compile(r"^\s*" + l["symbol"] + "\s?") @@ -18,4 +24,26 @@ for ext, l in pycco.main.languages.items(): # Get the Pygments Lexer for this language. l["lexer"] = lexers.get_lexer_by_name(l["name"]) +# and monkey-patch the function generating the output to do some post-processing +generate_documentation_orig = pycco.main.generate_documentation +generate_documentation_called = False +def generate_documentation(*args, **kwargs): + global generate_documentation_called + generate_documentation_called = True + result = generate_documentation_orig(*args, **kwargs) + # now patch it + result = patch_html(result, '', + '') + result = patch_html(result, '', 'Rust-101: ') + ## remove empty code blocks + result = re.sub('''<div class='code'> + *<div class="highlight"><pre>(<span></span>)?</pre></div> + *</div>''', '<!-- empty code block -->', result) + # done + return result +pycco.main.generate_documentation = generate_documentation + +# call pycco +assert len(sys.argv) == 2 pycco.main.main() +assert generate_documentation_called