I have a LaTeX document that I convert to HTML using Pandoc. My template references the $date$ variable like so:
$if(date)$
<p>$date$</p>
$endif$
As usual, the value of $date$ is the ISO formatted date (e.g., 2022-06-17 for today). I want to format it so that it has a human-readable format, so that I can get this output:
<p>17 Jun 2022</p>
Usually, one can achieve this using format strings (for the above example: %d %b %Y). Is there a way to do that for the Pandoc's output?
A simple Lua filter can achieve this. The following filter code is adapted from Setting the date in the metadata from the Pandoc documentation.
-- filters/date-format.lua
function Meta(meta)
if meta.date then
local format = "(%d+)-(%d+)-(%d+)"
local y, m, d = pandoc.utils.stringify(meta.date):match(format)
local date = os.time({
year = y,
month = m,
day = d,
})
local date_string = os.date("%d %b %Y", date)
meta.date = pandoc.Str(date_string)
return meta
end
end
---
# test.md
title: My Title
date: 2022-06-18
---
<!-- templates/date.html -->
$if(date)$
<p>$date$</p>
$endif$
Compile with the command pandoc --data-dir=. --template=date --lua-filter=date-format.lua test.md -o test.html.
The output should be <p>18 Jun 2022</p>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With