首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Pandoc中使用span (F)或字体颜色来标记html和pdf?

在Pandoc中使用span (F)或字体颜色来标记html和pdf?
EN

Stack Overflow用户
提问于 2020-07-10 09:24:06
回答 2查看 1.7K关注 0票数 6

我想用Markdown编写,然后使用<font color="red">red text</font><span style="color: red;">red text</span>对文本进行颜色化,这样当.md文件被签入时--比如Gitlab --在浏览HTML格式化版本时字体颜色会自动解析--但我也希望使用相同的.md文件作为PDF via (Xe)乳液的源。

我见过:

..。我的选择似乎是:

  • 为HTML显式使用<span>,通过Latex为\textcolor显式使用\textcolor,这迫使我保留两个版本的相同的标记文档
  • Use Lua filter,并编写类似violets are [blue]{color="blue"}的代码,这肯定不会被Gitlab和类似的

使用的标记到HTML引擎解析。

所以,我在想--必须有可能,我在我的文件中写<span><font> ( Gitlab和类似的解析器应该识别它),然后为pandoc设置一个Lua过滤器,在使用.md文件作为源创建PDF时,将这些过滤器转换成\textcolor

不幸的是,我对Lua很感兴趣,而且肯定不太了解Pandoc的内部文档模型,所以很容易猜到如何在Markdown文件中找到这类标记。所以我的问题是:在pandoc中是否已经有一个现有的过滤器或设置可以这样做--或者缺少这样的一个Lua过滤器脚本,可以在一个标记文档中查找这样的标记,我可以用一个基来进行这种过滤吗?

EN

回答 2

Stack Overflow用户

发布于 2020-07-10 12:11:23

好的,我想我找到了一些地方--这是color-text-span.lua (有点糟糕,因为正则表达式在color:冒号之后明确要求一个空格,但是嘿--总比没有强):

代码语言:javascript
运行
复制
-- 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:

代码语言:javascript
运行
复制
---
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>

呼叫命令:

代码语言:javascript
运行
复制
pandoc test.md --lua-filter=color-text-span.lua --pdf-engine=xelatex -o test.pdf

输出:

票数 6
EN

Stack Overflow用户

发布于 2021-02-21 21:11:50

(感谢有人能对此发表评论,我的声誉太低,不能发表评论)

如果删除regex模式中的空格,则可以省略显式空格。或者更好地允许使用空格和非强制性分号:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62831191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档