mystx.ext.live_preview#
live_preview — 在浏览器中实时预览 MyST 文档
基于 Docutils 与 MyST-Parser,将输入的 MyST 文本转换为 HTML 或其它格式, 并在 PyScript/Pyodide 环境中通过 DOM 事件实现即时刷新。
覆盖简化的 HTML 翻译器与 Writer,仅输出 body 内容并禁用样式注入;
解析 YAML 配置以设置 Docutils 转换参数,收集并显示警告信息;
通过页面输入与选择器,实时触发转换并更新预览区域。
Attributes#
Classes#
简化的 HTML 翻译器。 |
|
简化的 HTML Writer,仅输出正文内容。 |
|
Functions#
|
|
|
将 MyST 文本转换为指定输出格式。 |
|
读取页面输入并触发转换,更新预览与原始输出。 |
初始化浏览器端的 DOM 引用与事件绑定,并进行首轮转换。 |
Package Contents#
- class mystx.ext.live_preview.SimpleTranslator(document)[源代码]#
Bases:
docutils.writers.html5_polyglot.HTMLTranslator简化的 HTML 翻译器。
为代码块(literal_block)追加
highlight类,便于外部样式统一高亮;禁用样式表插入,避免在页面中重复注入样式。
- class mystx.ext.live_preview.SimpleWriter[源代码]#
Bases:
docutils.writers.html5_polyglot.Writer简化的 HTML Writer,仅输出正文内容。
通过
filter_settings_spec精简可用设置;在
apply_template中返回{body},避免包裹多余模板结构。
- settings_spec[源代码]#
Runtime settings specification. Override in subclasses.
Defines runtime settings and associated command-line options, as used by docutils.frontend.OptionParser. This is a tuple of:
Option group title (string or None which implies no group, just a list of single options).
Description (string or None).
A sequence of option tuples. Each consists of:
Help text (string)
List of option strings (e.g.
['-Q', '--quux']).Dictionary of keyword arguments sent to the OptionParser/OptionGroup
add_optionmethod.Runtime setting names are derived implicitly from long option names ('--a-setting' becomes
settings.a_setting) or explicitly from the 'dest' keyword argument.Most settings will also have a 'validator' keyword & function. The validator function validates setting values (from configuration files and command-line option arguments) and converts them to appropriate types. For example, the
docutils.frontend.validate_booleanfunction, required by all boolean settings, converts true values ('1', 'on', 'yes', and 'true') to 1 and false values ('0', 'off', 'no', 'false', and '') to 0. Validators need only be set once per setting. See the docutils.frontend.validate_* functions.See the optparse docs for more details.
More triples of group title, description, options, as many times as needed. Thus, settings_spec tuples can be simply concatenated.
- parts[源代码]#
Mapping of document part names to fragments of self.output.
See Writer.assemble_parts() below and <https://docutils.sourceforge.io/docs/api/publisher.html>.
- mystx.ext.live_preview.convert(input_config: str, input_myst: str, writer_name: str) dict[源代码]#
将 MyST 文本转换为指定输出格式。