2 if GetOption("literate") == nil then
3 AddOption("literate", true)
6 function startswith(str, start)
7 return string.sub(str,1,string.len(start))==start
10 function endswith(str, endStr)
11 return endStr=='' or string.sub(str,-string.len(endStr))==endStr
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)
21 function onViewOpen(view)
22 if not GetOption("literate") then return end
23 if not endswith(view.Buf.Path, ".lit") then
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]
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"
55 AddRuntimeFileFromMemory("literate", "syntax", "literate.yaml", syntaxFile)