3 from docutils.core import publish_string
4 from docutils import nodes
5 from docutils.nodes import Text, field_body, field_name
6 from docutils.writers.html4css1 import HTMLTranslator
7 from epydoc.markup import DocstringLinker
8 from epydoc.markup.restructuredtext import ParsedRstDocstring, _EpydocHTMLTranslator, \
9 _DocumentPseudoWriter, _EpydocReader
12 class RestHTMLTranslator(_EpydocHTMLTranslator):
13 def visit_field_name(self, node):
16 atts['class'] = 'docinfo-name'
18 atts['class'] = 'field-name'
20 self.context.append('')
21 atts['align'] = "right"
22 self.body.append(self.starttag(node, 'th', '', **atts))
24 def visit_field_body(self, node):
25 self.body.append(self.starttag(node, 'td', '', CLASS='field-body'))
26 parent_text = node.parent[0][0].astext()
27 if hasattr(node.parent, "type"):
29 self.body.append(self.starttag(node, 'a', '',
30 href='psi_element://#typename#' + node.parent.type))
31 self.body.append(node.parent.type)
32 self.body.append("</a>")
33 self.body.append(") ")
34 elif parent_text.startswith("type "):
35 index = parent_text.index("type ")
36 type_string = parent_text[index + 5]
37 self.body.append(self.starttag(node, 'a', '',
38 href='psi_element://#typename#' + type_string))
39 elif parent_text.startswith("rtype"):
40 type_string = node.children[0][0].astext()
41 self.body.append(self.starttag(node, 'a', '',
42 href='psi_element://#typename#' + type_string))
44 self.set_class_on_child(node, 'first', 0)
46 if (self.compact_field_list or
47 isinstance(field.parent, nodes.docinfo) or
48 field.parent.index(field) == len(field.parent) - 1):
49 # If we are in a compact list, the docinfo, or if this is
50 # the last field of the field list, do not add vertical
51 # space after last element.
52 self.set_class_on_child(node, 'last', -1)
54 def depart_field_body(self, node):
55 if node.parent[0][0].astext().startswith("type "):
56 self.body.append("</a>")
57 HTMLTranslator.depart_field_body(self, node)
59 def visit_reference(self, node):
62 atts['href'] = node['refuri']
63 if self.settings.cloak_email_addresses and atts['href'].startswith('mailto:'):
64 atts['href'] = self.cloak_mailto(atts['href'])
66 # atts['class'] += ' external'
68 assert 'refid' in node, 'References must have "refuri" or "refid" attribute.'
69 atts['href'] = '#' + node['refid']
70 atts['class'] += ' internal'
71 if not isinstance(node.parent, nodes.TextElement):
72 assert len(node) == 1 and isinstance(node[0], nodes.image)
73 atts['class'] += ' image-reference'
74 self.body.append(self.starttag(node, 'a', '', **atts))
76 def starttag(self, node, tagname, suffix='\n', **attributes):
77 attr_dicts = [attributes]
78 if isinstance(node, nodes.Node):
79 attr_dicts.append(node.attributes)
80 if isinstance(node, dict):
81 attr_dicts.append(node)
82 # Munge each attribute dictionary. Unfortunately, we need to
83 # iterate through attributes one at a time because some
84 # versions of docutils don't case-normalize attributes.
85 for attr_dict in attr_dicts:
86 # For some reason additional classes in bullet list make it render poorly.
87 # Such lists are used to render multiple return values in Numpy docstrings by Napoleon.
88 if tagname == 'ul' and isinstance(node.parent, field_body):
89 attr_dict.pop('class', None)
90 attr_dict.pop('classes', None)
93 for (key, val) in attr_dict.items():
94 # Prefix all CSS classes with "rst-"; and prefix all
95 # names with "rst-" to avoid conflicts.
96 if key.lower() in ('class', 'id', 'name'):
97 attr_dict[key] = 'rst-%s' % val
98 elif key.lower() in ('classes', 'ids', 'names'):
99 attr_dict[key] = ['rst-%s' % cls for cls in val]
100 elif key.lower() == 'href':
101 if attr_dict[key][:1] == '#':
102 attr_dict[key] = '#rst-%s' % attr_dict[key][1:]
104 if tagname == 'th' and isinstance(node, field_name):
105 attributes['valign'] = 'top'
107 # For headings, use class="heading"
108 if re.match(r'^h\d+$', tagname):
109 attributes['class'] = ' '.join([attributes.get('class', ''), 'heading']).strip()
111 return HTMLTranslator.starttag(self, node, tagname, suffix, **attributes)
113 def visit_field_list(self, node):
115 for n in node.children:
118 child = n.children[0]
119 rawsource = child.rawsource
120 if rawsource.startswith("param "):
121 index = rawsource.index("param ")
122 if not child.children:
124 # Strip leading escaped asterisks for vararg parameters in Google code style docstrings
125 trimmed_name = re.sub(r'\\\*', '*', rawsource[index + 6:])
126 child.children[0] = Text(trimmed_name)
127 fields[trimmed_name] = n
128 if rawsource == "return":
131 for n in node.children:
134 child = n.children[0]
135 rawsource = child.rawsource
136 if rawsource.startswith("type "):
137 index = rawsource.index("type ")
138 name = re.sub(r'\\\*', '*', rawsource[index + 5:])
140 fields[name].type = n.children[1][0][0]
141 node.children.remove(n)
142 if rawsource == "rtype":
143 if "return" in fields:
144 fields["return"].type = n.children[1][0][0]
145 node.children.remove(n)
147 HTMLTranslator.visit_field_list(self, node)
149 def unknown_visit(self, node):
150 """ Ignore unknown nodes """
152 def unknown_departure(self, node):
153 """ Ignore unknown nodes """
155 def visit_problematic(self, node):
156 """Don't insert hyperlinks to nowhere for e.g. unclosed asterisks."""
157 # Note that children text elements will be visited anyway
159 def depart_problematic(self, node):
162 def visit_block_quote(self, node):
163 self.body.append(self.emptytag(node, "br"))
165 def depart_block_quote(self, node):
168 def visit_literal(self, node):
169 """Process text to prevent tokens from wrapping."""
170 self.body.append(self.starttag(node, 'tt', '', CLASS='docutils literal'))
172 for token in self.words_and_spaces.findall(text):
174 self.body.append('<code>%s</code>'
175 % self.encode(token))
176 elif token in ('\n', ' '):
177 # Allow breaks at whitespace:
178 self.body.append(token)
180 # Protect runs of multiple spaces; the last space can wrap:
181 self.body.append(' ' * (len(token) - 1) + ' ')
182 self.body.append('</tt>')
186 class MyParsedRstDocstring(ParsedRstDocstring):
187 def __init__(self, document):
188 ParsedRstDocstring.__init__(self, document)
190 def to_html(self, docstring_linker, directory=None,
191 docindex=None, context=None, **options):
192 visitor = RestHTMLTranslator(self._document, docstring_linker,
193 directory, docindex, context)
194 self._document.walkabout(visitor)
195 return ''.join(visitor.body)
198 def parse_docstring(docstring, errors, **options):
199 writer = _DocumentPseudoWriter()
200 reader = _EpydocReader(errors) # Outputs errors to the list.
201 publish_string(docstring, writer=writer, reader=reader,
202 settings_overrides={'report_level': 10000,
204 'warning_stream': None})
205 return MyParsedRstDocstring(writer.document)
210 src = sys.stdin.read() if text is None else text
214 class EmptyLinker(DocstringLinker):
215 def translate_indexterm(self, indexterm):
218 def translate_identifier_xref(self, identifier, label=None):
221 docstring = parse_docstring(src, errors)
222 html = docstring.to_html(EmptyLinker())
224 if errors and not html:
225 sys.stderr.write("Error parsing docstring:\n")
227 sys.stderr.write(str(error) + "\n")
230 sys.stdout.write(html)
233 exc_type, exc_value, exc_traceback = sys.exc_info()
234 sys.stderr.write("Error calculating docstring: " + str(exc_value))
238 if __name__ == '__main__':