wapdown/tests/conftest.py
2026-09-26 13:08:50 +03:00

72 lines
2.2 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
from typing import Any
import pytest
from wapdown.cli import convert
from wapdown.config import DeckConfig
from wapdown.conversion.core import strip_frontmatter, read_lines
from wapdown.renderers.wml.renderer import DeckOutput
PROJECT_ROOT = Path(__file__).resolve().parent.parent
EXAMPLES = PROJECT_ROOT / "examples"
GOLDEN = Path(__file__).resolve().parent / "golden"
def render_source(text: str, **options: Any) -> str:
"""Render Markdown given as a string, returning the single deck's markup."""
lines = text.splitlines(keepends=True)
frontmatter, body = strip_frontmatter(lines)
config = DeckConfig.resolve(frontmatter, options)
return convert(body, config=config, base_path=EXAMPLES).text
def render_file(path: Path, *, renderer_name: str = "wml", **options: Any):
lines = read_lines(path)
frontmatter, body = strip_frontmatter(lines)
config = DeckConfig.resolve(frontmatter, options)
return convert(body, config=config, base_path=path.parent, renderer_name=renderer_name)
@pytest.fixture
def render():
return render_source
def render_xhtmlmp_source(text: str, **options: Any) -> str:
"""Like `render_source`, but through the XHTML-MP renderer."""
lines = text.splitlines(keepends=True)
frontmatter, body = strip_frontmatter(lines)
config = DeckConfig.resolve(frontmatter, options)
return convert(body, config=config, base_path=EXAMPLES, renderer_name="xhtmlmp")
@pytest.fixture
def render_xhtmlmp():
return render_xhtmlmp_source
@pytest.fixture
def render_output():
"""Like `render`, but exposes every output file (deck-per-card mode)."""
def _render(text: str, **options: Any) -> DeckOutput:
lines = text.splitlines(keepends=True)
frontmatter, body = strip_frontmatter(lines)
config = DeckConfig.resolve(frontmatter, options)
return convert(body, config=config, base_path=EXAMPLES)
return _render
@pytest.fixture(scope="session")
def example_decks() -> dict[str, str]:
"""Every examples/*.md rendered with its own frontmatter settings."""
return {
path.name: render_file(path).documents[0].text
for path in sorted(EXAMPLES.glob("*.md"))
}