From 405a4f396c097bfa156d5115348708ba2b774532 Mon Sep 17 00:00:00 2001 From: Jiri Kalvoda Date: Sat, 17 Feb 2024 20:55:12 +0100 Subject: [PATCH] =?UTF-8?q?=C4=8C=C3=ADslov=C3=A1n=C3=AD=20sekc=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formatitko.tex | 12 ++++++------ src/formatitko/context.py | 15 ++++++++++++++- src/formatitko/tex_generator.py | 2 +- src/formatitko/transform_processor.py | 9 +++++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/formatitko.tex b/formatitko.tex index 912ecb8..d77a742 100644 --- a/formatitko.tex +++ b/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} diff --git a/src/formatitko/context.py b/src/formatitko/context.py index caa321c..e24e3ac 100644 --- a/src/formatitko/context.py +++ b/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: diff --git a/src/formatitko/tex_generator.py b/src/formatitko/tex_generator.py index 9222a5d..476ca2a 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{") + self.write("\\"+"sub"*(e.level-1)+"section{"+str(e.attributes["number"])+"}{") self.generate(e.content) self.write(r"}") self.ensure_empty(2) diff --git a/src/formatitko/transform_processor.py b/src/formatitko/transform_processor.py index 32a0306..9e64e1b 100644 --- a/src/formatitko/transform_processor.py +++ b/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 +