繁体   English   中英

在 Pandoc markdown 中为 html 和 pdf 使用跨度(f)或字体颜色?

[英]Using span (f)or font color in Pandoc markdown for both html and pdf?

我想写在 Markdown 中,然后使用<font color="red">red text</font><span style="color: red;">red text</span>为文本着色,这样当.md 文件被签入说 Gitlab,浏览 HTML 格式的版本时会自动解析字体颜色 - 但我也想使用相同的.md 文件作为 ZBCD1B68617759B1DFCFF0403Axlate.58DFCFF0403Axlate.5 的源

我见过:

...我的选择似乎是:

  • 通过 Latex 将<span>显式用于 HTML,将\textcolor显式用于 PDF,这迫使我保留同一个 Z527DBEE2 文档0FC368977777 的两个版本
  • 使用 Lua 过滤器,并写得像violets are [blue]{color="blue"} - Gitlab 等使用的任何 Markdown-to-HTML 引擎绝对不会解析它

所以,我在想 - 一定有可能,我在我的文件中写<span><font> (这将/应该被 Gitlab 和此类解析器识别),然后有一个用于 pandoc 的 Lua 过滤器,这将转换当使用 .md 文件作为源来创建 PDF 时,这些进入\textcolor textcolor 。

不幸的是,我很讨厌 Lua,并且绝对不知道 Pandoc 的内部文件 model 足够多,以便能够很容易地猜到我怎么能在 Z2182A74BAB7188D959E795D9301E8 文件中找到这些类型的标签。 所以我的问题是: pandoc中是否有一个现有的过滤器或设置可以做到这一点 - 或者缺少一个 Lua 过滤器脚本,它在 markdown 文档中查找此类标签,我可以使用这种过滤的基础?

好的,我想我到了某个地方 - 这是color-text-span.lua (有点蹩脚,因为正则表达式明确要求color:冒号,但是嘿 - 总比没有好):

-- https://stackoverflow.com/questions/62831191/using-span-for-font-color-in-pandoc-markdown-for-both-html-and-pdf
-- https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html
-- https://ulriklyngs.com/post/2019/02/20/how-to-use-pandoc-filters-for-advanced-customisation-of-your-r-markdown-documents/

function Span (el)
  if string.find(el.attributes.style, "color") then
    stylestr = el.attributes.style
    thecolor = string.match(stylestr, "color: (%a+);")
    --print(thecolor)
    if FORMAT:match 'latex' then
      -- encapsulate in latex code
      table.insert(
        el.content, 1,
        pandoc.RawInline('latex', '\\textcolor{'..thecolor..'}{')
      )
      table.insert(
        el.content,
        pandoc.RawInline('latex', '}')
      )
      -- returns only span content
      return el.content
    else
      -- for other format return unchanged
      return el
    end
  else
    return el
  end
end

测试文件 - test.md:

---
title: "Test of color-text-span.lua"
author: Bob Alice
date: July 07, 2010
geometry: margin=2cm
fontsize: 12pt
output:
  pdf_document:
    pandoc_args: ["--lua-filter=color-text-span.lua"]
---

Hello, <span style="color: red;">red text</span>

And hello <span style="color: green;">green text</span>

调用命令:

pandoc test.md --lua-filter=color-text-span.lua --pdf-engine=xelatex -o test.pdf

Output:

输出

(感谢有人可以对此发表评论,我的声誉太低,无法发表评论)

如果删除正则表达式模式中的空格,则可以省略显式空格。 甚至更好地允许空格和非强制性分号:

将行thecolor = string.match(stylestr, "color: (%a+);")更改thecolor = string.match(stylestr, "color:%s*(%a+);?")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM