我想用Markdown编写,然后使用<font color="red">red text</font>或<span style="color: red;">red text</span>对文本进行颜色化,这样当.md文件被签入时--比如Gitlab --在浏览HTML格式化版本时字体颜色会自动解析--但我也希望使用相同的.md文件作为PDF via (Xe)乳液的源。
我见过:
..。我的选择似乎是:
<span>,通过Latex为\textcolor显式使用\textcolor,这迫使我保留两个版本的相同的标记文档violets are [blue]{color="blue"}的代码,这肯定不会被Gitlab和类似的使用的标记到HTML引擎解析。
所以,我在想--必须有可能,我在我的文件中写<span>或<font> ( Gitlab和类似的解析器应该识别它),然后为pandoc设置一个Lua过滤器,在使用.md文件作为源创建PDF时,将这些过滤器转换成\textcolor。
不幸的是,我对Lua很感兴趣,而且肯定不太了解Pandoc的内部文档模型,所以很容易猜到如何在Markdown文件中找到这类标记。所以我的问题是:在pandoc中是否已经有一个现有的过滤器或设置可以这样做--或者缺少这样的一个Lua过滤器脚本,可以在一个标记文档中查找这样的标记,我可以用一个基来进行这种过滤吗?
发布于 2020-07-10 12:11:23
好的,我想我找到了一些地方--这是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输出:

发布于 2021-02-21 21:11:50
(感谢有人能对此发表评论,我的声誉太低,不能发表评论)
如果删除regex模式中的空格,则可以省略显式空格。或者更好地允许使用空格和非强制性分号:
将行thecolor = string.match(stylestr, "color: (%a+);")更改为thecolor = string.match(stylestr, "color:%s*(%a+);?")
https://stackoverflow.com/questions/62831191
复制相似问题