X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/3f64488684dbf14b7e0005209d2dca878e1852d9..8722e4d3cfe77bd6945b8617b484fb9db6c4c9ca:/librarian/formats/html/__init__.py?ds=inline diff --git a/librarian/formats/html/__init__.py b/librarian/formats/html/__init__.py index 2cf2601..271fac9 100644 --- a/librarian/formats/html/__init__.py +++ b/librarian/formats/html/__init__.py @@ -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, get_provider class HtmlFormat(Format): @@ -40,7 +40,7 @@ class HtmlFormat(Format): t.find('.//div[@id="content"]').extend( self.render(self.doc.edoc.getroot(), ctx)) - #t.find('.//div[@id="toc"]').append(ctx.toc.render()) + # t.find('.//div[@id="toc"]').append(ctx.toc.render()) t.find('.//div[@id="footnotes"]').extend(ctx.footnotes.output) return OutputFile.from_string(etree.tostring( @@ -81,7 +81,8 @@ class Footnotes(object): def append(self, item): self.counter += 1 - e = etree.Element("a", + e = etree.Element( + "a", href="#footnote-anchor-%d" % self.counter, id="footnote-%d" % self.counter, style="float:left;margin-right:1em") @@ -89,7 +90,8 @@ class Footnotes(object): e.tail = " " self.output.append(e) self.output.extend(item) - anchor = etree.Element("a", + anchor = etree.Element( + "a", id="footnote-anchor-%d" % self.counter, href="#footnote-%d" % self.counter) anchor.text = "[%d]" % self.counter @@ -131,6 +133,7 @@ class TOC(object): HtmlFormat.renderers.register(core.Aside, None, NaturalText('aside')) HtmlFormat.renderers.register(core.Aside, 'comment', Silent()) + class AsideFootnote(NaturalText): def render(self, element, ctx): output = super(AsideFootnote, self).render(element, ctx) @@ -150,21 +153,23 @@ class Header(NaturalText): else: root[0].tag = 'h2' if root[0].text: - d = etree.SubElement(root[0], 'a', {'id': root[0].text, 'style': 'pointer: hand; color:#ddd; font-size:.8em'}) - #d.text = "per" + d = etree.SubElement( + root[0], 'a', {'id': root[0].text, 'style': 'pointer: hand; color:#ddd; font-size:.8em'}) + # d.text = "per" return root - + HtmlFormat.renderers.register(core.Header, None, Header('h1')) HtmlFormat.renderers.register(core.Div, None, NaturalText('div')) + class DivDefined(NaturalText): def render(self, element, ctx): output = super(DivDefined, self).render(element, ctx) output[0].text = (output[0].text or '') + ':' - output[0].attrib['id'] = output[0].text # not so cool? + output[0].attrib['id'] = output[0].text # not so cool? return output HtmlFormat.renderers.register(core.Div, 'defined', DivDefined('dt', {'style': 'display: inline-block'})) @@ -182,15 +187,36 @@ 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) + provider = get_provider(element.attrib.get('provider')) + video_id = element.attrib.get('videoid', '') + attribs = { + 'width': '854', + 'height': '480', + 'src': provider['embed'] % 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.Span, 'item', NaturalText('li')) HtmlFormat.renderers.register(core.Div, 'list', NaturalText('ul')) HtmlFormat.renderers.register(core.Div, 'list.enum', NaturalText('ol')) + class DivListDefinitions(NaturalText): def render(self, element, ctx): output = super(DivListDefinitions, self).render(element, ctx) - #if ctx.toc_level > 2: - # output[0].attrib['style'] = 'float: right' + # if ctx.toc_level > 2: + # output[0].attrib['style'] = 'float: right' return output HtmlFormat.renderers.register(core.Div, 'list.definitions', DivListDefinitions('ul')) @@ -215,6 +241,7 @@ HtmlFormat.renderers.register(core.Span, 'cite.code', LiteralText('code')) HtmlFormat.renderers.register(core.Span, 'emph', NaturalText('em')) HtmlFormat.renderers.register(core.Span, 'emp', NaturalText('strong')) + class SpanUri(LiteralText): def render(self, element, ctx): root = super(SpanUri, self).render(element, ctx) @@ -222,6 +249,7 @@ class SpanUri(LiteralText): return root HtmlFormat.renderers.register(core.Span, 'uri', SpanUri('a')) + class SpanLink(LiteralText): def render(self, element, ctx): root = super(SpanLink, self).render(element, ctx) @@ -231,4 +259,3 @@ class SpanLink(LiteralText): root[0].attrib['href'] = src return root HtmlFormat.renderers.register(core.Span, 'link', SpanLink('a')) -