summaryrefslogtreecommitdiff
path: root/src/t-rst.mkiv
blob: 48801b57a04bf4e491c54ca056b0b64fe6953dfc (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
%D \module [
%D         file=t-rst,
%D      version=0.6 ‘It’s the Arts’,
%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 \usemodule  [int-load]
%M \loadsetups [t-letterspace.xml]

%C Read the license conditions in the file \type{COPYING}.

%M \definecolor [gutenred] [x=bf221f] % rubrication from digitized Göttingen Gutenberg bible
%M \setupinteraction [contrastcolor=gutenred,color=gutenred]
%M
%M \define\beautifyshowsetups{%
%M   \unexpanded \def \setupnumfont  {\rm}%
%M   \unexpanded \def \setuptxtfont  {\rm}%
%M   \unexpanded \def \setupintfont  {\rm\sc\Word}%
%M   \unexpanded \def \setupvarfont  {\rm\it}%
%M   \unexpanded \def \setupoptfont  {\rm\it}%
%M   \unexpanded \def \setupalwcolor {gutenred}%
%M   \unexpanded \def \setupoptcolor {gutenred}%
%M   \defineframedtext [setuptext] [
%M     frame=off,
%M     background=color,
%M     backgroundcolor=gray:2,
%M     width=\hsize,
%M     height=fit,
%M     align=right,
%M     offset=0.75em,
%M   ]%
%M }
%M 
%M \let \Oldshowsetup \showsetup
%M 
%M \define [1] \showsetup {%
%M   \bgroup \beautifyshowsetups%
%M     \Oldshowsetup{#1}%
%M   \egroup%
%M }

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

\unprotect

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

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

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

%D Easy way to define a global test setting. Activated
%D by \type{\usemodule[rst][test=yes]}.

\startmoduletestsection
  \ctxlua{thirddata.rst_helpers.rst_debug = true}
\stopmoduletestsection

%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
  \catcode`\^^M = 12
  \catcode`\^^L = 12
  \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 \section {User-level Commands}
%D
%D \subsection{Typesetting reST-Files}
%D
%D \macros
%D    {typesetRSTfile}
%D
%D This command loads and processes an \type{*.rst} file.
%D All necessary setups for the elements to be used (e.g. tables)
%D have to be specified {\em before} this macro is called.
%D As \type{\typesetRSTfile} is intended to process a single file
%D only, it will handle \type{\start|stoptext} automatically.
%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_typeset_RST_file[#1]#2{%
  \iffirstargument
    \getparameters[RST][#1]%
    \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")}%
}

\def\typesetRSTfile{%
  \dosingleempty\do_typeset_RST_file%
}

%D \subsection{Typesetting Inline Snippets}
%D
%D reST markup can be handy in situations where \CONTEXT\ markup
%D would result in unappropriately verbose source code, e.g. when
%D typesetting tables with simple layout.
%D
%D \macros
%D    {RST,startRST}
%D
%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.
%D
%D \showsetup{RST}
%D \showsetup{startRST}

% Wolfgang’s code below.

\unexpanded \def \startRST{%
  \begingroup
  \setcatcodetable \RST_catcodes
  \do_start_RST%
}

\let \stopRST \relax

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

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

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

%D \subsection{Including multiple reST files}
%D
%D \macros
%D    {defineRSTinclusion,startRSTproject,RSTinclusion}
%D
%D When content is split among multiple files, these macros allow
%D for including them in arbitrary order. Note that setups from
%D previous includes, unless overwritten, will remain defined.
%D \type{\defineRSTinclusion} takes three arguments: the first one
%D will be the identifier that can be used to refer to the actual
%D inclusion, which is specified via the second argument as a
%D filename. The third optional argument receives the usual
%D setups \type{stripBOM}, \type{expandtab} and
%D \type{shiftwidth}.
%D
%D Defined inclusions can be typeset only within an the
%D \type{\startRSTproject} environment using the macro
%D \type{RSTinclusion}. Between those inclusion all kinds of
%D \TEX\ code except for \type{\starttext} and \type{\stoptext}
%D are permitted.

\def\do_define_RST_inclusion[#1][#2][#3]{%
  \ifthirdargument
    \getparameters[RST][#3]%
    \doifdefined{RSTstripBOM}  {\ctxlua{thirddata.rst.strip_BOM  = \RSTstripBOM}}%
    \doifdefined{RSTexpandtab} {\ctxlua{thirddata.rst.expandtab  = \RSTexpandtab}}%
    \doifdefined{RSTshiftwidth}{\ctxlua{thirddata.rst.shiftwidth = \RSTshiftwidth}}%
  \fi
  \ifsecondargument
    \ctxlua{thirddata.rst.do_rst_inclusion("#1", "#2")}%
  \fi%
}

\def\defineRSTinclusion{%
  \dotripleempty\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