X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/d1037d617d8cd2cafc60e67ac0272f86d2e24dfa..dc310ddf91e20a927cd1fc1db72e1c8cd803756d:/librarian/formats/epub/__init__.py diff --git a/librarian/formats/epub/__init__.py b/librarian/formats/epub/__init__.py index 80f9d5c..4b6cf13 100644 --- a/librarian/formats/epub/__init__.py +++ b/librarian/formats/epub/__init__.py @@ -13,7 +13,7 @@ import zipfile from urllib2 import urlopen from lxml import etree -from librarian import OPFNS, NCXNS, XHTMLNS, DCNS +from librarian import OPFNS, NCXNS, XHTMLNS, DCNS, BuildError from librarian import core from librarian.formats import Format from librarian.formats.cover.evens import EvensCover @@ -334,12 +334,15 @@ EpubFormat.renderers.register(core.Div, 'p', NaturalText('p')) EpubFormat.renderers.register(core.Div, 'list', NaturalText('ul')) EpubFormat.renderers.register(core.Div, 'list.enum', NaturalText('ol')) EpubFormat.renderers.register(core.Div, 'item', NaturalText('li')) +EpubFormat.renderers.register(core.Span, 'item', NaturalText('li')) class DivImageR(EpubRenderer): def render(self, element, ctx): src = element.attrib.get('src', '') ctx.images.append(src) + if '/' not in src: + raise BuildError('Bad image URL') src = src.rsplit('/', 1)[1] return super(DivImageR, self).render(element, Context(ctx, src=src)) @@ -352,6 +355,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)