From 8c8f6783423a44dc0623b0b1f409d3a71457c6ab Mon Sep 17 00:00:00 2001
From: Philipp Gesang <megas.kapaneus@gmail.com>
Date: Sun, 28 Aug 2011 15:33:18 +0200
Subject: rst projects

---
 mod/tex/context/third/rst/rst_parser.lua | 51 +++++++++++++++++++++++++++-----
 mod/tex/context/third/rst/t-rst.mkiv     | 35 ++++++++++++++++++++--
 2 files changed, 77 insertions(+), 9 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 ff5d648..5bdc89d 100644
--- a/mod/tex/context/third/rst/rst_parser.lua
+++ b/mod/tex/context/third/rst/rst_parser.lua
@@ -1423,7 +1423,9 @@ end
 
 local function get_setups (inline)
     local optional_setups = optional_setups -- might expect lots of calls
-    local setups = [[
+    local setups = ""
+    if not inline then
+        setups = setups .. [[
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
 %{                           Setups                            }%
@@ -1432,6 +1434,10 @@ local function get_setups (inline)
 % General                                                       %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+]]
+    end
+
+    setups = setups .. [[
 \setupcolors[state=start]
 \setupinteraction[state=start,focus=standard,color=darkgreen,contrastcolor=darkgreen]
 \setupbodyfontenvironment [default]  [em=italic]
@@ -1446,12 +1452,12 @@ local function get_setups (inline)
 }
 
 ]]
-
     for item, _ in next, state.addme do
         local f = optional_setups[item]
         setups = f and setups .. f() or setups
     end
-    setups = setups .. [[
+    if not inline then
+        setups = setups .. [[
 
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1460,9 +1466,8 @@ local function get_setups (inline)
 %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
+\starttext
 ]]
-    if not inline then
-        setups = setups .. "\n\\starttext"
     end
     return setups
 end
@@ -1512,11 +1517,43 @@ function thirddata.rst.do_rst_file(fname)
 
     if processed then
         processed = strip_comments:match(setups..processed.."\n\\stoptext\n")
-        save_file (tmp_file,processed)
+        save_file(tmp_file, processed)
         context.input("./"..tmp_file)
     end
 end
 
+local rst_inclusions = { }
+local rst_incsetups  = { }
+function thirddata.rst.do_rst_inclusion (iname, fname)
+    local rst_parser = parser
+    local raw_data   = load_file(fname)
+    local processed  = rst_parser:match(raw_data)
+    local setups     = get_setups(true)
+
+    local incnr    = #rst_incsetups  + 1
+    local tmp_file = tex.jobname .. fmt("–rst_inclusion-%d.tex.tmp", incnr)
+
+    if processed then
+        processed = strip_comments:match(processed)
+        save_file(tmp_file, processed)
+        rst_inclusions[iname] = tmp_file
+        rst_incsetups[#rst_incsetups +1] = setups
+    end
+end
+
+function thirddata.rst.do_rst_setups ()
+    local out = table.concat(rst_incsetups )
+    context(out)
+end
+
+function thirddata.rst.get_rst_inclusion (iname)
+    if rst_inclusions[iname] then
+        context.input(rst_inclusions[iname])
+    else
+        context(fmt("{\\bf File for inclusion “%s” not found.}\par ", iname))
+    end
+end
+
 function thirddata.rst.do_rst_snippet(txt)
     local processed  = parser:match(txt)
     local setups     = get_setups(true)
@@ -1525,7 +1562,7 @@ function thirddata.rst.do_rst_snippet(txt)
     if processed then
         warn("·cs·",txt)
         processed = strip_comments:match(setups..processed)
-        save_file (tmp_file,processed)
+        save_file(tmp_file,processed)
         context.input("./"..tmp_file)
     else
         warn("·cs·",txt)
diff --git a/mod/tex/context/third/rst/t-rst.mkiv b/mod/tex/context/third/rst/t-rst.mkiv
index bb727b0..7971e3f 100644
--- a/mod/tex/context/third/rst/t-rst.mkiv
+++ b/mod/tex/context/third/rst/t-rst.mkiv
@@ -122,7 +122,7 @@
 %D
 %D \showsetup{typesetRSTfile}
 
-\def\do_typesetRSTfile[#1]#2{%
+\def\do_typeset_RST_file[#1]#2{%
   \iffirstargument
     \getparameters[RST][#1]%
     \doifdefined{RSTstripBOM}  {\ctxlua{thirddata.rst.strip_BOM  = \RSTstripBOM}}%
@@ -133,7 +133,7 @@
 }
 
 \def\typesetRSTfile{%
-  \dosingleempty\do_typesetRSTfile%
+  \dosingleempty\do_typeset_RST_file%
 }
 
 %D \subsection{Typesetting Inline Snippets}
@@ -178,6 +178,37 @@
   \ctxlua{thirddata.rst.do_rst_snippet(\!!bs#1\!!es)}%
 }
 
+\def\do_define_RST_inclusion[#1][#2]{%
+  \ifsecondargument
+    \ctxlua{thirddata.rst.do_rst_inclusion("#1", "#2")}%
+  \fi%
+}
+
+\def\defineRSTinclusion{%
+  \dodoubleempty\do_define_RST_inclusion%
+}
+
+\def\do_RST_inclusion[#1]{%
+  \iffirstargument
+    \ctxlua{thirddata.rst.get_rst_inclusion("#1")}%
+  \fi%
+}
+
+\def\do_RST_setups{%
+  \ctxlua{thirddata.rst.do_rst_setups()}%
+}
+
+\def\startRSTproject{
+  \begingroup
+  \def\RSTinclusion{\dosingleempty\do_RST_inclusion}
+  \do_RST_setups
+  \starttext%
+}
+
+\def\stopRSTproject{
+  \stoptext \endgroup \endinput
+}
+
 \protect \endinput
 
 % vim:ft=context:sw=2:ts=2
-- 
cgit v1.2.3