From cef9941cecf782b98750c06a977ba231a6030840 Mon Sep 17 00:00:00 2001 From: John Schember Date: Sat, 27 Jun 2009 11:30:47 -0400 Subject: [PATCH] Txt-zip input: Generate html from text using markdown. --- src/calibre/ebooks/oeb/base.py | 27 ++++++--------------------- src/calibre/ebooks/txt/processor.py | 5 +++-- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index a3dadb995d..e5780609d1 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -868,33 +868,18 @@ class Manifest(object): def _parse_txt(self, data): if '' in data: return self._parse_xhtml(data) - from xml.sax.saxutils import escape - self.oeb.log.debug('Converting', self.href, '...') - paras = [] - lines = [] - for l in data.splitlines(): - if not l: - if lines: - paras.append('

'+'\n'.join(lines)+'

') - lines = [] - lines.append(escape(l)) - if lines: - paras.append('

'+'\n'.join(lines)+'

') + self.oeb.log.debug('Converting', self.href, '...') + + from calibre.ebooks.txt.processor import txt_to_markdown + title = self.oeb.metadata.title if title: title = unicode(title[0]) else: title = 'No title' - data = '''\ - - %s - %s - - '''%(title, '\n'.join(paras)) - data = self._parse_xhtml(data) - print etree.tostring(data) - return data + + return self._parse_xhtml(txt_to_markdown(data, title)) def _parse_css(self, data): diff --git a/src/calibre/ebooks/txt/processor.py b/src/calibre/ebooks/txt/processor.py index c8f2690622..01f44761dc 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -13,11 +13,12 @@ __license__ = 'GPL v3' __copyright__ = '2009, John Schember ' __docformat__ = 'restructuredtext en' -def txt_to_markdown(txt): +def txt_to_markdown(txt, title=''): md = markdown.Markdown( extensions=['footnotes', 'tables', 'toc'], safe_mode=False,) - html = '</head><body>'+md.convert(txt)+'</body></html>' + html = '<html><head><title>%s%s' % (title, + md.convert(txt)) return html