Skip to content
Permalink
Browse files

odt: use odfpy programmatically and fix unicode

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jun 11, 2015
1 parent 9552c90 commit f9f80a5f3de2943df56854608bd950ff78ad0894
Showing with 30 additions and 27 deletions.
  1. +1 −1 v7/odt/odt.plugin
  2. +29 −26 v7/odt/odt.py
@@ -7,6 +7,6 @@ MinVersion = 7.0.0

[Documentation]
Author = Roberto Alsina
Version = 0.1
Version = 0.1.1
Website = http://plugins.getnikola.com#odf
Description = Compile ODT files into HTML
@@ -31,14 +31,19 @@
"""

import os
import subprocess
import io
import shutil

import lxml.etree as etree

from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing

try:
from odf.odf2xhtml import ODF2XHTML
except ImportError:
ODF2XHTML = None

try:
from collections import OrderedDict
except ImportError:
@@ -52,31 +57,29 @@ class CompileODT(PageCompiler):

def compile_html(self, source, dest, is_two_file=True):
makedirs(os.path.dirname(dest))
binary = 'odf2xhtml'
try:
data = subprocess.check_output((binary, 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 open(dest, 'wb+') as outf:
outf.write(etree.tostring(body, encoding='unicode'))
except OSError as e:
if e.strreror == 'No such file or directory':
req_missing(['odfpy'], 'build this site (compile with odt)', python=False)
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 create_post(self, path, **kw):
onefile = kw.pop('onefile', False)

0 comments on commit f9f80a5

Please sign in to comment.
You can’t perform that action at this time.