markdown_it.lua

Sun, 06 Aug 2023 14:32:25 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Aug 2023 14:32:25 +0300
changeset 42
260f867d46c0
parent 39
5fc5f93a8cad
permissions
-rw-r--r--

Preliminary breadcrumb support

local markdown_it={}

local io = require('io')
local os = require('os')
local path = require('mypath')
local lfs = require('lfs')

function markdown_it.katex_config(env)
    katex_config = path.join(env.paths.tmpl, "katex_config.json")
    if lfs.attributes(katex_config) then
        return katex_config
    else
        return nil
    end
end

function markdown_it.markdown(str, env)
    -- Lua doesn't have bidirectional pipes without the posix library.
    -- Keep things and compatible and use temporary files.
    tmpname = os.tmpname()
    file = io.open(tmpname, 'w')
    file:write(str)
    cmd = string.format("node '%s/markdown_it.js' '%s'",
                        env.paths.lgen_location,
                        tmpname)
    -- Provide TEMPLATE_PATH/katex_config.json if it exists
    katex_config = markdown_it.katex_config(env)
    if katex_config then
        cmd = cmd .. " '" .. katex_config .. "'"
    end
    -- io.stdout:write('Executing ' .. cmd .. '\n')
    h = io.popen(cmd, 'r')
    result = h:read("*a")
    h:close()
    file:close()
    return result
end

function markdown_it.add_deps(deps, env)
    katex_config = markdown_it.katex_config(env)
    if katex_config then
        table.insert(deps, katex_config)
    end
end

return markdown_it

mercurial