back-exp.mkxl /size: 13 Kb    last modification: 2024-01-16 09:02
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% see t:/export/todo-fonts.tex for (future) experiment
15
16\writestatus{loading}{ConTeXt Backend Macros / XML export}
17
18\newinteger\tagparcounter
19
20\registerctxluafile{back-exp}        {autosuffix}
21\registerctxluafile{back-exp-imp-tag}{autosuffix}
22\registerctxluafile{back-exp-imp-mth}{autosuffix}
23\registerctxluafile{back-exp-imp-ref}{autosuffix}
24
25%D This is an experimental exporter and a logical follow up on tagging. The
26%D exporter assumes a properly tagged document. Some elements get a couple
27%D of attributes because otherwise rendering information would get lost. In
28%D general we assume that when the \XML\ is converted to \HTML\ some stylesheet
29%D is applied anyway.
30
31\unprotect
32
33% we can replace this by a more generic attributeset mechanism where we bind
34% to any element (needed anyway, see userdata thingies)
35
36\definesystemattribute[taggedpar][public]
37
38\permanent\tolerant\protected\def\setelementexporttag[#1]#*[#2]#*[#3]%
39  {\ifarguments\or\or
40     \clf_settagproperty{#1}{export}{#2}%
41   \or
42     \clf_settagproperty{#1}{#2}{#3}%
43   \fi}
44
45% todo: no need for calls when trialtypesetting
46
47\permanent\def\taggedctxcommand
48  {\iftrialtypesetting
49     \expandafter\gobbleoneargument
50   \else
51     \expandafter\ctxcommand
52   \fi}
53
54\permanent\protected\lettonothing\dotagsetparcounter
55\permanent\protected\lettonothing\doresetparcounter
56
57\appendtoks
58    \doresetparcounter
59\to \everyflushatnextpar
60
61% \appendtoks
62%     \dotagsetparcounter
63% \to \everypar
64
65\prependtoks
66    \dotagsetparcounter
67\to \everypar
68
69% \appendtoks
70%     \dotagsetparcounter
71% \to \neverypar
72
73\prependtoks
74    \dotagsetparcounter
75\to \neverypar
76
77\appendtoks
78    \dotagsetparcounter
79\to \everytabulatepar % tricky, maybe this should be neverypar
80
81\appendtoks
82    \enforced\permanent\protected\def\dotagTABLEcell
83      {\iftrialtypesetting\else
84         \clf_settagtablecell
85           \numexpr\tablecellrows\relax
86           \numexpr\tablecellcolumns\relax
87           \numexpr\raggedstatus\relax
88       \fi}%
89    % brrr, we need to tag empty cells (unless we start numbering)
90    \enforced\permanent\protected\def\dotagTABLEsignal
91      {\signalcharacter}%
92\to \everyenableelements
93
94\appendtoks
95    \enforced\permanent\protected\def\dotagtabulatecell
96      {\iftrialtypesetting\else
97         \clf_settagtabulatecell\c_tabl_tabulate_align\c_tabl_tabulate_kind
98       \fi}%
99    \enforced\permanent\protected\def\dotagtabulatesignal
100      {\dontleavehmode\signalcharacter\ignorespaces\ignorepars}%
101\to \everyenableelements
102
103\appendtoks
104    \enforced\permanent\protected\def\dotagsynonym
105      {\iftrialtypesetting\else\clf_settagsynonym{\currentsynonymtag}\fi}%
106\to \everyenableelements
107
108\appendtoks
109    \enforced\permanent\protected\def\dotagsorting
110      {\iftrialtypesetting\else\clf_settagsorting{\currentsortingtag}\fi}%
111\to \everyenableelements
112
113\appendtoks % frozen and assumed global per highlight class
114    \enforced\permanent\protected\def\dotagconstruct
115      {\iftrialtypesetting\else
116         \clf_settagconstruct
117           {\currentstartstop}%
118           {\startstopparameter\c!style}%
119           \c_attr_color
120           \ifvmode\plusone\else\zerocount\fi
121           \relax
122       \fi}%
123\to \everyenableelements
124
125\appendtoks % frozen and assumed global per highlight class
126    \enforced\permanent\protected\def\dotaghighlight
127      {\iftrialtypesetting\else
128         \clf_settaghighlight
129           {\currenthighlight}%
130           {\highlightparameter\c!style}
131           \c_attr_color
132           \ifvmode\plusone\else\zerocount\fi
133           \relax
134       \fi}%
135\to \everyenableelements
136
137\appendtoks % we can have differently scaled images
138    \enforced\permanent\protected\def\dotagfigure
139      {\iftrialtypesetting\else
140         \clf_settagfigure
141           {\figurefileoriginal}%
142           {\figurefullname}%
143           {\figurefilepage}%
144           \dimexpr\figurewidth\relax
145           \dimexpr\figureheight\relax
146           {\figurelabel}%
147       \fi}%
148\to \everyenableelements
149
150\appendtoks
151    \enforced\permanent\protected\def\dotagcombination
152      {\iftrialtypesetting\else
153         \clf_settagcombination
154            \numexpr\combinationparameter\c!nx\relax
155            \numexpr\combinationparameter\c!ny\relax
156       \fi}%
157\to \everyenableelements
158
159\appendtoks
160    \enforced\permanent\protected\def\dotagsetparcounter
161      {\global\advanceby\tagparcounter\plusone\c_attr_taggedpar\tagparcounter}%
162    \enforced\permanent\protected\def\doresetparcounter
163      {\c_attr_taggedpar\attributeunsetvalue}%
164\to \everyenableelements
165
166\appendtoks
167    \enforced\permanent\protected\def\dotagsetitemgroup
168      {\iftrialtypesetting\else
169         \clf_settagitemgroup
170           \ifconditional\c_strc_itemgroups_pack true\else false\fi\space
171           \numexpr\currentitemlevel\relax
172           {\currentitemgroupsymbol}%
173       \fi}%
174    \enforced\permanent\protected\def\dotagsetitem#1%
175      {\iftrialtypesetting\else
176         \clf_settagitem{#1}%
177       \fi}%
178\to \everyenableelements
179
180%     \appendtoks
181%         \enforced\permanent\protected\def\dotagsetdescription
182%           {\iftrialtypesetting\else
183%              \clf_settagdescription{\currentdescription}\currentdescriptionnumberentry\relax
184%            \fi}%
185%     \to \everyenableelements
186
187\appendtoks
188    \enforced\permanent\protected\def\dotagsetnotation
189      {\iftrialtypesetting\else
190        \clf_settagnotation{\currentnote}\currentnotenumber\relax
191       \fi}%
192\to \everyenableelements
193
194\appendtoks
195    \enforced\permanent\protected\def\dotagsetnotesymbol
196      {\iftrialtypesetting\else
197         \clf_settagnotationsymbol{\currentnote}\currentnotenumber\relax
198       \fi}%
199\to \everyenableelements
200
201\appendtoks
202    \enforced\permanent\protected\def\dotagregisterlocation
203      {\iftrialtypesetting\else
204         \clf_settagregister{\currentregister}\currentregisternumber\relax
205       \fi}%
206\to \everyenableelements
207
208\appendtoks
209    \enforced\permanent\protected\def\dotaglistlocation
210      {\iftrialtypesetting\else
211         \clf_settaglist\currentlistindex\relax
212       \fi}%
213\to \everyenableelements
214
215\appendtoks
216    \enforced\permanent\protected\def\dotagsetdelimitedsymbol#1%
217      {\iftrialtypesetting\else
218         \clf_settagdelimitedsymbol{#1}\relax
219       \fi}%
220\to \everyenableelements
221
222\appendtoks
223    \enforced\permanent\protected\def\dotagsetsubsentencesymbol#1%
224      {\iftrialtypesetting\else
225         \clf_settagsubsentencesymbol{#1}\relax
226       \fi}%
227\to \everyenableelements
228
229\appendtoks
230    \enforced\permanent\protected\def\dotagregisterfloat#1#2%
231      {\iftrialtypesetting\else
232         \clf_settagfloat{#1}{#2}\relax
233       \fi}%
234\to \everyenableelements
235
236\appendtoks
237    \enforced\let\specialfixedspace    \explicitfixedspace
238    \enforced\let\specialobeyedspace   \explicitobeyedspace
239    \enforced\let\specialstretchedspace\explicitstretchedspace
240    \enforced\let\specialcontrolspace  \explicitcontrolspace
241\to \everyenableelements
242
243\appendtoks
244    \enforced\permanent\protected\def\dotagregisterformula#1%
245      {\iftrialtypesetting\else
246         \clf_settagformulacontent#1\relax
247       \fi}%
248\to \everyenableelements
249
250\appendtoks
251    \enforced\permanent\protected\def\dotagmarginanchor#1%
252      {\iftrialtypesetting\else\clf_settagmarginanchor#1\relax\fi}%
253    \enforced\permanent\protected\def\dotagmargintext#1%
254      {\iftrialtypesetting\else\clf_settagmargintext#1\relax\fi}%
255\to \everyenableelements
256
257\appendtoks
258    \enforced\permanent\protected\def\dotagpublication#1#2%
259      {\iftrialtypesetting\else
260         \clf_settagpublication{#1}{#2}\relax
261       \fi}%
262\to \everyenableelements
263
264\appendtoks
265    \enforced\permanent\protected\def\dotagparagraph#1%
266      {\iftrialtypesetting\else
267         \clf_settagparagraph{#1}\relax
268       \fi}%
269\to \everyenableelements
270
271% For more elements, so an extra argument:
272
273\ifdefined\dotagcodepoint \else \aliased\let\dotagcodepoint\gobbletwoarguments \fi
274
275\appendtoks
276    \enforced\permanent\protected\def\dotagcodepoint#1#2%
277      {\iftrialtypesetting\else
278         \clf_settagcodepoint{#1}#2\relax
279       \fi}%
280\to \everyenableelements
281
282% The action: \setupbackend[export=yes] % or filename
283
284% maybe xhtml css settings will move to setupexport
285
286% maybe some day a definer
287
288\installcorenamespace{export}
289
290\installparameterhandler \??export {export}
291\installsetuphandler     \??export {export}
292
293\setupexport
294  [\c!align=\number\raggedstatus,
295   \c!bodyfont=\bodyfontsize,
296   \c!width=\textwidth,
297   \c!title={\directinteractionparameter\c!title},
298   \c!subtitle={\directinteractionparameter\c!subtitle},
299   \c!author={\directinteractionparameter\c!author},
300 % \c!firstpage=, % imagename
301 % \c!lastpage=,  % imagename
302   \c!properties=\v!no, % no: ignore, yes: as attribute, otherwise: use as prefix
303   \c!hyphen=\v!no,
304   \c!svgstyle=,
305   \c!cssfile=,
306   \c!file=]
307
308\resetsystemmode\v!export
309
310\permanent\protected\def\doinitializeexport
311  {\ifempty{\backendparameter\c!export}\else
312      % yes | xml
313      \setuptagging[\c!state=\v!start]%
314      \clf_initializeexport
315      \setsystemmode\v!export
316      \exportingtrue
317      \writestatus\m!backend\empty % so messages will stand out a bit
318      \expand\everyinitializeexport
319      \writestatus\m!backend\empty % so messages will stand out a bit
320      \enforced\glet\doinitializeexport\relax
321   \fi}
322
323\permanent\protected\def\dostartexport
324  {%\glet\dostartexport\relax
325   \lettonothing\currentexport
326   \clf_setupexport
327       align      {\exportparameter\c!align}%
328       bodyfont   \dimexpr\exportparameter\c!bodyfont\relax
329       width      \dimexpr\exportparameter\c!width\relax
330       properties {\exportparameter\c!properties}%
331       hyphen     {\exportparameter\c!hyphen}%
332       title      {\exportparameter\c!title}%
333       subtitle   {\exportparameter\c!subtitle}%
334       author     {\exportparameter\c!author}%
335       firstpage  {\exportparameter\c!firstpage}%
336       lastpage   {\exportparameter\c!lastpage}%
337       svgstyle   {\exportparameter\c!svgstyle}%
338       cssfile    {\exportparameter\c!cssfile}%
339       file       {\exportparameter\c!file}%
340       option     {\exportparameter\c!option}%
341       export     {\backendparameter\c!export}%
342   \relax}
343
344\permanent\protected\def\dostopexport
345  {\enforced\glet\dostopexport\relax
346   \clf_finishexport}
347
348\appendtoks
349    \doinitializeexport
350\to \everysetupbackend
351
352\appendtoks
353    \ifexporting
354        \dostartexport
355    \fi
356\to \everystarttext
357
358% better (before pdf gets closed, so we can embed), but it needs testing:
359
360\appendtoks
361    \ifexporting
362        \dostopexport
363    \fi
364\to \everystoptext
365
366\appendtoks
367    \ifexporting
368        \dostartexport % in case it is done inside \starttext
369    \fi
370\to \everystartdocument
371
372%D This can replace the older (rather unknown) attachment feature because it
373%D is more immediate (same run). I can also use this then for manuals.
374%D
375%D \setuptagging[state=start] % mandate here
376%D
377%D \starttyping
378%D \startmathexport % [buffer=yes]
379%D     test $ x = 1 + a $ test
380%D \stopmathexport
381%D
382%D \typebuffer
383%D
384%D \startmathexport[buffer=math]
385%D     test $ x = 1 + a^2 $ test
386%D     test $ x = 1 + a $ test
387%D     test $ x = z + \sqrt{a} $ test
388%D \stopmathexport
389%D
390%D \typebuffer[math]
391%D
392%D \startmathexport[filename=foo.xml]
393%D     test $ x = z + \sqrt{a} $ test
394%D \stopmathexport
395%D
396%D \dorecurse{\collectedmath} {\page \collectedmath #1 \page}
397%D \stoptyping
398%D
399%D This one also does buffers and files:
400%D
401%D \starttyping
402%D \startboxexport
403%D     \startparagraph
404%D         \input tufte
405%D     \stopparagraph
406%D     \startitemize
407%D         \startitem item one \stopitem
408%D         \startitem item two \stopitem
409%D     \stopitemize
410%D     \startparagraph
411%D         \input tufte
412%D     \stopparagraph
413%D \stopboxexport
414%D
415%D \typebuffer
416%D \stoptyping
417
418\installcorenamespace {boxexport}
419\installcorenamespace {mathexport}
420
421\installsetuponlycommandhandler \??boxexport  {boxexport}
422\installsetuponlycommandhandler \??mathexport {mathexport}
423
424% \permanent\tolerant\protected\def\startboxexport[#1]%
425%   {\dowithnextbox{\strc_export_box{#1}}\vbox\bgroup}
426%
427% \permanent\protected\def\stopboxexport
428%   {\egroup}
429%
430% \def\strc_export_box#1%
431%   {\ctxlua{buffers.assign("#1",structures.tags.exportbox(\number\nextbox))}}
432
433\permanent\tolerant\protected\def\startboxexport[#S#1]%
434  {\begingroup
435   \ifempty{#1}%
436      \letboxexportparameter\c!buffer\v!yes
437   \orelse\ifhastok={#1}\relax
438      \setupmathexport[#1]%
439   \else
440      \setboxexportparameter\c!buffer{#1}%
441   \fi
442   \setbox\nextbox\vbox\bgroup}
443
444\permanent\protected\def\stopboxexport
445  {\egroup
446   \clf_exportbox \nextbox
447     {\boxexportparameter\c!file}%
448     {\boxexportparameter\c!buffer}%
449   \endgroup}
450
451\permanent\tolerant\protected\def\startmathexport[#S#1]%
452  {\begingroup
453   \ifempty{#1}%
454      \letboxexportparameter\c!buffer\v!yes
455   \orelse\ifhastok={#1}\relax
456      \setupmathexport[#1]%
457   \else
458      \setboxexportparameter\c!buffer{#1}%
459   \fi
460   \clf_startcollectingmath}
461
462\permanent\protected\def\stopmathexport
463  {\clf_stopcollectingmath
464   \clf_processcollectedmath
465     {\boxexportparameter\c!file}
466     {\boxexportparameter\c!buffer}%
467   \endgroup}
468
469% Defined at the \LUA\ end:
470%
471% \the\collectedmath : number of snippets
472% \collectedmath n   : specific snippet
473
474\protect \endinput
475