def compile_html(self, source, dest, is_two_file=True): makedirs(os.path.dirname(dest)) if ODF2XHTML is None: req_missing(['odfpy'], 'build this site (compile odt)') odhandler = ODF2XHTML(True, False) data = odhandler.odf2xhtml(source) # Take the CSS from the head and put it in body doc = etree.fromstring(data) body = doc.find('{http://www.w3.org/1999/xhtml}body') for style in doc.findall('*//{http://www.w3.org/1999/xhtml}style'): style.getparent().remove(style) # keep only classes: filtered = [] for line in style.text.splitlines(): if line and line[0] in '.\t}': filtered.append(line) style.text = ''.join(filtered) body.insert(0, style) with io.open(dest, 'w+', encoding='utf-8') as outf: outf.write(etree.tostring(body, encoding='unicode'))
def main(): args = sys.argv[1:] if len(args) != 2: sys.stderr.write("Usage: %s file.odt file.css\n" % sys.argv[0]) sys.exit(2) reload(sys) sys.setdefaultencoding('utf-8') odt, css = args odt = odt.decode('utf-8') with open(css) as f: styles = f.read() handler = ODF2XHTML() generate_stylesheet = handler.generate_stylesheet def patched(*args, **kwargs): ret = generate_stylesheet(*args, **kwargs) handler.writeout(styles) return ret handler.generate_stylesheet = patched try: result = handler.odf2xhtml(odt).encode('us-ascii', 'xmlcharrefreplace') except: sys.stderr.write( "Unable to open file %s or file is not OpenDocument\n" % odt) raise sys.stdout.write(result)
def test_twolevellist(self): """ Check CSS has list styles for two level lists""" twolevellist_odt = os.path.join( os.path.dirname(__file__), "examples", "twolevellist.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(twolevellist_odt) assert has_rules(result,".L1_2","list-style-type: circle; font-family: StarSymbol, sans-serif;")
def get_content(self): generatecss = True embedable = True odhandler = ODF2XHTML(generatecss, embedable) doc = load(self.uploaded_file) return odhandler.odf2xhtml(doc)
def test_simpletable(self): """ Check CSS has table styles """ simpletable_odt = os.path.join( os.path.dirname(__file__), "examples", "simpletable.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(simpletable_odt) assert result.find(u"""<td class="TD-Tabel1_A1"><p class="P-Table_20_Contents">Cell 1</p>""") != -1 assert result.find(u"""<tr><td class="TD-Tabel1_A2"><p class="P-P1">Cell 3 (bold)</p>""") != -1 assert result.find(u"""<td class="TD-Tabel1_B2"><p class="P-P2">Cell 4 (italic)</p>""") != -1
def test_simplelist(self): """ Check CSS has list styles """ simplelist_odt = os.path.join( os.path.dirname(__file__), "examples", "simplelist.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(simplelist_odt) assert has_rules(result,".L1_1","list-style-type: disc;") assert has_rules(result,".L1_2","list-style-type: circle;") assert has_rules(result,".L1_3","list-style-type: square;") self.assertNotEqual(-1, result.find(u"""<p class="P-Standard">Line 1</p>\n<ul class="L1_1"><li><p class="P-P1">Item A</p>"""))
def test_images(self): """ Check CSS has frame styles for images """ odt = os.path.join(os.path.dirname(__file__), "examples", "images.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(odt) assert has_rules(result,".G-fr1","margin-left: 0px; margin-right: auto;") assert has_rules(result,".G-fr2","margin-left: auto; margin-right: 0px;") assert has_rules(result,".G-fr3","float: left") assert has_rules(result,".G-fr4","margin-right: auto;margin-left: auto;") assert has_rules(result,".G-fr5","float: right")
def test_simplestyles(self): """ Check CSS has text and paragraph styles """ simplestyles_odt = os.path.join( os.path.dirname(__file__), "examples", "simplestyles.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(simplestyles_odt) assert has_rules(result,".S-T1","font-weight: normal;") assert has_rules(result,".P-P2","font-weight: bold; font-style: italic;") assert has_rules(result,".S-T11","text-decoration: underline;") self.assertNotEqual(-1, result.find(u"""<p class="P-P2"><span class="S-T1">Italic</span></p>\n""")) self.assertNotEqual(-1, result.find(u"""\n\ttext-decoration: underline;\n""")) self.assertNotEqual(-1, result.find(u"""<p class="P-P3"><span class="S-T1">Underline italic</span></p>\n"""))
def test_css(self): """ Test css() method """ odt = os.path.join(os.path.dirname(__file__), "examples", "imageslabels.odt") odhandler = ODF2XHTML() odhandler.load(odt) result = odhandler.css() assert has_rules(result,".G-fr1","margin-left: 0px; margin-right: auto;") assert has_rules(result,".G-fr2","margin-left: auto; margin-right: 0px;") assert has_rules(result,".G-fr3","float: left") assert has_rules(result,".G-fr4","float: right") assert has_rules(result,".G-fr5","margin-right: auto;margin-left: auto;") assert has_rules(result,".G-fr7","margin-right: auto;margin-left: auto;") assert has_rules(result,".P-Illustration","font-size: 10pt;")
def test_positioned_shapes(self): """ Test positioned custom-shapes """ odt = os.path.join(os.path.dirname(__file__), "examples", "cols.odp") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(odt) self.assertNotEqual( -1, result.find( u'''<div style="position: absolute;width:5.503cm;height:1.905cm;left:2.117cm;top:3.175cm;" class="G-gr1">''' )) assert has_rules( result, ".MP-Default", "height: 19.05cm; width: 25.4cm; position: relative;")
def testParsing(self): """ Parse the test file """ odhandler = ODF2XHTML() outf = StringIO.StringIO() result = odhandler.odf2xhtml("TEST.odt") outf.write(result.encode('utf-8')) strresult = outf.getvalue() #self.assertEqual(strresult, htmlout) self.assertNotEqual(-1, strresult.find(u"""<p>The earth's climate has \ not changed many times in the course of its long history. \ <span class="S-Bold">This part is bold. </span>This is after bold.</p>""")) self.assertNotEqual(-1, strresult.find(u"""<h1>Simple test document<a id="anchor001"></a></h1>""")) self.assertNotEqual(-1, strresult.find(u""".S-Bold {""")) self.assertEqual(-1, strresult.find(u"<ol "))
def test_positioned_shapes(self): """ Test positioned custom-shapes """ odt = os.path.join(os.path.dirname(__file__), u"examples", u"cols.odp") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(odt) # Python3 can ouput style stances in a non-predictable order when # parsing an XML document; so the following test may fail # unexpectedly with Python3. It is replaced by a more robust test. ## self.assertNotEqual(-1, result.find(u'''<div style="position: absolute;width:5.503cm;height:1.905cm;left:2.117cm;top:3.175cm;" class="G-gr1">''')) assert (divWithClass_has_styles( result, u"G-gr1", u"position: absolute;width:5.503cm;height:1.905cm;left:2.117cm;top:3.175cm;" )) assert has_rules( result, u".MP-Default", u"height: 19.05cm; width: 25.4cm; position: relative;")
def test_imageslabels(self): """ Check CSS has frame styles for images with captions""" odt = os.path.join(os.path.dirname(__file__), u"examples", u"imageslabels.odt") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(odt) assert has_rules(result, u".G-fr1", u"margin-left: 0cm; margin-right: auto;") assert has_rules(result, u".G-fr2", u"margin-left: auto; margin-right: 0cm;") assert has_rules(result, u".G-fr3", u"float: left") assert has_rules(result, u".G-fr4", u"float: right") assert has_rules(result, u".G-fr5", u"margin-right: auto;margin-left: auto;") assert has_rules(result, u".G-fr7", u"margin-right: auto;margin-left: auto;") assert has_rules(result, u".P-Illustration", u"font-size: 10pt;")
def clean(self, data, initial=None): f = super(PlanFileField, self).clean(data, initial) if f is None: return None elif not data and initial: return initial # Detemine the file type, raise error if the file type is not correct if not (data.content_type == 'text/html' or data.content_type == 'text/plain' or data.content_type == 'application/octet-stream' or data.content_type == 'application/vnd.oasis.opendocument.text'): raise forms.ValidationError( self.error_messages['invalid_file_type']) # Process the ODF file if data.content_type == 'application/octet-stream' \ or data.content_type == \ 'application/vnd.oasis.opendocument.text': generatecss = True embedable = True odhandler = ODF2XHTML(generatecss, embedable) try: doc = load(data) plan_text = odhandler.odf2xhtml(doc) except Exception: raise forms.ValidationError( self.error_messages['unexcept_odf_error']) return plan_text # We need to get a file object. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): plan_text = data.temporary_file_path() elif hasattr(data, 'read'): plan_text = data.read() else: plan_text = data['content'] return plan_text
def test_mixedlist(self): """ Check CSS has list styles """ simplelist_odt = os.path.join(os.path.dirname(__file__), u"examples", u"ol.odp") odhandler = ODF2XHTML() result = odhandler.odf2xhtml(simplelist_odt) assert has_rules(result, u".L2_1", u"list-style-type: decimal;") assert has_rules(result, u".L2_2", u"list-style-type: circle;") assert has_rules(result, u".L2_3", u"list-style-type: square;") assert has_rules(result, u".L3_1", u"list-style-type: disc;") assert has_rules(result, u".L3_2", u"list-style-type: decimal;") assert has_rules(result, u".L3_3", u"list-style-type: square;") assert has_rules( result, u".MP-Default", u"height: 19.05cm; width: 25.4cm; position: relative;") self.assertNotEqual(-1, result.find(u"""<ol class="L2_1">""")) self.assertNotEqual(-1, result.find(u"""<ol class="L3_2">""")) self.assertNotEqual( -1, result.find( u"""position:absolute;width:22.86cm;height:3.176cm;left:1.27cm;top:0.762cm;""" ))
def __init__(self): ODF2XHTML.__init__(self, generate_css=False, embedable=True)
def as_html(self): with open(self.odf_filename, "rb") as odf: converter = ODF2XHTML(generate_css=False, embedable=True) converter.set_embedable() converter.load(odf) return self.make_proper_html(converter.xhtml())
def pages(slug): """ export FLASK_APP=views.py flask run Per rimuovere dipendenze: pip-autoremove pyquery :return: """ is_404 = False db = models.DocuwebDb() dbsession = db.sessionmaker() menu = utilities.menu_items(dbsession=dbsession, slug=slug) newdoc = dict(style='', body='', nav='', title='', status=200, is_front=False, menu_items=menu) try: # exclude special path assert slug not in [settings.SLUG_404, settings.FRONT_SLUG] # settings.FRONT_SLUG, # get current page from slug from database current_page = dbsession.query(models.Page).get(slug) assert current_page except AssertionError: if slug == settings.FRONT_SLUG: newdoc['is_front'] = True current_page = dbsession.query(models.Page).get( settings.FRONT_SLUG) else: is_404 = True current_page = dbsession.query(models.Page).get(settings.SLUG_404) newdoc['title'] = utilities.head_title(page=current_page, dbsession=dbsession) try: outf = io.BytesIO() docs_directory = settings.DOCUMENT_DIRECTORY filename = current_page.file # get the full document file path fullfilepath = path.join(docs_directory, filename) odhandler = ODF2XHTML() # convert OpenDocument to XHTML with python3-odfpy result = odhandler.odf2xhtml(fullfilepath) outf.write(result.encode('utf-8')) html_doc = outf.getvalue().decode('utf-8') # Parse the HTML document to get body and style. # Different results based on Accept: text/html | application/json print(request.headers['Accept']) rqc = ServeRequestedContent( soup=BeautifulSoup(html_doc, 'html.parser'), request_headers=request.headers['Accept'].split(',')[0].split( ';')[0], docdict=newdoc, is_404=is_404) return rqc.render() except TypeError: # document not specified, error newdoc['status'] = 500 return render_template('base', **newdoc)
def convert_content(self, req, input_type, source, output_type): odhandler = ODF2XHTML() out = odhandler.odf2xhtml(source).encode('us-ascii', 'xmlcharrefreplace') self.env.log.debug('HTML output for ODF') return (out, 'text/html')
def odf2xhtmlWithoutError(self): ODF2XHTML.__init__(self)