summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2011-01-04 23:20:58 +0100
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2011-01-04 23:20:58 +0100
commit9d00a058df52d261861fcc7a9d4dd6474a5f7f53 (patch)
tree4bd03ec65f73b851605661f9041b0e41fe057333
parent3ffabd8881aaeaeb1d159f89ca1e7dec46d0954d (diff)
downloadcontext-rst-9d00a058df52d261861fcc7a9d4dd6474a5f7f53.tar.gz
macros for inline reST parsing
-rw-r--r--doc/documentation.rst4
-rw-r--r--doc/hybridtest.tex42
-rw-r--r--mod/tex/context/third/rst/rst_parser.lua32
-rw-r--r--mod/tex/context/third/rst/t-rst.mkiv53
4 files changed, 124 insertions, 7 deletions
diff --git a/doc/documentation.rst b/doc/documentation.rst
index b561ce9..d2cdc43 100644
--- a/doc/documentation.rst
+++ b/doc/documentation.rst
@@ -84,7 +84,9 @@ changes in form of |TEX| code only, you should be able to use the
output of |rstcontext| as starting point.
However, using the module may have advantages when testing. There
-is a usage example in ``moduletest.tex``.
+is a usage example in ``moduletest.tex``. Another example in
+``hybridtest.tex`` demonstrates the |CONTEXT| command ``\RST`` as
+well as the corresponding environment.
To install the module simply copy the files into your local |TEX|
tree. ::
diff --git a/doc/hybridtest.tex b/doc/hybridtest.tex
new file mode 100644
index 0000000..ba3bed5
--- /dev/null
+++ b/doc/hybridtest.tex
@@ -0,0 +1,42 @@
+\usemodule[rst]
+\setuphead[chapter][page=no,style=bold]
+
+\def\RSTCTX{{\em rst}\kern.5pt\CONTEXT}
+\def\reST{{\rm re}{\ss Structured}{\rm Text}}
+
+\starttext
+
+\chapter{\RSTCTX\ Hybrid Documents}
+
+This example demonstrates the macro \type{\RST} which can be used
+to process \reST\ markup directly in a normal \CONTEXT\ document.
+
+\RST{
+-------------
+This Chapter,
+-------------
+
+… for instance, was given entirely in *reST* markup. Naturally,
+there are some :bold:`drawbacks` to expect when mixing markups:
+directives and hyperlink targets that have already been specified
+somewhere above the current section will *stay* accessible in
+later passages until you redefine them. Also, certain letters
+need to be thoroughly escaped in order to make it through to the
+*reST*-parser, e.g. *\\\{* (<left brace>), and you’ll have to be
+inventive to make a backslash (*\\letterbackslash*) pass through
+the parser.
+
+}
+
+\startRST
+
+-------------
+Alternatively
+-------------
+you may always use the matching environment ``\\[start|stop]RST``
+if you prefer.
+
+\stopRST
+
+
+\stoptext
diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua
index 580204c..ba9b4c8 100644
--- a/mod/tex/context/third/rst/rst_parser.lua
+++ b/mod/tex/context/third/rst/rst_parser.lua
@@ -1361,7 +1361,7 @@ local function save_file (name, data)
return 0
end
-local function get_setups ()
+local function get_setups (inline)
local optional_setups = optional_setups -- might expect lots of calls
local setups = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1391,7 +1391,7 @@ local function get_setups ()
local f = optional_setups[item]
setups = f and setups .. f() or setups
end
- return setups .. [[
+ setups = setups .. [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -1400,8 +1400,11 @@ local function get_setups ()
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\starttext
]]
+ if not inline then
+ setups = setups .. "\n\\starttext"
+ end
+ return setups
end
local function main()
@@ -1440,11 +1443,11 @@ do
strip_comments = Cs((comment + 1)^0)
end
-function do_rst_file(fname)
+function rst.do_rst_file(fname)
local rst_parser = parser
local raw_data = load_file(fname)
local processed = rst_parser:match(raw_data)
- local setups = get_setups()
+ local setups = get_setups(false)
local tmp_file = tex.jobname .. "–rst_temporary.tex.tmp"
if processed then
@@ -1454,6 +1457,25 @@ function do_rst_file(fname)
end
end
+function rst.do_rst_snippet(txt)
+ local processed = parser:match(txt)
+ local setups = get_setups(true)
+ local tmp_file = tex.jobname .. "–rst_temporary.tex.tmp"
+
+ if processed then
+ warn("·cs·",txt)
+ processed = strip_comments:match(setups..processed)
+ save_file (tmp_file,processed)
+ context.input("./"..tmp_file)
+ else
+ warn("·cs·",txt)
+ context.par()
+ context("{\\bf context-rst could not process snippet.\\par}")
+ context.type(txt)
+ context.par()
+ end
+end
+
if not context then
return main()
diff --git a/mod/tex/context/third/rst/t-rst.mkiv b/mod/tex/context/third/rst/t-rst.mkiv
index e76d3db..ffcd839 100644
--- a/mod/tex/context/third/rst/t-rst.mkiv
+++ b/mod/tex/context/third/rst/t-rst.mkiv
@@ -37,6 +37,57 @@
\setupreST [ ]
%D This command loads and processes the \type{*.rst} file.
-\def\typesetRSTfile#1{\ctxlua{do_rst_file("#1")}}
+\def\typesetRSTfile#1{\ctxlua{rst.do_rst_file("#1")}}
+
+%D To process inline reST markup we’ll have to reset all catcodes
+%D except for grouping, escaping and cs arguments.
+\ifdefined\RSTcatcodes \else
+ \newcatcodetable \RSTcatcodes
+ \startcatcodetable \RSTcatcodes
+ \catcode`\^^I = 12 % ascii tab is a blank space
+ \catcode`\^^M = 12 % ascii return is end-line
+ \catcode`\^^L = 12 % ascii form-feed
+ \catcode`\ = 10 % ascii space is blank space
+ \catcode`\^^Z = 12
+ \catcode`\\ = 0
+ \catcode`\% = 12
+ \catcode`\# = 6
+ \catcode`\_ = 12
+ \catcode`\^ = 12
+ \catcode`\& = 12
+ \catcode`\| = 12
+ \catcode`\{ = 1
+ \catcode`\} = 2
+ \catcode`\~ = 12
+ \catcode`\$ = 12
+ \stopcatcodetable
+\fi
+
+%D The environment \type{\[start|stop]RST} and the macro
+%D \type{\RST} allow access to reST-parser from inside a
+%D \CONTEXT-document when the module is loaded.
+\unexpanded\def\startRST{
+ \setcatcodetable \RSTcatcodes%
+ \dostartRST%
+}
+
+\let\stopRST\relax
+
+\def\dostartRST#1\stopRST{%
+ \edef\RSTdata{#1}%
+ \setcatcodetable \ctxcatcodes%
+ \ctxlua{rst.do_rst_snippet([[\RSTdata]])}%
+}
+
+\def\RST{%
+ \setcatcodetable \RSTcatcodes%
+ \doRST%
+}
+
+\def\doRST#1{%
+ \def\RSTdata{#1}%
+ \setcatcodetable \ctxcatcodes%
+ \ctxlua{rst.do_rst_snippet([[\RSTdata]])}%
+}
\protect \endinput