|
| 1 | +import logging |
| 2 | +import re, copy |
| 3 | +import lxml |
| 4 | + |
| 5 | +from lxml.html import builder |
| 6 | + |
| 7 | +from showdocs import structs |
| 8 | +from showdocs.filters import common |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +class CleanHtmlFilter(common.Filter): |
| 13 | + def process(self): |
| 14 | + for e in self.root.cssselect('.sect1 > h2'): |
| 15 | + if e.text.lower() == 'configuration file': |
| 16 | + return e.getparent() |
| 17 | + |
| 18 | + raise ValueError("couldn't find 'configuration file' section") |
| 19 | + |
| 20 | +class AnnotatingFilter(common.Filter): |
| 21 | + patterns = {'alias.*': 'section.alias', ' (deprecated)': ''} |
| 22 | + |
| 23 | + def _addoptionsforsection(self, root, section): |
| 24 | + for e in root.cssselect('dt.hdlist1'): |
| 25 | + self.handled.add(e) |
| 26 | + name = e.text_content().lower() |
| 27 | + self._spanify(e, '%s.%s' % (section, name), structs.decorate.BACK) |
| 28 | + |
| 29 | + def _spanify(self, e, group, decoration): |
| 30 | + assert e.tag == 'dt', 'expected tag dt, got %r' % e.tag |
| 31 | + |
| 32 | + # Wrap the inner html of e in a <span> because the <dt> stretches to |
| 33 | + # 100% width which messes up the back decoration. |
| 34 | + span = copy.deepcopy(e) |
| 35 | + span.tag = 'span' |
| 36 | + span.set('data-showdocs', group) |
| 37 | + span.classes.add(decoration) |
| 38 | + |
| 39 | + attrs = e.items() |
| 40 | + e.clear() |
| 41 | + for k, v in attrs: |
| 42 | + e.set(k, v) |
| 43 | + e.append(span) |
| 44 | + |
| 45 | + def process(self): |
| 46 | + self.handled = set() |
| 47 | + |
| 48 | + # Go over top level options. |
| 49 | + for e in self.root.cssselect('.sect2 > .dlist > dl > dt.hdlist1'): |
| 50 | + if e in self.handled: |
| 51 | + continue |
| 52 | + |
| 53 | + name = e.text_content().lower() |
| 54 | + |
| 55 | + # Replace any patterns found in name. |
| 56 | + for substring, replacewith in self.patterns.iteritems(): |
| 57 | + if substring in name: |
| 58 | + name = name.replace(substring, replacewith) |
| 59 | + break |
| 60 | + |
| 61 | + # Most options take this simple form. |
| 62 | + m = re.match(r'(\w+)\.(<\w+>\.)?(\w+)$', name) |
| 63 | + if m: |
| 64 | + self.handled.add(e) |
| 65 | + |
| 66 | + # Get rid of the subsection and set the group name to be |
| 67 | + # section.option-name. |
| 68 | + section, subsection, key = m.groups() |
| 69 | + self._spanify(e, '%s.%s' % (section, key), |
| 70 | + structs.decorate.BACK) |
| 71 | + elif name == 'advice.*': |
| 72 | + self.handled.add(e) |
| 73 | + self._addoptionsforsection(e.getnext(), 'advice') |
| 74 | + else: |
| 75 | + logger.warn("didn't annotate %r", e.text_content()) |
0 commit comments