[英]Using span (f)or font color in Pandoc markdown for both html and pdf?
I would like to write in Markdown, then use either <font color="red">red text</font>
or <span style="color: red;">red text</span>
to color text, so that when the.md file is checked in say Gitlab, the font color is automatically parsed when browsing the HTML formatted version - but I also want to use the same.md file as a source for a PDF via (xe)latex.我想写在 Markdown 中,然后使用
<font color="red">red text</font>
或<span style="color: red;">red text</span>
为文本着色,这样当.md 文件被签入说 Gitlab,浏览 HTML 格式的版本时会自动解析字体颜色 - 但我也想使用相同的.md 文件作为 ZBCD1B68617759B1DFCFF0403Axlate.58DFCFF0403Axlate.5 的源
I have seen:我见过:
... and my options seem to be: ...我的选择似乎是:
<span>
explicitly for HTML, and \textcolor
explicitly for PDF via Latex, which forces me to keep two versions of the same markdown document<span>
显式用于 HTML,将\textcolor
显式用于 PDF,这迫使我保留同一个 Z527DBEE2 文档0FC368977777 的两个版本violets are [blue]{color="blue"}
- which will definitely not be parsed by whatever Markdown-to-HTML engines used by Gitlab and suchviolets are [blue]{color="blue"}
- Gitlab 等使用的任何 Markdown-to-HTML 引擎绝对不会解析它So, I was thinking - it must be possible, that I write <span>
or <font>
in my file (which would/should be recognized by Gitlab and such parsers), and then have a Lua filter for pandoc, that would transform these into \textcolor
, when using the.md file as a source to create PDF.所以,我在想 - 一定有可能,我在我的文件中写
<span>
或<font>
(这将/应该被 Gitlab 和此类解析器识别),然后有一个用于 pandoc 的 Lua 过滤器,这将转换当使用 .md 文件作为源来创建 PDF 时,这些进入\textcolor
textcolor 。
Unfortunately, I suck at Lua, and definitely do not know the internal document model of Pandoc enough, to be able to easily guess how could I get to these kinds of tags in a Markdown file.不幸的是,我很讨厌 Lua,并且绝对不知道 Pandoc 的内部文件 model 足够多,以便能够很容易地猜到我怎么能在 Z2182A74BAB7188D959E795D9301E8 文件中找到这些类型的标签。 So my question is: is there an existing filter or setting in
pandoc
already that does this - or lacking that, a Lua filter script that looks up such tags in a markdown document, that I could use a base for this kind of filtering?所以我的问题是:
pandoc
中是否有一个现有的过滤器或设置可以做到这一点 - 或者缺少一个 Lua 过滤器脚本,它在 markdown 文档中查找此类标签,我可以使用这种过滤的基础?
Ok, I think I got somewhere - this is color-text-span.lua
(is a bit crappy, because the regex explicitly demands a space after the color:
colon, but hey - better than nothing):好的,我想我到了某个地方 - 这是
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 file - test.md:测试文件 - 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>
Call command:调用命令:
pandoc test.md --lua-filter=color-text-span.lua --pdf-engine=xelatex -o test.pdf
Output: Output:
(appreciate if someone can make this a comment, I have too low reputation to comment) (感谢有人可以对此发表评论,我的声誉太低,无法发表评论)
If you remove the space in the regex pattern you can omit the explicit space.如果删除正则表达式模式中的空格,则可以省略显式空格。 Or even better allow for whitespaces and a non-mandatory semi colon:
甚至更好地允许空格和非强制性分号:
Change the line thecolor = string.match(stylestr, "color: (%a+);")
to thecolor = string.match(stylestr, "color:%s*(%a+);?")
将行
thecolor = string.match(stylestr, "color: (%a+);")
更改thecolor = string.match(stylestr, "color:%s*(%a+);?")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.