-- mod-version:3
local core = require "core"
local command = require "core.command"
local keymap = require "core.keymap"
local html = [[
${title}
]]
command.add("core.docview!", {
["ghmarkdown:show-preview"] = function(dv)
local content = dv.doc:get_text(1, 1, math.huge, math.huge)
local esc = { ['"'] = '\\"', ["\n"] = '\\n' }
local text = html:gsub("${(.-)}", {
title = dv:get_name(),
content = content:gsub(".", esc)
})
local htmlfile = core.temp_filename(".html")
local fp = io.open(htmlfile, "w")
fp:write(text)
fp:close()
core.log("Opening markdown preview for \"%s\"", dv:get_name())
if PLATFORM == "Windows" then
system.exec("start " .. htmlfile)
elseif PLATFORM == "AmigaOS 4" then
system.exec('urlopen "file=' .. htmlfile .. '"')
elseif PLATFORM == "MorphOS" then
system.exec('openurl "' .. htmlfile .. '" FILE;')
else
system.exec(string.format("xdg-open %q", htmlfile))
end
core.add_thread(function()
coroutine.yield(5)
os.remove(htmlfile)
end)
end
})
keymap.add { ["ctrl+alt+m"] = "ghmarkdown:show-preview" }