Files
md2gost/md2gost/handlers/heading.py
T

62 lines
1.5 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
from md2gost.handlers.inline import render_inline
if TYPE_CHECKING:
from md2gost.render import Renderer
UNNUMBERED = {
"АННОТАЦИЯ",
"СОДЕРЖАНИЕ",
"ПЕРЕЧЕНЬ ПРИНЯТЫХ СОКРАЩЕНИЙ",
"ВВЕДЕНИЕ",
"ЗАКЛЮЧЕНИЕ",
"СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ",
}
NEWPAGE_BEFORE = {
"ВВЕДЕНИЕ",
"ЗАКЛЮЧЕНИЕ",
"СПИСОК ИСПОЛЬЗОВАННЫХ ИСТОЧНИКОВ",
}
def heading(r: Renderer) -> str:
tok = r.tokens[r.pos]
level = int(tok.tag[1])
inline_tok = r.tokens[r.pos + 1]
text = render_inline(inline_tok)
r.current_heading = text.upper()
r.pos += 2
if text.upper() in UNNUMBERED:
prefix = ""
if text.upper() in NEWPAGE_BEFORE:
prefix = "\\clearpage\n"
return (
f"{prefix}"
f"\\begin{{center}}\n"
f"\\fontsize{{14pt}}{{18pt}}\\selectfont \\bfseries {text}\n"
f"\\end{{center}}"
)
if level == 1:
r.section += 1
r.subsection = 0
r.subsubsection = 0
num = f"{r.section}. "
elif level == 2:
r.subsection += 1
r.subsubsection = 0
num = f"{r.section}.{r.subsection} "
elif level == 3:
r.subsubsection += 1
num = f"{r.section}.{r.subsection}.{r.subsubsection} "
else:
num = ""
return f"\\textbf{{{num}{text}}}"