summaryrefslogtreecommitdiff
path: root/python/helpers/epydoc_formatter.py
blob: 3d30e1ee3ba43762832e014d437f40962e2846ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import sys
from epydoc.markup import DocstringLinker
from epydoc.markup.epytext import parse_docstring, ParseError, _colorize
import epydoc.markup.epytext

def _add_para(doc, para_token, stack, indent_stack, errors):
  """Colorize the given paragraph, and add it to the DOM tree."""
  para = _colorize(doc, para_token, errors)
  if para_token.inline:
    para.attribs['inline'] = True
  stack[-1].children.append(para)

epydoc.markup.epytext._add_para = _add_para

def is_fatal():
  return False

ParseError.is_fatal = is_fatal

try:
  src = sys.stdin.read()
  errors = []

  class EmptyLinker(DocstringLinker):
    def translate_indexterm(self, indexterm):
      return ""

    def translate_identifier_xref(self, identifier, label=None):
      return identifier

  docstring = parse_docstring(src, errors)
  docstring, fields = docstring.split_fields()
  html = docstring.to_html(EmptyLinker())

  if errors and not html:
    sys.stderr.write("Error parsing docstring:\n")
    for error in errors:
      sys.stderr.write(str(error) + "\n")
    sys.exit(1)

  sys.stdout.write(html)
  sys.stdout.flush()
except:
  exc_type, exc_value, exc_traceback = sys.exc_info()
  sys.stderr.write("Error calculating docstring: " + str(exc_value))
  sys.exit(1)