拓冰建站拓冰建站
首页 / 资讯中心 / 正文

pandoc 命令测试精解:RST figure 指令的 alt 文本回退与跨格式往返语义

文档开发工具CLI【免费下载链接】pandocUniversal markup converter项目地址https://gitcode.com/gh_mirrors/pa/pandoc点击查看免费下载导读本文以 pandoc 仓库中的命令测试用例 test/command/11140.md 为骨架深入剖析 RSTreStructuredTextfigure指令中:alt:属性的解析规则alt缺省时如何回退为图注caption以及该信息在 native、Markdown、LaTeX 三种表示之间如何往返、如何避免丢失。读完本文你将掌握 pandoc 命令测试文件的书写与验证方法理解Figure块结构中替代文本alt与标题文本caption的分工并能据此调试自己项目中图片元数据的跨格式保真问题。一、测试用例 11140 在验证什么test/command/11140.md是一个典型的 pandoc 命令测试golden test它由 4 个相互独立的小节组成共同验证一个主题RSTfigure指令的替代文本alt如何进入 Pandoc AST以及它在 native / Markdown / LaTeX 之间的映射规则。按照命令测试框架的约定见 test/Tests/Command.hs每个测试块以%开头声明要执行的 pandoc 命令行随后是作为标准输入stdin喂给命令的文本输入以^D行结束之后紧跟期望的标准输出。读者可以逐条在本机复现# 场景 1带 :alt: 的 RST figure 转 native printf .. figure:: image.png\n :alt: my alt\n\n Caption\n | pandoc -f rst -t native# 场景 2不带 :alt: 的 RST figure 转 native printf .. figure:: image.png\n\n Caption\n | pandoc -f rst -t native# 场景 3native Figure 转 markdown # 输入为场景 1 得到的 native 表示# 场景 4LaTeX figure 环境含 \pandocbounded转 native四个场景覆盖了同一份图片元数据在“RST → AST → Markdown”主链路上的完整生命周期以及 LaTeX 输入侧的容错处理。二、场景 1带:alt:的 RST figure —— alt 成为图片的替代文本测试用例的第一段输入如下% pandoc -f rst -t native .. figure:: image.png :alt: my alt Caption ^D [ Figure ( , [] , [] ) (Caption Nothing [ Plain [ Str Caption ] ]) [ Plain [ Image ( , [] , [] ) [ Str my , Space , Str alt ] ( image.png , ) ] ] ]其要点外层是Figure块携带空属性(, [], [])图注Caption Nothing [Plain [Str Caption]]来自指令缩进正文中的Caption文本图体body是一个Plain段落内含Image内联元素其描述文本alt为[Str my, Space, Str alt]即:alt:字段的值my alt图片目标为(image.png, )即文件名与空标题。这一行为对应 RST reader 中figure指令的处理分支见 src/Text/Pandoc/Readers/RST.hsfigure - do (caption, legend) - parseFromString extractCaption body let src escapeURI $ trim top let (imgident, imgcls, aligncls, imgkvs) imgAttr class let (figclasskv, _) partition (( figclass) . fst) keyvals let figcls concatMap (T.words . snd) figclasskv let figattr (, figcls aligncls, []) let capt B.caption Nothing (B.plain caption legend) let alt maybe caption (B.text . trim) (lookup alt fields) return $ B.figureWith figattr capt $ B.plain (B.imageWith (imgident, imgcls, imgkvs) src alt)关键行是let alt maybe caption (B.text . trim) (lookup alt fields)lookup alt fields在指令的字段列表key-value中查找:alt:选项找到时用B.text . trim将其转为 Inline 文本去除首尾空白作为图片描述未找到时整个表达式回退为caption下一节详述。同时注意figure指令与image指令同文件 第 930-939 行在缺省 alt 上的差异image指令缺省 alt 为字符串image而figure指令缺省回退为图注。这是 pandoc 有意为之的设计——RST 的 figure 天然“图注即替代文本兜底”。三、场景 2不带:alt:的 figure —— alt 自动回退为图注测试第二段验证回退行为% pandoc -f rst -t native .. figure:: image.png Caption ^D [ Figure ( , [] , [] ) (Caption Nothing [ Plain [ Str Caption ] ]) [ Plain [ Image ( , [] , [] ) [ Str Caption ] ( image.png , ) ] ] ]当:alt:字段缺省时lookup alt fields返回Nothingmaybe caption将 alt 替换为caption于是图片描述文本变成了[Str Caption]。也就是说RSTfigure的替代文本语义是“显式 alt 优先缺省回退图注”保证任何 figure 最终都拥有一个非空的替代文本。这一“alt 回退 caption”的策略并非 RST reader 独有而是 pandoc 对图片块的统一约定。Markdown reader 的隐式 figure 处理implicitFigure见 src/Text/Pandoc/Readers/Markdown.hs遵循同样的逻辑implicitFigure (ident, classes, attribs) capt url title let alt case alt lookup attribs of Just alt - B.text alt _ - capt ... in B.figureWith figattr caption figbody可见无论输入是 RST 还是 Markdownpandoc 都保证Figure内部Image的描述文本不会为空。这为下游输出如 HTML 的alt属性提供了无障碍访问accessibility的基础保障。四、场景 3native → Markdown 往返 —— alt 无损写回测试第三段演示了反向路径把上一场景得到的 ASTalt 为my alt、图注为Caption用-t markdown写出% pandoc -f native -t markdown [ Figure ( , [] , [] ) (Caption Nothing [ Plain [ Str Caption ] ]) [ Plain [ Image ( , [] , [] ) [ Str my, Space, Str alt ] ( image.png , ) ] ] ] ^D Caption{altmy alt}输出中有两个层次的信息Markdown 图片语法Caption中的描述文本是图注Caption真正的替代文本my alt被写进图片属性{altmy alt}通过 pandoc 的属性扩展语法保留。也就是说Markdown 的“图片描述”位置承载的是图注而独立的alt属性承载替代文本二者在输出时被显式分开互不覆盖。该行为由 Markdown writer 的 Figure 分支实现见 src/Text/Pandoc/Writers/Markdown.hs。其核心过滤逻辑为combinedAttr imgattr case imgattr of (, cls, kvs) | (figid, [], []) - figattr - Just (figid, cls, [(k,v) | (k,v) - kvs , k / alt || v / v / trim (stringify capt)]) _ - Nothing该守卫会丢弃值为空串、或与图注文本完全相同的alt键避免冗余输出。随后还有一步补写逻辑同文件 第 753-758 行当图片描述这里为Caption与真实 altmy alt不一致且属性中尚无alt键时writer 会主动补上(alt, stringifyInlines alt)。这一“写出前补写、重复则剔除”的组合策略保证了RST figure(:alt:) → native → Markdown全程 alt 信息不丢失且输出干净、无冗余属性。五、场景 4LaTeX 侧的\pandocbounded容错测试第四段以注释 “Ignore\pandocbounded:” 开头验证 LaTeX 输入中的\pandocbounded命令被安全忽略% pandoc -f latex -t native \begin{figure} \centering \pandocbounded{\includegraphics[keepaspectratio,alt{Caption}]{image.png}} \caption{Caption} \end{figure} ^D [ Figure ( , [] , [] ) (Caption Nothing [ Plain [ Str Caption ] ]) [ Para [ Image ( , [] , [] ) [ Str Caption ] ( image.png , ) ] ] ]结果与场景 2 完全同构LaTeX 图片选项中的alt{Caption}被解析为图片描述[Str Caption]此时与图注相同故无独立 alt 键。理解\pandocbounded的来源有助于看懂这一测试的用意。pandoc 的 LaTeX writer 在输出图片时若图片未显式指定宽高会将其包裹进\pandocbounded{...}以约束尺寸见 src/Text/Pandoc/Writers/LaTeX.hsreturn $ (if inHeading then \\protect else ) (case dimension Width attr mplus dimension Height attr of Just _ - id Nothing - (\\pandocbounded ) . braces) ((if isSVG then \\includesvg else \\includegraphics) options braces (literal source))同时LaTeX writer 会把图片的 alt 文本渲染进\includegraphics的选项列表同文件 第 1181-1214 行优先取图片属性中的alt键缺省时回退为对描述文本stringifyInlines description的转义最终以alt{...}形式输出图片未指定宽高时还会附加keepaspectratio。这正是测试输入中\includegraphics[keepaspectratio,alt{Caption}]的来源——它是 pandoc 自己生成的代码。因此该场景验证的是往返自洽性pandoc 自己写出的 LaTeX含\pandocbounded与alt选项在重新读回时能还原出等价的 AST\pandocbounded这一内部包装命令被读取端忽略不产生任何多余节点。六、总结alt 文本的完整数据流与一致性设计将四个场景串起来可以得到 pandoc 关于图片替代文本的完整数据流阶段输入关键行为源码依据RST → AST:alt: my altalt 显式值进入 Image 描述文本RST.hs L927RST → AST无:alt:alt 回退为图注文本RST.hs L927AST → Markdown图注/alt 不同图注进![]alt 进{alt...}属性Markdown.hs L733-L758AST → Markdownalt 等于图注剔除冗余alt属性Markdown.hs L737-L739LaTeX → AST\pandocboundedalt{...}包装命令被忽略alt 选项还原为描述LaTeX.hs L1224-L1230设计要点可以归纳为三条非空保证无论 RST 还是 Markdown 输入Figure内Image的替代文本在缺省时都会回退为图注或image保证任何图片都带可读的描述信息分层Markdown 输出把图注与替代文本分置于![]描述位与{alt...}属性位两者仅在值相同/为空时才合并或剔除避免歧义与冗余往返自洽pandoc 自己生成的 LaTeX含\pandocbounded包装与alt选项能被自身读取并还原为等价 AST为基于 pandoc 的文档流水线提供了可靠的幂等性基础。该测试还可以与仓库中的其他 figure 用例互为参照例如 test/command/11486.md 涉及 revealjs 输出下的图注行为而 src/Text/Pandoc/Shared.hs 的figureDiv展示了不支持 figure 标记的格式如何降级为带figure类的 Div。若读者需要扩展对 RST figure 指令如:figclass:、对齐类、图片尺寸的掌握可直接阅读 RST.hs 中 figure 分支 的完整实现并通过pandoc -f rst -t native快速验证各种字段组合。赞分享文档开发工具CLI【免费下载链接】pandocUniversal markup converter项目地址https://gitcode.com/gh_mirrors/pa/pandoc点击查看免费下载相关推荐Pandoc GFM 删除线Strikethrough往返转换与命令回归测试解析Pandoc GFM 删除线Strikethrough往返转换与命令回归测试解析 本篇文章以 pandoc 仓库中的命令回归测试用例 test/comman文档开发工具CLIPandoc RST 读取器简单表格解析实战从命令测试看空单元格与 续行语义Pandoc RST 读取器简单表格解析实战从命令测试看空单元格与 续行语义 本文以 Pandoc 仓库中 test/command/10093.md h文档开发工具CLIPandoc RST 读取器简单表格的多行表头解析命令测试 10338 源码级剖析Pandoc RST 读取器简单表格的多行表头解析命令测试 10338 源码级剖析 导读 本文以 pandoc 仓库中的命令测试用例 test/command文档开发工具CLI创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门