PY-20125 Epydoc formatter also uses explicit UTF-8 encoding to communicate with the IDE
[idea/community.git] / python / helpers / epydoc_formatter.py
1 import sys
2
3 import epydoc.markup.epytext
4 from epydoc.markup import DocstringLinker
5 from epydoc.markup.epytext import parse_docstring, ParseError, _colorize
6 from rest_formatter import read_safe, print_safe
7
8
9 def _add_para(doc, para_token, stack, indent_stack, errors):
10     """Colorize the given paragraph, and add it to the DOM tree."""
11     para = _colorize(doc, para_token, errors)
12     if para_token.inline:
13         para.attribs['inline'] = True
14     stack[-1].children.append(para)
15
16
17 epydoc.markup.epytext._add_para = _add_para
18 ParseError.is_fatal = lambda self: False
19
20 src = read_safe()
21 errors = []
22
23
24 class EmptyLinker(DocstringLinker):
25     def translate_indexterm(self, indexterm):
26         return ""
27
28     def translate_identifier_xref(self, identifier, label=None):
29         return identifier
30
31
32 docstring = parse_docstring(src, errors)
33 docstring, fields = docstring.split_fields()
34 html = docstring.to_html(EmptyLinker())
35
36 if errors and not html:
37     print_safe(u'Error parsing docstring:\n', error=True)
38     for error in errors:
39         # This script is run only with Python 2 interpreter
40         print_safe(unicode(error) + "\n", error=True)
41     sys.exit(1)
42
43 print_safe(html)