Permalink
Browse files

Many FB2 fixes

  • Loading branch information...
1 parent cba7611 commit 3754989331c91f1d78cd5c1904f768a4cf80f07a @rczajka rczajka committed Sep 27, 2012
View
@@ -7,6 +7,7 @@
import os
import os.path
+import re
import subprocess
from StringIO import StringIO
from copy import deepcopy
@@ -109,31 +110,74 @@ def find_annotations(annotations, source, part_no):
find_annotations(annotations, child, part_no)
+class Stanza(object):
+ """
+ Converts / verse endings into verse elements in a stanza.
+
+ Slashes may only occur directly in the stanza. Any slashes in subelements
+ will be ignored, and the subelements will be put inside verse elements.
+
+ >>> s = etree.fromstring("<strofa>a/\\nb<x>x/\\ny</x>c/ \\nd</strofa>")
+ >>> Stanza(s).versify()
+ >>> print etree.tostring(s)
+ <strofa><wers_normalny>a</wers_normalny><wers_normalny>b<x>x/
+ y</x>c</wers_normalny><wers_normalny>d</wers_normalny></strofa>
+
+ """
+ def __init__(self, stanza_elem):
+ self.stanza = stanza_elem
+ self.verses = []
+ self.open_verse = None
+
+ def versify(self):
+ self.push_text(self.stanza.text)
+ for elem in self.stanza:
+ self.push_elem(elem)
+ self.push_text(elem.tail)
+ tail = self.stanza.tail
+ self.stanza.clear()
+ self.stanza.tail = tail
+ self.stanza.extend(self.verses)
+
+ def open_normal_verse(self):
+ self.open_verse = self.stanza.makeelement("wers_normalny")
+ self.verses.append(self.open_verse)
+
+ def get_open_verse(self):
+ if self.open_verse is None:
+ self.open_normal_verse()
+ return self.open_verse
+
+ def push_text(self, text):
+ if not text or not text.strip():
+ return
+ for i, verse_text in enumerate(re.split(r"/\s*\n", text)):
+ if i:
+ self.open_normal_verse()
+ verse = self.get_open_verse()
+ if len(verse):
+ verse[-1].tail = (verse[-1].tail or "") + verse_text.strip()
+ else:
+ verse.text = (verse.text or "") + verse_text.strip()
+
+ def push_elem(self, elem):
+ if elem.tag.startswith("wers"):
+ verse = deepcopy(elem)
+ verse.tail = None
+ self.verses.append(verse)
+ self.open_verse = verse
+ else:
+ appended = deepcopy(elem)
+ appended.tail = None
+ self.get_open_verse().append(appended)
+
+
def replace_by_verse(tree):
""" Find stanzas and create new verses in place of a '/' character """
stanzas = tree.findall('.//' + WLNS('strofa'))
- for node in stanzas:
- for child_node in node:
- if child_node.tag in ('slowo_obce', 'wyroznienie'):
- foreign_verses = inner_xml(child_node).split('/\n')
- if len(foreign_verses) > 1:
- new_foreign = ''
- for foreign_verse in foreign_verses:
- if foreign_verse.startswith('<wers'):
- new_foreign += foreign_verse
- else:
- new_foreign += ''.join(('<wers_normalny>', foreign_verse, '</wers_normalny>'))
- set_inner_xml(child_node, new_foreign)
- verses = inner_xml(node).split('/\n')
- if len(verses) > 1:
- modified_inner_xml = ''
- for verse in verses:
- if verse.startswith('<wers') or verse.startswith('<extra'):
- modified_inner_xml += verse
- else:
- modified_inner_xml += ''.join(('<wers_normalny>', verse, '</wers_normalny>'))
- set_inner_xml(node, modified_inner_xml)
+ for stanza in stanzas:
+ Stanza(stanza).versify()
def add_to_manifest(manifest, partno):
View
@@ -12,6 +12,28 @@
functions.reg_substitute_entities()
+functions.reg_person_name()
+
+
+def sectionify(tree):
+ """Finds section headers and adds a tree of _section tags."""
+ sections = ['naglowek_czesc',
+ 'naglowek_akt', 'naglowek_rozdzial', 'naglowek_scena',
+ 'naglowek_podrozdzial']
+ section_level = dict((v,k) for (k,v) in enumerate(sections))
+
+ # We can assume there are just subelements an no text at section level.
+ for level, section_name in reversed(list(enumerate(sections))):
+ for header in tree.findall('//' + section_name):
+ section = header.makeelement("_section")
+ header.addprevious(section)
+ section.append(header)
+ sibling = section.getnext()
+ while (sibling is not None and
+ section_level.get(sibling.tag, 1000) > level):
+ section.append(sibling)
+ sibling = section.getnext()
+
def transform(wldoc, verbose=False,
cover=None, flags=None):
@@ -32,6 +54,7 @@ def transform(wldoc, verbose=False,
style = etree.parse(style_filename)
replace_by_verse(document.edoc)
+ sectionify(document.edoc)
result = document.transform(style)
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+ This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+ Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+
+-->
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+ xmlns:wl="http://wolnelektury.pl/functions"
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
+ xmlns="http://www.gribuser.ru/xml/fictionbook/2.0"
+ xmlns:l="http://www.w3.org/1999/xlink">
+
+ <xsl:template mode="para" match="lista_osob">
+ <empty-line/>
+ <xsl:apply-templates mode="para"/>
+ <empty-line/>
+ </xsl:template>
+
+ <xsl:template mode="para" match="kwestia">
+ <empty-line/>
+ <xsl:apply-templates mode="para"/>
+ <empty-line/>
+ </xsl:template>
+
+ <xsl:template mode="para" match="lista_osoba">
+ <p><xsl:apply-templates mode="inline"/></p>
+ </xsl:template>
+
+ <xsl:template mode="para" match="naglowek_listy|naglowek_osoba">
+ <p><strong><xsl:apply-templates mode="inline"/></strong></p>
+ </xsl:template>
+
+ <xsl:template mode="para" match="miejsce_czas|didaskalia|didask_tekst">
+ <p><emphasis><xsl:apply-templates mode="inline"/></emphasis></p>
+ </xsl:template>
+
+ <xsl:template mode="inline" match="didaskalia|didask_tekst">
+ <emphasis><xsl:apply-templates mode="inline"/></emphasis>
+ </xsl:template>
+
+</xsl:stylesheet>
@@ -7,6 +7,7 @@
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wl="http://wolnelektury.pl/functions"
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns="http://www.gribuser.ru/xml/fictionbook/2.0"
xmlns:l="http://www.w3.org/1999/xlink">
@@ -16,6 +17,7 @@
<xsl:include href="paragraphs.xslt"/>
<xsl:include href="poems.xslt"/>
<xsl:include href="sections.xslt"/>
+ <xsl:include href="drama.xslt"/>
<xsl:strip-space elements="*"/>
<xsl:output encoding="utf-8" method="xml" indent="yes"/>
@@ -31,12 +33,13 @@
</xsl:template>
<!-- we can't handle lyrics nicely yet -->
- <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp" mode="outer">
+ <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp" mode="outer">
<body> <!-- main body for main book flow -->
<xsl:if test="autor_utworu or nazwa_utworu">
<title>
<xsl:apply-templates mode="title"
- select="autor_utworu|dzielo_nadrzedne|nazwa_utworu"/>
+ select="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul"/>
+ <xsl:call-template name="translators" />
</title>
</xsl:if>
@@ -49,24 +52,7 @@
</p>
</epigraph>
- <xsl:variable name="sections" select="count(naglowek_rozdzial)"/>
- <section>
- <xsl:choose>
- <xsl:when test="local-name() = 'liryka_l'">
- <poem>
- <xsl:apply-templates mode="para"/>
- </poem>
- </xsl:when>
-
- <xsl:otherwise>
- <xsl:apply-templates mode="para"
- select="*[count(following-sibling::naglowek_rozdzial)
- = $sections]"/>
- </xsl:otherwise>
- </xsl:choose>
- </section>
-
- <xsl:apply-templates mode="sections"/>
+ <xsl:call-template name="section" />
</body>
</xsl:template>
@@ -79,6 +65,23 @@
<p><xsl:apply-templates mode="inline"/></p>
</xsl:template>
+ <xsl:template name="translators">
+ <xsl:if test="//dc:contributor.translator">
+ <p>
+ <xsl:text>tłum. </xsl:text>
+ <xsl:for-each select="//dc:contributor.translator">
+ <xsl:if test="position() != 1">, </xsl:if>
+ <xsl:apply-templates mode="person" />
+ </xsl:for-each>
+ </p>
+ </xsl:if>
+ </xsl:template>
+
+ <xsl:template match="text()" mode="person">
+ <xsl:value-of select="wl:person_name(.)" />
+ </xsl:template>
+
+
<xsl:template match="uwaga" mode="title"/>
<xsl:template match="extra" mode="title"/>
</xsl:stylesheet>
@@ -12,21 +12,24 @@
xmlns:l="http://www.w3.org/1999/xlink">
<!-- footnote body mode -->
- <xsl:template match="pe" mode="footnotes">
+ <xsl:template match="pa|pe|pr|pt" mode="footnotes">
<!-- we number them absolutely -->
- <xsl:variable name="n" select="count(preceding::pe) + 1"/>
+ <xsl:variable name="n" select="count(preceding::pa) + count(preceding::pe) + count(preceding::pr) + count(preceding::pt) + 1"/>
<xsl:element name="section">
<xsl:attribute name="id">fn<xsl:value-of select="$n"/></xsl:attribute>
- <p><xsl:apply-templates mode="inline"/></p>
+ <p><xsl:apply-templates mode="inline"/>
+ <xsl:if test="local-name() = 'pa'">
+ <xsl:text> [przypis autorski]</xsl:text>
+ </xsl:if></p>
</xsl:element>
</xsl:template>
<xsl:template match="text()" mode="footnotes"/>
<!-- footnote links -->
- <xsl:template match="pe" mode="inline">
- <xsl:variable name="n" select="count(preceding::pe) + 1"/>
+ <xsl:template match="pa|pe|pr|pt" mode="inline">
+ <xsl:variable name="n" select="count(preceding::pa) + count(preceding::pe) + count(preceding::pr) + count(preceding::pt) + 1"/>
<xsl:element name="a">
<xsl:attribute name="type">note</xsl:attribute>
<xsl:attribute name="l:href">#fn<xsl:value-of select="$n"/></xsl:attribute>
@@ -17,12 +17,19 @@
<xsl:template match="motyw" mode="inline"/>
<!-- formatting -->
- <xsl:template match="slowo_obce|tytul_dziela">
+ <xsl:template match="slowo_obce" mode="inline">
<emphasis>
<xsl:apply-templates mode="inline"/>
</emphasis>
</xsl:template>
- <xsl:template match="wyroznienie">
+ <xsl:template match="tytul_dziela" mode="inline">
+ <emphasis>
+ <xsl:if test="@typ">„</xsl:if>
+ <xsl:apply-templates mode="inline"/>
+ <xsl:if test="@typ">”</xsl:if>
+ </emphasis>
+ </xsl:template>
+ <xsl:template match="wyroznienie" mode="inline">
<strong>
<xsl:apply-templates mode="inline"/>
</strong>
@@ -13,12 +13,34 @@
<!-- in paragraph mode -->
- <xsl:template mode="para" match="akap|akap_dialog">
+ <xsl:template mode="para" match="akap|akap_dialog|akap_cd|motto_podpis">
<!-- paragraphs & similar -->
<p><xsl:apply-templates mode="inline"/></p>
</xsl:template>
+ <xsl:template mode="para" match="dlugi_cytat|motto|dedykacja|nota">
+ <cite><xsl:apply-templates mode="para"/></cite>
+ </xsl:template>
+
+ <xsl:template mode="para" match="srodtytul">
+ <p><strong><xsl:apply-templates mode="inline"/></strong></p>
+ </xsl:template>
+
+ <xsl:template mode="para" match="sekcja_swiatlo">
+ <empty-line/><empty-line/><empty-line/>
+ </xsl:template>
+
+ <xsl:template mode="para" match="sekcja_asterysk">
+ <empty-line/><p>*</p><empty-line/>
+ </xsl:template>
+
+ <xsl:template mode="para" match="separator_linia">
+ <empty-line/><p>————————</p><empty-line/>
+ </xsl:template>
+
+
+
<xsl:template mode="para" match="*"/>
<xsl:template mode="sections" match="*"/>
</xsl:stylesheet>
@@ -33,7 +33,7 @@
puts it here -->
<xsl:template match="motyw" mode="poem"/>
- <xsl:template mode="poem" match="wers_normalny">
+ <xsl:template mode="poem" match="wers_normalny|wers_cd|wers_wciety|wers_akap">
<v><xsl:apply-templates mode="inline"/></v>
</xsl:template>
</xsl:stylesheet>
Oops, something went wrong.

0 comments on commit 3754989

Please sign in to comment.