From: Radek Czajka Date: Wed, 18 Sep 2024 10:40:41 +0000 (+0200) Subject: Counting belongs in document. X-Git-Tag: 24.9~1 X-Git-Url: https://git.mdrn.pl/librarian.git/commitdiff_plain/eb25cada3f68db8f58689bff431edd321620fd72 Counting belongs in document. --- diff --git a/src/librarian/builders/html.py b/src/librarian/builders/html.py index ac6b0a7..5bbe76a 100644 --- a/src/librarian/builders/html.py +++ b/src/librarian/builders/html.py @@ -32,7 +32,6 @@ class HtmlBuilder: 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') @@ -71,8 +70,7 @@ class HtmlBuilder: 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: @@ -82,12 +80,6 @@ class HtmlBuilder: self.postprocess(document) return self.output() - def assign_ids(self, tree): - # Assign IDs depth-first, to account for any 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: @@ -135,7 +127,7 @@ class HtmlBuilder: 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) diff --git a/src/librarian/document.py b/src/librarian/document.py index d61abb4..7f468fa 100644 --- a/src/librarian/document.py +++ b/src/librarian/document.py @@ -1,7 +1,7 @@ # 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 @@ -17,6 +17,7 @@ class WLDocument: 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() @@ -41,6 +42,12 @@ class WLDocument: for _e, elem in etree.iterwalk(self.tree, ('start',), 'strofa'): elem.preprocess() + def assign_ids(self): + # Assign IDs depth-first, to account for any 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 []: diff --git a/src/librarian/elements/base.py b/src/librarian/elements/base.py index 060449c..8f4acc5 100644 --- a/src/librarian/elements/base.py +++ b/src/librarian/elements/base.py @@ -354,16 +354,16 @@ class WLElement(etree.ElementBase): 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 diff --git a/src/librarian/elements/tools/__init__.py b/src/librarian/elements/tools/__init__.py index 69a26ea..1fe7e0b 100644 --- a/src/librarian/elements/tools/__init__.py +++ b/src/librarian/elements/tools/__init__.py @@ -6,8 +6,9 @@ from ..base import WLElement 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): diff --git a/tests/test_ref.py b/tests/test_ref.py index d75228e..2a54fb5 100644 --- a/tests/test_ref.py +++ b/tests/test_ref.py @@ -11,9 +11,7 @@ from lxml import etree 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():