From 873d52f79c5d42f5ae7c0bddf894e38988cd8190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Winterhalter?= Date: Fri, 25 Oct 2024 14:03:59 +0200 Subject: [PATCH] Make paper a function --- pandoc/paper.lua | 146 ++++++++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 70 deletions(-) diff --git a/pandoc/paper.lua b/pandoc/paper.lua index b2efc15..3f1baae 100644 --- a/pandoc/paper.lua +++ b/pandoc/paper.lua @@ -1,83 +1,89 @@ -function CodeBlock(el) +function paper(content) - if el.classes[1] == "paper" then - local content = string.format("{ %s }", el.text) - local data = pandoc.json.decode(content, false) + local data = pandoc.json.decode(content, false) - if not data then - error("Failed to decode JSON:\n" .. el.text) - end + if not data then + error("Failed to decode JSON:\n" .. el.text) + end - local title = data.title or "" - local url = data.url - local authors = data.authors or "" - local venue = data.venue - local year = data.year - local files = data.files or pandoc.List() + local title = data.title or "" + local url = data.url + local authors = data.authors or "" + local venue = data.venue + local year = data.year + local files = data.files or pandoc.List() - local header = {} + local header = {} + + if url then + header = { pandoc.Link(title, url) } + else + header = { pandoc.Str(title) } + end + + local sub = {} + + if venue and year then + sub = { pandoc.Str(string.format("%s (%s)", venue, year)) } + elseif venue then + sub = { pandoc.Str(venue) } + elseif year then + sub = { pandoc.Str(year) } + end - if url then - header = { pandoc.Link(title, url) } + local file_info = files:map(function(data) + local s = data.text or "" + local type = data.type + local src = data.src or "" + + local icon = "" + + if type == "pdf" then + icon = "" + elseif type == "bib" then + icon = "" + elseif type == "code" then + icon = "" + elseif type == "video" then + icon = "" + elseif type == "txt" then + icon = "" + elseif type == "img" then + icon = "" + elseif type == "zip" then + icon = "" + elseif type == "slides" then + icon = "" else - header = { pandoc.Str(title) } + icon = "" end - local sub = {} + local html_output = string.format( + "%s %s", + src, icon, s + ) - if venue and year then - sub = { pandoc.Str(string.format("%s (%s)", venue, year)) } - elseif venue then - sub = { pandoc.Str(venue) } - elseif year then - sub = { pandoc.Str(year) } - end + return pandoc.RawBlock("html", html_output) + end) + + local div_content = { + pandoc.Header(3, header), + pandoc.Para({pandoc.Str(authors)}, {class = "authors"}), + pandoc.Para(sub, {class = "venue"}), -- It seems the class cannot be set + pandoc.Div(file_info, { class = "files" }) + } + + local div = pandoc.Div(div_content, {class = "paper"}) + + return div + +end + +function CodeBlock(el) - local file_info = files:map(function(data) - local s = data.text or "" - local type = data.type - local src = data.src or "" - - local icon = "" - - if type == "pdf" then - icon = "" - elseif type == "bib" then - icon = "" - elseif type == "code" then - icon = "" - elseif type == "video" then - icon = "" - elseif type == "txt" then - icon = "" - elseif type == "img" then - icon = "" - elseif type == "zip" then - icon = "" - elseif type == "slides" then - icon = "" - else - icon = "" - end - - local html_output = string.format( - "%s %s", - src, icon, s - ) - - return pandoc.RawBlock("html", html_output) - end) - - local div_content = { - pandoc.Header(3, header), - pandoc.Para({pandoc.Str(authors)}, {class = "authors"}), - pandoc.Para(sub, {class = "venue"}), -- It seems the class cannot be set - pandoc.Div(file_info, { class = "files" }) - } - - local div = pandoc.Div(div_content, {class = "paper"}) - - return div + if el.classes[1] == "paper" then + local content = string.format("{ %s }", el.text) + return paper(content) end return el