コード例 #1
0
ファイル: parser.py プロジェクト: wei-y/yamd
 def parse(self, source):
     if type(source) is not unicode:
         self.source = source.decode(self.encoding)
     else:
         self.source = source
     self.source = self.source.replace("\t", "    ")
     self.ast = Root()
     lines = self.source.split(u"\n")
     self.log(u"%d lines to parse" % len(lines))
     for i, l in enumerate(lines):
         self.log(u"\n    ==>Line %d: %s" % (i, l))
         self.ast.add_line(l.replace("\t", "    "))
         self.show()
     self.ast.inline_break_down()
     self.show()
コード例 #2
0
ファイル: parser.py プロジェクト: wei-y/yamd
class YAMD(object):

    def __init__(self, encoding="utf-8"):
        self.source = ""
        self.ast = None
        self.encoding = encoding
        self.logger = logging.getLogger("yamd.Parser")

    def parse_file(self, fname):
        self.log(u"Start parsing file: %s" % fname)
        f = open(fname)
        self.parse(f.read().decode(self.encoding))
        f.close()
        self.log(u"Parsing file done!")

    def parse(self, source):
        if type(source) is not unicode:
            self.source = source.decode(self.encoding)
        else:
            self.source = source
        self.source = self.source.replace("\t", "    ")
        self.ast = Root()
        lines = self.source.split(u"\n")
        self.log(u"%d lines to parse" % len(lines))
        for i, l in enumerate(lines):
            self.log(u"\n    ==>Line %d: %s" % (i, l))
            self.ast.add_line(l.replace("\t", "    "))
            self.show()
        self.ast.inline_break_down()
        self.show()

    def show(self):
        self.log("")
        self._show_source()
        self.log("")
        self._show_node(self.ast)
        self.log("")
        self._show_link_ref()

    def _show_link_ref(self):
        tab = [("ID", "LINK", "TITLE")]
        kl = ll = tl = 0
        for k, v in self.ast.link_ref_table.items():
            l, t = v["link"], v["title"]
            kl = len(k) if kl < len(k) else kl
            ll = len(l) if ll < len(l) else ll
            tl = len(t) if tl < len(k) else tl
            tab.append((k, l, t))
        kl = (len("ID") if kl < len("ID") else kl) + 2
        ll = (len("LINK") if ll < len("LINK") else ll) + 2
        tl = (len("TITLE") if tl < len("TITLE") else tl) + 2
        self.log(u"<-----LINK REFERENCE TABLE----->")
        self.log(u"    +%s+%s+%s+" % ("-"*kl, "-"*ll, "-"*tl))
        first = True
        for r in tab:
            ksl = kl - len(r[0]) - 1
            lsl = ll - len(r[1]) - 1
            tsl = tl - len(r[2]) - 1
            self.log(u"    | %s%s| %s%s| %s%s|" %
                     (r[0], " "*ksl, r[1], " "*lsl, r[2], " "*tsl))
            if first:
                self.log(u"    +%s+%s+%s+" % ("-"*kl, "-"*ll, "-"*tl))
                first = False
        self.log(u"    +%s+%s+%s+" % ("-"*kl, "-"*ll, "-"*tl))

    def _show_source(self):
        content = self.ast.source.split("\n")
        title = "SOURCE"
        width = max(len(c) for c in content) + 10
        fwd = (width - len(title)) / 2
        bwd = fwd+1 if width % 2 == 1 else fwd
        self.log("    %s%s%s" % ("-"*fwd, title, "-"*bwd))
        for c in content:
            spc = width - 6 - len(c)
            self.log("    |%s%s%s|" % (" "*4, c, " "*spc))
        self.log("    " + "-" * width)

    def _show_node(self, node, level=0, leading=()):
        prefix = ""
        for l in leading:
            c = "    |" if l else "     "
            prefix += c
        if node.parent and node is node.parent.last_child:
            prefix = prefix[:-1] + u"|--"
        else:
            prefix += u"--"
        self.log(prefix + unicode(node))
        if hasattr(node, "children"):
            for c in node.children:
                self._show_node(c, level+1,
                                leading+(c is not node.last_child,))

    def html(self, fname, title):
        html_file = open(fname, "w")
        self.open_html(html_file, title)
        self.ast.write_html(html_file, self.encoding)
        self.close_html(html_file)
        html_file.close()

    def html_body(self, fname):
        html_file = open(fname, "w")
        for c in self.ast.children:
            c.write_html(html_file, self.encoding)
        html_file.close()

    def open_html(self, html_file, title):
        html_file.write("<!DOCTYPE html>\n")
        html_file.write("<html>\n<head>\n")
        html_file.write("<meta charset=\"%s\">\n" % self.encoding)
        html_file.write("<title>%s</title>\n" % title)
        html_file.write("</head>\n")

    def close_html(self, html_file):
        html_file.write("</html>\n")

    def log(self, line):
        self.logger.debug(line)