self.header = etree.Element('h1')
self.footnotes = etree.Element('div', id='footnotes')
- self.counters = defaultdict(lambda: 1)
self.nota_red = etree.Element('div', id='nota_red')
def build(self, document, element=None, **kwargs):
self.document = document
-
- self.assign_ids(self.document.tree)
+ self.document.assign_ids()
self.prepare_images()
if element is None:
self.postprocess(document)
return self.output()
- def assign_ids(self, tree):
- # Assign IDs depth-first, to account for any <numeracja> inside.
- for _e, elem in etree.iterwalk(tree, events=('end',)):
- if getattr(elem, 'NUMBERING', None):
- elem.assign_id(self)
-
def prepare_images(self):
# Temporarily use the legacy method, before transitioning to external generators.
if self.gallery_path is None:
if self.with_toc:
add_table_of_contents(self.tree)
- if self.counters['fn'] > 1:
+ if self.document.counters['fn'] > 1:
fnheader = etree.Element("h3")
fnheader.text = _("Footnotes")
self.footnotes.insert(0, fnheader)
# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
#
-from collections import Counter
+from collections import defaultdict, Counter
import gettext
import os
import re
source = filename or urllib.request.urlopen(url)
tree = etree.parse(source, parser=parser)
self.tree = tree
+ self.counters = defaultdict(lambda: 1)
tree.getroot().document = self
self.preprocess()
for _e, elem in etree.iterwalk(self.tree, ('start',), 'strofa'):
elem.preprocess()
+ def assign_ids(self):
+ # Assign IDs depth-first, to account for any <numeracja> inside.
+ for _e, elem in etree.iterwalk(self.tree, events=('end',)):
+ if getattr(elem, 'NUMBERING', None):
+ elem.assign_id(self)
+
@property
def children(self):
for part_uri in self.meta.parts or []:
prefix = 'f' # default numbering prefix
return prefix
- def assign_id(self, builder):
+ def assign_id(self, document):
numbering = self.numbering
if numbering:
- number = str(builder.counters[numbering])
+ number = str(document.counters[numbering])
self.attrib['_id'] = self.id_prefix + number
- builder.counters[numbering] += 1
+ document.counters[numbering] += 1
if numbering == 'main':
- self.attrib['_visible_numbering'] = str(builder.counters['_visible'])
- builder.counters['_visible'] += 1
+ self.attrib['_visible_numbering'] = str(document.counters['_visible'])
+ document.counters['_visible'] += 1
if numbering == 'fn':
self.attrib['_visible_numbering'] = number
class Numeracja(WLElement):
NUMBERING = True
- def assign_id(self, builder):
- builder.counters['_visible'] = int(self.get('start', 1))
+
+ def assign_id(self, document):
+ document.counters['_visible'] = int(self.get('start', 1))
class Rownolegle(WLElement):
class RefTests(TestCase):
def test_snippet(self):
doc = WLDocument(filename=get_fixture('text', 'miedzy-nami-nic-nie-bylo.xml'))
-
- hb = builders['html']()
- hb.assign_ids(doc.tree)
+ doc.assign_ids()
refs = []
for ref in doc.references():