From f44dcbb87123a87dbfff3f734fc338f0865daf69 Mon Sep 17 00:00:00 2001 From: rxi Date: Sun, 24 May 2020 14:10:17 +0100 Subject: Added `ghmarkdown` plugin --- plugins/ghmarkdown.lua | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 plugins/ghmarkdown.lua (limited to 'plugins/ghmarkdown.lua') diff --git a/plugins/ghmarkdown.lua b/plugins/ghmarkdown.lua new file mode 100644 index 0000000..b49e248 --- /dev/null +++ b/plugins/ghmarkdown.lua @@ -0,0 +1,74 @@ +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() + local dv = core.active_view + + 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 = ".lite_ghmarkdown_" .. os.tmpname():gsub("%W", "") .. ".html" + local fp = io.open(htmlfile, "w") + fp:write(text) + fp:close() + + core.log("Opening markdown preview for \"%s\"", dv:get_name()) + local path = system.absolute_path(".") .. "/" .. htmlfile + if PLATFORM == "Windows" then + system.exec("start " .. path) + else + system.exec(string.format("xdg-open %q", path)) + end + + core.add_thread(function() + coroutine.yield(5) + os.remove(htmlfile) + end) + end +}) + + +keymap.add { ["ctrl+shift+m"] = "ghmarkdown:show-preview" } -- cgit v1.2.3