latex fragments
authorMarcin Koziej <marcinkoziej@nowoczesnapolska.org.pl>
Wed, 22 Jan 2014 09:06:13 +0000 (10:06 +0100)
committerMarcin Koziej <marcinkoziej@nowoczesnapolska.org.pl>
Wed, 22 Jan 2014 09:06:55 +0000 (10:06 +0100)
librarian/epub.py
librarian/epub/xsltAnnotations.xsl
librarian/epub/xsltScheme.xsl

index 43eb295..8dc11c7 100644 (file)
@@ -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,23 @@ def transform_chunk(chunk_xml, chunk_no, annotations, empty=False, _empty_html_s
     return output_html, toc, chars
 
 
+def render_latex(wldoc, prefix="latex"):
+    """
+    Renders <latex>CODE</latex> 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,
@@ -443,6 +460,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):
index f3e6443..11ca655 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
   <xsl:output method="html" version="1.0" encoding="utf-8" />
   <xsl:output doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" />
   <xsl:output doctype-public="-//W3C//DTD XHTML 1.1//EN" />
     </p>
   </xsl:template>
 
+  <xsl:template match="img">
+    <img rel="math formula">
+      <xsl:attribute name="src">
+       <xsl:value-of select="text()"/>
+      </xsl:attribute>
+      <xsl:attribute name="class">latex</xsl:attribute>
+    </img>
+  </xsl:template>
+
+
 </xsl:stylesheet>
index 8ee6114..f273c4b 100644 (file)
     </xsl:if>
   </xsl:template>
 
+  <xsl:template match="nota_red">
+      <div class="note_red" xmlns="http://www.w3.org/1999/xhtml">
+       <xsl:apply-templates />
+      </div>
+  </xsl:template>
+
+
   <xsl:template match="lista_osob" >
     <div class="person-list" xmlns="http://www.w3.org/1999/xhtml">
       <div class="h3" xmlns="http://www.w3.org/1999/xhtml">
 
   <xsl:template match="uwaga" />
 
-  <xsl:template match="nota_red" />
 
   <!--pominiÄ™cie tych metadanych-->
   <xsl:template match="rdf:RDF" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" />
     </img>
   </xsl:template>
 
+  <xsl:template match="img">
+    <img rel="math formula">
+      <xsl:attribute name="src">
+       <xsl:value-of select="text()"/>
+      </xsl:attribute>
+      <xsl:attribute name="class">latex</xsl:attribute>
+    </img>
+  </xsl:template>
+
   <xsl:template match="wyimek_extra">
   </xsl:template>