back-exp.mkiv /size: 9 Kb    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=back-exp,
3%D        version=2010.08.22,
4%D          title=\CONTEXT\ Backend Macros,
5%D       subtitle=XML export,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14\writestatus{loading}{ConTeXt Backend Macros / XML export}
15
16\registerctxluafile{back-exp}{}
17
18%D This is an experimental exporter and a logical follow up on tagging. The
19%D exporter assumes a properly tagged document. Some elements get a couple
20%D of attributes because otherwise rendering information would get lost. In
21%D general we assume that when the \XML\ is converted to \HTML\ some stylesheet
22%D is applied anyway.
23
24\unprotect
25
26% we can replace this by a more generic attributeset mechanism where we bind
27% to any element (needed anyway, see userdata thingies)
28
29\definesystemattribute[taggedpar][public]
30
31\unexpanded\def\setelementexporttag
32  {\dotripleargument\back_export_set_element_tag}
33
34\def\back_export_set_element_tag
35  {\ifthirdargument
36     \expandafter      \back_export_set_element_tag_a
37   \else\ifsecondargument
38     \doubleexpandafter\back_export_set_element_tag_b
39   \fi\fi}
40
41\def\back_export_set_element_tag_a[#1][#2][#3]{\clf_settagproperty{#1}{#2}{#3}}
42\def\back_export_set_element_tag_b[#1][#2][#3]{\clf_settagproperty{#1}{export}{#2}}
43
44% todo: no need for calls when trialtypesetting
45
46\def\taggedctxcommand
47  {\iftrialtypesetting
48     \expandafter\gobbleoneargument
49   \else
50     \expandafter\ctxcommand
51   \fi}
52
53\newcount\tagparcounter
54
55\let\dotagsetparcounter\relax
56\let\doresetparcounter \relax
57
58\appendtoks
59    \doresetparcounter
60\to \everyflushatnextpar
61
62% \appendtoks
63%     \dotagsetparcounter
64% \to \everypar
65
66\prependtoks
67    \dotagsetparcounter
68\to \everypar
69
70% \appendtoks
71%     \dotagsetparcounter
72% \to \neverypar
73
74\prependtoks
75    \dotagsetparcounter
76\to \neverypar
77
78\appendtoks
79    \dotagsetparcounter
80\to \everytabulatepar % tricky, maybe this should be neverypar
81
82\appendtoks
83    \unexpanded\def\dotagTABLEcell
84      {\iftrialtypesetting\else
85         \clf_settagtablecell
86           \numexpr\tablecellrows\relax
87           \numexpr\tablecellcolumns\relax
88           \numexpr\raggedstatus\relax
89       \fi}%
90    % brrr, we need to tag empty cells (unless we start numbering)
91    \unexpanded\def\dotagTABLEsignal
92      {\signalcharacter}%
93\to \everyenableelements
94
95\appendtoks
96    \unexpanded\def\dotagtabulatecell
97      {\iftrialtypesetting\else
98         \clf_settagtabulatecell\c_tabl_tabulate_align\c_tabl_tabulate_kind
99       \fi}%
100    \unexpanded\def\dotagtabulatesignal
101      {\dontleavehmode\signalcharacter\ignorespaces}%
102\to \everyenableelements
103
104\appendtoks
105    \unexpanded\def\dotagsynonym
106      {\iftrialtypesetting\else\clf_settagsynonym{\currentsynonymtag}\fi}%
107\to \everyenableelements
108
109\appendtoks
110    \unexpanded\def\dotagsorting
111      {\iftrialtypesetting\else\clf_settagsorting{\currentsortingtag}\fi}%
112\to \everyenableelements
113
114\appendtoks % frozen and assumed global per highlight class
115    \unexpanded\def\dotagconstruct
116      {\iftrialtypesetting\else
117         \clf_settagconstruct
118           {\currentstartstop}%
119           {\startstopparameter\c!style}%
120           \c_attr_color
121           \ifvmode\plusone\else\zerocount\fi
122           \relax
123       \fi}%
124\to \everyenableelements
125
126\appendtoks % frozen and assumed global per highlight class
127    \unexpanded\def\dotaghighlight
128      {\iftrialtypesetting\else
129         \clf_settaghighlight
130           {\currenthighlight}%
131           {\highlightparameter\c!style}
132           \c_attr_color
133           \ifvmode\plusone\else\zerocount\fi
134           \relax
135       \fi}%
136\to \everyenableelements
137
138\appendtoks % we can have differently scaled images
139    \unexpanded\def\dotagfigure
140      {\iftrialtypesetting\else
141         \clf_settagfigure
142           {\figurefileoriginal}%
143           {\figurefullname}%
144           {\figurefilepage}%
145           \dimexpr\figurewidth\relax
146           \dimexpr\figureheight\relax
147           {\figurelabel}%
148       \fi}%
149\to \everyenableelements
150
151\appendtoks
152    \unexpanded\def\dotagcombination
153      {\iftrialtypesetting\else
154         \clf_settagcombination
155            \numexpr\combinationparameter\c!nx\relax
156            \numexpr\combinationparameter\c!ny\relax
157       \fi}%
158\to \everyenableelements
159
160\appendtoks
161    \unexpanded\def\dotagsetparcounter
162      {\global\advance\tagparcounter\plusone\c_attr_taggedpar\tagparcounter}%
163    \unexpanded\def\doresetparcounter
164      {\c_attr_taggedpar\attributeunsetvalue}%
165\to \everyenableelements
166
167\appendtoks
168    \unexpanded\def\dotagsetitemgroup
169      {\iftrialtypesetting\else
170         \clf_settagitemgroup
171           \ifconditional\c_strc_itemgroups_pack true\else false\fi\space
172           \numexpr\currentitemlevel\relax
173           {\currentitemgroupsymbol}%
174       \fi}%
175    \unexpanded\def\dotagsetitem#1%
176      {\iftrialtypesetting\else
177         \clf_settagitem{#1}%
178       \fi}%
179\to \everyenableelements
180
181%     \appendtoks
182%         \unexpanded\def\dotagsetdescription
183%           {\iftrialtypesetting\else
184%              \clf_settagdescription{\currentdescription}\currentdescriptionnumberentry\relax
185%            \fi}%
186%     \to \everyenableelements
187
188\appendtoks
189    \unexpanded\def\dotagsetnotation
190      {\iftrialtypesetting\else
191        \clf_settagnotation{\currentnote}\currentnotenumber\relax
192       \fi}%
193\to \everyenableelements
194
195\appendtoks
196    \unexpanded\def\dotagsetnotesymbol
197      {\iftrialtypesetting\else
198         \clf_settagnotationsymbol{\currentnote}\currentnotenumber\relax
199       \fi}%
200\to \everyenableelements
201
202\appendtoks
203    \unexpanded\def\dotagregisterlocation
204      {\iftrialtypesetting\else
205         \clf_settagregister{\currentregister}\currentregisternumber\relax
206       \fi}%
207\to \everyenableelements
208
209\appendtoks
210    \unexpanded\def\dotaglistlocation
211      {\iftrialtypesetting\else
212         \clf_settaglist\currentlistindex\relax
213       \fi}%
214\to \everyenableelements
215
216\appendtoks
217    \unexpanded\def\dotagsetdelimitedsymbol#1%
218      {\iftrialtypesetting\else
219         \clf_settagdelimitedsymbol{#1}\relax
220       \fi}%
221\to \everyenableelements
222
223\appendtoks
224    \unexpanded\def\dotagsetsubsentencesymbol#1%
225      {\iftrialtypesetting\else
226         \clf_settagsubsentencesymbol{#1}\relax
227       \fi}%
228\to \everyenableelements
229
230\appendtoks
231    \unexpanded\def\dotagregisterfloat#1#2%
232      {\iftrialtypesetting\else
233         \clf_settagfloat{#1}{#2}\relax
234       \fi}%
235\to \everyenableelements
236
237\appendtoks
238    \let\specialfixedspace    \explicitfixedspace
239    \let\specialobeyedspace   \explicitobeyedspace
240    \let\specialstretchedspace\explicitstretchedspace
241    \let\specialcontrolspace  \explicitcontrolspace
242\to \everyenableelements
243
244\appendtoks
245    \unexpanded\def\dotagregisterformula#1%
246      {\iftrialtypesetting\else
247         \clf_settagformulacontent#1\relax
248       \fi}%
249\to \everyenableelements
250
251\appendtoks
252    \unexpanded\def\dotagmarginanchor#1%
253      {\iftrialtypesetting\else\clf_settagmarginanchor#1\relax\fi}%
254    \unexpanded\def\dotagmargintext#1%
255      {\iftrialtypesetting\else\clf_settagmargintext#1\relax\fi}%
256\to \everyenableelements
257
258\appendtoks
259    \unexpanded\def\dotagpublication#1#2%
260      {\iftrialtypesetting\else
261         \clf_settagpublication{#1}{#2}\relax
262       \fi}%
263\to \everyenableelements
264
265\appendtoks
266    \unexpanded\def\dotagparagraph#1%
267      {\iftrialtypesetting\else
268         \clf_settagparagraph{#1}\relax
269       \fi}%
270\to \everyenableelements
271
272% The action: \setupbackend[export=yes] % or filename
273
274% maybe xhtml css settings will move to setupexport
275
276% maybe some day a definer
277
278\installcorenamespace{export}
279
280\installparameterhandler \??export {export}
281\installsetuphandler     \??export {export}
282
283\setupexport
284  [\c!align=\number\raggedstatus,
285   \c!bodyfont=\bodyfontsize,
286   \c!width=\textwidth,
287   \c!title={\directinteractionparameter\c!title},
288   \c!subtitle={\directinteractionparameter\c!subtitle},
289   \c!author={\directinteractionparameter\c!author},
290 % \c!firstpage=, % imagename
291 % \c!lastpage=,  % imagename
292   \c!properties=\v!no, % no: ignore, yes: as attribute, otherwise: use as prefix
293   \c!hyphen=\v!no,
294   \c!svgstyle=,
295   \c!cssfile=,
296   \c!file=]
297
298\resetsystemmode\v!export
299
300\unexpanded\def\doinitializeexport
301  {\edef\p_export{\backendparameter\c!export}%
302   \ifx\p_export\empty \else
303      % yes | xml
304      \setuptagging[\c!state=\v!start]%
305      \clf_initializeexport
306      \setsystemmode\v!export
307      \exportingtrue
308      \writestatus\m!backend\empty % so messages will stand out a bit
309      \the\everyinitializeexport
310      \writestatus\m!backend\empty % so messages will stand out a bit
311      \glet\doinitializeexport\relax
312   \fi}
313
314\unexpanded\def\dostartexport
315  {%\glet\dostartexport\relax
316   \let\currentexport\empty
317   \clf_setupexport
318       align      {\exportparameter\c!align}%
319       bodyfont   \dimexpr\exportparameter\c!bodyfont\relax
320       width      \dimexpr\exportparameter\c!width\relax
321       properties {\exportparameter\c!properties}%
322       hyphen     {\exportparameter\c!hyphen}%
323       title      {\exportparameter\c!title}%
324       subtitle   {\exportparameter\c!subtitle}%
325       author     {\exportparameter\c!author}%
326       firstpage  {\exportparameter\c!firstpage}%
327       lastpage   {\exportparameter\c!lastpage}%
328       svgstyle   {\exportparameter\c!svgstyle}%
329       cssfile    {\exportparameter\c!cssfile}%
330       file       {\exportparameter\c!file}%
331       export     {\backendparameter\c!export}%
332   \relax}
333
334\unexpanded\def\dostopexport
335  {\glet\dostopexport\relax
336   \clf_finishexport}
337
338\appendtoks
339    \doinitializeexport
340\to \everysetupbackend
341
342\appendtoks
343    \ifexporting
344        \dostartexport
345    \fi
346\to \everystarttext
347
348% better (before pdf gets closed, so we can embed), but it needs testing:
349
350\appendtoks
351    \ifexporting
352        \dostopexport
353    \fi
354\to \everystoptext
355
356\appendtoks
357    \ifexporting
358        \dostartexport % in case it is done inside \starttext
359    \fi
360\to \everystartdocument
361
362\protect \endinput
363