Using yaml inside a Div for custom HTML printing #10334
-
Hello, I'm pretty new to ::: paper
title: The title
authors: Authors
url: https://some.url
venue: Some place
year: 2024
::: and then be able to print it nicely in HTML. It seems the proper solution would be to go, e.g, for a lua filter like so function Div(el)
if el.classes[1] == "paper" then
-- Somehow extract the data from el.content
local html_output = string.format(
'<div class="paper">\n' ..
' <h3><a href="%s">%s</a></h3>\n' ..
' <p class="authors">%s</p>\n' ..
' <p class="venue">%s (%s)</p>\n' ..
'</div>\n',
url, title, authors, venue, year
)
return pandoc.RawBlock("html", html_output)
end
return el
end But in the documentation I couldn't find anything about using Any pointers about the idiomatic Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
If you want to have something in a Markdown document that should be parsed as something other than Markdown, the best strategy to put it in a code block. Code blocks can have classes, so you can give the code block a For the output, you can make a raw block as in your example. Alternatively, you could use AST elements to construct the output, which would work for any output format. You could also combine the two approaches by using a template to create an HTML or Markdown string which you then parse with |
Beta Was this translation helpful? Give feedback.
-
Alright, thank you very much. In the end this is what I came up with. function CodeBlock(el)
if el.classes[1] == "paper" then
local data = pandoc.json.decode(el.text, false)
if not data then
error("Failed to decode JSON:\n" .. el.text)
end
local div_content = {
pandoc.Header(3, {pandoc.Link(data.title, data.url)}),
pandoc.Para({pandoc.Str(data.authors)}, {class = "authors"}),
pandoc.Para({pandoc.Str(data.venue .. "(" .. data.year .. ")")}, {class = "venue"}) -- It seems the class cannot be set
}
local div = pandoc.Div(div_content, {class = "paper"})
return div
end
return el
end While the input looks like ``` paper
{
"title": "Title",
"authors": "Author",
"venue": "Journal",
"year": "2024",
"url": "foo.com"
}
``` I would have liked something a bit lighter, but that's a start I guess. Thanks! |
Beta Was this translation helpful? Give feedback.
If you want to have something in a Markdown document that should be parsed as something other than Markdown, the best strategy to put it in a code block. Code blocks can have classes, so you can give the code block a
paper
class and look for aCodeBlock
with that class in the filter. Then your filter can get the unparsed text of the code block and parse it in an appropriate way. In the filter, you can use pandoc.json to parse the text as JSON or pandoc.read() to produce an AST from any format pandoc can parse, including a Markdown document that contains only a YAML block. JSON is convenient if you just want strings to insert in your template (pandoc,json.decode(block.text, false)
). A YAML…