xml_docstring.py
Go to the documentation of this file.
1 class XmlDocString(object):
2  def __init__(self, index):
3  self.index = index
4  self.tags = {
5  "para": self.para,
6  "ref": self.ref,
7  "briefdescription": self.otherTags,
8  "detaileddescription": self.otherTags,
9  "parameterlist": self.parameterlist,
10  "parameterdescription": self.otherTags,
11  "emphasis": self.emphasis,
12  "simplesect": self.simplesect,
13  "formula": self.formula,
14  "itemizedlist": self.itemizedlist,
15  "listitem": self.listitem,
16  }
17  self.unkwownTags = set()
18  self.unkwownReferences = dict()
19  self._linesep = '\\n"\n"'
20 
21  try:
22  from pylatexenc.latex2text import LatexNodes2Text
23 
24  self.latex = LatexNodes2Text()
25  except ImportError:
26  self.latex = None
27 
28  def clear(self):
29  self.lines = []
30  self.unkwownTags.clear()
32 
33  def writeErrors(self, output):
34  ret = False
35  for t in self.unkwownTags:
36  output.warn("Unknown tag: ", t)
37  ret = True
38  for ref, node in self.unkwownReferences.items():
39  output.warn("Unknown reference: ", ref, node.text)
40  ret = True
41  return ret
42 
43  def _write(self, str):
44  nlines = str.split("\n")
45  if len(self.lines) == 0:
46  self.lines += nlines
47  else:
48  self.lines[-1] += nlines[0]
49  self.lines += nlines[1:]
50  # self.lines += nlines[1:]
51 
52  def _newline(self, n=1):
53  self.lines.extend(
54  [
55  "",
56  ]
57  * n
58  )
59 
60  def _clean(self):
61  s = 0
62  for l in self.lines:
63  if len(l.strip()) == 0:
64  s += 1
65  else:
66  break
67  e = len(self.lines)
68  for l in reversed(self.lines):
69  if len(l.strip()) == 0:
70  e -= 1
71  else:
72  break
73  self.lines = self.lines[s:e]
74 
75  def getDocString(self, brief, detailled, output):
76  self.clear()
77  if brief is not None:
78  self.visit(brief)
79  if detailled is not None and len(detailled.getchildren()) > 0:
80  if brief is not None:
81  self._newline()
82  self.visit(detailled)
83  from sys import stdout, stderr, version_info
84 
85  self.writeErrors(output)
86  self._clean()
87  if version_info[0] == 2:
88  return self._linesep.join(self.lines).encode("utf-8")
89  else:
90  return self._linesep.join(self.lines)
91 
92  def visit(self, node):
93  assert isinstance(node.tag, str)
94  tag = node.tag
95  if tag not in self.tags:
96  self.unknownTag(node)
97  else:
98  self.tags[tag](node)
99 
100  def unknownTag(self, node):
101  self.unkwownTags.add(node.tag)
102  self.otherTags(node)
103 
104  def otherTags(self, node):
105  if node.text:
106  self._write(node.text.strip().replace('"', r"\""))
107  for c in node.iterchildren():
108  self.visit(c)
109  if c.tail:
110  self._write(c.tail.strip().replace('"', r"\""))
111 
112  def emphasis(self, node):
113  self._write("*")
114  self.otherTags(node)
115  self._write("*")
116 
117  def simplesect(self, node):
118  self._write(node.attrib["kind"].title() + ": ")
119  self.otherTags(node)
120 
121  def para(self, node):
122  if node.text:
123  self._write(node.text.replace('"', r"\""))
124  for c in node.iterchildren():
125  self.visit(c)
126  if c.tail:
127  self._write(c.tail.replace('"', r"\""))
128  self._newline()
129 
130  def ref(self, node):
131  refid = node.attrib["refid"]
132  if self.index.hasref(refid):
133  self._write(self.index.getref(refid).name)
134  else:
135  self.unkwownReferences[refid] = node
136  self._write(node.text)
137  assert len(node.getchildren()) == 0
138 
139  def parameterlist(self, node):
140  self._newline()
141  self._write(node.attrib["kind"].title())
142  self._newline()
143  for item in node.iterchildren("parameteritem"):
144  self.parameteritem(item)
145 
146  def parameteritem(self, node):
147  indent = " "
148  self._write(indent + "- ")
149  # should contain two children
150  assert len(node.getchildren()) == 2
151  namelist = node.find("parameternamelist")
152  desc = node.find("parameterdescription")
153  sep = ""
154  for name in namelist.iterchildren("parametername"):
155  self._write(sep + name.text)
156  sep = ", "
157  self._write(" ")
158  self.visit(desc)
159 
160  def itemizedlist(self, node):
161  self._newline()
162  self.otherTags(node)
163 
164  def listitem(self, node):
165  self._write("- ")
166  self.otherTags(node)
167 
168  def formula(self, node):
169  if node.text:
170  if self.latex is None:
171  self._write(node.text.strip())
172  else:
173  self._write(self.latex.latex_to_text(node.text))
def parameteritem(self, node)
def writeErrors(self, output)
def parameterlist(self, node)
def simplesect(self, node)
def unknownTag(self, node)
def itemizedlist(self, node)
def __init__(self, index)
Definition: xml_docstring.py:2
def getDocString(self, brief, detailled, output)


hpp-fcl
Author(s):
autogenerated on Fri Jun 2 2023 02:39:02