fix pdf rendering on production
[librarian.git] / librarian / formats / pdf / __init__.py
index e8e936b..8bb68b0 100644 (file)
@@ -11,7 +11,7 @@ from lxml import etree
 from urllib import urlretrieve
 from StringIO import StringIO
 from Texml.processor import process
 from urllib import urlretrieve
 from StringIO import StringIO
 from Texml.processor import process
-from librarian import DCNS, XMLNamespace
+from librarian import DCNS, XMLNamespace, BuildError
 from librarian.formats import Format
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer
 from librarian.formats import Format
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer
@@ -52,7 +52,8 @@ class PdfFormat(Format):
 
     def add_file(self, ctx, filename, url=None, path=None, image=False):
         from subprocess import call
 
     def add_file(self, ctx, filename, url=None, path=None, image=False):
         from subprocess import call
-        assert url or path
+        if not url and not path:
+            raise BuildError('No URL or path for image')
         save_as = os.path.join(ctx.workdir, filename)
         if path is not None:
             ext = path.rsplit('.', 1)[-1]
         save_as = os.path.join(ctx.workdir, filename)
         if path is not None:
             ext = path.rsplit('.', 1)[-1]
@@ -72,6 +73,8 @@ class PdfFormat(Format):
             if url.startswith('/'):
                 url = 'http://milpeer.eu' + url
 
             if url.startswith('/'):
                 url = 'http://milpeer.eu' + url
 
+            if '.' not in url:
+                raise BuildError('Linked file without extension: %s' % url)
             ext = url.rsplit('.', 1)[-1]
             if image:
                 urlretrieve(url, save_as + '_.' + ext)
             ext = url.rsplit('.', 1)[-1]
             if image:
                 urlretrieve(url, save_as + '_.' + ext)
@@ -111,32 +114,14 @@ class PdfFormat(Format):
         title = self.doc.meta.title()
         t.append(texml_cmd("author", author))
         t.append(texml_cmd("title", title))
         title = self.doc.meta.title()
         t.append(texml_cmd("author", author))
         t.append(texml_cmd("title", title))
+        t.append(texml_cmd("organization", build_ctx.organization))
 
         doc = etree.SubElement(t, TexmlNS('env'), name="document")
 
         doc = etree.SubElement(t, TexmlNS('env'), name="document")
-        doc.append(texml_cmd("thispagestyle", "empty"))
-
-        # title page
-        height_left = 297
-        cover_url = self.doc.meta.get_one(DCNS('relation.coverimage.url'))
-        if cover_url:
-            self.add_file(build_ctx, 'cover.png', cover_url, image=True)
-            
-            img = Image.open(self.get_file(build_ctx, 'cover.png'))
-            size = img.size
-
-            if size[1] > size[0]:
-                img = img.crop((0, 0, size[0], size[0]))
-                img.save(self.get_file(build_ctx, 'cover.png'), format=img.format, quality=90)
-            size = img.size
-
-            # TODO: hardcoded paper size here
-            height = 210.0 * size[1] / size[0]
-            doc.append(texml_cmd("makecover", "%fmm" % height))
-        else:
-            doc.append(texml_cmd("vfill*"))
 
         # Wielkości!
 
         # Wielkości!
-        grp = etree.SubElement(doc, 'group')
+        title_field = texml_cmd("titlefield", "")
+        doc.append(title_field)
+        grp = title_field[0]
         grp.append(texml_cmd("raggedright"))
         grp.append(texml_cmd("vfill"))
         if author:
         grp.append(texml_cmd("raggedright"))
         grp.append(texml_cmd("vfill"))
         if author:
@@ -154,8 +139,6 @@ class PdfFormat(Format):
             p[0].append(texml_cmd("noindent"))
             p[0].append(texml_cmd("nohyphens", title))
             # p[0][-1].tail = title
             p[0].append(texml_cmd("noindent"))
             p[0].append(texml_cmd("nohyphens", title))
             # p[0][-1].tail = title
-        doc.append(texml_cmd("vfill"))
-        doc.append(texml_cmd("vfill"))
 
         # IOFile probably would be better
         cover_logo_url = getattr(build_ctx, 'cover_logo', None)
 
         # IOFile probably would be better
         cover_logo_url = getattr(build_ctx, 'cover_logo', None)
@@ -165,29 +148,27 @@ class PdfFormat(Format):
         if cover_logo_url:
             self.add_file(build_ctx, 'coverlogo.png', cover_logo_url, image=True)
             size = Image.open(self.get_file(build_ctx, 'coverlogo.png')).size
         if cover_logo_url:
             self.add_file(build_ctx, 'coverlogo.png', cover_logo_url, image=True)
             size = Image.open(self.get_file(build_ctx, 'coverlogo.png')).size
-            p = texml_cmd("par", "")
-            doc.append(p)
-            p[0].append(texml_cmd("noindent"))
-            p[0].append(texml_cmd("insertimage", 'coverlogo.png', "%fcm" % (1.0 * size[0] / size[1]), "1cm"))
-            
-        # logo organizacji!
-        doc.append(texml_cmd("clearpage"))
+            doc.append(texml_cmd("toplogo", 'coverlogo.png', "%fcm" % (2.0 * size[0] / size[1]), "2cm"))
+
+        doc.append(texml_cmd("vspace", "2em"))
 
         ctx = Context(build_ctx, format=self, img=1)
 
         ctx = Context(build_ctx, format=self, img=1)
-        doc.extend(self.render(self.doc.edoc.getroot(), ctx))
+        root = self.doc.edoc.getroot()
+        root.remove(root[1])
+        doc.extend(self.render(root, ctx))
 
         # Redakcyjna na końcu.
 
         # Redakcyjna na końcu.
-        doc.append(texml_cmd("clearpage"))
-
         doc.append(texml_cmd("section*", "Information about the resource"))
         doc.append(texml_cmd("vspace", "1em"))
 
         doc.append(texml_cmd("section*", "Information about the resource"))
         doc.append(texml_cmd("vspace", "1em"))
 
-        for m, f in (
-                ('Publisher: ', DCNS('publisher')),
-                ('Rights: ', DCNS('rights')),
-                ('Intended audience: ', DCNS('audience')),
-                ('', DCNS('description'))):
-            v = self.doc.meta.get_one(f)
+        for m, f, multiple in (
+                ('Publisher: ', DCNS('publisher'), False),
+                ('Rights: ', DCNS('rights'), False),
+                ('', DCNS('description'), False)):
+            if multiple:
+                v = ', '.join(self.doc.meta.get(f))
+            else:
+                v = self.doc.meta.get_one(f)
             if v:
                 e = texml_cmd("par", "")
                 e[0].append(texml_cmd("noindent"))
             if v:
                 e = texml_cmd("par", "")
                 e[0].append(texml_cmd("noindent"))
@@ -332,8 +313,21 @@ class ImgRenderer(CmdRenderer):
 PdfFormat.renderers.register(core.Div, 'img', ImgRenderer('insertimage'))
 
 
 PdfFormat.renderers.register(core.Div, 'img', ImgRenderer('insertimage'))
 
 
+class VideoRenderer(CmdRenderer):
+    def render(self, element, ctx):
+        root = super(VideoRenderer, self).render(element, ctx)
+        url = 'https://www.youtube.com/watch?v=%s' % element.attrib.get('videoid')
+        link = texml_cmd('href', url, url)
+        root[0][0].text = None
+        root[0][0].append(link)
+        return root
+
+PdfFormat.renderers.register(core.Div, 'video', VideoRenderer('par'))
+
+
 PdfFormat.renderers.register(core.Div, 'defined', CmdRenderer('textbf'))
 PdfFormat.renderers.register(core.Div, 'item', CmdRenderer('item'))
 PdfFormat.renderers.register(core.Div, 'defined', CmdRenderer('textbf'))
 PdfFormat.renderers.register(core.Div, 'item', CmdRenderer('item'))
+PdfFormat.renderers.register(core.Span, 'item', CmdRenderer('item'))
 PdfFormat.renderers.register(core.Div, 'list', EnvRenderer('itemize'))
 PdfFormat.renderers.register(core.Div, 'list.enum', EnvRenderer('enumerate'))
 
 PdfFormat.renderers.register(core.Div, 'list', EnvRenderer('itemize'))
 PdfFormat.renderers.register(core.Div, 'list.enum', EnvRenderer('enumerate'))