mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #1170 (Image inclusion failure in text files parsed with Markdown.)
This commit is contained in:
parent
d0b4cce3cd
commit
007f0a2edb
@ -3,7 +3,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
"""
|
||||
Convert .txt files to .lrf
|
||||
"""
|
||||
import os, sys, codecs, logging
|
||||
import os, sys, codecs, logging, re, shutil
|
||||
|
||||
from calibre.ptempfile import PersistentTemporaryDirectory
|
||||
from calibre.ebooks.lrf import option_parser as lrf_option_parser
|
||||
@ -24,12 +24,22 @@ _('''%prog [options] mybook.txt
|
||||
dest='debug_html_generation', help=_('Print generated HTML to stdout and quit.'))
|
||||
return parser
|
||||
|
||||
def fix_image_includes(tdir, match):
|
||||
path = match.group(1).split('/')
|
||||
src = os.path.join(os.getcwd(), *path)
|
||||
dest = os.path.join(tdir, *path)
|
||||
p = os.path.dirname(dest)
|
||||
if not os.path.exists(p):
|
||||
os.makedirs(p)
|
||||
shutil.copyfile(src, dest)
|
||||
|
||||
|
||||
def generate_html(txtfile, encoding, tdir):
|
||||
'''
|
||||
Convert txtfile to html and return a PersistentTemporaryFile object pointing
|
||||
to the file with the HTML.
|
||||
'''
|
||||
txtfile = os.path.abspath(txtfile)
|
||||
enc = encoding
|
||||
if not encoding:
|
||||
encodings = ['cp1252', 'latin-1', 'utf8', 'iso-8859-1', 'koi8_r', 'koi8_u']
|
||||
@ -52,6 +62,8 @@ def generate_html(txtfile, encoding, tdir):
|
||||
safe_mode=False,
|
||||
)
|
||||
html = '<html><body>'+md.convert(txt)+'</body></html>'
|
||||
for match in re.finditer(r'<img\s+[^>]*src="([^"]+)"', html):
|
||||
fix_image_includes(tdir, match)
|
||||
p = os.path.join(tdir, 'index.html')
|
||||
open(p, 'wb').write(html.encode('utf-8'))
|
||||
mi = MetaInformation(os.path.splitext(os.path.basename(txtfile))[0], [_('Unknown')])
|
||||
|
@ -72,7 +72,7 @@ Images
|
||||
------
|
||||
|
||||
`txt2lrf` also has support for inline images like
|
||||
 this one.
|
||||
 this one.
|
||||
|
||||
___
|
||||
[Table of Contents](#toc)
|
||||
|
BIN
src/calibre/ebooks/lrf/txt/demo/small.jpg
Normal file
BIN
src/calibre/ebooks/lrf/txt/demo/small.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
Loading…
x
Reference in New Issue
Block a user