1from zzipdoc.match import Match
2
3def markup_link_syntax(text):
4    """ markup the link-syntax ` => somewhere ` in the text block """
5    return (text
6            & Match(r"(?m)(^|\s)\=\>\"([^\"]*)\"")
7            >> r"\1<link>\2</link>"
8            & Match(r"(?m)(^|\s)\=\>\'([^\']*)\'")
9            >> r"\1<link>\2</link>"
10            & Match(r"(?m)(^|\s)\=\>\s(\w[\w.]*\w\(\d+\))")
11            >> r"\1<link>\2</link>"
12            & Match(r"(?m)(^|\s)\=\>\s([^\s\,\.\!\?]+)")
13            >> r"\1<link>\2</link>")
14
15class CommentMarkup:
16    """ using a structure having a '.comment' item - it does pick it up
17    and enhances its text with new markups so that they can be represented
18    in xml. Use self.xml_text() to get markup text (knows 'this function') """
19    def __init__(self, header = None):
20        self.header = header
21        self.text = None     # xml'text
22    def get_filename(self):
23        if self.header is None:
24            return None
25        return self.header.get_filename()
26    def parse(self, header = None):
27        if header is not None:
28            self.header = header
29        if self.header is None:
30            return False
31        comment = self.header.comment
32        try:
33            comment = self.header.get_otherlines()
34        except Exception as e:
35            pass
36        mode = ""
37        text = ""
38        for line in comment.split("\n"):
39            check = Match()
40            if line & check(r"^\s?\s?\s?[*]\s+[*]\s(.*)"):
41                if mode != "ul":
42                    if mode: text += "</"+mode+">"
43                    mode = "ul" ; text += "<"+mode+">"
44                line = check.group(1)
45                text += "<li><p> "+self.markup_para_line(line)+" </p></li>\n"
46            elif line & check(r"^\s?\s?\s?[*](.*)"):
47                if mode != "para":
48                    if mode: text += "</"+mode+">"
49                    mode = "para" ; text += "<"+mode+">"
50                line = check.group(1)
51                if line.strip() == "":
52                    text += "</para><para>"+"\n"
53                else:
54                    text += " "+self.markup_para_line(line)+"\n"
55            else:
56                if mode != "screen":
57                    if mode: text += "</"+mode+">"
58                    mode = "screen" ; text += "<"+mode+">"
59                text += " "+self.markup_screen_line(line)+"\n"
60        if mode: text += "</"+mode+">"+"\n"
61        self.text = (text
62                     & Match(r"(<para>)(\s*[R]eturns)") >>r"\1This function\2"
63                     & Match(r"(?s)<para>\s*</para><para>") >> "<para>"
64                     & Match(r"(?s)<screen>\s*</screen>") >> "")
65        return True
66    def markup_screen_line(self, line):
67        return self.markup_line(line.replace("&","&amp;")
68                                .replace("<","&lt;")
69                                .replace(">","&gt;"))
70    def markup_para_line(self, line):
71        return markup_link_syntax(self.markup_line(line))
72    def markup_line(self, line):
73        return (line
74                .replace("<c>","<code>")
75                .replace("</c>","</code>"))
76    def xml_text(self, functionname = None):
77        if self.text is None:
78            if not self.parse(): return None
79        text = self.text
80        if functionname is not None:
81            def function(text): return "<function>"+text+"</function> function"
82            text = (text
83                    .replace("this function", "the "+function(functionname))
84                    .replace("This function", "The "+function(functionname)))
85        return text
86