Txt-zip input: Generate html from text using markdown.

This commit is contained in:
John Schember 2009-06-27 11:30:47 -04:00
parent a7d0e84abd
commit cef9941cec
2 changed files with 9 additions and 23 deletions

View File

@ -868,33 +868,18 @@ class Manifest(object):
def _parse_txt(self, data): def _parse_txt(self, data):
if '<html>' in data: if '<html>' in data:
return self._parse_xhtml(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('<p>'+'\n'.join(lines)+'</p>')
lines = []
lines.append(escape(l))
if lines: self.oeb.log.debug('Converting', self.href, '...')
paras.append('<p>'+'\n'.join(lines)+'</p>')
from calibre.ebooks.txt.processor import txt_to_markdown
title = self.oeb.metadata.title title = self.oeb.metadata.title
if title: if title:
title = unicode(title[0]) title = unicode(title[0])
else: else:
title = 'No title' title = 'No title'
data = '''\
<html> return self._parse_xhtml(txt_to_markdown(data, title))
<head><title>%s</title></head>
<body>%s</body>
</html>
'''%(title, '\n'.join(paras))
data = self._parse_xhtml(data)
print etree.tostring(data)
return data
def _parse_css(self, data): def _parse_css(self, data):

View File

@ -13,11 +13,12 @@ __license__ = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>' __copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
def txt_to_markdown(txt): def txt_to_markdown(txt, title=''):
md = markdown.Markdown( md = markdown.Markdown(
extensions=['footnotes', 'tables', 'toc'], extensions=['footnotes', 'tables', 'toc'],
safe_mode=False,) safe_mode=False,)
html = '<html><head><title /></head><body>'+md.convert(txt)+'</body></html>' html = '<html><head><title>%s</title></head><body>%s</body></html>' % (title,
md.convert(txt))
return html return html