]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/literate/literate.lua
Merge
[micro.git] / runtime / plugins / literate / literate.lua
1 -- VERSION = "1.0.0"
2 if GetOption("literate") == nil then
3     AddOption("literate", true)
4 end
5
6 function startswith(str, start)
7    return string.sub(str,1,string.len(start))==start
8 end
9
10 function endswith(str, endStr)
11    return endStr=='' or string.sub(str,-string.len(endStr))==endStr
12 end
13
14 function split(string, sep)
15         local sep, fields = sep or ":", {}
16         local pattern = string.format("([^%s]+)", sep)
17         string:gsub(pattern, function(c) fields[#fields+1] = c end)
18         return fields
19 end
20
21 function onViewOpen(view)
22     if not GetOption("literate") then return end
23     if not endswith(view.Buf.Path, ".lit") then
24         return
25     end
26
27     local codetype = "unknown"
28     for i=1,view.Buf.NumLines do
29         local line = view.Buf:Line(i-1)
30         if startswith(line, "@code_type") then
31             codetype = split(line, " ")[2]
32             break
33         end
34     end
35
36     local syntaxFile = ""
37     syntaxFile = syntaxFile .. "filetype: literate-" .. codetype .. "\n"
38     syntaxFile = syntaxFile .. "detect:\n"
39     syntaxFile = syntaxFile .. "    filename: \"\\\\.lit$\"\n"
40     syntaxFile = syntaxFile .. "rules:\n"
41     syntaxFile = syntaxFile .. "    - include: \"markdown\"\n"
42     syntaxFile = syntaxFile .. "    - special: \"^(@s|@title|@code_type|@comment_type|@include|@change|@change_end)\"\n"
43     syntaxFile = syntaxFile .. "    - special: \"(@add_css|@overwrite_css|@colorscheme|@compiler|@error_format|@book)\"\n"
44     syntaxFile = syntaxFile .. "    - default:\n"
45     syntaxFile = syntaxFile .. "        start: \"---.*$\"\n"
46     syntaxFile = syntaxFile .. "        end: \"---$\"\n"
47     syntaxFile = syntaxFile .. "        limit-group: \"identifier\"\n"
48     syntaxFile = syntaxFile .. "        rules:\n"
49     syntaxFile = syntaxFile .. "            - special:\n"
50     syntaxFile = syntaxFile .. "                start: \"@\\\\{\"\n"
51     syntaxFile = syntaxFile .. "                end: \"\\\\}\"\n"
52     syntaxFile = syntaxFile .. "                rules: []\n"
53     syntaxFile = syntaxFile .. "            - include: " .. codetype .. "\n"
54
55     AddRuntimeFileFromMemory("literate", "syntax", "literate.yaml", syntaxFile)
56     Reload()
57 end