60 lines
1.8 KiB
Python
60 lines
1.8 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, **options: Any) -> DeckOutput:
|
||
|
|
lines = read_lines(path)
|
||
|
|
frontmatter, body = strip_frontmatter(lines)
|
||
|
|
config = DeckConfig.resolve(frontmatter, options)
|
||
|
|
return convert(body, config=config, base_path=path.parent)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def render():
|
||
|
|
return render_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"))
|
||
|
|
}
|