From 5135e076096ce04b1dbbb2f6210512d1a44fa237 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Fri, 23 Dec 2016 12:52:17 +0100 Subject: [PATCH] render video --- librarian/formats/epub/__init__.py | 15 +++++++++++++++ librarian/formats/html/__init__.py | 18 ++++++++++++++++++ librarian/formats/pdf/__init__.py | 12 ++++++++++++ 3 files changed, 45 insertions(+) diff --git a/librarian/formats/epub/__init__.py b/librarian/formats/epub/__init__.py index 80f9d5c..38443ee 100644 --- a/librarian/formats/epub/__init__.py +++ b/librarian/formats/epub/__init__.py @@ -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) diff --git a/librarian/formats/html/__init__.py b/librarian/formats/html/__init__.py index ae6470a..01dcbde 100644 --- a/librarian/formats/html/__init__.py +++ b/librarian/formats/html/__init__.py @@ -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&rel=0&showinfo=0&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')) diff --git a/librarian/formats/pdf/__init__.py b/librarian/formats/pdf/__init__.py index e8e936b..0ff985f 100644 --- a/librarian/formats/pdf/__init__.py +++ b/librarian/formats/pdf/__init__.py @@ -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')) -- 2.20.1