pdf with motifs (dirty hack for marginpar-orphan problem)
authorRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 25 Oct 2010 14:24:18 +0000 (16:24 +0200)
committerRadek Czajka <radoslaw.czajka@nowoczesnapolska.org.pl>
Mon, 25 Oct 2010 14:24:18 +0000 (16:24 +0200)
librarian/pdf.py
librarian/pdf/wl.sty
librarian/xslt/wl2tex.xslt

index 659390d..b9ead15 100644 (file)
@@ -10,6 +10,7 @@ import shutil
 from StringIO import StringIO
 from tempfile import mkdtemp
 import re
+from copy import deepcopy
 
 import sys
 sys.path.append('..') # for running from working copy
@@ -96,6 +97,32 @@ def transform(provider, slug, output_file=None, output_dir=None):
 
         document = load_including_children(provider, slug)
 
+        # dirty hack for the marginpar-creates-orphans LaTeX problem
+        # see http://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/2304
+        for motif in document.edoc.findall('//strofa//motyw'):
+            # find relevant verse-level tag
+            verse, stanza = motif, motif.getparent()
+            while stanza is not None and stanza.tag != 'strofa':
+                verse, stanza = stanza, stanza.getparent()
+            breaks_before = sum(1 for i in verse.itersiblings('br', preceding=True))
+            breaks_after = sum(1 for i in verse.itersiblings('br'))
+            if (breaks_before == 0 and breaks_after > 0) or breaks_after == 1:
+                move_by = 1
+                if breaks_after == 2:
+                    move_by += 1
+                moved_motif = deepcopy(motif)
+                motif.tag = 'span'
+                motif.text = None
+                moved_motif.tail = None
+                moved_motif.set('moved', str(move_by))
+
+                for br in verse.itersiblings(tag='br'):
+                    if move_by > 1:
+                        move_by -= 1
+                        continue
+                    br.addnext(moved_motif)
+                    break
+
         substitute_hyphens(document.edoc)
         fix_hanging(document.edoc)
 
index f904646..0f60d20 100644 (file)
@@ -12,7 +12,7 @@
 \makeFNabove
 
 \usepackage{color}
-\definecolor{theme-gray}{gray}{.3}
+\definecolor{theme}{gray}{.3}
 
 \setlength{\marginparsep}{2em}
 \setlength{\marginparwidth}{8.5em}
@@ -26,9 +26,9 @@
 \pagestyle{fancy}
 \renewcommand{\headrulewidth}{0pt}
 \renewcommand{\footrulewidth}{0.4pt}
-\lfoot{\footnotesize \@author, \emph{\@title}}
+\lfoot{{\footnotesize \@author, \emph{\@title}}}
 \cfoot{}
-\rfoot{\footnotesize \thepage}
+\rfoot{{\footnotesize \thepage}}
 
 \clubpenalty=100000
 \widowpenalty=100000
 
 
 \renewcommand{\maketitle}{
+    {
     \thispagestyle{empty}
     \footnotesize
-    \color{theme-gray}
+    \color{theme}
 
     \noindent \begin{minipage}[t]{.35\textwidth}\vspace{0pt}
         \includegraphics[width=\textwidth]{pdf/wl-logo.png}
@@ -60,7 +61,7 @@
 
     \begin{figure}[b!]
         \footnotesize
-        \color{theme-gray}
+        \color{theme}
         \noindent \rule{\linewidth}{0.4pt}
 
         Szkolna biblioteka internetowa Wolne Lektury tworzona jest dzięki pracy Wolontariuszy, ale to osoby
@@ -68,5 +69,5 @@
         tę sekcję, ale autor konwertera chciał się popisać, że umie coś takiego zrobić.
         \color{black}
     \end{figure}
+    }
 }
-
index 5de6d85..dd0a878 100644 (file)
 <!-- Themes -->
 
 
-<xsl:template match="begin" mode="inline">
-    <xsl:variable name="mnum" select="concat('m', substring(@id, 2))" />
-    <!--cmd name="mbox" />
-    <cmd name="marginpar">
-        <parm><cmd name="raggedright"><parm>
-            <cmd name="hspace"><parm>0pt</parm></cmd>
-            <cmd name="footnotesize"><parm>
-                <cmd name="color"><parm>theme-gray</parm><parm>
-                    <xsl:value-of select="string(following::motyw[@id=$mnum]/text())" />
-                </parm></cmd>
-            </parm></cmd>
-        </parm></cmd></parm>
-    </cmd-->
-</xsl:template>
-
-<xsl:template match="begin|end">
+<xsl:template match="begin|end|motyw">
     <xsl:apply-templates select='.' mode="inline" />
 </xsl:template>
 
+<xsl:template match="begin" mode="inline" />
 <xsl:template match="end" mode="inline" />
-<xsl:template match="motyw" mode="inline" />
+
+<xsl:template match="motyw" mode="inline">
+    <cmd name="mbox" />
+    <cmd name="marginpar">
+        <parm>
+            <cmd name="vspace"><parm>-8pt</parm></cmd>
+            <xsl:if test="@moved">
+                <cmd name="vspace"><parm>-<xsl:value-of select="@moved" /><cmd name="baselineskip" /></parm></cmd>
+            </xsl:if>
+            <cmd name="raggedright"><parm>
+                <cmd name="hspace"><parm>0pt</parm></cmd>
+                <cmd name="footnotesize"><parm>
+                    <cmd name="color"><parm>theme</parm><parm>
+                        <xsl:value-of select="." />
+                    </parm></cmd>
+                </parm></cmd>
+            </parm></cmd>
+        </parm>
+    </cmd>
+</xsl:template>
 
 
 <!-- ============== -->