X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/9123398bd03d1e08256bdc97959cf981cc1a5f76..134f8c2e9895aae155922f2b6bef9144ded7dfdc:/librarian/epub.py?ds=inline diff --git a/librarian/epub.py b/librarian/epub.py index 43eb295..de136ea 100644 --- a/librarian/epub.py +++ b/librarian/epub.py @@ -19,7 +19,7 @@ from mimetypes import guess_type from librarian import RDFNS, WLNS, NCXNS, OPFNS, XHTMLNS, OutputFile from librarian.cover import WLCover, FutureOfCopyrightCover - +from librarian.latex import LatexFragment from librarian import functions, get_resource functions.reg_person_name() @@ -330,6 +330,29 @@ def transform_chunk(chunk_xml, chunk_no, annotations, empty=False, _empty_html_s return output_html, toc, chars +def flatten_image_paths(wldoc): + root = wldoc.edoc.getroot() + for node in root.findall(".//ilustr"): + node.attrib['src'] = os.path.basename(node.attrib['src']) + return wldoc + +def render_latex(wldoc, prefix="latex"): + """ + Renders CODE as images and returns + (changed_wldoc, [ (epub_filepath1, latexfragment_object1), ... ] +""" + root = wldoc.edoc.getroot() + latex_nodes = root.findall(".//latex") + images = [] + for ln in latex_nodes: + fragment = LatexFragment(ln.text, resize=40) + images.append((os.path.join(prefix, fragment.filename), fragment)) + ln.tag = "img" + ln.text = os.path.join(prefix, fragment.filename) + + return wldoc, images + + def transform(wldoc, verbose=False, style=None, html_toc=False, sample=None, cover=None, flags=None, resources=None, @@ -381,6 +404,8 @@ def transform(wldoc, verbose=False, if main_text.tag == RDFNS('RDF'): main_text = None + flatten_image_paths(wldoc) + if main_text is not None: for chunk_xml in chop(main_text): empty = False @@ -443,6 +468,12 @@ def transform(wldoc, verbose=False, if not style: style = get_resource('epub/style.css') zip.write(style, os.path.join('OPS', 'style.css')) + + document, latex_images = render_latex(document) + for image in latex_images: + zip.write(image[1].path, os.path.join('OPS', image[0])) + image[1].remove() + if resources: if os.path.isdir(resources): for dp, dirs, files in os.walk(resources):