[ADD] вывод предупреждений и ошибок LaTeX наружу из контейнера
This commit is contained in:
+28
-6
@@ -6,15 +6,25 @@ import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import md2gost
|
||||
from md2gost.latex.log import STRICT_KINDS, format_diagnostics, parse_log
|
||||
from md2gost.parser import Parser, iter_image_sources
|
||||
from md2gost.render import Renderer
|
||||
|
||||
|
||||
def _print_latex_errors(log_path: Path) -> None:
|
||||
if log_path.exists():
|
||||
for line in log_path.read_text(errors="replace").splitlines():
|
||||
if line.startswith("!"):
|
||||
print(f" {line}", file=sys.stderr)
|
||||
def _report_latex_log(log_path: Path) -> bool:
|
||||
"""Печатает диагностику сборки. Возвращает True при серьёзных дефектах.
|
||||
|
||||
Журнал живёт во временном каталоге и исчезает вместе с ним, поэтому всё,
|
||||
о чём сообщил xelatex, нужно вывести наружу здесь — иначе при запуске
|
||||
в контейнере узнать о дефектах вёрстки неоткуда.
|
||||
"""
|
||||
if not log_path.exists():
|
||||
return False
|
||||
diags = parse_log(log_path.read_text(errors="replace"))
|
||||
summary = format_diagnostics(diags)
|
||||
if summary:
|
||||
print(summary, file=sys.stderr)
|
||||
return any(d.kind in STRICT_KINDS for d in diags)
|
||||
|
||||
|
||||
def run_xelatex(tex_path: Path, output_dir: Path) -> bool:
|
||||
@@ -85,6 +95,11 @@ def main() -> None:
|
||||
action="store_true",
|
||||
help="только .tex, без компиляции в PDF",
|
||||
)
|
||||
cli.add_argument(
|
||||
"--strict",
|
||||
action="store_true",
|
||||
help="выйти с ошибкой, если xelatex сообщил о предупреждениях",
|
||||
)
|
||||
args = cli.parse_args()
|
||||
|
||||
if not args.input.exists():
|
||||
@@ -118,13 +133,20 @@ def main() -> None:
|
||||
if not run_xelatex(tex_path, cache_dir):
|
||||
stage = "" if attempt == 1 else " (2-й проход)"
|
||||
print(f"ошибка: xelatex{stage} не собрал PDF", file=sys.stderr)
|
||||
_print_latex_errors(log_path)
|
||||
_report_latex_log(log_path)
|
||||
sys.exit(1)
|
||||
|
||||
final_pdf = args.output / pdf_name
|
||||
shutil.copy2(cache_dir / pdf_name, final_pdf)
|
||||
print(f"-> {final_pdf}")
|
||||
|
||||
# Разбирается журнал второго прохода: после первого ссылки и номера
|
||||
# страниц ещё не разложены, и часть предупреждений ложная.
|
||||
has_defects = _report_latex_log(log_path)
|
||||
if has_defects and args.strict:
|
||||
print("ошибка: --strict, а вёрстка содержит дефекты", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user