From b64354f85b6cd7f2b1668ee21b8f6eda8a3d84b7 Mon Sep 17 00:00:00 2001 From: erjemin Date: Sun, 11 May 2025 18:57:03 +0300 Subject: [PATCH] =?UTF-8?q?mod:=20=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=BE=D0=BD=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etpgrf/typograph.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 etpgrf/typograph.py diff --git a/etpgrf/typograph.py b/etpgrf/typograph.py new file mode 100644 index 0000000..ffc0730 --- /dev/null +++ b/etpgrf/typograph.py @@ -0,0 +1,47 @@ +from etpgrf.config import UTF, MNEMO_CODE +from etpgrf.comutil import parse_and_validate_langs +from etpgrf.hyphenation import Hyphenator + + +# --- Основной класс Typographer --- +class Typographer: + def __init__(self, + langs: str | list[str] | tuple[str, ...] | frozenset[str] = 'ru', + code_out: str = 'mnemo', + hyphenation_rule: Hyphenator | None = None, # Перенос слов и параметры расстановки переносов + # glue_prepositions_rule: GluePrepositionsRule | None = None, # Для других правил + # ... другие модули правил ... + ): + + # --- Обработка и валидация параметра langs --- + self.langs: frozenset[str] = parse_and_validate_langs(langs) + + # --- Обработка и валидация параметра code_out --- + if code_out not in MNEMO_CODE | UTF: + raise ValueError(f"etpgrf: code_out '{code_out}' is not supported. Supported codes: {MNEMO_CODE | UTF}") + + # Сохраняем переданные модули правил + self.hyphenation_rule = hyphenation_rule + + # TODO: вынести все соответствия UTF ⇄ MNEMO_CODE в отдельный класс + # self.hyphen_char = "­" if code_out in UTF else "­" # Мягкий перенос по умолчанию + + # Конвейер для обработки текста + def process(self, text: str) -> str: + processed_text = text + if self.hyphenation_rule: + # Передаем активные языки и символ переноса, если модуль Hyphenator + # не получает их в своем __init__ напрямую от пользователя, + # а конструируется с настройками по умолчанию, а потом конфигурируется. + # В нашем примере Hyphenator уже получает их в __init__. + processed_text = self.hyphenation_rule.hyp_in_text(processed_text) + + # if self.glue_prepositions_rule: + # processed_text = self.glue_prepositions_rule.hyp_in_text(processed_text, non_breaking_space_char=self._get_nbsp()) + + # ... вызовы других активных модулей правил ... + return processed_text + + # def _get_nbsp(self): # Пример получения неразрывного пробела + # return "\u00A0" if self.code_out in UTF else " " +