简体   繁体   English

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

[英]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: ...我的选择似乎是:

  • Use <span> explicitly for HTML, and \textcolor explicitly for PDF via Latex, which forces me to keep two versions of the same markdown document通过 Latex 将<span>显式用于 HTML,将\textcolor显式用于 PDF,这迫使我保留同一个 Z527DBEE2 文档0FC368977777 的两个版本
  • Use Lua filter, and write like violets are [blue]{color="blue"} - which will definitely not be parsed by whatever Markdown-to-HTML engines used by Gitlab and such使用 Lua 过滤器,并写得像violets 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.

相关问题 将 Markdown 警告语法转换为 HTML,将 Lua 用于 Pandoc - Convertng Markdown admonition syntax to HTML, using Lua for Pandoc 从 markdown 转换为 HTML 时,使用 Pandoc Lua 过滤器替换 HTML 标签 - Replace HTML tags using Pandoc Lua filter when converting from markdown to HTML Pandoc:在 a.md 文件中,如何修改 YAML 元数据块并使用 lua 过滤器将修改后的块放入 markdown 格式 - Pandoc: in a .md file, how to modifiy the YAML metadata block and put the modified block in markdown format using a lua filter 在 Pandoc 过滤器中嵌套额外的 Span 会使图像消失 - Nesting extra Span in Pandoc filter disappears the image 用于Pandoc的Lua过滤器以附加html - Lua filter for pandoc to append html Pandoc Lua过滤器:如何为Span元素指定属性 - Pandoc Lua filters: how to specify attributes for Span element Pandoc Lua:如何在 header 周围添加 markdown 块而不丢失 Z590FC197FE73DB0AA2EC03687A372 - Pandoc Lua : how to add a markdown block around a header without losing the markdown syntax # 如何更改 Pandoc 元素的 HTML 呈现? - How to change the HTML rendering of a Pandoc element? Pandoc 使用 Lua 过滤器将字符串替换为表 - Pandoc replace string with table using Lua filter 如何在 R Markdown 中将两个单独的 lua 过滤器应用于 Span 元素? - How to apply two separate lua filters to Span elements, in R Markdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM