[英]How do I replace part of a string with a lua filter in Pandoc, to convert from .md to .pdf?
I am writing markdown files in Obsidian.md and trying to convert them via Pandoc and LaTeX to PDF. Text itself works fine doing this, howerver, in Obsidian I use ==equal signs== to highlight something, however this doesn't work in LaTeX.我正在 Obsidian.md 中编写 markdown 个文件,并尝试通过 Pandoc 和 LaTeX 将它们转换为 PDF。文本本身可以很好地执行此操作,但是,在 Obsidian 中我使用 ==equal signs== 来突出显示某些内容,但这不起作用在 LaTeX。
So I'd like to create a filter that either removes the equal signs entirely, or replaces it with something LaTeX can render, eg \hl{something}
.所以我想创建一个过滤器,要么完全删除等号,要么用 LaTeX 可以呈现的东西替换它,例如
\hl{something}
。 I think this would be the same process.我认为这将是相同的过程。
I have a filter that looks like this:我有一个看起来像这样的过滤器:
return {
{
Str = function (elem)
if elem.text == "hello" then
return pandoc.Emph {pandoc.Str "hello"}
else
return elem
end
end,
}
}
this works, it replaces any instance of "hello" with an italicized version of the word.这是有效的,它用单词的斜体版本替换了“hello”的任何实例。 HOWEVER, it only works with whole words.
但是,它只适用于整个单词。 eg if "hello" were part of a word, it wouldn't touch it.
例如,如果“你好”是一个词的一部分,它就不会触及它。 Since the equal signs are read as part of one word, it won't touch those.
因为等号是作为一个词的一部分来读的,所以它不会触及那些。
How do I modify this (or, please, suggest another filter) so that it CAN replace and change parts of a word?我如何修改它(或者,请建议另一个过滤器)以便它可以替换和更改单词的一部分?
Thank you!谢谢!
this works, it replaces any instance of "hello" with an italicized version of the word.这是有效的,它用单词的斜体版本替换了“hello”的任何实例。 HOWEVER, it only works with whole words.
但是,它只适用于整个单词。 eg if "hello" were part of a word, it wouldn't touch it.
例如,如果“你好”是一个词的一部分,它就不会触及它。 Since the equal signs are read as part of one word, it won't touch those.
因为等号是作为一个词的一部分来读的,所以它不会触及那些。
How do I modify this (or, please, suggest another filter) so that it CAN replace and change parts of a word?我如何修改它(或者,请建议另一个过滤器)以便它可以替换和更改单词的一部分?
Thank you!谢谢!
A string like Hello, World!
像
Hello, World!
这样的字符串becomes a list of inlines in pandoc: [ Str "Hello,", Space, Str "World!" ]
成为 pandoc 中的内联列表:
[ Str "Hello,", Space, Str "World!" ]
[ Str "Hello,", Space, Str "World!" ]
. [ Str "Hello,", Space, Str "World!" ]
。 Lua filters don't make matching on that particularly convenient: the best method is currently to write a filter for Inlines
and then iterate over the list to find matching items. Inlines
过滤器并不能使匹配特别方便:目前最好的方法是为内联编写一个过滤器,然后遍历列表以找到匹配项。
For a complete example, seehttps://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870 .有关完整示例,请参阅https://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870 。
Assuming we already found the highlighted text and converted it to a Span with with class mark
.假设我们已经找到突出显示的文本并将其转换为带有 class
mark
的 Span。 Then we can convert that to LaTeX with然后我们可以将其转换为 LaTeX
function Span (span)
if span.classes:includes 'mark' then
return {pandoc.RawInline('latex', '\\hl{')} ..
span.content ..
{pandoc.RawInline('latex', '}')}
end
end
Note that the current development version of pandoc, which will become pandoc 3 at some point, supports highlighted text out of the box when called with请注意,pandoc 的当前开发版本(在某些时候将成为 pandoc 3)支持在调用时开箱即用的高亮文本
pandoc --from=markdown+mark ...
Eg,例如,
echo '==Hi Mom!==' | pandoc -f markdown+mark -t latex
⇒ \hl{Hi Mom!}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.