30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
"""Explicit rendering inputs shared by the reader, previews and exports."""
|
|
from dataclasses import asdict, dataclass
|
|
from pathlib import Path
|
|
|
|
from .document_tree import code_language_for_path, is_code_document
|
|
from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RenderOptions:
|
|
theme: str = "light"
|
|
font_scale: float = 1.0
|
|
wheel_scroll_speed: float = 1.0
|
|
document_width_scale: float = 1.0
|
|
document_font_family: str = ""
|
|
document_font_size: int = 16
|
|
document_style_css: str = ""
|
|
math_engine: str = "katex"
|
|
|
|
def kwargs(self) -> dict:
|
|
return asdict(self)
|
|
|
|
|
|
def render_document(renderer, path: Path, source: str | None, options: RenderOptions) -> str:
|
|
if is_code_document(path):
|
|
return renderer.render_code_source(read_text_with_fallback(path, DEFAULT_TEXT_PREVIEW_BYTES), path,
|
|
language=code_language_for_path(path), **options.kwargs())
|
|
return renderer.render_source(path.read_text(encoding="utf-8") if source is None else source,
|
|
path, **options.kwargs())
|