Skip to content

Commit

Permalink
odt: use odfpy programmatically and fix unicode
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jun 11, 2015
1 parent 9552c90 commit f9f80a5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion v7/odt/odt.plugin
Expand Up @@ -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
55 changes: 29 additions & 26 deletions v7/odt/odt.py
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit f9f80a5

Please sign in to comment.