From ab0a580b61ae53c4d22cb93fd52006cb102b92e9 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <megas.kapaneus@gmail.com>
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 +++++++++++++++++++++++++++++---
 mod/tex/context/third/rst/t-rst.mkiv     | 12 ++++++++++-
 2 files changed, 43 insertions(+), 4 deletions(-)

(limited to 'mod/tex/context')

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)
diff --git a/mod/tex/context/third/rst/t-rst.mkiv b/mod/tex/context/third/rst/t-rst.mkiv
index 0a3e25b..bb727b0 100644
--- a/mod/tex/context/third/rst/t-rst.mkiv
+++ b/mod/tex/context/third/rst/t-rst.mkiv
@@ -112,12 +112,22 @@
 %D Thus, the user should never supply any of these manually,
 %D neither before nor after \type{\typesetRSTfile}.
 %D
+%D We now handle rogue utf-8 byte order marks on demand, just set
+%D the optional parameter \type{stripBOM} to {\em true}.
+%D
+%D There also is an option \type{expandtab} to convert tabs
+%D (ascii 0x09) to indents prior to converting reST input. The
+%D expansion width defaults to {\em 4} and can be configured
+%D through the parameter \type{shiftwidth} (takes an integer).
+%D
 %D \showsetup{typesetRSTfile}
 
 \def\do_typesetRSTfile[#1]#2{%
   \iffirstargument
     \getparameters[RST][#1]%
-    \doifdefined{\RSTstripBOM}{\ctxlua{thirddata.rst.strip_BOM = "\RSTstripBOM"}}%
+    \doifdefined{RSTstripBOM}  {\ctxlua{thirddata.rst.strip_BOM  = \RSTstripBOM}}%
+    \doifdefined{RSTexpandtab} {\ctxlua{thirddata.rst.expandtab  = \RSTexpandtab}}%
+    \doifdefined{RSTshiftwidth}{\ctxlua{thirddata.rst.shiftwidth = \RSTshiftwidth}}%
   \fi
   \ctxlua{thirddata.rst.do_rst_file("#2")}%
 }
-- 
cgit v1.2.3