53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
from markdown_it.token import Token
|
|
|
|
from md2gost.handlers.inline import render_inline
|
|
from md2gost.registry import block
|
|
|
|
|
|
@block("paragraph_open")
|
|
def paragraph(r, tok: Token) -> str:
|
|
inline_tok = r.tokens[r.pos + 1]
|
|
text = render_inline(inline_tok)
|
|
r.pos += 2
|
|
|
|
if r.current_heading == "ПЕРЕЧЕНЬ ПРИНЯТЫХ СОКРАЩЕНИЙ":
|
|
return abbreviations(text)
|
|
|
|
return text
|
|
|
|
|
|
def abbreviations(text: str) -> str:
|
|
lines = text.split("~\\\\")
|
|
rows = []
|
|
for line in lines:
|
|
line = line.strip()
|
|
if "\u2014" not in line:
|
|
continue
|
|
parts = line.split("\u2014", 1)
|
|
abbr = parts[0].strip()
|
|
desc = parts[1].strip()
|
|
rows.append((abbr, desc))
|
|
|
|
if not rows:
|
|
return text
|
|
|
|
max_abbr = max(len(r[0]) for r in rows)
|
|
abbr_col = max_abbr + 2
|
|
tabular_rows = []
|
|
for abbr, desc in rows:
|
|
pad = max_abbr - len(abbr) + 2
|
|
tabular_rows.append(
|
|
f"{abbr} {' ' * pad}& \\hangafter=1\\hangindent=2em --- {desc} \\\\"
|
|
)
|
|
|
|
# Отбивки колонок убраны с обеих сторон и между ними: иначе сумма ширин
|
|
# превышает \linewidth на 2\tabcolsep и перечень выходит за правое поле.
|
|
col_spec = (
|
|
f"@{{\\hspace{{0pt}}}}p{{{abbr_col}em}}"
|
|
f"@{{}}p{{\\dimexpr\\linewidth-{abbr_col}em\\relax}}@{{\\hspace{{0pt}}}}"
|
|
)
|
|
body = "\n".join(tabular_rows)
|
|
return f"\\noindent\\begin{{tabular}}{{{col_spec}}}\n{body}\n\\end{{tabular}}"
|