From be8124154b2b3e761578a186c42a18ace5a43865 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 27 May 2017 17:16:21 -0400 Subject: [PATCH] Re-add literate supportg --- runtime/plugins/literate/README.md | 5 +++ runtime/plugins/literate/literate.lua | 53 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 runtime/plugins/literate/README.md create mode 100644 runtime/plugins/literate/literate.lua diff --git a/runtime/plugins/literate/README.md b/runtime/plugins/literate/README.md new file mode 100644 index 00000000..771971a3 --- /dev/null +++ b/runtime/plugins/literate/README.md @@ -0,0 +1,5 @@ +# Literate Micro + +Support for reading `.lit` files from [zyedidia/Literate](https://github.com/zyedidia/Literate). + +This plugin will automatically detect the filetype and highlight the correct language inside the code blocks. diff --git a/runtime/plugins/literate/literate.lua b/runtime/plugins/literate/literate.lua new file mode 100644 index 00000000..aa41f415 --- /dev/null +++ b/runtime/plugins/literate/literate.lua @@ -0,0 +1,53 @@ +VERSION = "1.0.0" + +function startswith(str, start) + return string.sub(str,1,string.len(start))==start +end + +function endswith(str, endStr) + return endStr=='' or string.sub(str,-string.len(endStr))==endStr +end + +function split(string, sep) + local sep, fields = sep or ":", {} + local pattern = string.format("([^%s]+)", sep) + string:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end + +function onViewOpen(view) + if not endswith(view.Buf.Path, ".lit") then + return + end + + local codetype = "unknown" + for i=1,view.Buf.NumLines do + local line = view.Buf:Line(i-1) + if startswith(line, "@code_type") then + codetype = split(line, " ")[2] + break + end + end + + local syntaxFile = "" + syntaxFile = syntaxFile .. "filetype: literate-" .. codetype .. "\n" + syntaxFile = syntaxFile .. "detect:\n" + syntaxFile = syntaxFile .. " filename: \"\\\\.lit$\"\n" + syntaxFile = syntaxFile .. "rules:\n" + syntaxFile = syntaxFile .. " - include: \"markdown\"\n" + syntaxFile = syntaxFile .. " - special: \"^(@s|@title|@code_type|@comment_type|@include|@change|@change_end)\"\n" + syntaxFile = syntaxFile .. " - special: \"(@add_css|@overwrite_css|@colorscheme|@compiler|@error_format|@book)\"\n" + syntaxFile = syntaxFile .. " - default:\n" + syntaxFile = syntaxFile .. " start: \"---.*$\"\n" + syntaxFile = syntaxFile .. " end: \"---$\"\n" + syntaxFile = syntaxFile .. " limit-group: \"identifier\"\n" + syntaxFile = syntaxFile .. " rules:\n" + syntaxFile = syntaxFile .. " - special:\n" + syntaxFile = syntaxFile .. " start: \"@\\\\{\"\n" + syntaxFile = syntaxFile .. " end: \"\\\\}\"\n" + syntaxFile = syntaxFile .. " rules: []\n" + syntaxFile = syntaxFile .. " - include: " .. codetype .. "\n" + + AddRuntimeFileFromMemory("literate", "syntax", "literate.yaml", syntaxFile) + Reload() +end