From 9d00a058df52d261861fcc7a9d4dd6474a5f7f53 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 4 Jan 2011 23:20:58 +0100 Subject: macros for inline reST parsing --- mod/tex/context/third/rst/rst_parser.lua | 32 ++++++++++++++++--- mod/tex/context/third/rst/t-rst.mkiv | 53 +++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) (limited to 'mod') 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 -- cgit v1.2.3