[AI]
Поддержка тем
This commit is contained in:
@@ -14,9 +14,10 @@ def convert(
|
|||||||
output_dir: Path = typer.Option(..., "--output", "-o", help="Папка для результата"),
|
output_dir: Path = typer.Option(..., "--output", "-o", help="Папка для результата"),
|
||||||
style_config: Path = typer.Option("resources/style_config.yaml", "--style", "-s", help="Файл стилей"),
|
style_config: Path = typer.Option("resources/style_config.yaml", "--style", "-s", help="Файл стилей"),
|
||||||
template: Optional[Path] = typer.Option(None, "--template", "-t", help="Шаблон .dotx"),
|
template: Optional[Path] = typer.Option(None, "--template", "-t", help="Шаблон .dotx"),
|
||||||
|
theme: Optional[str] = typer.Option(None, "--theme", "-T", help="Название темы (gost, academic...)"),
|
||||||
verbose: bool = typer.Option(False, "--verbose", "-v", help="Включить отладочные сообщения"),
|
verbose: bool = typer.Option(False, "--verbose", "-v", help="Включить отладочные сообщения"),
|
||||||
):
|
):
|
||||||
log_level = logging.DEBUG if verbose else logging.INFO
|
log_level = logging.DEBUG if verbose else logging.INFO
|
||||||
logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s')
|
logging.basicConfig(level=log_level, format='%(levelname)s: %(message)s')
|
||||||
|
|
||||||
process_document(input_dir, output_dir, style_config, template)
|
process_document(input_dir, output_dir, style_config, template, theme)
|
||||||
@@ -9,7 +9,13 @@ import logging
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def process_document(input_dir: Path, output_dir: Path, style_config_path: Path, template_path: Optional[Path] = None):
|
def process_document(input_dir: Path, output_dir: Path, style_config_path: Path, template_path: Optional[Path] = None, theme_name: Optional[str] = None):
|
||||||
|
if theme_name:
|
||||||
|
theme_path = Path("resources/themes") / f"{theme_name}.yaml"
|
||||||
|
if theme_path.exists():
|
||||||
|
style_config_path = theme_path
|
||||||
|
else:
|
||||||
|
logger.warning(f"Тема {theme_name} не найдена, используем стандартный стиль")
|
||||||
for doc_dir in input_dir.iterdir():
|
for doc_dir in input_dir.iterdir():
|
||||||
if not doc_dir.is_dir():
|
if not doc_dir.is_dir():
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -46,3 +46,39 @@ def test_process_document_with_template(create_example_doc):
|
|||||||
# Удаляем файл после теста
|
# Удаляем файл после теста
|
||||||
output_path.unlink(missing_ok=True)
|
output_path.unlink(missing_ok=True)
|
||||||
tpl_path.unlink(missing_ok=True)
|
tpl_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
def test_process_document_with_theme():
|
||||||
|
# Создаём тему
|
||||||
|
theme_dir = Path("resources/themes")
|
||||||
|
theme_dir.mkdir(exist_ok=True)
|
||||||
|
theme_path = theme_dir / "test_theme.yaml"
|
||||||
|
theme_path.write_text("""
|
||||||
|
styles:
|
||||||
|
Heading 1:
|
||||||
|
font:
|
||||||
|
name: "Arial"
|
||||||
|
size: 14
|
||||||
|
bold: true
|
||||||
|
paragraph:
|
||||||
|
space_before: 10
|
||||||
|
space_after: 5
|
||||||
|
""", encoding="utf-8")
|
||||||
|
|
||||||
|
# Создаём фикстуру
|
||||||
|
doc_dir = Path("tests/fixtures/inputs/theme_test")
|
||||||
|
doc_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
(doc_dir / "document.md").write_text("# Заголовок", encoding="utf-8")
|
||||||
|
(doc_dir / "images").mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
process_document(
|
||||||
|
input_dir=Path("tests/fixtures/inputs"),
|
||||||
|
output_dir=Path("tests/output"),
|
||||||
|
style_config_path=Path("resources/style_config.yaml"),
|
||||||
|
theme_name="test_theme"
|
||||||
|
)
|
||||||
|
|
||||||
|
output_path = Path("tests/output/theme_test.docx")
|
||||||
|
assert output_path.exists()
|
||||||
|
|
||||||
|
output_path.unlink(missing_ok=True)
|
||||||
|
theme_path.unlink(missing_ok=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user