Enhance reader styles exports and settings
This commit is contained in:
@@ -9,8 +9,9 @@ from urllib.parse import unquote, urlsplit
|
||||
from markdown_it import MarkdownIt
|
||||
from markdown_it.token import Token
|
||||
from pygments import highlight
|
||||
from pygments.formatters import HtmlFormatter
|
||||
from pygments.lexers import TextLexer, get_lexer_by_name
|
||||
from pygments.formatters.html import HtmlFormatter
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.lexers.special import TextLexer
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
from .markdown_utils import slugify_heading
|
||||
@@ -44,8 +45,10 @@ class MarkdownRenderer:
|
||||
markdown_path: Path,
|
||||
theme: str = "light",
|
||||
font_scale: float = 1.0,
|
||||
wheel_scroll_speed: float = 1.0,
|
||||
document_font_family: str = "",
|
||||
document_font_size: int | None = None,
|
||||
document_style_css: str = "",
|
||||
math_engine: str = "katex",
|
||||
) -> str:
|
||||
source = markdown_path.read_text(encoding="utf-8")
|
||||
@@ -55,8 +58,10 @@ class MarkdownRenderer:
|
||||
title=markdown_path.name,
|
||||
theme=theme,
|
||||
font_scale=font_scale,
|
||||
wheel_scroll_speed=wheel_scroll_speed,
|
||||
document_font_family=document_font_family,
|
||||
document_font_size=document_font_size,
|
||||
document_style_css=document_style_css,
|
||||
math_engine=math_engine,
|
||||
)
|
||||
|
||||
@@ -66,8 +71,10 @@ class MarkdownRenderer:
|
||||
markdown_path: Path,
|
||||
theme: str = "light",
|
||||
font_scale: float = 1.0,
|
||||
wheel_scroll_speed: float = 1.0,
|
||||
document_font_family: str = "",
|
||||
document_font_size: int | None = None,
|
||||
document_style_css: str = "",
|
||||
math_engine: str = "katex",
|
||||
) -> str:
|
||||
body = self.markdown.render(source, {"path": str(markdown_path)})
|
||||
@@ -76,8 +83,46 @@ class MarkdownRenderer:
|
||||
title=markdown_path.name,
|
||||
theme=theme,
|
||||
font_scale=font_scale,
|
||||
wheel_scroll_speed=wheel_scroll_speed,
|
||||
document_font_family=document_font_family,
|
||||
document_font_size=document_font_size,
|
||||
document_style_css=document_style_css,
|
||||
math_engine=math_engine,
|
||||
)
|
||||
|
||||
def render_code_source(
|
||||
self,
|
||||
source: str,
|
||||
code_path: Path,
|
||||
language: str = "text",
|
||||
theme: str = "light",
|
||||
font_scale: float = 1.0,
|
||||
wheel_scroll_speed: float = 1.0,
|
||||
document_font_family: str = "",
|
||||
document_font_size: int | None = None,
|
||||
document_style_css: str = "",
|
||||
math_engine: str = "katex",
|
||||
) -> str:
|
||||
label = html.escape(language or code_path.suffix.lstrip(".") or "text")
|
||||
language_class = f" language-{html.escape(language)}" if language else ""
|
||||
highlighted = self._highlight_code(source, language, line_anchors=True)
|
||||
title = html.escape(code_path.name)
|
||||
body = (
|
||||
f"<h1>{title}</h1>"
|
||||
'<div class="code-block code-file-view">'
|
||||
f'<div class="code-title">{label}</div>'
|
||||
f'<pre class="highlight{language_class}"><code>{highlighted}</code></pre>'
|
||||
"</div>"
|
||||
)
|
||||
return self._wrap_html(
|
||||
body=body,
|
||||
title=code_path.name,
|
||||
theme=theme,
|
||||
font_scale=font_scale,
|
||||
wheel_scroll_speed=wheel_scroll_speed,
|
||||
document_font_family=document_font_family,
|
||||
document_font_size=document_font_size,
|
||||
document_style_css=document_style_css,
|
||||
math_engine=math_engine,
|
||||
)
|
||||
|
||||
@@ -87,8 +132,10 @@ class MarkdownRenderer:
|
||||
title: str,
|
||||
theme: str = "light",
|
||||
font_scale: float = 1.0,
|
||||
wheel_scroll_speed: float = 1.0,
|
||||
document_font_family: str = "",
|
||||
document_font_size: int | None = None,
|
||||
document_style_css: str = "",
|
||||
math_engine: str = "katex",
|
||||
) -> str:
|
||||
return self._wrap_html(
|
||||
@@ -96,8 +143,10 @@ class MarkdownRenderer:
|
||||
title=title,
|
||||
theme=theme,
|
||||
font_scale=font_scale,
|
||||
wheel_scroll_speed=wheel_scroll_speed,
|
||||
document_font_family=document_font_family,
|
||||
document_font_size=document_font_size,
|
||||
document_style_css=document_style_css,
|
||||
math_engine=math_engine,
|
||||
)
|
||||
|
||||
@@ -263,16 +312,25 @@ class MarkdownRenderer:
|
||||
uri = f"{uri}#{parsed.fragment}"
|
||||
return uri
|
||||
|
||||
def _highlight_code(self, code: str, language: str) -> str:
|
||||
def _highlight_code(self, code: str, language: str, line_anchors: bool = False) -> str:
|
||||
normalized = language.lower()
|
||||
if normalized in {"pseudocode", "pseudo"}:
|
||||
return self._highlight_pseudocode(code)
|
||||
highlighted = self._highlight_pseudocode(code)
|
||||
return self._add_line_anchors(highlighted) if line_anchors else highlighted
|
||||
|
||||
try:
|
||||
lexer = get_lexer_by_name(normalized) if normalized else TextLexer()
|
||||
except ClassNotFound:
|
||||
lexer = TextLexer()
|
||||
return highlight(code, lexer, self.formatter)
|
||||
highlighted = highlight(code, lexer, self.formatter)
|
||||
return self._add_line_anchors(highlighted) if line_anchors else highlighted
|
||||
|
||||
def _add_line_anchors(self, highlighted: str) -> str:
|
||||
lines = highlighted.splitlines(keepends=True)
|
||||
return "".join(
|
||||
f'<span id="line-{index}" class="code-line-anchor"></span>{line}'
|
||||
for index, line in enumerate(lines, start=1)
|
||||
)
|
||||
|
||||
def _highlight_pseudocode(self, code: str) -> str:
|
||||
escaped = html.escape(code)
|
||||
@@ -437,8 +495,10 @@ class MarkdownRenderer:
|
||||
title: str,
|
||||
theme: str,
|
||||
font_scale: float,
|
||||
wheel_scroll_speed: float,
|
||||
document_font_family: str,
|
||||
document_font_size: int | None,
|
||||
document_style_css: str,
|
||||
math_engine: str,
|
||||
) -> str:
|
||||
css = (self.resources_dir / "reader.css").read_text(encoding="utf-8")
|
||||
@@ -449,13 +509,17 @@ class MarkdownRenderer:
|
||||
escaped_title = html.escape(title)
|
||||
escaped_theme = html.escape(theme, quote=True)
|
||||
escaped_math_engine = html.escape(self._normalized_math_engine(math_engine), quote=True)
|
||||
try:
|
||||
normalized_wheel_speed = max(0.5, min(3.0, float(wheel_scroll_speed)))
|
||||
except (TypeError, ValueError):
|
||||
normalized_wheel_speed = 1.0
|
||||
fallback_font_size = int(round(max(0.8, min(font_scale, 1.6)) * 16))
|
||||
font_size = max(12, min(int(document_font_size or fallback_font_size), 28))
|
||||
font_family = self._css_font_family(document_font_family)
|
||||
escaped_font_family = html.escape(font_family, quote=False)
|
||||
|
||||
return f"""<!doctype html>
|
||||
<html lang="zh-CN" data-theme="{escaped_theme}" data-math-engine="{escaped_math_engine}">
|
||||
<html lang="zh-CN" data-theme="{escaped_theme}" data-math-engine="{escaped_math_engine}" data-wheel-scroll-speed="{normalized_wheel_speed:.1f}">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
@@ -467,6 +531,7 @@ class MarkdownRenderer:
|
||||
}}
|
||||
{pygments_css}
|
||||
{css}
|
||||
{document_style_css}
|
||||
</style>
|
||||
{math_assets["css"]}
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user