简体   繁体   中英

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.

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
  • 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

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.

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. 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?

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):

-- 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:

---
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:

输出

(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+);?")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM