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