💾 Archived View for nickj.flounder.online › pandocGmi.gmi captured on 2021-12-17 at 13:26:06. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-12-03)
-=-=-=-=-=-=-
Whilst faffing with <gemtext> [1] it ocurred to me that <pandoc> [2] could do the heavy lifting when it came to generating .gmi files.
Still interested? Read on!
Gemini is a <recent proposal> [3] for a lightweight web protocol with a mime type of text/gmi. Gemtext is, by design, childishly simple to write by hand, but it’s another story if you have reams of stuff you want to put in geminispace. Hence the interest in converting to text/gmi.
Pandoc is a well established document converter, typically used to get from markdown to html. /However/ it takes lots of different <formats> [4] as input and output. Its input is converted into an internal representation which is emitted in the users choice of output format. Critically, it also provides facilities for emitting custom formats (see -F PROGRAM, –filter=PROGRAM under <Reader options> [5]). One of these <facilities> [6] enables you to use <Lua> [7] to convert Pandoc’s internal representation to the desired output.
You need `gmi.lua`. `default.gmi` will probably come in handy.
pandoc -t gmi.lua markdownFile
will convert markdown to text/gmi on stdout. If you want the title block etc etc to show up, use
pandoc -t gmi.lua --template default.gmi markdownFile
To convert from another format, do something like
pandoc -f FORMAT -t gmi.lua --template default.gmi file.format
where `FORMAT` is one of pandoc’s input formats. See <options> [8] for more details.
Owing to a combination of my laziness and inadequacy, some of the conversions aren’t very nice.
Oh, you wanted citations?
function Cite(s, cs) return "\nsorry cite not implemented\n" end
Nothing.
I may faff about with it to make it better suit my purposes over the next few years.
Should you wish to do the same, read on.
Using Lua to customize the output involves writing functions that are invoked when particular bits of pandoc’s internal representation come to be emitted. I don’t understand Pandoc, Haskell or Lua and luckily enough, you don’t have to either.
Doing
pandoc --print-default-data-file sample.lua > sample.lua
gives you a <lua program> [9] mimicking pandoc’s conversion to `html`
`gmi.lua` is just a hack of `sample.lua`
do
pandoc -v | grep "User data directory:" | sed "s/User data directory: //"
to find `$DATADIR`
Lua is fairly straightforward at the expression level so with luck, if you don’t like what I’m doing with subscripts in `gmi.lua`:
function Subscript(s) return "_" .. s end
then, knowing that `..` is the string concatenation operator, it’s pretty easy to mangle the results. Pandoc puts this on a plate in front of you and, short of eating it for you, there isn’t a lot more they could do.
Pandoc’s sample lua has some funky programming to deal with html output and needs a fairly big rewrite for our purposes, but it gives a good insight into the changes that have to be made. For instance, if you want to do citations, do
pandoc --print-default-data-file sample.lua > sample.lua
as mentioned above and pick the bones out of `function Cite(s, cs)`.
There isn’t any really, you just need `gmi.lua` and `default.gmi` somewhere you can remember and then reference them on the command line. Putting `default.gmi` in `$DATADIR/templates`, as <recommended> [12] works for me on windows and removes the needto explicitly specify a `gmi` template, however putting `gmi.lua` in `$DATADIR/filters` didn’t work (see -L SCRIPT, –lua-filter=SCRIPT under <Reader options> [13]), but that may be a windows thing?
`md2gmn` is an effective markdown to gmi converter - <download here> [14]