Expand description
mhchem 化学公式转译器(\ce/\pu → LaTeX,server-only)。
mhchem 化学公式转译器:把 \ce{...} / \pu{...} 预转译为标准 LaTeX,
再交给 katex 渲染。
这是 mhchemParser 4.2.2(Apache-2.0)
的机械移植:状态机 + texify 输出。mhchemParser 是纯字符串→字符串转译器,
ce("H2O") → \mathrm{H}\sb{2}\mathrm{O} 之类,无需嵌入 katex 内部解析树。
移植要点(TS→Rust):
- 动态
buffer对象 →Buffer结构体(Option字段 +clear)。 - 动态
Parsed = string | object→Parsed枚举;节点字段用Field(字符串或节点向量)。 - 正则模式(含 lookahead
(?=)/(?!),Rustregex不支持)→fancy-regex(已被 syntect 间接引入,此处显式声明)。 findObserveGroups花括号配对扫描 →find_observe_groups。- 状态机转移表 →
build_transitions展开(对应_mhchemCreateTransitions)。
公开 API:ce / pu。任意内部异常都回退为原样输出(绝不 panic,与
katex.rs 的容错哲学一致)。
§Copyright 2015-2023 Martin Hensel(mhchemParser 原作者)。Apache-2.0。
Macros§
- re 🔒
- 惰性编译字面量正则,编译失败降级为
CompiledPat(None)。
Structs§
- Action
Ref 🔒 - Buffer 🔒
- 状态机缓冲区(对应 TS 的动态
buffer对象)。 - Compiled
Pat 🔒 - 编译产物:把「坏正则降级为不匹配」的语义封装在一处。
- MMatch 🔒
- 模式匹配结果。
- Node
Data 🔒 - RawEntry 🔒
- 原始转移条目(对应 TS 字面量,
patterns/states均为|分隔的合并名)。 - Span 🔒
- Task 🔒
- Transition 🔒
Enums§
- Field 🔒
- 解析中间表示的字段值:字符串或子表达式节点向量。
- MVal 🔒
- 模式匹配值:单字符串或捕获组数组(正则 >1 个捕获组时为数组)。
- Out 🔒
- Parsed 🔒
- 解析中间表示:字面字符串或节点。
- Pat 🔒
- 字面量或正则模式(
findObserveGroups的参数)。
Statics§
- A 🔒
- BD 🔒
- CE 🔒
- GREEK_
NAMES 🔒 - 希腊字母宏集合(
letters/\greek等模式用到)。 - GREEK_
RE 🔒 - LETTERS_
RE 🔒 - NUM99 🔒
- O 🔒
- ONE_
GREEK_ 🔒RE - OXIDATION 🔒
- PQ 🔒
- PU 🔒
- PU2 🔒
- PU99 🔒
- TEXT 🔒
- TEX_
MATH 🔒 - TEX_
MATH_ 🔒TIGHT
Functions§
- append_
field 🔒 - append_
str 🔒 - arrow_
sub 🔒 - bd_
action 🔒 - build_
transitions 🔒 - 展开
{pattern: {state: task}}为{state => [(pattern, task)]}。 对应_mhchemCreateTransitions:拆分|、'*'插入所有状态。 - ce
- 把
\ce{...}内容转译为 LaTeX。 - ce_
action 🔒 - compile_
or_ 🔒log - 编译正则,失败时记日志并返回
None(绝不 panic)。 - concat 🔒
- exec_
action 🔒 - fancy_
match 🔒 - 把 fancy-regex 的捕获结果转为
MMatch(锚定^,whole 从 0 起)。 - fg 🔒
find_observe_groups的简写封装。- field_
str 🔒 - find_
observe_ 🔒end - 从
rest0的start位置扫描,跟踪花括号深度,在深度 0 遇到end_chars时返回其区间。 - find_
observe_ 🔒groups findObserveGroups:花括号感知的定界符配对扫描。- generic_
action 🔒 - get_
arrow 🔒 - get_
bond 🔒 - get_
operator 🔒 - go 🔒
- 主解析循环。
- gof 🔒
- is_
empty_ 🔒pat - machine_
action 🔒 - match_
pattern 🔒 - 按名匹配模式(对应
_mhchemParser.patterns.match_)。 - mval_
pair 🔒 - mval_
str 🔒 - mval_
to_ 🔒out - num99_
action 🔒 - oxidation_
action 🔒 - pat_
amount 🔒 amount模式。- pat_
enumber 🔒 (-)(9.,9)(e)(99)模式。- pat_
formula 🔒 formula$模式。- pat_
match_ 🔒head - 在
input起始处匹配pat,返回匹配到的文本(不含 remainder)。 - pat_
state_ 🔒of_ aggregation state of aggregation $模式。- pq_
action 🔒 - pu
- 把
\pu{...}内容转译为 LaTeX(容错同ce)。 - pu2_
action 🔒 - pu99_
action 🔒 - pu_
action 🔒 - raw 🔒
- task 🔒
- texify_
go 🔒 - texify_
go2 🔒 - texmath_
action 🔒 - text_
action 🔒 - thousands_
split 🔒 - 千分位拆分(pu-9,9 的 output-0 / output-o)。
- to_tex 🔒
- transitions_
for 🔒