From: Jan Szejko Date: Thu, 15 Mar 2018 15:06:46 +0000 (+0100) Subject: eliminate weasy references in code (mostly) X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/f5c1682d0da0aee458474ef5a9000dd3d62171f4 eliminate weasy references in code (mostly) --- diff --git a/librarian/parser.py b/librarian/parser.py index d515954..12fffb3 100644 --- a/librarian/parser.py +++ b/librarian/parser.py @@ -171,10 +171,6 @@ class WLDocument(object): from librarian import pyhtml as html return html.transform(self, *args, **kwargs) - def as_weasy(self, *args, **kwargs): - from librarian import weasy - return weasy.transform(self, *args, **kwargs) - def as_text(self, *args, **kwargs): from librarian import text return text.transform(self, *args, **kwargs) diff --git a/librarian/pdf-from-html/activity-kind.svg b/librarian/pdf-from-html/activity-kind.svg new file mode 100644 index 0000000..21f93a0 --- /dev/null +++ b/librarian/pdf-from-html/activity-kind.svg @@ -0,0 +1,85 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/librarian/pdf-from-html/activity-time.svg b/librarian/pdf-from-html/activity-time.svg new file mode 100644 index 0000000..f9db82e --- /dev/null +++ b/librarian/pdf-from-html/activity-time.svg @@ -0,0 +1,89 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/librarian/pdf-from-html/activity-tools.svg b/librarian/pdf-from-html/activity-tools.svg new file mode 100644 index 0000000..63cd72a --- /dev/null +++ b/librarian/pdf-from-html/activity-tools.svg @@ -0,0 +1,79 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + + + + diff --git a/librarian/pdf-from-html/bullet.svg b/librarian/pdf-from-html/bullet.svg new file mode 100644 index 0000000..93038b3 --- /dev/null +++ b/librarian/pdf-from-html/bullet.svg @@ -0,0 +1,67 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/librarian/pdf-from-html/pdf-style.css b/librarian/pdf-from-html/pdf-style.css new file mode 100644 index 0000000..e0ce17b --- /dev/null +++ b/librarian/pdf-from-html/pdf-style.css @@ -0,0 +1,506 @@ +@charset "UTF-8"; +/*@import url(https://fonts.googleapis.com/css?family=Dosis:400,700&subset=latin,latin-ext);*/ + +@page { + size: A4; + margin: 1.5cm; + @bottom-center { + counter-increment: page; + content: "Strona " counter(page); + font-family: Dosis; + font-size: 12pt; + } +} + +a { + text-decoration: none; + color: #ed7831; } + a img { + width: 8cm; + border: 0; + padding: 0; + display: block; + margin-left: auto; + margin-right: auto; } + +ul { + list-style-image: url('bullet.svg'); +} + +h1, h2, h3 { + page-break-after: avoid; +} + +body { + font-family: Dosis; + background: white; + color: #363a3e; + margin: 0; } + +.link-list { + list-style: none; + padding: 0; } + .link-list li { + margin-bottom: .5em; } + .link-list a:before { + content: "→"; + margin-right: .5em; } + .link-list a { + color: #363a3e; } + .link-list a:hover { + color: #ed7831; } + +.link-list-colored a { + color: #ed7831; } + +.plain { + margin: 0; + padding: 0; + list-style: none; } + .plain li { + margin: 1em 0; } + +.lesson-footer { + clear: both; + border-top: 1px solid #777; + margin-top: 2em; + padding-top: 1em; } + .lesson-footer .section-info { + text-align: center; } + .lesson-footer .previous-lesson { + float: left; } + .lesson-footer .next-lesson { + float: right; } + +ol.alpha { + counter-reset: list; } + ol.alpha > li { + list-style: none; + position: relative; } + ol.alpha > li:before { + counter-increment: list; + content: counter(list,lower-alpha) ") "; + position: absolute; + left: -1.4em; } + +blockquote { + font-size: 0.875em; } + +/* =================== */ +/* = Custom elements = */ +/* =================== */ +div.stanza { + margin: 1.5em 0 0; } + +p.paragraph { + text-align: justify; + margin: 1.5em 0 0; } + +hr.spacer { + height: 3em; + visibility: hidden; } + +hr.spacer-line { + margin: 1.5em 0; + border: none; + border-bottom: 0.1em solid #000; } + +p.spacer-asterisk { + padding: 0; + margin: 1.5em 0; + text-align: center; } + +table.framed { + border-collapse: collapse; +} + +table.framed td, table.framed.th { + border: 1px #888 solid; +} + +.activities-header, #slowniczek { + color: white; + background: #ed7831; + display: inline-block; + padding: 0.1cm 0.3cm; + margin-bottom: -0.1cm; + border-radius: 0.3cm 0.3cm 0 0; + page-break-after: avoid; +} + +#slowniczek { + margin-left: 2cm; +} + +.activity { + clear: both; } + +.activity.first { + margin-top: 0; + page-break-before: avoid; +} + +.activity .text { + padding-top: 0.5cm; +} + +.activity.odd .text { + border: thick solid #ed7831; + margin-right: -3cm; + padding-right: 3cm; + padding-bottom: 1cm; +} + +.slowniczek { + border: thick solid #ed7831; + margin-left: 2cm; + margin-right: -3cm; + padding: 0.5cm 3cm 0.5cm 0.5cm; + page-break-before: avoid; +} + +.activity.even .text { + padding-bottom: 0.5cm; + margin-left: 2cm; +} + +.activity .description { + width: 13cm; + margin-left: 2cm; + position: relative; +} +.activity .description p:first-child { + margin-top: 0; } + +.activity { + margin-top: 2em; } + .activity.odd { + page-break-inside: avoid; } + .activity .act_counter { + position: absolute; + top: 0; + left: -1.2cm; + font-weight: bold; + font-size: 2em; } + .activity.odd .info { + float: right; + position: relative; + top: -0.8cm; + margin-bottom: -0.8cm; + } + .activity .info { + width: 50%; + padding: 0.3cm; + border: thick solid #ed7831; + border-radius: 0.8cm; + page-break-before: avoid; + page-break-inside: avoid; + background-color: white; } + .activity .info .infobox { + padding: 1em 0; + border-top: 1px solid #c9ccce; } + .activity .info .infobox th { + text-transform: uppercase; + margin: 0 0 0.5em -2.1875em; + padding-left: 2.1875em; + line-height: 24px; + font-size: 1em; } + .activity .info .infobox p { + margin: 0; } + .activity table.info { border-spacing: 0.2cm } + .activity .info th, .activity .info td { vertical-align: top; } + .activity .info th { + background: no-repeat 0 0; + background-size: 25px 25px; } + .activity .info .time th { + background-image: url("activity-time.svg"); } + .activity .info .kind th { + background-image: url("activity-kind.svg"); } + .activity .info .materials th { + background-image: url("activity-tools.svg"); } + +.materials-list { + border: thick solid #ed7831; + border-radius: 0.5cm; + width: 60%; + padding: 0 0.3cm 0.3cm; + margin-top: 0.5cm; + page-break-inside: avoid; +} +.materials-list p { + margin-top: 0.2cm; + margin-bottom: 0.2cm; +} + +dl { + width: 100%; + padding: 0; + margin: 0; +} +dt { + display: inline-block; + width: 20%; + padding: 0.1cm; + margin: 0; + font-weight: bold; + vertical-align: top; +} +dd { + display: inline-block; + width: 70%; + padding: 0.1cm; + margin: 0; + vertical-align: top; +} + +.lista .paragraph { + margin: .3em 0; } +.lista li { + margin: .75em 0; } + +.clearboth { + clear: both; } + +#book-text .top-link { + margin-top: 1em; } + +.help { + font-size: .7em; + padding: 0 .5em; + color: #888; + vertical-align: super; } + +.exercise img { + vertical-align: top; } +.exercise .question-piece { + border-radius: 0.6875em; + padding: 0.5em 0.6875em; } +.exercise .lista.punkt { + list-style: none; + margin: 0; + padding: 0; } +.exercise .lista li { + margin: 0.3125em 0; } +.exercise .question-piece.correct { + background-color: #16a487; + color: white; } +.exercise .question-piece.incorrect { + background-color: #f00; + color: white; } +.exercise .placeholder.dragover { + background: #ed7831 !important; } +.exercise .remove { + float: right; + display: inline-block; + border-radius: 0.5em; + padding: 0.25em 0.5em; + margin: -0.25em -0.5em -0.25em 0.5em; + background: #ed7831; + color: white; } +.exercise .buttons { + background: #5e6165; + border-radius: 0.5625em; + height: 1.875em; + line-height: 1.875em; + overflow: hidden; + color: white; + margin-top: 1.875em; } + .exercise .buttons input { + height: 100%; + border: 0; + background: #8e9093; + color: white; + text-transform: uppercase; + font-weight: bold; } + .exercise .buttons .message { + float: right; + padding: 0 1.25em; + background: red; } + .exercise .buttons .maxscore { + background: #16a487; } + .exercise .buttons input.check { + background: #ed7831; } + +.exercise .instruction { + display: block; + margin: .3em; + color: green; + font-size: .9em; } +.exercise .instruction:before { + content: "☞ "; } + +.luki .question-piece { + background-color: #d4d6d8; + color: #363a3e; + z-index: 2; + display: inline-block; + margin: 0.3125em; + max-width: 38em; } +.luki .question-piece.disabled { + background-color: #eee; + color: #d4d6d8; } +.luki .placeholder { + border-radius: 0.6875em; + padding: 0.5em 0.6875em; + display: inline-block; + margin: 0.3125em; + width: 4em; + background-color: #eee; + z-index: 1; } +.luki .placeholder:after { + content: "\0000a0"; } + +.zastap .question-piece { + background-color: #d4d6d8; + color: #363a3e; + display: inline-block; + margin: 0.3125em; + z-index: 2; } +.zastap .question-piece.disabled { + background-color: #eee; + color: #d4d6d8; } +.zastap .question-piece.placeholder { + background-color: inherit; + color: inherit; + z-index: inherit; + display: inline; + margin: 0; + padding: 0; + border-radius: 0; } + +.uporzadkuj .question-piece { + background-color: #d4d6d8; + color: #363a3e; + z-index: 2; } +.uporzadkuj .question-piece.disabled { + background-color: #eee; + color: #d4d6d8; } +.uporzadkuj .lista { + list-style: none; + margin: 0; + padding: 0; + counter-reset: answer; } + .uporzadkuj .lista li { + counter-increment: answer; + padding-right: 3em; + background: url("/static/img/ornaments/draggable.c03941f77eb9.png") no-repeat 100% 50%; + } + .uporzadkuj .lista li:before { + border-radius: 0.6875em; + padding: 0.5em 0.6875em; + background-color: #d4d6d8; + color: #363a3e; + z-index: 2; + content: counter(answer); + float: left; + margin: -.5em 0 0 -3em; } + .uporzadkuj .lista li.ui-sortable-placeholder { + counter-increment: answer 0; } + .uporzadkuj .lista li.ui-sortable-helper:before { + content: none; } + +.przyporzadkuj .question-piece { + background-color: #d4d6d8; + color: #363a3e; + z-index: 2; + width: 38.625em; } +.przyporzadkuj .question-piece.short { + display: inline-block; + margin: .2em .1em; + width: auto; } +.przyporzadkuj span.question-piece { + display: inline-block; + margin: .1em; + width: auto; } +.przyporzadkuj .question-piece.disabled { + background-color: #eee; + color: #d4d6d8; } +.przyporzadkuj .predicate { + list-style: none; + margin: 0; + padding: 0; } + .przyporzadkuj .predicate > li { + border-radius: 0.6875em; + padding: 0.5em 0.6875em; + display: inline-block; + background-color: #5e6165; + color: white; + width: 11.75em; + position: relative; + vertical-align: top; } + .przyporzadkuj .predicate > li .subjects { + list-style: none; + min-height: 2.8125em; + position: relative; + margin: 0.5em -0.6875em -0.5em -0.6875em; + padding: 0.5em 0.6875em; } + .przyporzadkuj .predicate > li .subjects li { + width: auto; } + .przyporzadkuj .predicate > li .subjects .placeholder { + border-radius: 0.6875em; + padding: 0.5em 0.6875em; + text-align: right; + position: relative; + margin: 0 -0.6875em; } + .przyporzadkuj .predicate > li .subjects .multiple { + position: absolute; + z-index: -1; + top: 0; + bottom: 0; + left: 0; + right: 0; + margin: 0; } + .przyporzadkuj .predicate > li .subjects .placeholder.dragover { + z-index: 1; } + .przyporzadkuj .predicate > li .subjects .placeholder:after { + content: "upuść tutaj"; } +.przyporzadkuj .subject { + list-style: none; + margin: 0; + padding: 0; } + +.prawdafalsz .question li.question-piece { + position: relative; + padding-bottom: 0; + padding-top: 0; } +.prawdafalsz .question .question-piece-text { + display: inline-block; + width: 10cm; + vertical-align: top; + margin-top: 0.3cm; +}.prawdafalsz .question .buttons { + margin-top: 0; + top: 0.2cm; + left: 0; + position: relative; + background-color: #d4d6d8; + color: #363a3e; + display: inline-block; + margin-right: 0.5cm; } + .prawdafalsz .question .buttons a { + color: #363a3e; + text-transform: uppercase; + font-weight: bold; + display: inline-block; + padding: 0 0.6875em; } + .prawdafalsz .question .buttons a.chosen, + [data-solution="false"] .false, + [data-solution="true"] .true { + background: #ed7831; + color: white; + border: 4px solid black; + height: 29px; + box-sizing: border-box; + line-height: initial; } + [data-solution="false"] .false { + border-top-right-radius: 8px; + border-bottom-right-radius: 8px; } + [data-solution="true"] .true { + border-top-left-radius: 8px; + border-bottom-left-radius: 8px; } + +.wybor .question .lista { + list-style: none; + padding: 0; } + .wybor .question .lista li { + margin: 0; } diff --git a/librarian/pdf_from_html.py b/librarian/pdf_from_html.py new file mode 100644 index 0000000..b299abf --- /dev/null +++ b/librarian/pdf_from_html.py @@ -0,0 +1,722 @@ +# -*- coding: utf-8 -*- +# +# This file is part of Librarian, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +import os +import shutil +from tempfile import mkdtemp, NamedTemporaryFile + +from lxml import etree +from subprocess import call + +from librarian import IOFile, Format, ParseError, get_resource +from xmlutils import Xmill, tag, tagged, ifoption, tag_open_close +from librarian import functions +import re +import random +from copy import deepcopy + +IMAGE_THUMB_WIDTH = 300 + + +class EduModule(Xmill): + def __init__(self, options=None): + super(EduModule, self).__init__(options) + self.activity_counter = 0 + self.activity_last = None + self.exercise_counter = 0 + + # text filters + def swap_endlines(txt): + if self.options['strofa']: + txt = txt.replace("/\n", "
\n") + return txt + self.register_text_filter(functions.substitute_entities) + self.register_escaped_text_filter(swap_endlines) + + @tagged('div', 'stanza') + def handle_strofa(self, element): + self.options = {'strofa': True} + return "", "" + + def handle_powiesc(self, element): + return u""" + + + + Edukacja medialna + + + + +
+""", u""" +
+ +""" + + handle_autor_utworu = tag("span", "author") + handle_dzielo_nadrzedne = tag("span", "collection") + handle_podtytul = tag("span", "subtitle") + handle_naglowek_akt = handle_naglowek_czesc = handle_srodtytul = tag("h2") + handle_naglowek_scena = tag('h2') + handle_naglowek_osoba = tag('h3') + handle_akap = handle_akap_dialog = handle_akap_cd = tag('p', 'paragraph') + + handle_wyroznienie = tag('em') + handle_tytul_dziela = tag('em', 'title') + handle_slowo_obce = tag('em', 'foreign') + + def naglowek_to_anchor(self, naglowek): + return self.options['urlmapper'].naglowek_to_anchor(naglowek) + + def handle_nazwa_utworu(self, element): + return "

", "

" + + def handle_naglowek_rozdzial(self, element): + return tag_open_close("h2", id=self.naglowek_to_anchor(element)) + + def handle_naglowek_podrozdzial(self, element): + self.activity_counter = 0 + if element.text.strip() == u'Przebieg zajęć': + return tag('h3', 'activities-header')(self, element) + return tag('h3')(self, element) + + def handle_uwaga(self, _e): + return None + + def handle_aktywnosc(self, element): + self.activity_counter += 1 + parity = 'odd' if self.activity_counter % 2 == 1 else 'even' + if self.activity_counter == 1: + parity += ' first' + self.options = { + 'activity': True, + 'activity_counter': self.activity_counter, + } + submill = EduModule(dict(self.options.items() + {'sub_gen': True}.items())) + + if element.xpath('opis'): + opis = submill.generate(element.xpath('opis')[0]) + else: + opis = '' + + n = element.xpath('wskazowki') + if n: + wskazowki = submill.generate(n[0]) + else: + wskazowki = '' + + n = element.xpath('pomoce') + if n: + pomoce = submill.generate(n[0]) + else: + pomoce = '' + + forma = ''.join(element.xpath('forma/text()')) + get_forma_url = self.options['urlmapper'].get_forma_url + forms = [] + for form_name in forma.split(','): + name = form_name.strip() + url = get_forma_url(name) + if url: + forms.append("%s" % (url, name)) + else: + forms.append(name) + forma = ', '.join(forms) + if forma: + forma = 'Metoda

%s

' % forma + + czas = ''.join(element.xpath('czas/text()')) + if czas: + czas = 'Czas

%s min

' % czas + + counter = self.activity_counter + + if element.getnext().tag == 'aktywnosc' or (len(self.activity_last) and self.activity_last.getnext() == element): + counter_html = """%(counter)d.""" % {'counter': counter} + else: + counter_html = '' + + self.activity_last = element + + return ( + u""" +
+
+
+ %(counter_html)s + %(opis)s""" % {'counter_html': counter_html, 'opis': opis, 'parity': parity}, + u"""%(wskazowki)s +
+
+ + %(czas)s + %(forma)s + %(pomoce)s +
+
+
+""" % {'wskazowki': wskazowki, 'czas': czas, 'forma': forma, 'pomoce': pomoce}) + + handle_opis = ifoption(sub_gen=True)(tag('div', 'desc')) + handle_wskazowki = ifoption(sub_gen=True)(tag('div', ('hints', 'teacher'))) + + @ifoption(sub_gen=True) + @tagged('tr', 'infobox materials') + def handle_pomoce(self, _): + return """Pomoce""", "" + + def handle_czas(self, *_): + return + + def handle_forma(self, *_): + return + + def handle_cwiczenie(self, element): + exercise_handlers = { + 'wybor': Wybor, + 'uporzadkuj': Uporzadkuj, + 'luki': Luki, + 'zastap': Zastap, + 'przyporzadkuj': Przyporzadkuj, + 'prawdafalsz': PrawdaFalsz + } + + typ = element.attrib['typ'] + self.exercise_counter += 1 + self.options = {'exercise_counter': self.exercise_counter} + handler = exercise_handlers[typ](self.options) + return handler.generate(element) + + # Lists + def handle_lista(self, element, attrs=None): + if attrs is None: + attrs = {} + ltype = element.attrib.get('typ', 'punkt') + if not element.findall("punkt"): + if ltype == 'czytelnia': + return '

W przygotowaniu.

' + else: + return None + if ltype == 'slowniczek': + surl = element.attrib.get('src', None) + if surl is None: + # print '** missing src on , setting default' + surl = 'http://edukacjamedialna.edu.pl/lekcje/slowniczek/' + sxml = etree.fromstring(self.options['provider'].by_uri(surl).get_string()) + + self.options = {'slowniczek': True, 'slowniczek_xml': sxml} + return '
', '
' + + listtag = { + 'num': 'ol', + 'punkt': 'ul', + 'alfa': 'ul', + 'czytelnia': 'ul'}[ltype] + + classes = attrs.get('class', '') + if classes: + del attrs['class'] + + attrs_s = ' '.join(['%s="%s"' % kv for kv in attrs.items()]) + if attrs_s: + attrs_s = ' ' + attrs_s + + return '<%s class="lista %s %s"%s>' % (listtag, ltype, classes, attrs_s), '' % listtag + + def handle_punkt(self, element): + if self.options['slowniczek']: + return '', '' + else: + return '
  • ', '
  • ' + + def handle_definiendum(self, element): + nxt = element.getnext() + definiens_s = '' + + if not element.text: + print "!! Empty " + return None + + # let's pull definiens from another document + if self.options['slowniczek_xml'] is not None and (nxt is None or nxt.tag != 'definiens'): + sxml = self.options['slowniczek_xml'] + if "'" in (element.text or ''): + defloc = sxml.xpath("//definiendum[text()=\"%s\"]" % (element.text or '').strip()) + else: + defloc = sxml.xpath("//definiendum[text()='%s']" % (element.text or '').strip()) + if defloc: + definiens = defloc[0].getnext() + if definiens.tag == 'definiens': + subgen = EduModule(self.options) + definiens_s = subgen.generate(definiens) + else: + print ("!! Missing definiendum in source: '%s'" % element.text).encode('utf-8') + + return u"
    " % self.naglowek_to_anchor(element), u"
    " + definiens_s + + def handle_definiens(self, element): + return u"
    ", u"
    " + + def handle_podpis(self, element): + return u"""
    """, u"
    " + + def handle_tabela(self, element): + has_frames = int(element.attrib.get("ramki", "0")) + frames_c = "framed" if has_frames else "" + return u"""""" % frames_c, u"
    " + + def handle_wiersz(self, element): + return u"", u"" + + def handle_kol(self, element): + return u"", u"" + + def handle_rdf__RDF(self, _): + # ustal w opcjach rzeczy :D + return + + def handle_link(self, element): + if 'url' in element.attrib: + return tag('a', href=element.attrib['url'])(self, element) + elif 'material' in element.attrib: + material_err = u' [BRAKUJĄCY MATERIAŁ]' + slug = element.attrib['material'] + + def make_url(f): + return self.options['urlmapper'].url_for_material(slug, f) + + formats = self.options['urlmapper'].materials(slug) + + try: + def_href = make_url(formats[0][0]) + def_err = u"" + except (IndexError, self.options['urlmapper'].MaterialNotFound): + def_err = material_err + def_href = u"" + fmt_links = [] + for f in formats[1:]: + try: + fmt_links.append(u'%s' % (make_url(f[0]), f[0].upper())) + except self.options['urlmapper'].MaterialNotFound: + fmt_links.append(u'%s%s' % (f[0].upper(), material_err)) + more_links = u' (%s)' % u', '.join(fmt_links) if fmt_links else u'' + + return u"" % def_href, u'%s%s' % (def_err, more_links) + + def handle_obraz(self, element): + format = self.options['urlmapper'] + name = element.attrib.get('nazwa', '').strip() + if not name: + print '!! missing "nazwa"' + return + alt = element.attrib.get('alt', '') + if not alt: + print '** missing "alt"' + slug, ext = name.rsplit('.', 1) + image = format.image(slug, ext) + name = image.name.rsplit('/', 1)[-1] + e = etree.Element("a", attrib={"class": "image"}) + e.append(etree.Element("img", attrib={ + "src": name, + "alt": alt, + "width": str(IMAGE_THUMB_WIDTH)})) + format.attachments[name] = self.options['media_root'] + image.name + return etree.tostring(e, encoding=unicode), u"" + + def handle_video(self, element): + url = element.attrib.get('url') + if not url: + print '!!