Counting belongs in document.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 18 Sep 2024 10:40:41 +0000 (12:40 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 18 Sep 2024 10:40:41 +0000 (12:40 +0200)
src/librarian/builders/html.py
src/librarian/document.py
src/librarian/elements/base.py
src/librarian/elements/tools/__init__.py
tests/test_ref.py

index ac6b0a7..5bbe76a 100644 (file)
@@ -32,7 +32,6 @@ class HtmlBuilder:
         self.header = etree.Element('h1')
 
         self.footnotes = etree.Element('div', id='footnotes')
         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')
 
 
         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
 
     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.prepare_images()
 
         if element is None:
@@ -82,12 +80,6 @@ class HtmlBuilder:
         self.postprocess(document)
         return self.output()
 
         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:
     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.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)
             fnheader = etree.Element("h3")
             fnheader.text = _("Footnotes")
             self.footnotes.insert(0, fnheader)
index d61abb4..7f468fa 100644 (file)
@@ -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.
 #
 # 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
 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
         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()
         tree.getroot().document = self
 
         self.preprocess()
@@ -41,6 +42,12 @@ class WLDocument:
         for _e, elem in etree.iterwalk(self.tree, ('start',), 'strofa'):
             elem.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 []:
     @property
     def children(self):
         for part_uri in self.meta.parts or []:
index 060449c..8f4acc5 100644 (file)
@@ -354,16 +354,16 @@ class WLElement(etree.ElementBase):
             prefix = 'f' # default numbering prefix
         return prefix
 
             prefix = 'f' # default numbering prefix
         return prefix
 
-    def assign_id(self, builder):
+    def assign_id(self, document):
         numbering = self.numbering
         if numbering:
         numbering = self.numbering
         if numbering:
-            number = str(builder.counters[numbering])
+            number = str(document.counters[numbering])
             self.attrib['_id'] = self.id_prefix + number
             self.attrib['_id'] = self.id_prefix + number
-            builder.counters[numbering] += 1
+            document.counters[numbering] += 1
 
             if numbering == 'main':
 
             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
 
             if numbering == 'fn':
                 self.attrib['_visible_numbering'] = number
index 69a26ea..1fe7e0b 100644 (file)
@@ -6,8 +6,9 @@ from ..base import WLElement
 
 class Numeracja(WLElement):
     NUMBERING = True
 
 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 Rownolegle(WLElement):
index d75228e..2a54fb5 100644 (file)
@@ -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'))
 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():
 
         refs = []
         for ref in doc.references():