From ab0a580b61ae53c4d22cb93fd52006cb102b92e9 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 20 Aug 2011 22:35:49 +0200 Subject: optional tab as indent handling; fixed missing blank line at eof bug --- mod/tex/context/third/rst/rst_parser.lua | 35 +++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'mod/tex/context/third/rst/rst_parser.lua') 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) -- cgit v1.2.3