#!/usr/bin/env python3 """Преобразование HTML в структурированный текст.""" from __future__ import annotations import argparse import re import textwrap from pathlib import Path from bs4 import BeautifulSoup HEADINGS = {"h1": 1, "h2": 2, "h3": 3, "h4": 4, "h5": 5, "h6": 6} def convert(path: Path, encoding: str, width: int, keep_links: bool) -> str: soup = BeautifulSoup(path.read_text(encoding=encoding, errors="replace"), "html.parser") for tag in soup(["script", "style", "noscript", "template", "svg", "head", "title", "meta", "link"]): tag.decompose() for tag in soup.find_all(list(HEADINGS)): tag.insert_before("\n\n" + "#" * HEADINGS[tag.name] + " ") tag.insert_after("\n\n") for tag in soup.find_all("li"): tag.insert_before("\n- ") tag.insert_after("\n") for tag in soup.find_all(["p", "div", "section", "article", "tr", "blockquote"]): tag.insert_after("\n") for br in soup.find_all("br"): br.replace_with("\n") raw = soup.get_text("") lines = [re.sub(r"[ \t]+", " ", line).strip() for line in raw.splitlines()] blocks: list[str] = [] for line in lines: if line: if width > 0 and not line.startswith("#") and not line.startswith("- "): blocks.extend(textwrap.wrap(line, width=width) or [line]) else: blocks.append(line) elif blocks and blocks[-1] != "": blocks.append("") if keep_links: links = [] for anchor in soup.find_all("a", href=True): label = anchor.get_text(" ", strip=True) or anchor["href"] links.append(f"- {label}: {anchor['href']}") if links: blocks.extend(["", "Ссылки:", *links]) return "\n".join(blocks).strip() + "\n" def main() -> int: parser = argparse.ArgumentParser(description="Превратить HTML в текст") parser.add_argument("input", type=Path, help="HTML-файл") parser.add_argument("--output", type=Path, help="Куда сохранить текст") parser.add_argument("--encoding", default="utf-8", help="Кодировка входа") parser.add_argument("--width", type=int, default=0, help="Ширина переноса, 0 отключает") parser.add_argument("--links", action="store_true", help="Добавить список ссылок") args = parser.parse_args() source = args.input.expanduser() if not source.is_file(): parser.error(f"Файл не найден: {source}") result = convert(source, args.encoding, args.width, args.links) if args.output: target = args.output.expanduser() target.parent.mkdir(parents=True, exist_ok=True) target.write_text(result, encoding="utf-8") print(f"Сохранено: {target}") else: print(result, end="") return 0 if __name__ == "__main__": raise SystemExit(main())