将HTML中的样式ID/名称传递到.docx中?

是否有可能通过某种方式告诉 pandoc 将原始HTML中的样式名称传递到 .docx 中?

我知道,为了调整实际的样式,我应该使用 pandoc 生成的 reference.docx 文件。但是,reference.docx 仅限于其所拥有的样式:标题、正文、块文本等等。

我想要:

  1. 在输入的HTML中指定 "myStyle" 样式(通过 "class" 属性,通过任何其他HTML属性甚至通过用Lua编写的过滤器代码),

    <html>
      <body>
        <p>你好</p>
        <p class="myStyle">世界!</p>
      </body>
    </html>
    
  2. 使用 Word 添加自定义的 "myStyle" 到 reference.docx 中,

  3. 运行 html->docx 转换并期望 pandoc 生成一个带有 "myStyle" 的段落元素(而不是 BodyText,我相信它是默认设置的(https://github.com/jgm/pandoc/blob/41d1ae0fdde099bd902607c577a6eca5a9ed3f22/data/docx/word/styles.xml#L23)),使最终结果看起来像这样(省略了输出结果中 word/document.xml 的内容以节省篇幅):

    <w:p>
      <w:pPr>
        <w:pStyle w:val="BodyText" />
      </w:pPr>
      <w:r>
        <w:txml:space="preserve">你好</w:t>
      </w:r>
    </w:p>
    <w:p>
      <w:pPr>
        <w:pStyle w:val="myStyle" />
      </w:pPr>
      <w:r>
        <w:txml:space="preserve">世界!</w:t>
      </w:r>
    </w:p>
    

有一些证据(https://github.com/jgm/pandoc/search?q=styleid&unscoped_q=styleid)表明 styleId 可以“传递”(https://github.com/jgm/pandoc/blob/5a20cc07ddee4115bee48ceabe890f0d490d9a62/src/Text/Pandoc/Writers/Docx.hs#L618),但我并不真正了解它,也找不到任何相关文档。

在有关 使用Lua过滤器 的说明中表明,当操作 pandoc.div 时可以访问 attrs,但它并未说明 pandoc 是否以有意义的方式解释其中的任何 attr。

点赞
用户80851
用户80851

最终,我找到了我需要的东西 - 自定义样式。这有限制,但比我之前得到的结果要好,当然比没有什么都好 :)

以下是一个逐步指南,以防有任何人遇到类似的问题。

首先,生成一个 reference.docx 文件如下:

pandoc --print-default-data-file reference.docx > styles.docx

然后在 MS Word 中打开该文件(我正在使用 macOS 版本),您将看到以下内容:

enter image description here

点击右侧的“新样式...”按钮,并创建您喜欢的样式。在我的例子中,我使文本变为粗体,是蓝色的:

enter image description here

由于我正在从 HTML 转换成 DOCX,在这里是我的 input.html

<html>
  <body>
    <div>Page 1</div>
    <div custom-style="eugene-is-testing">Page 2</div>
    <div>Page 3</div>
  </body>
</html>

运行:

pandoc --standalone --reference-doc styles.docx --output output.docx input.html

最后,享受结果:

enter image description here

2020-07-03 13:55:56