簡體   English   中英

在 Pandoc markdown 中為 html 和 pdf 使用跨度(f)或字體顏色?

[英]Using span (f)or font color in Pandoc markdown for both html and pdf?

我想寫在 Markdown 中,然后使用<font color="red">red text</font><span style="color: red;">red text</span>為文本着色,這樣當.md 文件被簽入說 Gitlab,瀏覽 HTML 格式的版本時會自動解析字體顏色 - 但我也想使用相同的.md 文件作為 ZBCD1B68617759B1DFCFF0403Axlate.58DFCFF0403Axlate.5 的源

我見過:

...我的選擇似乎是:

  • 通過 Latex 將<span>顯式用於 HTML,將\textcolor顯式用於 PDF,這迫使我保留同一個 Z527DBEE2 文檔0FC368977777 的兩個版本
  • 使用 Lua 過濾器,並寫得像violets are [blue]{color="blue"} - Gitlab 等使用的任何 Markdown-to-HTML 引擎絕對不會解析它

所以,我在想 - 一定有可能,我在我的文件中寫<span><font> (這將/應該被 Gitlab 和此類解析器識別),然后有一個用於 pandoc 的 Lua 過濾器,這將轉換當使用 .md 文件作為源來創建 PDF 時,這些進入\textcolor textcolor 。

不幸的是,我很討厭 Lua,並且絕對不知道 Pandoc 的內部文件 model 足夠多,以便能夠很容易地猜到我怎么能在 Z2182A74BAB7188D959E795D9301E8 文件中找到這些類型的標簽。 所以我的問題是: pandoc中是否有一個現有的過濾器或設置可以做到這一點 - 或者缺少一個 Lua 過濾器腳本,它在 markdown 文檔中查找此類標簽,我可以使用這種過濾的基礎?

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

Output:

輸出

(感謝有人可以對此發表評論,我的聲譽太低,無法發表評論)

如果刪除正則表達式模式中的空格,則可以省略顯式空格。 甚至更好地允許空格和非強制性分號:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM