简体   繁体   English

通过pandoc lua过滤器添加元数据字段

[英]add metadata field via pandoc lua filter

I'm processing a markdown document such as 我正在处理降价文件,例如

---
title: "dummy title"
highlight: "c"
highlighted: 'highlighted'
---     
body text

into a highly-customised latex template where, among other things "c" is being highlighted as part of a larger tex macro. 进入高度自定义的乳胶模板中,其中“ c”作为较大的tex宏的一部分被突出显示。 One last step is resisting me: the dummy metadata "highlighted" should not have to be specified here, but I can't find a way to generate it in the lua filter below: 最后一个步骤是拒绝我:这里不必指定虚拟元数据“突出显示”,但是我找不到在下面的lua过滤器中生成它的方法:

text = require 'text'
newstring = '\\textit{a,b,c,d}'

function meta_vars (m)
       -- m.highlighted = 'highlighted' -- not working
        highlight = m.highlight
  return m
end

function replace (elem)
      if elem.text == 'highlighted' then
        newstring = newstring:gsub(pandoc.utils.stringify(highlight),
                       '{\\bfseries '..pandoc.utils.stringify(highlight)..'}')
        return pandoc.RawInline("latex", newstring)
        else
        return elem
      end
    end

return {{Meta = meta_vars}, {Str = replace}}

with the following custom template 使用以下自定义模板

\documentclass[12pt]{article}

\begin{document}

\title{$title$ -- $highlighted$}
\maketitle
$body$
\end{document}

The command to run this example is 运行此示例的命令是

pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --template _tpl.tex --lua-filter=filter.lua 

producing, as advertised 生产,如广告所示

enter image description here 在此处输入图片说明

How can I get rid off the dummy highlighted tag in my YAML header? 如何摆脱YAML标头中的虚拟highlighted标签?

I don't really understand exactly what you're doing with the latex macros here, but the crux of your question seems to be about adding a new metadata element. 我在这里不太了解您在使用乳胶宏的确切含义,但是问题的症结似乎在于添加新的元数据元素。 The line that you have commented out with --not working seems to work fine for me. 你已经有注释掉线--not working似乎为我工作的罚款。 You might want to explicitly set the type with 您可能需要显式设置类型

m.highlighted=pandoc.MetaString('highlighted')

but I don't think that will change anything. 但我认为这不会改变任何事情。

I also note that as it is written the newstring variable will be modified whenever the word highlighted appears in the text. 我还注意到,在撰写本文时,只要highlighted显示的单词出现在文本中,就会修改newstring变量。 With two lines of highlighted the newstring will equal \\textit{a,b,{\\bfseries {\\bfseries c}},d} . 用两行highlighted的新字符串将等于\\textit{a,b,{\\bfseries {\\bfseries c}},d}

Perhaps you could restate your question with the latex macro stuff removed and just focus on what you want the filter to do. 也许您可以删除乳胶宏内容来重述您的问题,而只关注您希望过滤器执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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