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):
|
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):
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user