fix pycco-rs to get rid of empty code blocks
authorRalf Jung <post@ralfj.de>
Wed, 18 May 2016 08:27:53 +0000 (10:27 +0200)
committerRalf Jung <post@ralfj.de>
Wed, 18 May 2016 08:27:53 +0000 (10:27 +0200)
pycco-rs

index b1d7f43d6274da6379f7e7afa2b4686b2f6f2bb6..d250b20e1e36976028ec2e653f6df7749a3abc3a 100755 (executable)
--- a/pycco-rs
+++ b/pycco-rs
@@ -2,7 +2,13 @@
 # A little wrapper around pycco, to add Rust support.
 import pycco, pycco_resources
 from pygments import lexers, formatters
-import re
+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)
 
 # now, monkey-patch pycco for Rust support
 pycco.main.languages[".rs"] = { "name": "rust", "symbol": "//"}
@@ -17,19 +23,27 @@ for ext, l in pycco.main.languages.items():
     l["divider_html"] = re.compile(r'\n*<span class="c[1]?">' + l["symbol"] + 'DIVIDER</span>\n*')
     # Get the Pygments Lexer for this language.
     l["lexer"] = lexers.get_lexer_by_name(l["name"])
-# and monkey-patch for a custom CSS file
-html_src = pycco_resources.html
-
-css_marker = '<link rel="stylesheet" href="{{ stylesheet }}">'
-custom_css = '<link rel="stylesheet" href="pycco_custom.css"><meta name="viewport" content="width=device-width">'
-assert html_src.count(css_marker) == 1
-html_src = html_src.replace(css_marker, css_marker+custom_css, 1)
-
-title_marker = '<title>'
-assert html_src.count(title_marker) == 1
-html_src = html_src.replace(title_marker, title_marker + 'Rust-101: ', 1)
 
-pycco_resources.html = html_src
-pycco.main.pycco_template = pycco_resources.pycco_template = pycco_resources.template(pycco_resources.html)
+# 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, '<link rel="stylesheet" href="pycco.css">',
+                        '<link rel="stylesheet" href="pycco_custom.css"><meta name="viewport" content="width=device-width">')
+    result = patch_html(result, '<title>', '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