render video
authorJan Szejko <janek37@gmail.com>
Fri, 23 Dec 2016 11:52:17 +0000 (12:52 +0100)
committerJan Szejko <janek37@gmail.com>
Fri, 23 Dec 2016 11:52:17 +0000 (12:52 +0100)
librarian/formats/epub/__init__.py
librarian/formats/html/__init__.py
librarian/formats/pdf/__init__.py

index 80f9d5c..38443ee 100644 (file)
@@ -352,6 +352,21 @@ class DivImageR(EpubRenderer):
 EpubFormat.renderers.register(core.Div, 'img', DivImageR('img'))
 
 
+class DivVideoR(Silent):
+    def render(self, element, ctx):
+        src = 'https://www.youtube.com/watch?v=%s' % element.attrib.get('videoid', '')
+        return super(DivVideoR, self).render(element, Context(ctx, src=src))
+
+    def container(self, ctx):
+        root, inner = super(DivVideoR, self).container(ctx)
+        src = getattr(ctx, 'src', '')
+        link = etree.Element('a', {'href': src})
+        link.text = src
+        inner.append(link)
+        return root, inner
+EpubFormat.renderers.register(core.Div, 'video', DivVideoR('p'))
+
+
 class HeaderR(NaturalText):
     def subcontext(self, element, ctx):
         return Context(ctx, inline=True)
index ae6470a..01dcbde 100644 (file)
@@ -187,6 +187,24 @@ class DivImage(NaturalText):
 
 HtmlFormat.renderers.register(core.Div, 'img', DivImage('img'))
 
+
+class DivVideo(NaturalText):
+    def render(self, element, ctx):
+        output = super(DivVideo, self).render(element, ctx)
+        video_id = element.attrib.get('videoid', '')
+        attribs = {
+            'width': '854',
+            'height': '480',
+            'src': '//www.youtube.com/embed/%s?controls=2&amp;rel=0&amp;showinfo=0&amp;theme=light' % video_id,
+            'frameborder': '0',
+            'allowfullscreen': '',
+        }
+        for attrib, value in attribs.iteritems():
+            output[0].attrib[attrib] = value
+        return output
+
+HtmlFormat.renderers.register(core.Div, 'video', DivVideo('iframe'))
+
 HtmlFormat.renderers.register(core.Div, 'item', NaturalText('li'))
 HtmlFormat.renderers.register(core.Div, 'list', NaturalText('ul'))
 HtmlFormat.renderers.register(core.Div, 'list.enum', NaturalText('ol'))
index e8e936b..0ff985f 100644 (file)
@@ -332,6 +332,18 @@ class ImgRenderer(CmdRenderer):
 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, 'list', EnvRenderer('itemize'))