示例#1
0
    def insert_html(self, html, **kw):

        if html is None or html == '':
            # Testing for `if not html:` caused a FutureWarning: The
            # behavior of this method will change in future versions.
            # Use specific 'len(elem)' or 'elem is not None' test
            # instead.
            return ''

        if iselement(html):
            html = tostring(html)

        try:
            html = html2xhtml(html)
        except Exception as e:
            raise Exception(
                "20150923 html2xhtml(%r) failed: %s" % (html, e))
        # dd.logger.info("20180831 insert_html() got:<<<\n%s\n>>>", html)
        # print(__file__, ">>>")
        # print(html)
        # print("<<<", __file__)
        try:
            return self.renderXhtml(html, **kw)
        except Exception as e:
            if not isinstance(html, six.string_types):
                raise
            # some sax parsers refuse unicode strings.
            # appy.pod always expects utf-8 encoding.
            # See /blog/2011/0622.
            html = html.encode('utf-8')
            # logger.info("20120726 renderXhtml(%r) failed : %s", html, e)
            return self.renderXhtml(html, **kw)
示例#2
0
    def insert_html(self, html, **kw):
        if not HAS_TIDYLIB:
            raise Exception("You must install tidylib on your system")
        if html is None or html == '':
            # Testing for `if not html:` caused a FutureWarning: The
            # behavior of this method will change in future versions.
            # Use specific 'len(elem)' or 'elem is not None' test
            # instead.
            return ''

        if iselement(html):
            html = tostring(html)

        try:
            html = html2xhtml(html)
        except Exception as e:
            raise Exception(
                "20150923 html2xhtml(%r) failed: %s" % (html, e))
        # dd.logger.info("20180831 insert_html() got:<<<\n%s\n>>>", html)
        # print(__file__, ">>>")
        # print(html)
        # print("<<<", __file__)
        try:
            return self.renderXhtml(html, **kw)
        except Exception as e:
            if not isinstance(html, six.string_types):
                raise
            # some sax parsers refuse unicode strings.
            # appy.pod always expects utf-8 encoding.
            # See /blog/2011/0622.
            html = html.encode('utf-8')
            # logger.info("20120726 renderXhtml(%r) failed : %s", html, e)
            return self.renderXhtml(html, **kw)
示例#3
0
 def html_func(self,html,**kw):
     """
     Render a string that is in HTML (not XHTML).
     """
     if not html:
         return ''
     #~ logger.debug("html_func() got:<<<\n%s\n>>>",html)
     #~ print __file__, ">>>"
     #~ print html
     #~ print "<<<", __file__
     html = html2xhtml(html)
     if isinstance(html,unicode):
         # some sax parsers refuse unicode strings. 
         # appy.pod always expects utf-8 encoding.
         # See /blog/2011/0622.
         html = html.encode('utf-8')
         #~ logger.info("20120726 html_func() %r",html)
     return self.renderXhtml(html,**kw)
示例#4
0
文件: base.py 项目: cuchac/lino
        def as_odt(self):
            xhtml = ''.join([ln for ln in self.main.html_lines()])
            xhtml = html2xhtml(xhtml)
            #~ xhtml = "<div>%s</div>" % xhtml
            #~ xhtml = "<p>%s</p>" % xhtml
            #~ xhtml = '<html><body>%s</body></html>' % xhtml
            xhtml = '<html xmlns="http://www.w3.org/1999/xhtml"><body>%s</body></html>' % xhtml
            if not True:
                f = open("before.xml", "wt")
                f.write(xhtml.encode('utf-8'))
                f.close()

            #~ logger.info("Gonna do it with %r",xhtml)
            xhtml = self.odtfile.xhtml_to_odt(xhtml)
            if True:
                f = open("after.xml", "wt")
                f.write(xhtml)
                #~ f.write(xhtml.encode('utf-8'))
                f.close()
            return xhtml.decode('utf-8')
示例#5
0
文件: base.py 项目: DarioGT/lino
        def as_odt(self):
            xhtml = ''.join([ln for ln in self.main.html_lines()])
            xhtml = html2xhtml(xhtml)
            #~ xhtml = "<div>%s</div>" % xhtml
            #~ xhtml = "<p>%s</p>" % xhtml
            #~ xhtml = '<html><body>%s</body></html>' % xhtml
            xhtml = '<html xmlns="http://www.w3.org/1999/xhtml"><body>%s</body></html>' % xhtml
            if not True:
                f = open("before.xml", "wt")
                f.write(xhtml.encode('utf-8'))
                f.close()

            #~ logger.info("Gonna do it with %r",xhtml)
            xhtml = self.odtfile.xhtml_to_odt(xhtml)
            if True:
                f = open("after.xml", "wt")
                f.write(xhtml)
                #~ f.write(xhtml.encode('utf-8'))
                f.close()
            return xhtml.decode('utf-8')
示例#6
0
 def html_func(self, html, **kw):
     """
     Render a string that is in HTML (not XHTML).
     """
     if not html:
         return ''
     if E.iselement(html):
         html = E.tostring(html)
     try:
         html = html2xhtml(html)
     except Exception as e:
         print(20150923, e)
     # logger.debug("20141210 html_func() got:<<<\n%s\n>>>", html)
     # print __file__, ">>>"
     # print html
     # print "<<<", __file__
     if isinstance(html, str):
         # some sax parsers refuse unicode strings.
         # appy.pod always expects utf-8 encoding.
         # See /blog/2011/0622.
         html = html.encode('utf-8')
         #~ logger.info("20120726 html_func() %r",html)
     return self.renderXhtml(html, **kw)
示例#7
0
 def html_func(self, html, **kw):
     """
     Render a string that is in HTML (not XHTML).
     """
     if not html:
         return ''
     if E.iselement(html):
         html = E.tostring(html)
     try:
         html = html2xhtml(html)
     except Exception as e:
         print 20150923, e
     # logger.debug("20141210 html_func() got:<<<\n%s\n>>>", html)
     # print __file__, ">>>"
     # print html
     # print "<<<", __file__
     if isinstance(html, unicode):
         # some sax parsers refuse unicode strings.
         # appy.pod always expects utf-8 encoding.
         # See /blog/2011/0622.
         html = html.encode('utf-8')
         #~ logger.info("20120726 html_func() %r",html)
     return self.renderXhtml(html, **kw)
示例#8
0
    def html_func(self, html, **kw):
        """
        Insert a chunk of HTML (not XHTML).
        This might be provided as a string or as an etree element.
        """

        if html is None or html == '':
            # Testing for `if not html:` caused a FutureWarning: The
            # behavior of this method will change in future versions.
            # Use specific 'len(elem)' or 'elem is not None' test
            # instead.
            return ''

        if E.iselement(html):
            html = E.tostring(html)

        try:
            html = html2xhtml(html)
        except Exception as e:
            raise Exception(
                "20150923 html2xhtml(%r) failed: %s" % (html, e))
        # logger.info("20160330 html_func() got:<<<\n%s\n>>>", html)
        # print(__file__, ">>>")
        # print(html)
        # print("<<<", __file__)
        try:
            return self.renderXhtml(html, **kw)
        except Exception as e:
            if not isinstance(html, six.string_types):
                raise
            # some sax parsers refuse unicode strings.
            # appy.pod always expects utf-8 encoding.
            # See /blog/2011/0622.
            html = html.encode('utf-8')
            # logger.info("20120726 renderXhtml(%r) failed : %s", html, e)
            return self.renderXhtml(html, **kw)
示例#9
0
文件: base.py 项目: cuchac/lino
    def old_as_odt(self):
        from xhtml2odt import ODTFile
        from lino.utils import AttrDict
        from lino.utils.html2xhtml import html2xhtml
        options = AttrDict(
            url="",
            with_network=False,
            verbose=True,
            template=self.input_template,
            top_header_level=1,
            img_width="8cm",
            img_height="6cm",
        )

        # ~ version=False # help="Show the version and exit")
        #~ input=input", metavar="FILE",
                          #~ help="Read the html from this file")
        #~ parser.add_option("-o", "--output", dest="output", metavar="FILE",
                          #~ help="Location of the output ODT file")
        #~ parser.add_option("-t", "--template", dest="template", metavar="FILE",
                          #~ help="Location of the template ODT file")
        #~ parser.add_option("-u", "--url", dest="url",
                          #~ help="Use this URL for relative links")
        #~ parser.add_option("-v", "--verbose", dest="verbose",
                          #~ action="store_true", default=False,
                          #~ help="Show what's going on")
        #~ parser.add_option("--html-id", dest="htmlid", metavar="ID",
                          #~ help="Only export from the element with this ID")
        #~ parser.add_option("--replace", dest="replace_keyword",
                          #~ default="ODT-INSERT", metavar="KEYWORD",
                          #~ help="Keyword to replace in the ODT template "
                          #~ "(default is %default)")
        #~ parser.add_option("--cut-start", dest="cut_start",
                          #~ default="ODT-CUT-START", metavar="KEYWORD",
                          #~ help="Keyword to start cutting text from the ODT "
                          #~ "template (default is %default)")
        #~ parser.add_option("--cut-stop", dest="cut_stop",
                          #~ default="ODT-CUT-STOP", metavar="KEYWORD",
                          #~ help="Keyword to stop cutting text from the ODT "
                          #~ "template (default is %default)")
        #~ parser.add_option("--top-header-level", dest="top_header_level",
                          #~ type="int", default="1", metavar="LEVEL",
                          #~ help="Level of highest header in the HTML "
                          #~ "(default is %default)")
        #~ parser.add_option("--img-default-width", dest="img_width",
                          #~ metavar="WIDTH", default="8cm",
                          #~ help="Default image width (default is %default)")
        #~ parser.add_option("--img-default-height", dest="img_height",
                          #~ metavar="HEIGHT", default="6cm",
                          #~ help="Default image height (default is %default)")
        #~ parser.add_option("--dpi", dest="img_dpi", type="int",
                          #~ default=96, metavar="DPI", help="Screen resolution "
                          #~ "in Dots Per Inch (default is %default)")
        #~ parser.add_option("--no-network", dest="with_network",
                          #~ action="store_false", default=True,
                          #~ help="Do not download remote images")
        #~ options, args = parser.parse_args()
        odtfile = ODTFile(options)
        odtfile.open()

        xhtml = ''.join([ln for ln in self.main.html_lines()])
        xhtml = html2xhtml(xhtml)
        #~ xhtml = "<DIV>%s</DIV>" % xhtml
        xhtml = """\
<html xmlns="http://www.w3.org/1999/xhtml"><body>%s</body></html>""" % xhtml
        #~ xhtml = "<p>%s</p>" % xhtml
        if True:
            f = open("before.xml", "wt")
            f.write(xhtml.encode('utf-8'))
            f.close()

        #~ logger.info("Gonna do it with %r",xhtml)
        xhtml = odtfile.xhtml_to_odt(xhtml)
        if True:
            f = open("after.xml", "wt")
            f.write(xhtml)
            #~ f.write(xhtml.encode('utf-8'))
            f.close()
        return xhtml
示例#10
0
文件: base.py 项目: DarioGT/lino
    def old_as_odt(self):
        from xhtml2odt import ODTFile
        from lino.utils import AttrDict
        from lino.utils.html2xhtml import html2xhtml
        options = AttrDict(
            url="",
            with_network=False,
            verbose=True,
            template=self.input_template,
            top_header_level=1,
            img_width="8cm",
            img_height="6cm",
        )

        # ~ version=False # help="Show the version and exit")
        #~ input=input", metavar="FILE",
        #~ help="Read the html from this file")
        #~ parser.add_option("-o", "--output", dest="output", metavar="FILE",
        #~ help="Location of the output ODT file")
        #~ parser.add_option("-t", "--template", dest="template", metavar="FILE",
        #~ help="Location of the template ODT file")
        #~ parser.add_option("-u", "--url", dest="url",
        #~ help="Use this URL for relative links")
        #~ parser.add_option("-v", "--verbose", dest="verbose",
        #~ action="store_true", default=False,
        #~ help="Show what's going on")
        #~ parser.add_option("--html-id", dest="htmlid", metavar="ID",
        #~ help="Only export from the element with this ID")
        #~ parser.add_option("--replace", dest="replace_keyword",
        #~ default="ODT-INSERT", metavar="KEYWORD",
        #~ help="Keyword to replace in the ODT template "
        #~ "(default is %default)")
        #~ parser.add_option("--cut-start", dest="cut_start",
        #~ default="ODT-CUT-START", metavar="KEYWORD",
        #~ help="Keyword to start cutting text from the ODT "
        #~ "template (default is %default)")
        #~ parser.add_option("--cut-stop", dest="cut_stop",
        #~ default="ODT-CUT-STOP", metavar="KEYWORD",
        #~ help="Keyword to stop cutting text from the ODT "
        #~ "template (default is %default)")
        #~ parser.add_option("--top-header-level", dest="top_header_level",
        #~ type="int", default="1", metavar="LEVEL",
        #~ help="Level of highest header in the HTML "
        #~ "(default is %default)")
        #~ parser.add_option("--img-default-width", dest="img_width",
        #~ metavar="WIDTH", default="8cm",
        #~ help="Default image width (default is %default)")
        #~ parser.add_option("--img-default-height", dest="img_height",
        #~ metavar="HEIGHT", default="6cm",
        #~ help="Default image height (default is %default)")
        #~ parser.add_option("--dpi", dest="img_dpi", type="int",
        #~ default=96, metavar="DPI", help="Screen resolution "
        #~ "in Dots Per Inch (default is %default)")
        #~ parser.add_option("--no-network", dest="with_network",
        #~ action="store_false", default=True,
        #~ help="Do not download remote images")
        #~ options, args = parser.parse_args()
        odtfile = ODTFile(options)
        odtfile.open()

        xhtml = ''.join([ln for ln in self.main.html_lines()])
        xhtml = html2xhtml(xhtml)
        #~ xhtml = "<DIV>%s</DIV>" % xhtml
        xhtml = """\
<html xmlns="http://www.w3.org/1999/xhtml"><body>%s</body></html>""" % xhtml
        #~ xhtml = "<p>%s</p>" % xhtml
        if True:
            f = open("before.xml", "wt")
            f.write(xhtml.encode('utf-8'))
            f.close()

        #~ logger.info("Gonna do it with %r",xhtml)
        xhtml = odtfile.xhtml_to_odt(xhtml)
        if True:
            f = open("after.xml", "wt")
            f.write(xhtml)
            #~ f.write(xhtml.encode('utf-8'))
            f.close()
        return xhtml