summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/t-rst.mkiv
blob: 829a89522d8b4ad37e4645cb3518559dd01c3d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
%D \module [
%D         file=t-rst,
%D      version=0.3 ‘How to Recognise Different Types of Trees from Quite a Long Way Away’
%D        title=\CONTEXT\ User Module,
%D     subtitle=reStructuredText,
%D       author=Philipp Gesang,
%D         date=\currentdate,
%D    copyright=Philipp Gesang,
%D      license=2-clause BSD,
%D ]

%M \usemodule[rst]
%M \loadsetups[t-rst.xml]

\writestatus{loading}{ConTeXt User Module / reStructuredText}
\unprotect

\startinterface all
  \setinterfacevariable {RST} {RST}
\stopinterface

\definenamespace [RST] [
  type=module,
  comment=reStructuredText module,
  version=0.3,
  name=RST,
  style=no,
  command=yes,
  setup=list,
  parent=RST,
]

%D Loading the reStructuredText parser.
\ctxloadluafile{rst_parser}

\def\RST_enable_verbose{%
  \doif{\RSTparameter{debug}}\v!yes\ctxlua{thirddata.rst_helpers.rst_debug = true}%
}
\appendtoks \RST_enable_verbose \to \everysetupRST

%D Setting some globals.
\setupRST [debug=no]

%D This command loads and processes the \type{*.rst} file.
\def\typesetRSTfile#1{\ctxlua{thirddata.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.
\newcatcodetable   \RST_catcodes
\startcatcodetable \RST_catcodes
    \catcode`\^^I = 12 % ascii tab is a blank space
    \catcode`\^^M = 12 % ascii return is end-line
    \catcode`\^^L = 12 % ascii form-feed
    \catcode`\    = 12
    \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

%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 \RST_catcodes%
  \do_start_RST%
}

\let\stopRST\relax

\def\do_start_RST#1\stopRST{%
  \edef\RST_data{#1}%
  \setcatcodetable \ctxcatcodes%
  \ctxlua{thirddata.rst.do_rst_snippet([[\RST_data]])}%
}

\def\RST{%
  \setcatcodetable \RST_catcodes%
  \do_RST%
}

\def\do_RST#1{%
  \def\RST_data{#1}%
  \setcatcodetable \ctxcatcodes%
  \ctxlua{thirddata.rst.do_rst_snippet([[\RST_data]])}%
}

%%% Wolfgang’s code below; needs testing
%\unexpanded\def\startRST{%
  %\begingroup
  %\setcatcodetable \RSTcatcodes
  %\dostartRST%
%}

%\let\stopRST\relax

%\def\dostartRST#1\stopRST{%
  %\ctxlua{thirddata.rst.do_rst_snippet(\!!bs#1\!!es)}%
  %\endgroup%
%}

%\def\RST{%
  %\begingroup
  %\setcatcodetable \RSTcatcodes
  %\doRST%
%}

%\def\doRST#1{%
  %\ctxlua{thirddata.rst.do_rst_snippet(\!!bs#1\!!es)}%
  %\endgroup%
%}

\protect \endinput

% vim:ft=context:sw=2:ts=2