"""Tests for md2fodt (MD → Flat ODF Text).""" from __future__ import annotations import xml.etree.ElementTree as ET from pathlib import Path from md2fodt.converter import convert_md_to_fodt from md2fodt.emitter import emit_fodt from md2fodt.escape import escape_text, xml_id NS = { "office": "urn:oasis:names:tc:opendocument:xmlns:office:1.0", "text": "urn:oasis:names:tc:opendocument:xmlns:text:1.0", "table": "urn:oasis:names:tc:opendocument:xmlns:table:1.0", } def test_escape_and_id(): assert "&" in escape_text("a&b") assert xml_id("foo bar", "fig") == "fig-foo-bar" def test_emit_fodt_has_core_elements(): md = """# 1 Введение Абзац с **жирным** и *курсивом*. См. @Таблица:t1. %t1 Пример | A | B | |---|---| | 1 | 2 | ## 1.1 Подраздел - пункт один - пункт два ```python print("hi") ``` """ xml = emit_fodt(md, work_dir=Path(".")) assert "office:document" in xml root = ET.fromstring(xml) assert root.tag == "{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document" hs = root.findall(".//text:h", NS) assert len(hs) >= 1 tables = root.findall(".//table:table", NS) assert len(tables) == 1 headers = root.findall(".//table:table-header-rows", NS) assert len(headers) == 1 assert "table:table-header-rows" in xml assert "tab-t1" in xml assert "Таблица" in xml or "&#" in xml # caption present (encoding-safe: bookmark + table) def test_convert_writes_file(tmp_path: Path): md = tmp_path / "sample.md" md.write_text("# Заголовок\n\nТекст.\n", encoding="utf-8") out = convert_md_to_fodt(md, tmp_path / "out.fodt") assert out.is_file() assert out.suffix == ".fodt" text = out.read_text(encoding="utf-8") assert "office:document" in text ET.fromstring(text)