summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_parser.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <megas.kapaneus@gmail.com>2011-08-20 22:35:49 +0200
committerPhilipp Gesang <megas.kapaneus@gmail.com>2011-08-20 22:35:49 +0200
commitab0a580b61ae53c4d22cb93fd52006cb102b92e9 (patch)
treeeaaa5155d54a3288300bf73bd74f73afaf7d5aaa /mod/tex/context/third/rst/rst_parser.lua
parentbc3bb79d16c52531387c70c24f94513b62dcfa67 (diff)
downloadcontext-rst-ab0a580b61ae53c4d22cb93fd52006cb102b92e9.tar.gz
optional tab as indent handling; fixed missing blank line at eof bug
Diffstat (limited to 'mod/tex/context/third/rst/rst_parser.lua')
-rw-r--r--mod/tex/context/third/rst/rst_parser.lua35
1 files changed, 32 insertions, 3 deletions
diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua
index f5c9fbc..38ccecd 100644
--- a/mod/tex/context/third/rst/rst_parser.lua
+++ b/mod/tex/context/third/rst/rst_parser.lua
@@ -35,6 +35,8 @@ else
end
rst.stripBOM = false
+rst.expandtab = false
+rst.shiftwidth = 4
helpers.rst_debug = false
local warn = function(str, ...)
@@ -1351,22 +1353,49 @@ local parser = P{
table_header_hline = P"=",
}
-local function strip_BOM (raw)
+local file_helpers = { }
+
+function file_helpers.strip_BOM (raw)
if raw:match"^\239\187\191" then
return raw:sub(4)
end
return raw
end
+do
+ local space = " "
+
+ local tab = P"\t"
+ local eol = P"\n"
+ local indent = eol * tab
+ function file_helpers.expandtab (raw)
+ local spaces = space:rep(rst.shiftwidth)
+ local detabber = Cs(tab / spaces * (indent / "\n" .. spaces + 1)^0)
+ return detabber:match(raw)
+ end
+end
+
+function file_helpers.insert_blank (raw)
+ if not raw:match"\n%s$" then
+ return raw .. "\n\n"
+ end
+ return raw
+end
+
local function load_file (name)
f = assert(io.open(name, "r"), "Not a file!")
if not f then return 1 end
local tmp = f:read("*all")
f:close()
+
+ local fh = file_helpers
if rst.strip_BOM then
- return strip_BOM(tmp)
+ tmp = fh.strip_BOM(tmp)
+ end
+ if rst.expandtab then
+ tmp = fh.expandtab(tmp)
end
- return tmp
+ return fh.insert_blank(tmp)
end
local function save_file (name, data)