1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from librarian.formats import Format
9 from librarian.output import OutputFile
10 from librarian.renderers import Register, TreeRenderer
11 from librarian.utils import Context, get_resource
12 from librarian import core
15 class HtmlFormat(Format):
19 renderers = Register()
21 def __init__(self, doc, standalone=False):
22 super(HtmlFormat, self).__init__(doc)
23 self.standalone = standalone
25 def build(self, files_path=None):
27 tmpl = get_resource("formats/html/res/html_standalone.html")
29 tmpl = get_resource("formats/html/res/html.html")
32 ctx = Context(format=self)
33 ctx.files_path = files_path
36 ctx.footnotes = Footnotes()
39 t.find('head/title').text = u"%s (%s)" % (self.doc.meta.title(), self.doc.meta.author())
41 t.find('.//div[@id="content"]').extend(
42 self.render(self.doc.edoc.getroot(), ctx))
43 #t.find('.//div[@id="toc"]').append(ctx.toc.render())
44 t.find('.//div[@id="footnotes"]').extend(ctx.footnotes.output)
46 return OutputFile.from_string(etree.tostring(
47 t, encoding='utf-8', method="html"))
49 def render(self, element, ctx):
50 return self.renderers.get_for(element).render(element, ctx)
55 class NaturalText(TreeRenderer):
56 def render_text(self, text, ctx):
57 root, inner = self.text_container()
58 chunks = re.split('(?<=\s\w) ', text)
59 inner.text = chunks[0]
60 for chunk in chunks[1:]:
61 x = etree.Entity("nbsp")
67 class LiteralText(TreeRenderer):
71 class Silent(TreeRenderer):
72 def render_text(self, text, ctx):
73 root, inner = self.text_container()
77 class Footnotes(object):
80 self.output = etree.Element("_")
82 def append(self, item):
84 e = etree.Element("a",
85 href="#footnote-anchor-%d" % self.counter,
86 id="footnote-%d" % self.counter,
87 style="float:left;margin-right:1em")
88 e.text = "[%d]" % self.counter
91 self.output.extend(item)
92 anchor = etree.Element("a",
93 id="footnote-anchor-%d" % self.counter,
94 href="#footnote-%d" % self.counter)
95 anchor.text = "[%d]" % self.counter
104 def add(self, title, level=0):
106 self.items.append((level, title, self.counter))
110 out = etree.Element("ul", id="toc")
113 for level, title, counter in self.items:
114 while level > curr_level:
115 ins = etree.Element("ul")
119 while level < curr_level:
120 cursor = cursor.getparent()
122 ins = etree.Element("li")
123 ins.append(etree.Element("a", href="#sect%d" % counter))
131 HtmlFormat.renderers.register(core.Aside, None, NaturalText('aside'))
132 HtmlFormat.renderers.register(core.Aside, 'comment', Silent())
134 class AsideFootnote(NaturalText):
135 def render(self, element, ctx):
136 output = super(AsideFootnote, self).render(element, ctx)
137 anchor = ctx.footnotes.append(output)
138 root, inner = self.container()
141 HtmlFormat.renderers.register(core.Aside, 'footnote', AsideFootnote())
144 class Header(NaturalText):
145 def render(self, element, ctx):
146 root = super(Header, self).render(element, ctx)
147 if ctx.toc_level == 1:
148 d = etree.SubElement(root, 'div', {'class': "page-header"})
153 d = etree.SubElement(root[0], 'a', {'id': root[0].text, 'style': 'pointer: hand; color:#ddd; font-size:.8em'})
158 HtmlFormat.renderers.register(core.Header, None, Header('h1'))
161 HtmlFormat.renderers.register(core.Div, None, NaturalText('div'))
163 class DivDefined(NaturalText):
164 def render(self, element, ctx):
165 output = super(DivDefined, self).render(element, ctx)
166 output[0].text = (output[0].text or '') + ':'
167 output[0].attrib['id'] = output[0].text # not so cool?
170 HtmlFormat.renderers.register(core.Div, 'defined', DivDefined('dt', {'style': 'display: inline-block'}))
173 class DivImage(NaturalText):
174 def render(self, element, ctx):
175 output = super(DivImage, self).render(element, ctx)
176 src = element.attrib.get('src', '')
177 if src.startswith('file://'):
178 src = ctx.files_path + src[7:]
179 output[0].attrib['src'] = src
180 output[0].attrib['style'] = 'display: block; width: 60%; margin: 3em auto'
183 HtmlFormat.renderers.register(core.Div, 'img', DivImage('img'))
185 HtmlFormat.renderers.register(core.Div, 'item', NaturalText('li'))
186 HtmlFormat.renderers.register(core.Div, 'list', NaturalText('ul'))
187 HtmlFormat.renderers.register(core.Div, 'list.enum', NaturalText('ol'))
189 class DivListDefinitions(NaturalText):
190 def render(self, element, ctx):
191 output = super(DivListDefinitions, self).render(element, ctx)
192 #if ctx.toc_level > 2:
193 # output[0].attrib['style'] = 'float: right'
196 HtmlFormat.renderers.register(core.Div, 'list.definitions', DivListDefinitions('ul'))
197 HtmlFormat.renderers.register(core.Div, 'p', NaturalText('p'))
200 class Section(NaturalText):
201 def subcontext(self, element, ctx):
202 return Context(ctx, toc_level=ctx.toc_level + 1)
204 def render(self, element, ctx):
205 counter = ctx.toc.add(element.meta.title(), ctx.toc_level)
206 root = super(Section, self).render(element, ctx)
207 root[0].set("id", "sect%d" % counter)
209 HtmlFormat.renderers.register(core.Section, None, Section('section'))
212 HtmlFormat.renderers.register(core.Span, None, NaturalText('span'))
213 HtmlFormat.renderers.register(core.Span, 'cite', NaturalText('cite'))
214 HtmlFormat.renderers.register(core.Span, 'cite.code', LiteralText('code'))
215 HtmlFormat.renderers.register(core.Span, 'emph', NaturalText('em'))
216 HtmlFormat.renderers.register(core.Span, 'emp', NaturalText('strong'))
218 class SpanUri(LiteralText):
219 def render(self, element, ctx):
220 root = super(SpanUri, self).render(element, ctx)
221 root[0].attrib['href'] = element.text
223 HtmlFormat.renderers.register(core.Span, 'uri', SpanUri('a'))
225 class SpanLink(LiteralText):
226 def render(self, element, ctx):
227 root = super(SpanLink, self).render(element, ctx)
228 src = element.attrib.get('href', '')
229 if src.startswith('file://'):
230 src = ctx.files_path + src[7:]
231 root[0].attrib['href'] = src
233 HtmlFormat.renderers.register(core.Span, 'link', SpanLink('a'))