Chromium Code Reviews| Index: tools/md_browser/md_browser.py |
| diff --git a/tools/md_browser/md_browser.py b/tools/md_browser/md_browser.py |
| index 40b0db9b17e74d4b975e1641026f2dfa724847df..62758e4e10c0e2a602475607418b4887e09668d1 100755 |
| --- a/tools/md_browser/md_browser.py |
| +++ b/tools/md_browser/md_browser.py |
| @@ -17,11 +17,12 @@ import sys |
| import threading |
| import time |
| import webbrowser |
| +from xml.etree import ElementTree |
| THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| SRC_DIR = os.path.dirname(os.path.dirname(THIS_DIR)) |
| -sys.path.append(os.path.join(SRC_DIR, 'third_party', 'Python-Markdown')) |
| +sys.path.insert(0, os.path.join(SRC_DIR, 'third_party', 'Python-Markdown')) |
| import markdown |
| @@ -155,10 +156,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
| } |
| contents = self._Read(path[1:]) |
| - md_fragment = markdown.markdown(contents, |
| - extensions=extensions, |
| - extension_configs=extension_configs, |
| - output_format='html4').encode('utf-8') |
| + |
| + md = markdown.Markdown(extensions=extensions, |
| + extension_configs=extension_configs, |
| + output_format='html4') |
| + md.treeprocessors['adjust_toc'] = _AdjustTOC() |
| + |
| + md_fragment = md.convert(contents).encode('utf-8') |
| + |
| try: |
| self._WriteHeader('text/html') |
| self._WriteTemplate('header.html') |
| @@ -199,5 +204,47 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
| self.wfile.write(contents.encode('utf-8')) |
| + |
| +class _AdjustTOC(markdown.treeprocessors.Treeprocessor): |
| + def run(self, tree): |
| + # A table of contents with a single H1 header is translated to |
| + # <div class='toc'> |
| + # <ul> |
| + # <li> |
| + # <a>H1> |
| + # <ul> |
| + # <li>first H2 |
| + # <li>second H2</li></ul></li><ul> |
| + # |
| + # We want to replace the first ul with the second one. |
| + # |
| + # Also, we want to insert an <h2>Contents and then indent |
|
jsbell
2016/11/07 17:32:06
Can you depict the "after" structure as you did th
Dirk Pranke
2016/11/07 18:17:27
Will do.
|
| + # the rest in a <div class='toc-aux'>. |
| + toc_nodes = tree.findall(".//*[@class='toc']") |
| + if toc_nodes: |
|
jsbell
2016/11/07 17:32:06
Maybe early return if falsy, to reduce indentation
Dirk Pranke
2016/11/07 18:17:27
Good suggestion, will do.
|
| + if len(toc_nodes) == 1: |
| + toc = toc_nodes[0] |
| + toc_ul = toc[0] |
| + toc_ul_li = toc_ul[0] |
| + toc_ul_li_ul = toc_ul_li[1] |
| + del toc[0] |
| + children = toc_ul_li_ul |
| + else: |
| + children = list(toc) |
|
jsbell
2016/11/07 17:32:06
What is toc in this else branch, and below if this
Dirk Pranke
2016/11/07 18:17:27
See above, will fix.
|
| + |
| + while len(toc): |
| + toc.remove[0] |
| + contents = ElementTree.SubElement(toc, 'h2') |
| + contents.text = 'Contents' |
| + contents.tail = '\n' |
| + toc_aux = ElementTree.SubElement(toc, 'div', attrib={'class': 'toc-aux'}) |
| + toc_aux.text = '\n' |
| + toc_aux_ul = ElementTree.SubElement(toc_aux, 'ul') |
| + toc_aux_ul.text = '\n' |
| + toc_aux_ul.extend(children) |
| + toc_aux_ul.tail = '\n' |
| + toc_aux.tail = '\n' |
| + |
| + |
| if __name__ == '__main__': |
| sys.exit(main(sys.argv[1:])) |