From: Radek Czajka Date: Fri, 22 Oct 2010 13:59:50 +0000 (+0200) Subject: pdf converter, still missing motifs X-Git-Tag: 1.7~263 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/167d19bdfe76a77754d2e14a80ac0c25c725c379 pdf converter, still missing motifs --- diff --git a/librarian/functions.py b/librarian/functions.py index 6d52b84..8427ba8 100644 --- a/librarian/functions.py +++ b/librarian/functions.py @@ -41,6 +41,26 @@ def reg_strip(): _register_function(strip) +def reg_starts_white(): + def starts_white(context, text): + if isinstance(text, list): + text = ''.join(text) + if not text: + return False + return text[0].isspace() + _register_function(starts_white) + + +def reg_ends_white(): + def ends_white(context, text): + if isinstance(text, list): + text = ''.join(text) + if not text: + return False + return text[-1].isspace() + _register_function(ends_white) + + def reg_wrap_words(): def wrap_words(context, text, wrapping): """XPath extension function automatically wrapping words in passed text""" @@ -63,3 +83,13 @@ def reg_wrap_words(): return '\n'.join(' '.join(line) for line in lines) _register_function(wrap_words) + +def reg_person_name(): + def person_name(context, text): + """ Converts "Name, Forename" to "Forename Name" """ + if isinstance(text, list): + text = ''.join(text) + return ' '.join([t.strip() for t in text.split(',', 1)[::-1]]) + _register_function(person_name) + + diff --git a/librarian/pdf.py b/librarian/pdf.py index 169d661..659390d 100644 --- a/librarian/pdf.py +++ b/librarian/pdf.py @@ -3,6 +3,7 @@ # This file is part of Librarian, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from __future__ import with_statement import os import os.path import shutil @@ -10,6 +11,9 @@ from StringIO import StringIO from tempfile import mkdtemp import re +import sys +sys.path.append('..') # for running from working copy + from Texml.processor import process from lxml import etree from lxml.etree import XMLSyntaxError, XSLTApplyError @@ -18,7 +22,13 @@ from librarian.parser import WLDocument from librarian import ParseError from librarian import functions + + functions.reg_substitute_entities() +functions.reg_person_name() +functions.reg_strip() +functions.reg_starts_white() +functions.reg_ends_white() STYLESHEETS = { 'wl2tex': 'xslt/wl2tex.xslt', @@ -26,7 +36,14 @@ STYLESHEETS = { def insert_tags(doc, split_re, tagname): - print tagname + """ inserts for every occurence of `split_re' in text nodes in the `doc' tree + + >>> t = etree.fromstring('A-B-CX-Y-Z'); + >>> insert_tags(t, re.compile('-'), 'd'); + >>> print etree.tostring(t) + ABCXYZ + """ + for elem in doc.iter(): if elem.text: chunks = split_re.split(elem.text) @@ -42,7 +59,7 @@ def insert_tags(doc, split_re, tagname): elem.tail = chunks.pop(0) while chunks: ins = etree.Element(tagname) - ins.tail = chunks.pop(0) + ins.tail = chunks.pop() parent.insert(ins_index, ins) @@ -58,8 +75,11 @@ def fix_hanging(doc): "nbsp") +def get_resource(path): + return os.path.join(os.path.dirname(__file__), path) + def get_stylesheet(name): - return os.path.join(os.path.dirname(__file__), STYLESHEETS[name]) + return get_resource(STYLESHEETS[name]) def transform(provider, slug, output_file=None, output_dir=None): """ produces a pdf file @@ -78,8 +98,6 @@ def transform(provider, slug, output_file=None, output_dir=None): substitute_hyphens(document.edoc) fix_hanging(document.edoc) - - print etree.tostring(document.edoc) # if output to dir, create the file if output_dir is not None: @@ -96,6 +114,8 @@ def transform(provider, slug, output_file=None, output_dir=None): fout.close() del texml + shutil.copy(get_resource('pdf/wl.sty'), temp) + shutil.copy(get_resource('pdf/wl-logo.png'), temp) print "pdflatex -output-directory %s %s" % (temp, os.path.join(temp, 'doc.tex')) if os.system("pdflatex -output-directory %s %s" % (temp, os.path.join(temp, 'doc.tex'))): raise ParseError("Error parsing .tex file") diff --git a/librarian/pdf/wl-logo.png b/librarian/pdf/wl-logo.png new file mode 100644 index 0000000..03b46b5 Binary files /dev/null and b/librarian/pdf/wl-logo.png differ diff --git a/librarian/pdf/wl.sty b/librarian/pdf/wl.sty new file mode 100644 index 0000000..f904646 --- /dev/null +++ b/librarian/pdf/wl.sty @@ -0,0 +1,72 @@ +\usepackage[MeX]{polski} +\usepackage[utf8]{inputenc} +\pagestyle{plain} +%\usepackage{antpolt} +\usepackage{graphicx} +\usepackage{fancyhdr} + +\makeatletter + +% bottom figure below footnotes +\usepackage{fnpos} +\makeFNabove + +\usepackage{color} +\definecolor{theme-gray}{gray}{.3} + +\setlength{\marginparsep}{2em} +\setlength{\marginparwidth}{8.5em} +\setlength{\oddsidemargin}{0pt} +\setlength{\voffset}{0pt} +\setlength{\topmargin}{0pt} +\setlength{\headheight}{0pt} +\setlength{\headsep}{0pt} +\setlength{\textheight}{24cm} + +\pagestyle{fancy} +\renewcommand{\headrulewidth}{0pt} +\renewcommand{\footrulewidth}{0.4pt} +\lfoot{\footnotesize \@author, \emph{\@title}} +\cfoot{} +\rfoot{\footnotesize \thepage} + +\clubpenalty=100000 +\widowpenalty=100000 +%\interlinepenalty=8000 + + +\renewcommand{\maketitle}{ + \thispagestyle{empty} + \footnotesize + \color{theme-gray} + + \noindent \begin{minipage}[t]{.35\textwidth}\vspace{0pt} + \includegraphics[width=\textwidth]{pdf/wl-logo.png} + \end{minipage} + \begin{minipage}[t]{.65\textwidth}\vspace{0pt} + Wejdź na stronę http://wolnelektury.pl/ i~zobacz, jak wiele możliwości daje interaktywna wersja szkolnej biblioteki + internetowej Wolne Lektury. + \vspace{.5em} + + Ten utwór nie jest chroniony prawem autorskim i~znajduje się w~domenie publicznej, co oznacza, że możesz go swobodnie wykorzystywać, publikować i~rozpowszechniać. + + \vspace{.6em} + + \end{minipage} + \noindent \rule{\linewidth}{0.4pt} + + \vspace{.6em} + \color{black} + + \begin{figure}[b!] + \footnotesize + \color{theme-gray} + \noindent \rule{\linewidth}{0.4pt} + + Szkolna biblioteka internetowa Wolne Lektury tworzona jest dzięki pracy Wolontariuszy, ale to osoby + zatrudnione w~FNP będą musiały wymyślić, co powinno zostać napisane w~tym miejscu. Można zupełnie usunąć + tę sekcję, ale autor konwertera chciał się popisać, że umie coś takiego zrobić. + \color{black} + \end{figure} +} + diff --git a/librarian/xslt/wl2tex.xslt b/librarian/xslt/wl2tex.xslt index 74292a7..5de6d85 100644 --- a/librarian/xslt/wl2tex.xslt +++ b/librarian/xslt/wl2tex.xslt @@ -12,33 +12,30 @@ - \documentclass[a4paper, oneside, 11pt]{book} - \usepackage[MeX]{polski} - \usepackage[utf8]{inputenc} - \pagestyle{plain} - \usepackage{antpolt} - \usepackage[bottom]{footmisc} - - \usepackage{color} - \definecolor{theme-gray}{gray}{.3} - - - \setlength{\marginparsep}{2em} - \setlength{\marginparwidth}{8.5em} - \setlength{\oddsidemargin}{0pt} - \clubpenalty=10000 - \widowpenalty=10000 + \usepackage{wl} - - + + + + + + + + + + + + + + @@ -46,52 +43,75 @@ - - - - + + + + + + + + + + + + + + + + - - - - + + + - - + + + + + + + - - + + + + - - - - - - + + - - + + + + - - - - + + + + + + + + + + + + + + + + @@ -110,22 +130,23 @@ - + - + - + 0.4 + 0.6 - + @@ -157,26 +178,15 @@ - - - - - - - - - - - - - - + +X + + + - - @@ -283,16 +293,19 @@ - - - + 0.4 + 0.6 + + + + - + @@ -318,7 +331,6 @@ - @@ -354,7 +366,7 @@ - + @@ -400,7 +412,15 @@ - + + + + + + + + +