# 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
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
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',
def insert_tags(doc, split_re, tagname):
- print tagname
+ """ inserts <tagname> for every occurence of `split_re' in text nodes in the `doc' tree
+
+ >>> t = etree.fromstring('<a><b>A-B-C</b>X-Y-Z</a>');
+ >>> insert_tags(t, re.compile('-'), 'd');
+ >>> print etree.tostring(t)
+ <a><b>A<d/>B<d/>C</b>X<d/>Y<d/>Z</a>
+ """
+
for elem in doc.iter():
if elem.text:
chunks = split_re.split(elem.text)
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)
"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
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:
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")
--- /dev/null
+\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}
+}
+
<xsl:output encoding="utf-8" indent="yes" version="2.0" />
-
<xsl:template match="utwor">
<TeXML xmlns="http://getfo.sourceforge.net/texml/ns1">
<TeXML escape="0">
\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}
</TeXML>
- <xsl:apply-templates select="rdf:RDF" mode='title' />
- <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='title' />
+ <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
+ <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
<env name="document">
<cmd name="maketitle" />
+
+ <xsl:choose>
+ <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
+ <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
+ <!-- title in master -->
+ </xsl:when>
+ <xsl:otherwise>
+ <!-- look for author title in dc -->
+ <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
+ <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
+ </xsl:otherwise>
+ </xsl:choose>
<xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
<xsl:apply-templates select="utwor" mode="part" />
</env>
</xsl:template>
<xsl:template match="utwor" mode="part">
- <xsl:if test="utwor">
- <xsl:apply-templates select="rdf:RDF" mode='subtitle' />
- <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='subtitle' />
- </xsl:if>
+ <!-- title for empty dc -->
+ <xsl:choose>
+ <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
+ <!-- title in master -->
+ </xsl:when>
+ <xsl:otherwise>
+ <!-- look for title in dc -->
+ <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
+ <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
+ </xsl:otherwise>
+ </xsl:choose>
+
<xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
<xsl:apply-templates select="utwor" mode="part" />
</xsl:template>
+<!-- =================== -->
+<!-- = MAIN TITLE PAGE = -->
+<!-- = (from DC) = -->
+<!-- =================== -->
-<!-- ============================================================================== -->
-<!-- = MASTER TAG = -->
-<!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
-<!-- ============================================================================== -->
+<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
+ <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
+</xsl:template>
-<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="title">
- <xsl:apply-templates select="rdf:RDF" mode='title' />
+<xsl:template match="rdf:RDF" mode="titlepage">
+ <cmd name='title'><parm>
+ <xsl:value-of select=".//dc:title/text()" />
+ </parm></cmd>
+ <cmd name='author'><parm>
+ <xsl:value-of select="wl:person_name(.//dc:creator/text())" />
+ </parm></cmd>
</xsl:template>
-<xsl:template match="rdf:RDF" mode="title">
-<!-- TODO!
- <cmd name='title'><parm>
- <xsl:apply-templates select="dzielo_nadrzedne|podtytul" mode="header" />
- </parm></cmd>
- czytanie z tagów nazwa_utworu, autor_utworu
--->
+<!-- ============== -->
+<!-- = BOOK TITLE = -->
+<!-- = (from DC) = -->
+<!-- ============== -->
- <cmd name='title'><parm>
- <xsl:apply-templates select=".//dc:title" mode="header" />
- </parm></cmd>
- <cmd name='author'><parm>
- <xsl:apply-templates select=".//dc:author" mode="header" />
- </parm></cmd>
+<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
+ <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
</xsl:template>
-<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="subtitle">
- <xsl:apply-templates select="rdf:RDF" mode='subtitle' />
+<xsl:template match="rdf:RDF" mode="dctitle">
+ <cmd name="section*"><parm>
+ <xsl:value-of select=".//dc:title/text()" />
+ </parm></cmd>
</xsl:template>
-<xsl:template match="rdf:RDF" mode="subtitle">
- <cmd name='part*'><parm>
- <xsl:apply-templates select=".//dc:title" mode="header" />
- </parm></cmd>
+
+<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
+ <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
+</xsl:template>
+
+<xsl:template match="rdf:RDF" mode="firstdctitle">
+ <cmd name="subsection*"><parm>
+ <xsl:value-of select="wl:person_name(.//dc:creator/text())" />
+ </parm></cmd>
+ <cmd name="section*"><parm>
+ <xsl:value-of select=".//dc:title/text()" />
+ </parm></cmd>
</xsl:template>
+<!-- ============================================================================== -->
+<!-- = MASTER TAG = -->
+<!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
+<!-- ============================================================================== -->
<xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="lista_osob">
- <cmd name="par">
+ <cmd name="par"><parm>
<cmd name="textbf">
<parm><xsl:value-of select="naglowek_listy" /></parm>
</cmd>
<env name="itemize">
<xsl:apply-templates select="lista_osoba" />
</env>
- </cmd>
+ </parm></cmd>
</xsl:template>
<xsl:template match="dedykacja">
- <cmd name="raggedleft"><parm>
+ <cmd name="hspace"><parm>0.4<cmd name='linewidth' /></parm></cmd>
+ <env name="minipage"><parm>0.6<cmd name='linewidth' /></parm>
<env name="em">
<xsl:apply-templates />
</env>
- </parm></cmd>
+ </env>
</xsl:template>
<xsl:template match="kwestia">
<!-- = PARAGRAPH TAGS = -->
<!-- = (can contain inline and special tags) = -->
<!-- ========================================== -->
-<!-- Title page -->
-<xsl:template match="autor_utworu" mode="header">
- <xsl:apply-templates mode="inline" />
-</xsl:template>
-
-<xsl:template match="nazwa_utworu" mode="header">
- <xsl:apply-templates mode="inline" />
-</xsl:template>
-
-<xsl:template match="dzielo_nadrzedne" mode="header">
- <xsl:apply-templates mode="inline" />
-</xsl:template>
-<xsl:template match="podtytul" mode="header">
- <xsl:apply-templates mode="inline" />
+<!-- only in root -->
+<xsl:template match="autor_utworu" mode="title">X
+ <cmd name="subsection*"><parm>
+ <xsl:apply-templates mode="inline" />
+ </parm></cmd>
</xsl:template>
-
<xsl:template match="nazwa_utworu">
- <cmd name="pagebreak" />
<cmd name="section*"><parm>
<xsl:apply-templates mode="inline" />
</parm></cmd>
</xsl:template>
<xsl:template match="motto_podpis">
- <cmd name="raggedleft"><parm>
- <xsl:apply-templates mode="inline" />
- </parm></cmd>
+ <cmd name="hspace"><parm>0.4<cmd name='linewidth' /></parm></cmd>
+ <env name="minipage"><parm>0.6<cmd name='linewidth' /></parm>
+ <env name="em">
+ <xsl:apply-templates mode="inline" />
+ </env>
+ </env>
</xsl:template>
-
<!-- ================================================ -->
<!-- = INLINE TAGS = -->
<!-- = (contain other inline tags and special tags) = -->
<!-- ================================================ -->
+
<!-- Annotations -->
<xsl:template match="pa|pe|pr|pt" mode="inline">
<cmd name="footnote"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
</xsl:template>
<xsl:template match="wyroznienie" mode="inline">
- <!-- TODO: letterspacing 1pt -->
<cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
</xsl:template>
<xsl:template match="begin" mode="inline">
<xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
- <cmd name="mbox" />
+ <!--cmd name="mbox" />
<cmd name="marginpar">
<parm><cmd name="raggedright"><parm>
<cmd name="hspace"><parm>0pt</parm></cmd>
</parm></cmd>
</parm></cmd>
</parm></cmd></parm>
- </cmd>
+ </cmd-->
</xsl:template>
<xsl:template match="begin|end">
<!-- ======== -->
<xsl:template match="text()" />
<xsl:template match="text()" mode="inline">
- <xsl:value-of select="wl:substitute_entities(.)" />
+ <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
+ <xsl:text> </xsl:text>
+ </xsl:if>
+
+ <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
+
+ <xsl:if test="following-sibling::node() and wl:ends_white(.)">
+ <xsl:text> </xsl:text>
+ </xsl:if>
</xsl:template>