3
from urllib import quote
5
class Parser(parser.Parser):
6
def __init__(self, pctxt):
7
parser.Parser.__init__(self, pctxt)
8
self.keywordPattern = re.compile(r'^(%s%s)(%s)' % (
9
'([a-z][a-z0-9\-_\.]*[a-z0-9\-_)])', # keyword
10
'( [a-z0-9\-_]+)*', # subkeywords
11
'(\((<[a-z0-9]+>)((\[?,|/)(<[a-z0-9]+>)(\]?))*\))?' # arg (ex: (<backend>), (<frontend>/<backend>), (<offset1>,<length>[,<offset2>]) ...
14
def parse(self, line):
16
keywords = pctxt.keywords
17
keywordsCount = pctxt.keywordsCount
18
chapters = pctxt.chapters
22
if line != "" and not re.match(r'^ ', line):
23
parsed = self.keywordPattern.match(line)
26
keyword = parsed.group(1)
28
parameters = line[len(keyword) + len(arg):]
29
if parameters != "" and not re.match("^ +(<|\[|\{|/|\(deprecated\))", parameters):
32
splitKeyword = keyword.split(" ")
33
parameters = arg + parameters
37
if keyword and (len(splitKeyword) <= 5):
38
toplevel = pctxt.details["toplevel"]
39
for j in xrange(0, len(splitKeyword)):
40
subKeyword = " ".join(splitKeyword[0:j + 1])
41
if subKeyword != "no":
42
if not subKeyword in keywords:
43
keywords[subKeyword] = set()
44
keywords[subKeyword].add(pctxt.details["chapter"])
45
res += '<a class="anchor" name="%s"></a>' % subKeyword
46
res += '<a class="anchor" name="%s-%s"></a>' % (toplevel, subKeyword)
47
res += '<a class="anchor" name="%s-%s"></a>' % (pctxt.details["chapter"], subKeyword)
48
res += '<a class="anchor" name="%s (%s)"></a>' % (subKeyword, chapters[toplevel]['title'])
49
res += '<a class="anchor" name="%s (%s)"></a>' % (subKeyword, chapters[pctxt.details["chapter"]]['title'])
51
deprecated = parameters.find("(deprecated)")
55
parameters = parameters.replace("(deprecated)", '<span class="label label-warning">(deprecated)</span>')
60
nextline = pctxt.get_line(1)
62
while nextline.startswith(" "):
63
# Found parameters on the next line
64
parameters += "\n" + nextline
66
if pctxt.has_more_lines(1):
67
nextline = pctxt.get_line(1)
72
parameters = self.colorize(parameters)
73
res += '<div class="keyword">%s<b><a class="anchor" name="%s"></a><a href="#%s">%s</a></b>%s%s</div>' % (prefix, keyword, quote("%s-%s" % (pctxt.details["chapter"], keyword)), keyword, parameters, suffix)
76
elif line.startswith("/*"):
77
# Skip comments in the documentation
78
while not pctxt.get_line().endswith("*/"):
82
# This is probably not a keyword but a text, ignore it
89
# Used to colorize keywords parameters
90
# TODO : use CSS styling
91
def colorize(self, text):
94
[ "[" , "]" , "#008" ],
95
[ "{" , "}" , "#800" ],
96
[ "<", ">", "#080" ],
100
while pos < len(text):
101
substring = text[pos:]
104
if substring.startswith(tag[0]):
107
colorized += '<span style="color: %s">%s' % (tag[2], substring[0:len(tag[0])])
111
elif substring.startswith(tag[1]):
114
# pop opening tags until the corresponding one is found
116
while heap and openingTag != tag:
117
openingTag = heap.pop()
118
if openingTag != tag:
119
colorized += '</span>'
120
# all intermediate tags are now closed, we can display the tag
121
colorized += substring[0:len(tag[1])]
122
# and the close it if it was previously opened
123
if openingTag == tag:
124
colorized += '</span>'
129
colorized += substring[0]
131
# close all unterminated tags
134
colorized += '</span>'