mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Txt-zip input: Generate html from text using markdown.
This commit is contained in:
parent
a7d0e84abd
commit
cef9941cec
@ -868,33 +868,18 @@ class Manifest(object):
|
||||
def _parse_txt(self, data):
|
||||
if '<html>' 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('<p>'+'\n'.join(lines)+'</p>')
|
||||
lines = []
|
||||
lines.append(escape(l))
|
||||
|
||||
if lines:
|
||||
paras.append('<p>'+'\n'.join(lines)+'</p>')
|
||||
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 = '''\
|
||||
<html>
|
||||
<head><title>%s</title></head>
|
||||
<body>%s</body>
|
||||
</html>
|
||||
'''%(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):
|
||||
|
@ -13,11 +13,12 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
__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 = '<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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user