Fixed bugs in rst_parser.py; added parsing of 'seealso' blocks

This commit is contained in:
Andrey Kamaev 2011-07-07 16:33:02 +00:00
parent fe86d25694
commit 426c358352

View File

@ -113,6 +113,7 @@ class RstParser(object):
else: else:
func["method"] = section_name func["method"] = section_name
capturing_seealso = False
skip_code_lines = False skip_code_lines = False
expected_brief = True expected_brief = True
fdecl = DeclarationParser() fdecl = DeclarationParser()
@ -126,25 +127,15 @@ class RstParser(object):
self.add_new_fdecl(func, fdecl) self.add_new_fdecl(func, fdecl)
continue continue
# skip lines if line-skipping mode is activated # continue capture seealso
if skip_code_lines: if capturing_seealso:
if not l or l.startswith(" "): if l.startswith(" "):
seealso = func.get("seealso",[])
seealso.extend(l.split(","))
func["seealso"] = seealso
continue continue
else: else:
skip_code_lines = False capturing_seealso = False
ll = l.strip()
if ll == "..": #strange construction...
continue
# turn on line-skipping mode for code fragments
if ll.endswith("::"):
skip_code_lines = True
ll = ll[:len(ll)-3]
if ll.startswith(".. code-block::"):
skip_code_lines = True
continue
# continue param parsing # continue param parsing
if pdecl.active: if pdecl.active:
@ -155,12 +146,48 @@ class RstParser(object):
self.add_new_pdecl(func, pdecl) self.add_new_pdecl(func, pdecl)
# do not continue - current line can contain next parameter definition # do not continue - current line can contain next parameter definition
ll = l.strip()
if ll == "..":
expected_brief = False
skip_code_lines = False
continue
# skip lines if line-skipping mode is activated
if skip_code_lines:
if not l or l.startswith(" "):
continue
else:
skip_code_lines = False
if ll.startswith(".. "):
expected_brief = False
elif ll.endswith("::"):
# turn on line-skipping mode for code fragments
skip_code_lines = True
ll = ll[:len(ll)-2]
if ll.startswith(".. code-block::") or ll.startswith(".. math::") or ll.startswith(".. image::"):
skip_code_lines = True
continue
# todo: parse structure members; skip them for now # todo: parse structure members; skip them for now
if ll.startswith(".. ocv:member::"): if ll.startswith(".. ocv:member::"):
skip_code_lines = True skip_code_lines = True
continue continue
# todo: parse ".. seealso::" sections # parse ".. seealso::" blocks
if ll.startswith(".. seealso::"):
if ll.endswith(".. seealso::"):
capturing_seealso = True
else:
seealso = func.get("seealso",[])
seealso.extend(ll[ll.find("::")+2:].split(","))
func["seealso"] = seealso
continue
# skip ".. index::"
if ll.startswith(".. index::"):
continue
# parse class & struct definitions # parse class & struct definitions
if ll.startswith(".. ocv:class::"): if ll.startswith(".. ocv:class::"):
@ -184,25 +211,18 @@ class RstParser(object):
fdecl = DeclarationParser(ll) fdecl = DeclarationParser(ll)
if fdecl.isready(): if fdecl.isready():
self.add_new_fdecl(func, fdecl) self.add_new_fdecl(func, fdecl)
expected_brief = False
continue continue
# parse parameters # parse parameters
if pdecl.hasDeclaration(l): if pdecl.hasDeclaration(l):
pdecl = ParamParser(l) pdecl = ParamParser(l)
expected_brief = False
continue continue
# record brief description # record brief description
if expected_brief and len(ll) == 0:
if "brief" in func:
expected_brief = False
continue
if expected_brief: if expected_brief:
func["brief"] = func.get("brief", "") + "\n" + ll func["brief"] = func.get("brief", "") + "\n" + ll
if skip_code_lines: if skip_code_lines:
expected_brief = False #force end brief if code block begins expected_brief = False # force end brief if code block begins
continue continue
# record other lines as long description # record other lines as long description
@ -313,6 +333,8 @@ class RstParser(object):
print "declarations:" print "declarations:"
for d in func["decls"]: for d in func["decls"]:
print " %7s: %s" % (d[0], re.sub(r"[ ]+", " ", d[1])) print " %7s: %s" % (d[0], re.sub(r"[ ]+", " ", d[1]))
if "seealso" in func:
print "seealso: ", func["seealso"]
if "params" in func: if "params" in func:
print "parameters:" print "parameters:"
for name, comment in func["params"].items(): for name, comment in func["params"].items():
@ -380,6 +402,13 @@ class RstParser(object):
if cmt: if cmt:
params[name] = cmt params[name] = cmt
func["params"] = params func["params"] = params
if "seealso" in func:
seealso = []
for see in func["seealso"]:
item = self.normalizeText(see.rstrip(".")).strip("\"")
if item:
seealso.append(item)
func["seealso"] = list(set(seealso))
# special case for old C functions - section name should omit "cv" prefix # special case for old C functions - section name should omit "cv" prefix
if not func.get("isclass",False) and not func.get("isstruct",False): if not func.get("isclass",False) and not func.get("isstruct",False):
@ -418,12 +447,16 @@ class RstParser(object):
s = re.sub(r"\n[ ]*([_,])\n", r"\1", s) s = re.sub(r"\n[ ]*([_,])\n", r"\1", s)
# remove extra line breaks after ` # remove extra line breaks after `
#s = re.sub(r"`\n", "` ", s) #s = re.sub(r"`\n", "` ", s)
# remove extra line breaks after ".. note::"
s = re.sub(r"\.\. note::\n+", ".. note:: ", s)
# remove extra line breaks before * # remove extra line breaks before *
s = re.sub(r"\n\n\*", "\n\*", s) s = re.sub(r"\n\n\*", "\n*", s)
# remove extra line breaks after *
s = re.sub(r"\n\*\n+", "\n* ", s)
# remove extra line breaks before #. # remove extra line breaks before #.
s = re.sub(r"\n\n#\.", "\n#.", s) s = re.sub(r"\n\n#\.", "\n#.", s)
# remove extra line breaks after #. # remove extra line breaks after #.
s = re.sub(r"\n#\.\n", "\n#. ", s) s = re.sub(r"\n#\.\n+", "\n#. ", s)
# remove extra line breaks before ` # remove extra line breaks before `
s = re.sub(r"\n[ ]*`", " `", s) s = re.sub(r"\n[ ]*`", " `", s)
# remove trailing whitespaces # remove trailing whitespaces
@ -435,17 +468,25 @@ class RstParser(object):
# unescape # unescape
s = re.sub(r"\\(.)", "\\1", s) s = re.sub(r"\\(.)", "\\1", s)
# compress whitespace # compress whitespace
s = re.sub(r"[ ]+", " ", s) s = re.sub(r" +", " ", s)
# compress linebreaks
s = re.sub(r"\n\n+", "\n\n", s)
s = s.replace("**", "") s = s.replace("**", "")
s = s.replace("``", "\"") s = s.replace("``", "\"")
s = s.replace("`", "\"") s = s.replace("`", "\"")
s = s.replace("\"\"", "\"") s = s.replace("\"\"", "\"")
s = s.replace(":ocv:cfunc:","") s = s.replace(":ocv:cfunc:","")
s = s.replace(":ref:", "")
s = s.replace(":math:", "") s = s.replace(":math:", "")
s = s.replace(":ocv:class:", "") s = s.replace(":ocv:class:", "")
s = s.replace(":ocv:func:", "") s = s.replace(":ocv:func:", "")
s = s.replace(":c:type:", "")
s = s.replace("]_", "]") s = s.replace("]_", "]")
s = s.replace(".. note::", "Note:")
s = s.replace(".. ocv:function::", "")
s = s.replace(".. ocv:cfunction::", "")
s = s.strip() s = s.strip()
return s return s