Ejemplo n.º 1
0
 def _float_attrs(self, flt):
     buf = list()
     if flt.number != u"":
         buf.append(u' number="%s"' % escape(flt.number))
     if flt.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(flt.pagenr))
     return u"".join(buf)
Ejemplo n.º 2
0
 def _float_attrs(self, flt):
     buf = list()
     if flt.number != u"":
         buf.append(u' number="%s"' % escape(flt.number))
     if flt.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(flt.pagenr))
     return u"".join(buf)
Ejemplo n.º 3
0
 def _section_attrs(self, sec):
     buf = list()
     if sec.number != u"":
         buf.append(u' number="%s"' % escape(sec.number))
     buf.append(u' title="%s"' % escape(sec.title))
     if sec.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(sec.pagenr))
     return u"".join(buf)
Ejemplo n.º 4
0
 def _section_attrs(self, sec):
     buf = list()
     if sec.number != u"":
         buf.append(u' number="%s"' % escape(sec.number))
     buf.append(u' title="%s"' % escape(sec.title))
     if sec.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(sec.pagenr))
     return u"".join(buf)
Ejemplo n.º 5
0
 def _paragraph_attrs(self, para):
     buf = list()
     if para.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(para.pagenr))
     if para.font != u"":
         buf.append(u' font="%s"' % escape(para.font))
     if para.fontsize != u"":
         buf.append(u' fontsize="%s"' % escape(para.fontsize))
     if len(para.emph) > 0:
         emph_str = EMPH_SEPARATOR.join(para.emph)
         if emph_str != u"":
             buf.append(u' emph="%s"' % escape(emph_str))
     return u"".join(buf)
Ejemplo n.º 6
0
 def _paragraph_attrs(self, para):
     buf = list()
     if para.pagenr != u"":
         buf.append(u' pagenr="%s"' % escape(para.pagenr))
     if para.font != u"":
         buf.append(u' font="%s"' % escape(para.font))
     if para.fontsize != u"":
         buf.append(u' fontsize="%s"' % escape(para.fontsize))
     if len(para.emph) > 0:
         emph_str = EMPH_SEPARATOR.join(para.emph)
         if emph_str != u"":
             buf.append(u' emph="%s"' % escape(emph_str))
     return u"".join(buf)
Ejemplo n.º 7
0
    def to_XML(self, doc, pretty=False, linesep=u"", ident=u"", header=True):
        """Converts a Document to structure oriented XML.
        Args:
            doc:     The Document or part of a document or list of Documents
                     to convert.
            pretty:  If true: returns a human readable XML string.
                     Takes care of parameters linesep and ident automatically.
            linesep: Line seperator. Is overridden in case pretty is set.
            ident:   Ident for each line. Mainly used for recursion.
            header:  Place the XML header with version and encoding at the
                     beginning of the XML string.
        Return:
            Unicode string (XML markup).
        """
        if doc == None:
            return u""

        buf = list()
        if header:
            buf.append(XML_HEADER)
        if pretty:
            linesep = u"\n"

        if type(doc) == list:
            buf.append(u"<documents>")
            for d in doc:
                buf.append(self.to_XML(d, linesep=linesep, ident=PRETTY_IDENT, header=False))
            buf.append(u"</documents>")

        elif type(doc) == Document:
            buf.append(ident + u"<document>")
            if doc.meta:
                buf.append(self.to_XML(doc.meta, linesep=linesep, ident=ident + PRETTY_IDENT, header=False))
            for c in doc.children():
                buf.append(self.to_XML(c, linesep=linesep, ident=ident + PRETTY_IDENT, header=False))
            buf.append(ident + u"</document>")

        elif type(doc) == Meta:
            buf.append(ident + u"<meta>")
            if doc.title != u"":
                title = u"%s%s<title>%s</title>" % (ident, PRETTY_IDENT, escape(doc.title))
                buf.append(title)
            for author in doc.authors:
                author_xml = u"%s%s<author>%s</author>" % (ident, PRETTY_IDENT, escape(author))
                buf.append(author_xml)
            if doc.language != u"":
                lang = u"%s%s<langauge>%s</language>" % (ident, PRETTY_IDENT, escape(doc.language))
                buf.append(lang)
            buf.append(ident + u"</meta>")

        elif type(doc) == Section:
            buf.append(u"%s<section%s>" % (ident, self._section_attrs(doc)))
            for c in doc.children():
                buf.append(self.to_XML(c, linesep=linesep, ident=ident + PRETTY_IDENT, header=False))
            buf.append(ident + u"</section>")

        elif type(doc) == Chapter:
            buf.append(u"%s<chapter%s>" % (ident, self._section_attrs(doc)))
            for c in doc.children():
                buf.append(self.to_XML(c, linesep=linesep, ident=ident + PRETTY_IDENT, header=False))
            buf.append(ident + u"</chapter>")

        elif type(doc) == Paragraph:
            buf.append(u"%s<paragraph%s>" % (ident, self._paragraph_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</paragraph>")

        elif type(doc) == Float:
            buf.append(ident + u"%s<float%s>" % (ident, self._float_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</float>")

        elif type(doc) == Footnote:
            buf.append(ident + u"%s<footnote%s>" % (ident, self._float_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</footnote>")

        return linesep.join(buf)
Ejemplo n.º 8
0
    def to_XML(self, doc, pretty=False, linesep=u"", ident=u"", header=True):
        """Converts a Document to structure oriented XML.
        Args:
            doc:     The Document or part of a document or list of Documents
                     to convert.
            pretty:  If true: returns a human readable XML string.
                     Takes care of parameters linesep and ident automatically.
            linesep: Line seperator. Is overridden in case pretty is set.
            ident:   Ident for each line. Mainly used for recursion.
            header:  Place the XML header with version and encoding at the
                     beginning of the XML string.
        Return:
            Unicode string (XML markup).
        """
        if doc == None:
            return u""

        buf = list()
        if header:
            buf.append(XML_HEADER)
        if pretty:
            linesep = u"\n"

        if type(doc) == list:
            buf.append(u"<documents>")
            for d in doc:
                buf.append(
                    self.to_XML(d,
                                linesep=linesep,
                                ident=PRETTY_IDENT,
                                header=False))
            buf.append(u"</documents>")

        elif type(doc) == Document:
            buf.append(ident + u"<document>")
            if doc.meta:
                buf.append(
                    self.to_XML(doc.meta,
                                linesep=linesep,
                                ident=ident + PRETTY_IDENT,
                                header=False))
            for c in doc.children():
                buf.append(
                    self.to_XML(c,
                                linesep=linesep,
                                ident=ident + PRETTY_IDENT,
                                header=False))
            buf.append(ident + u"</document>")

        elif type(doc) == Meta:
            buf.append(ident + u"<meta>")
            if doc.title != u"":
                title = u"%s%s<title>%s</title>" % (ident, PRETTY_IDENT,
                                                    escape(doc.title))
                buf.append(title)
            for author in doc.authors:
                author_xml = u"%s%s<author>%s</author>" % (ident, PRETTY_IDENT,
                                                           escape(author))
                buf.append(author_xml)
            if doc.language != u"":
                lang = u"%s%s<langauge>%s</language>" % (ident, PRETTY_IDENT,
                                                         escape(doc.language))
                buf.append(lang)
            buf.append(ident + u"</meta>")

        elif type(doc) == Section:
            buf.append(u"%s<section%s>" % (ident, self._section_attrs(doc)))
            for c in doc.children():
                buf.append(
                    self.to_XML(c,
                                linesep=linesep,
                                ident=ident + PRETTY_IDENT,
                                header=False))
            buf.append(ident + u"</section>")

        elif type(doc) == Chapter:
            buf.append(u"%s<chapter%s>" % (ident, self._section_attrs(doc)))
            for c in doc.children():
                buf.append(
                    self.to_XML(c,
                                linesep=linesep,
                                ident=ident + PRETTY_IDENT,
                                header=False))
            buf.append(ident + u"</chapter>")

        elif type(doc) == Paragraph:
            buf.append(u"%s<paragraph%s>" %
                       (ident, self._paragraph_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</paragraph>")

        elif type(doc) == Float:
            buf.append(ident + u"%s<float%s>" %
                       (ident, self._float_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</float>")

        elif type(doc) == Footnote:
            buf.append(ident + u"%s<footnote%s>" %
                       (ident, self._float_attrs(doc)))
            buf.append(escape(doc.text))
            buf.append(ident + u"</footnote>")

        return linesep.join(buf)