add vimeo
authorJan Szejko <janek37@gmail.com>
Wed, 5 Dec 2018 14:11:23 +0000 (15:11 +0100)
committerJan Szejko <janek37@gmail.com>
Wed, 5 Dec 2018 14:11:23 +0000 (15:11 +0100)
librarian/__init__.py
librarian/formats/epub/__init__.py
librarian/formats/html/__init__.py
librarian/formats/pdf/__init__.py

index a0d70e7..c9db26f 100644 (file)
@@ -8,6 +8,18 @@ import urllib
 from .utils import XMLNamespace
 
 
+VIDEO_PROVIDERS = {
+    'youtube': {
+        'url': 'https://www.youtube.com/watch?v=%s',
+        'embed': '//www.youtube.com/embed/%s?controls=2&amp;rel=0&amp;showinfo=0&amp;theme=light',
+    },
+    'vimeo': {
+        'url': 'https://vimeo.com/%s',
+        'embed': '//player.vimeo.com/video/%s',
+    },
+}
+
+
 class UnicodeException(Exception):
     def __str__(self):
         """ Dirty workaround for Python Unicode handling problems. """
index 36891be..4e2e6e5 100644 (file)
@@ -13,7 +13,7 @@ import zipfile
 from urllib2 import urlopen
 
 from lxml import etree
-from librarian import OPFNS, NCXNS, XHTMLNS, DCNS, BuildError
+from librarian import OPFNS, NCXNS, XHTMLNS, DCNS, BuildError, VIDEO_PROVIDERS
 from librarian import core
 from librarian.formats import Format
 from librarian.formats.cover.evens import EvensCover
@@ -360,7 +360,7 @@ 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', '')
+        src = VIDEO_PROVIDERS[element.attrib.get('provider')]['url'] % element.attrib.get('videoid', '')
         return super(DivVideoR, self).render(element, Context(ctx, src=src))
 
     def container(self, ctx):
index e1825a5..064eba7 100644 (file)
@@ -9,7 +9,7 @@ from librarian.formats import Format
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer
 from librarian.utils import Context, get_resource
-from librarian import core
+from librarian import core, VIDEO_PROVIDERS
 
 
 class HtmlFormat(Format):
@@ -191,11 +191,12 @@ HtmlFormat.renderers.register(core.Div, 'img', DivImage('img'))
 class DivVideo(NaturalText):
     def render(self, element, ctx):
         output = super(DivVideo, self).render(element, ctx)
+        provider = element.attrib.get('provider', '')
         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,
+            'src': VIDEO_PROVIDERS[provider]['embed'] % video_id,
             'frameborder': '0',
             'allowfullscreen': '',
         }
index 8bb68b0..de45fb8 100644 (file)
@@ -11,7 +11,7 @@ from lxml import etree
 from urllib import urlretrieve
 from StringIO import StringIO
 from Texml.processor import process
-from librarian import DCNS, XMLNamespace, BuildError
+from librarian import DCNS, XMLNamespace, BuildError, VIDEO_PROVIDERS
 from librarian.formats import Format
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer
@@ -316,7 +316,7 @@ 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')
+        url = VIDEO_PROVIDERS[element.attrib.get('provider')]['url'] % element.attrib.get('videoid', '')
         link = texml_cmd('href', url, url)
         root[0][0].text = None
         root[0][0].append(link)