Číslování sekcí

This commit is contained in:
Jiří Kalvoda 2024-02-17 20:55:12 +01:00
parent affc2c4279
commit 405a4f396c
4 changed files with 30 additions and 8 deletions

View file

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

View file

@ -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 typing import Union, Callable
from types import ModuleType 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 # 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:
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: class Context:
parent: Union["Context", None] parent: Union["Context", None]
_commands: dict[str, Union[CommandCallable, None]] _commands: dict[str, Union[CommandCallable, None]]
@ -29,6 +37,9 @@ class Context:
dir: str dir: str
filename: 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): def __init__(self, doc: Doc, path: str, parent: Union['Context', None]=None, trusted: bool=True):
self.parent = parent self.parent = parent
self._commands = {} self._commands = {}
@ -39,6 +50,8 @@ class Context:
self.filename = os.path.basename(path) self.filename = os.path.basename(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
self.section_counters = [0 for i in range(6)]
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:

View file

@ -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{") self.write("\\"+"sub"*(e.level-1)+"section{"+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)

View file

@ -267,3 +267,12 @@ class TransformProcessor(NOPProcessor):
else: else:
return e 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