【问题标题】:Using span (f)or font color in Pandoc markdown for both html and pdf?在 Pandoc markdown 中为 html 和 pdf 使用 span (f) 或字体颜色?
【发布时间】:2020-07-10 09:24:06
【问题描述】:

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

我见过:

...我的选择似乎是:

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

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

不幸的是,我对 Lua 很差劲,而且对 Pandoc 的内部文档模型还不够了解,所以很容易猜到我怎么能在 Markdown 文件中找到这些类型的标签。所以我的问题是:pandoc 中是否有一个现有的过滤器或设置可以做到这一点 - 或者缺少一个在降价文档中查找此类标签的 Lua 过滤器脚本,我可以使用这种过滤器的基础?

【问题讨论】:

    标签: lua markdown pandoc


    【解决方案1】:

    好的,我想我到了某个地方 - 这是 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
    

    输出:

    【讨论】:

    【解决方案2】:

    (如果有人可以发表评论,我的声誉太低,无法发表评论)

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 2013-07-27
      • 1970-01-01
      相关资源
      最近更新 更多