Browse Source

Číslování

jk-bakalarka
Jiří Kalvoda 8 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 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:

4
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"}")

10
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)

Loading…
Cancel
Save