Browse Source

Číslování sekcí

jk-bakalarka
Jiří Kalvoda 9 months ago
parent
commit
405a4f396c
  1. 12
      formatitko.tex
  2. 15
      src/formatitko/context.py
  3. 2
      src/formatitko/tex_generator.py
  4. 9
      src/formatitko/transform_processor.py

12
formatitko.tex

@ -55,12 +55,12 @@
\fncount=1
\def\fnmark{\superscript{\the\fncount}}
\def\fn#1{\footnote\fnmark{#1}\advance\fncount by 1}
\def\section#1{{\parskip1em\settextsize{18}\bf #1}}
\def\subsection#1{{\parskip1em\settextsize{16}\bf #1}}
\def\subsubsection#1{{\parskip1em\settextsize{14}\bf #1}}
\def\subsubsubsection#1{{\parskip1em\settextsize{12}\bf #1}}
\def\subsubsubsubsection#1{{\parskip1em\settextsize{10}\bf #1}}
\def\subsubsubsubsubsection#1{{\parskip1em\settextsize{10}\bi #1}}
\def\section#1#2{\vfil\eject{\parskip1em\settextsize{20}\bf #1\kern 1em #2}}
\def\subsection#1#2{{\parskip1em\settextsize{18}\bf #1\kern 1em #2}}
\def\subsubsection#1#2{{\parskip1em\settextsize{16}\bf #1\kern 1em #2}}
\def\subsubsubsection#1#2{{\parskip1em\settextsize{14}\bf #1\kern 1em #2}}
\def\subsubsubsubsection#1#2{{\parskip1em\settextsize{12}\bf #1\kern 1em #2}}
\def\subsubsubsubsubsection#1#2{{\parskip1em\settextsize{10}\bi #1\kern 1em #2}}
\long\def\blockquote#1{\vskip\lineskip\vskip\parskip\hbox{\vrule\hskip5pt\vbox{#1}}}
\def\strikeout#1{FIXME: Strikeout not implemented}
\def\underline#1{FIXME: Underline not implemented}

15
src/formatitko/context.py

@ -1,4 +1,4 @@
from panflute import Doc, Element, Div, Span
from panflute import Doc, Element, Div, Span, Header
from typing import Union, Callable
from types import ModuleType
@ -20,6 +20,14 @@ CommandCallable = Callable[[Command, 'Context'], list[Element]] # This is here b
#
# 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:
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]))
class Context:
parent: Union["Context", None]
_commands: dict[str, Union[CommandCallable, None]]
@ -29,6 +37,9 @@ class Context:
dir: str
filename: str
section_counters: list[int]
number_generator: Callable[[Header, 'Context'], str]
def __init__(self, doc: Doc, path: str, parent: Union['Context', None]=None, trusted: bool=True):
self.parent = parent
self._commands = {}
@ -39,6 +50,8 @@ class Context:
self.filename = os.path.basename(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)]
def get_command(self, command: str) -> Union[CommandCallable, None]:
if command in self._commands:

2
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{")
self.write("\\"+"sub"*(e.level-1)+"section{"+str(e.attributes["number"])+"}{")
self.generate(e.content)
self.write(r"}")
self.ensure_empty(2)

9
src/formatitko/transform_processor.py

@ -267,3 +267,12 @@ class TransformProcessor(NOPProcessor):
else:
return e
def transform_Header(self, e: Header) -> Header:
if "number" not in e.attributes:
if 'unnumbered' in e.classes:
e.attributes["number"] = ""
else:
e.attributes["number"] = self.context.number_generator(e, self.context)
print("TH", e)
return e

Loading…
Cancel
Save