Browse Source

Číslování

jk-bakalarka
Jiří Kalvoda 9 months ago
parent
commit
88d653af15
  1. 28
      src/formatitko/context.py
  2. 4
      src/formatitko/tex_generator.py
  3. 10
      src/formatitko/transform_processor.py

28
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 typing import Union, Callable
from types import ModuleType 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 # This class is basically an extension to panflute's doc, this is why metadata
# is read directly from it. # 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 l = e.level
context.section_counters[l-1] += 1 section_counters = context.get_data("section_counters")
for i in range(l, len(context.section_counters)): section_counters[l-1] += 1
context.section_counters[i] = 0 for i in range(l, len(section_counters)):
return ".".join(map(str, context.section_counters[:l])) 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: class Context:
@ -58,8 +67,11 @@ class Context:
self.add_dep(path) self.add_dep(path)
if self.get_metadata("flags", immediate=True) is None: if self.get_metadata("flags", immediate=True) is None:
self.set_metadata("flags", {}) self.set_metadata("flags", {})
self.number_generator = default_number_generator if not parent:
self.section_counters = [0 for i in range(6)] 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]: def get_command(self, command: str) -> Union[CommandCallable, None]:
if command in self._commands: if command in self._commands:

4
src/formatitko/tex_generator.py

@ -97,7 +97,7 @@ class UCWTexGenerator(OutputGenerator):
def generate_Header(self, e: Header): def generate_Header(self, e: Header):
self.ensure_empty(2) 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.generate(e.content)
self.write(r"}") self.write(r"}")
self.ensure_empty(2) self.ensure_empty(2)
@ -163,6 +163,8 @@ class UCWTexGenerator(OutputGenerator):
self.writeln(r"}") self.writeln(r"}")
self.writeln(r"\centerline{") self.writeln(r"\centerline{")
self.indent_more() self.indent_more()
if 'number' in e.attributes:
self.writeln(f"Obrázek {e.attributes['number']}:")
self.generate(e.caption) self.generate(e.caption)
self.indent_less() self.indent_less()
self.writeln(r"}") self.writeln(r"}")

10
src/formatitko/transform_processor.py

@ -319,7 +319,13 @@ class TransformProcessor(NOPProcessor):
if 'unnumbered' in e.classes: if 'unnumbered' in e.classes:
e.attributes["number"] = "" e.attributes["number"] = ""
else: else:
e.attributes["number"] = self.context.number_generator(e, self.context) e.attributes["number"] = self.context.get_data("section_number_generator")(e, self.context)
print("TH", e)
return e 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)

Loading…
Cancel
Save