From 88d653af152a61d3433221202d554b17134773ac Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Tue, 27 Feb 2024 10:22:37 +0100 Subject: [PATCH] =?UTF-8?q?=C4=8C=C3=ADslov=C3=A1n=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/formatitko/context.py | 28 +++++++++++++++++++-------- src/formatitko/tex_generator.py | 4 +++- src/formatitko/transform_processor.py | 10 ++++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/formatitko/context.py b/src/formatitko/context.py index 167135c..29dd952 100644 --- a/src/formatitko/context.py +++ b/src/formatitko/context.py @@ -1,4 +1,4 @@ -from panflute import Doc, Element, Div, Span, Header +from panflute import Doc, Element, Div, Span, Header, Figure from typing import Union, Callable from types import ModuleType @@ -19,12 +19,21 @@ CommandCallable = Callable[[Command, 'Context', 'NOPProcessor'], list[Element]] # # This class is basically an extension to panflute's doc, this is why metadata # is read directly from it. -def default_number_generator(e: Header, context: 'Context') -> str: +def default_section_number_generator(e: Header, context: 'Context') -> list[Union[str, int]]: l = e.level - context.section_counters[l-1] += 1 - for i in range(l, len(context.section_counters)): - context.section_counters[i] = 0 - return ".".join(map(str, context.section_counters[:l])) + section_counters = context.get_data("section_counters") + section_counters[l-1] += 1 + for i in range(l, len(section_counters)): + section_counters[i] = 0 + return list(section_counters[:l]) + +def default_figure_number_generator(e: Figure, context: 'Context') -> Union[str, int]: + figure_type = e.attributes.get("type", "img") + figure_counters = context.get_data("figure_counters") + figure_counters.setdefault(figure_type, 0) + figure_counters[figure_type] += 1 + return figure_counters[figure_type] + class Context: @@ -58,8 +67,11 @@ class Context: self.add_dep(path) if self.get_metadata("flags", immediate=True) is None: self.set_metadata("flags", {}) - self.number_generator = default_number_generator - self.section_counters = [0 for i in range(6)] + if not parent: + self.set_data('section_number_generator', default_section_number_generator) + self.set_data('figure_number_generator', default_figure_number_generator) + self.set_data('section_counters', [0 for i in range(6)]) + self.set_data('figure_counters', {}) def get_command(self, command: str) -> Union[CommandCallable, None]: if command in self._commands: diff --git a/src/formatitko/tex_generator.py b/src/formatitko/tex_generator.py index 484f504..20f6d4a 100644 --- a/src/formatitko/tex_generator.py +++ b/src/formatitko/tex_generator.py @@ -97,7 +97,7 @@ class UCWTexGenerator(OutputGenerator): def generate_Header(self, e: Header): self.ensure_empty(2) - self.write("\\"+"sub"*(e.level-1)+"section{"+str(e.attributes["number"])+"}{") + self.write("\\"+"sub"*(e.level-1)+"section{"+".".join(map(str, e.attributes["number"]))+"}{") self.generate(e.content) self.write(r"}") self.ensure_empty(2) @@ -163,6 +163,8 @@ class UCWTexGenerator(OutputGenerator): self.writeln(r"}") self.writeln(r"\centerline{") self.indent_more() + if 'number' in e.attributes: + self.writeln(f"Obrázek {e.attributes['number']}:") self.generate(e.caption) self.indent_less() self.writeln(r"}") diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index f4257ca..5a4c25e 100644 --- a/src/formatitko/transform_processor.py +++ b/src/formatitko/transform_processor.py @@ -319,7 +319,13 @@ class TransformProcessor(NOPProcessor): if 'unnumbered' in e.classes: e.attributes["number"] = "" else: - e.attributes["number"] = self.context.number_generator(e, self.context) - print("TH", e) + e.attributes["number"] = self.context.get_data("section_number_generator")(e, self.context) return e + def transform_Figure(self, e:Figure) -> Figure: + if "number" not in e.attributes: + if 'unnumbered' in e.classes: + e.attributes["number"] = "" + else: + e.attributes["number"] = self.context.get_data("figure_number_generator")(e, self.context) + return super().transform_Figure(e)