markdown_it.lua

Sun, 06 Sep 2020 22:12:52 +0300

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 06 Sep 2020 22:12:52 +0300
changeset 35
2f927eae429b
parent 30
3ac53da03439
child 39
5fc5f93a8cad
permissions
-rw-r--r--

Don't auto-create directories that will be empty

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