lxml-ini.mkxl /size: 15 Kb    last modification: 2024-01-16 09:02
1%D \module
2%D   [       file=lxml-ini,
3%D        version=2007.08.17,
4%D          title=\CONTEXT\ \XML\ Support,
5%D       subtitle=Initialization,
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%D Todo: auto apply setups (manage at lua end)
15%D Todo: manual: \xmlinclusion \xmlinclusions
16
17\writestatus{loading}{ConTeXt XML Support / Initialization}
18
19%registerctxluafile{lxml-tab}{} % loader
20%registerctxluafile{lxml-lpt}{} % parser
21%registerctxluafile{lxml-xml}{} % xml finalizers
22%registerctxluafile{lxml-aux}{} % extras using parser
23%registerctxluafile{lxml-mis}{} % extras independent of parser
24\registerctxluafile{lxml-ent}{} % entity hacks
25\registerctxluafile{lxml-tex}{} % tex finalizers
26\registerctxluafile{lxml-dir}{} % ctx hacks
27\registerctxluafile{lxml-ini}{} % interface
28
29\unprotect
30
31% todo: { } mandate so that we can alias
32
33% undocumented:
34
35\permanent\def\ctxlxml#1{\ctxlua{lxml.#1}}
36
37% for now indirect .. this will change when we have updated luatex (scan_argument)
38
39\permanent\def\xmlconcat          #1#2#3{\clf_xmlconcat     {#1}{#2}{\detokenize{#3}}}
40\permanent\def\xmlconcatrange #1#2#3#4#5{\clf_xmlconcatrange{#1}{#2}{#3}{#4}{\detokenize{#5}}}
41
42%D Maybe I should add \type {\protected} here:
43
44\permanent\def\xmlloadfile  #1#2{\clf_xmlloadfile  {#1}{#2}{\directxmlparameter\c!compress}}
45\permanent\def\xmlloadbuffer#1#2{\clf_xmlloadbuffer{#1}{#2}{\directxmlparameter\c!compress}}
46\permanent\def\xmlloaddata  #1#2{\clf_xmlloaddata  {#1}{#2}{\directxmlparameter\c!compress}}
47
48\aliased\let\xmlload\xmlloadfile
49
50%D These are defined at the \LUA\ end:
51
52% \xmlall \xmlatt \xmlattdef \xmlattribute \xmlattributedef \xmlbadinclusions
53% \xmlchainatt \xmlchainattdef \xmlchecknamespace \xmlcommand \xmlcontext \xmlcount
54% \xmldelete \xmldirect \xmldirectives \xmldirectivesafter \xmldirectivesbefore
55% \xmldisplayverbatim \xmlelement \xmlfilter \xmlfilterlist \xmlfirst \xmlflush
56% \xmlflushcontext \xmlflushlinewise \xmlflushpure \xmlflushspacewise \xmlflushtext
57% \xmlfunction \xmlinclude \xmlincludeoptions \xmlinclusion \xmlinclusionbase
58% \xmlinclusions \xmlindex \xmlinlineverbatim \xmllast \xmllastatt \xmllastmatch
59% \xmllastpar \xmlloaddirectives \xmlmain \xmlmatch \xmlname \xmlnamespace
60% \xmlnonspace \xmlpar \xmlparam \xmlpath \xmlpopmatch \xmlpos \xmlpure
61% \xmlpushmatch \xmlraw \xmlrefatt \xmlregisterns \xmlremapname \xmlremapnamespace
62% \xmlsave \xmlsetatt \xmlsetattribute \xmlsetpar \xmlsetparam \xmlsetsetup
63% \xmlsnippet \xmlstrip \xmlstripanywhere \xmlstripnolines \xmlstripped
64% \xmlstrippednolines \xmltag \xmltext \xmltobuffer \xmltobuffertextonly
65% \xmltobufferverbose \xmltofile \xmltoparameters \xmlverbatim
66
67\aliased\let\xmlposition\xmlindex
68
69\permanent\protected\def\xmlinfo#1{\hbox{\ttxx[\xmlname{#1}]}}
70\permanent\protected\def\xmlshow#1{\startpacked\ttx\xmlverbatim{#1}\stoppacked}
71
72% the next one is handy for mode runs because it enforces a consistent
73% #1 indexing (needed when using \xmltext{main:123}{...} like calls
74
75%let\xmladdindex               \clf_xmladdindex
76
77% we need to pass the last argument as function, so
78
79\permanent\protected\def\xmlsetfunction#1#2#3{\ctxcommand{xmlsetfunction("#1",\!!bs#2\!!es,#3)}}
80
81% goodie:
82
83\immutable\def\xmltempbuffername{xml-temp}
84
85\permanent\protected\def\prettyprintbuffer#1#2% only used here
86  {\ifdefined\scitebuffer % we could predefine to relax
87     \scitebuffer[#2][#1]%
88   \else
89     \typebuffer[#1][\c!option=#2]%
90   \fi}
91
92\permanent\protected\def\xmlprettyprint#1#2%
93  {\xmltobufferverbose{#1}{.}{\xmltempbuffername}%
94   \prettyprintbuffer\xmltempbuffername{#2}}
95
96\permanent\protected\def\xmlprettyprinttext#1#2%
97  {\xmltobuffertextonly{#1}{.}{\xmltempbuffername}%
98   \prettyprintbuffer\xmltempbuffername{#2}}
99
100\permanent\protected\def\inlineprettyprintbuffer#1#2% only used here
101  {\ifdefined\sciteinlinebuffer
102     \sciteinlinebuffer[#2][#1]%
103   \else
104     \typeinlinebuffer[#1][\c!option=#2]%
105   \fi}
106
107\permanent\protected\def\xmlinlineprettyprint#1#2%
108  {\xmltobufferverbose{#1}{.}{\xmltempbuffername}%
109   \inlineprettyprintbuffer\xmltempbuffername{#2}}
110
111\permanent\protected\def\xmlinlineprettyprinttext#1#2%
112  {\xmltobuffertextonly{#1}{.}{\xmltempbuffername}%
113   \inlineprettyprintbuffer\xmltempbuffername{#2}}
114
115% kind of special:
116
117\aliased\let\xmlstartraw\clf_xmlstartraw
118\aliased\let\xmlstopraw \clf_xmlstopraw
119
120\aliased\let\startxmlraw\clf_xmlstartraw
121\aliased\let\stopxmlraw \clf_xmlstopraw
122
123% These are expandable and defined at the \LUA\ end:
124
125% \xmldoif \xmldoifnot \xmldoifelse \xmldoiftext \xmldoifnottext \xmldoifelsetext
126% \xmldoifatt \xmldoifnotatt \xmldoifelseatt \xmldoifempty \xmldoifnotempty
127% \xmldoifelseempty \xmldoifselfempty \xmldoifnotselfempty \xmldoifelseselfempty
128
129\aliased\let\xmldoiftextelse      \xmldoifelsetext
130\aliased\let\xmldoifemptyelse     \xmldoifelseempty
131\aliased\let\xmldoifselfemptyelse \xmldoifelseselfempty
132
133% \startxmlsetups xml:include
134%     \xmlinclude{main}{include}{filename|href}
135% \stopxmlsetups
136%
137% \xmlprependsetup{xml:include}
138
139% \let\xmlgrab\xmlsetsetup % obsolete
140% \let\xmlself\s!unknown   % obsolete
141
142\aliased\let\xmlsetup\setupwithargumentswapped
143\aliased\let\xmls    \setupwithargumentswapped % hardly any faster
144\aliased\let\xmlw    \setupwithargument        % hardly any faster
145
146% todo: 1:xml:whatever always before 3:xml:something
147
148\permanent\protected\def\xmlprependsetup            #1{\clf_xmlprependsetup{*}{#1}}
149\permanent\protected\def\xmlappendsetup             #1{\clf_xmlappendsetup {*}{#1}}
150\permanent\protected\def\xmlbeforesetup           #1#2{\clf_xmlbeforesetup {*}{#1}{#2}}
151\permanent\protected\def\xmlaftersetup            #1#2{\clf_xmlaftersetup  {*}{#1}{#2}}
152\permanent\protected\def\xmlremovesetup             #1{\clf_xmlremovesetup {*}{#1}}
153\permanent\protected\def\xmlresetsetups               {\clf_xmlresetsetups {*}}
154
155\permanent\protected\def\xmlprependdocumentsetup  #1#2{\clf_xmlprependsetup{#1}{#2}}
156\permanent\protected\def\xmlappenddocumentsetup   #1#2{\clf_xmlappendsetup {#1}{#2}}
157\permanent\protected\def\xmlbeforedocumentsetup #1#2#3{\clf_xmlbeforesetup {#1}{#2}{#3}}
158\permanent\protected\def\xmlafterdocumentsetup  #1#2#3{\clf_xmlaftersetup  {#1}{#2}{#3}}
159\permanent\protected\def\xmlremovedocumentsetup   #1#2{\clf_xmlremovesetup {#1}{#2}}
160\permanent\protected\def\xmlresetdocumentsetups     #1{\clf_xmlresetsetups {#1}}
161
162\permanent\protected\def\xmlflushsetups             #1{\clf_xmlflushsetups {#1}{*}{}}   % #1 == id where to apply *
163\permanent\protected\def\xmlflushdocumentsetups   #1#2{\clf_xmlflushsetups {#1}{*}{#2}} % #1 == id where to apply * and #2
164
165\aliased\let\xmlregistersetup        \xmlappendsetup
166\aliased\let\xmlregisterdocumentsetup\xmlappenddocumentsetup
167
168\mutable\def\xmldocument{main}
169
170\permanent\protected\def\xmlregisteredsetups
171  {\xmlstarttiming
172   \xmlflushsetups\xmldocument
173   \xmldefaulttotext\xmldocument % after include
174   \xmlstoptiming}
175
176\permanent\protected\def\xmlregistereddocumentsetups#1#2% id setups
177  {\xmlstarttiming
178   % todo: test for duplicates !
179   \xmlflushdocumentsetups{#1}{#2}%
180   \xmldefaulttotext{#1}% after include
181   \xmlstoptiming}
182
183\permanent\protected\def\xmlstarttiming{\clf_xmlstarttiming} % undocumented
184\permanent\protected\def\xmlstoptiming {\clf_xmlstoptiming}  % undocumented
185
186\def\lxml_process#1#2#3#4#5% flag \loader id name what initializersetup
187  {\begingroup
188   \edef\xmldocument{#3}% #2 can be \xmldocument and set as such
189   %xmlpushdocument{#3}%
190   #2{#3}{#4}%
191   \setcatcodetable\notcatcodes
192   \xmlsetup{#3}{\ifempty{#5}xml:process\else#5\fi}%
193   %xmlpopdocument
194   \endgroup}
195
196% This still doesn't solve a problem with weird grouping (start inside xml and
197% end outside, so one has to do proper grouping inside xml mode).
198%
199% \def\lxml_process#1#2#3#4#5% flag \loader id name what initializersetup
200%   {%\begingroup
201%    \pushmacro\xmldocument
202%    \edef\xmldocument{#3}% #2 can be \xmldocument and set as such
203%    #2{#3}{#4}%
204%    \pushcatcodetable
205%    \setcatcodetable\notcatcodes
206%    \xmlsetup{#3}{\ifempty{#5}xml:process\else#5\fi}%
207%    \popcatcodetable
208%    \popmacro\xmldocument
209%    }%\endgroup}
210
211\permanent\protected\def\xmlprocessfile  {\lxml_process\plusone  \xmlload}
212\permanent\protected\def\xmlprocessdata  {\lxml_process\zerocount\xmlloaddata}
213\permanent\protected\def\xmlprocessbuffer{\lxml_process\zerocount\xmlloadbuffer}
214\aliased            \let\xmlprocess       \xmlprocessfile
215
216\startxmlsetups xml:flush
217    \xmlflush{#1}
218\stopxmlsetups
219
220\startxmlsetups xml:process
221    \xmlregistereddocumentsetups{#1}{#1}
222    \xmlmain{#1}
223\stopxmlsetups
224
225\permanent\protected\def\xmlloadonly#1#2#3%
226  {\xmlload{#1}{#2}%
227   \xmlregistereddocumentsetups{#1}{#3}}
228
229% replaced by concat
230%
231% \protected\def\xmlconnect#1#2#3% inefficient
232%   {\scratchcounter\xmlcount{#1}{#2}\relax
233%    \ifcase\scratchcounter \or
234%      \xmlall{#1}{#2}%
235%    \else
236%      \dorecurse \scratchcounter
237%        {\ifnum\recurselevel>\plusone#3\fi
238%         \xmlidx{#1}{#2}\recurselevel}%
239%    \fi}
240
241\permanent\protected\def\xmlcdataobeyedline {\obeyedline}
242\permanent\protected\def\xmlcdataobeyedspace{\strut\obeyedspace}
243\permanent\protected\def\xmlcdatabefore     {\begingroup\tt}
244\permanent\protected\def\xmlcdataafter      {\endgroup}
245
246% verbatim (dodo:pre/post whitespace, maybe splot verbatim and
247% cdata commands), experimental:
248%
249% \xmlsetfunction{main}{verbatim}{lxml.displayverbatim}
250% \xmlsetfunction{main}{verb}    {lxml.inlineverbatim}
251
252% we use an xml: namespace so one has to define a suitable verbatim, say
253%
254% \definetyping[xml:verbatim][typing]
255%
256% this is experimental!
257
258\permanent\tolerant\protected\def\startxmldisplayverbatim[#1]%
259  {\startpacked
260   \cdef\currenttyping{#1}%
261   \ifempty\currenttyping
262     \let\currenttyping\v!typing
263   \else % maybe test for existence
264     \cdef\currenttyping{xml:\currenttyping}%
265   \fi
266   \enforced\protected\def\stopxmldisplayverbatim
267     {\endofverbatimlines
268      \stoppacked}%
269   \doinitializeverbatim
270   \beginofverbatimlines}
271
272\permanent\protected\lettonothing\stopxmldisplayverbatim
273
274\permanent\tolerant\protected\def\startxmlinlineverbatim[#1]%
275  {\begingroup
276   \cdef\currenttype{#1}%
277   \ifempty\currenttype
278     \let\currenttype\v!type
279   \else % maybe test for existence
280     \cdef\currenttype{xml:\currenttype}%
281   \fi
282   \enforced\let\stopxmlinlineverbatim\endgroup
283   \doinitializeverbatim}
284
285\permanent\protected\lettonothing\stopxmlinlineverbatim
286
287% processing instructions
288
289\permanent\protected\def\xmlinstalldirective#1#2%
290  {\clf_xmlinstalldirective{#1}{\csstring#2}}
291
292% an example:
293
294% <?context-tex-directive bgroup ?>
295
296\appendtoks
297    \xmlinstalldirective{tex}{xmltexcommand}%
298\to \everyjob
299
300\permanent\def\xmltexcommand#1{\begincsname#1\endcsname}
301
302% \def\xmlcontextdirective#1% kind class key value
303%   {\executeifdefined{xml#1directive}\gobblethreearguments}
304
305% setting up xml:
306%
307% \setupxml[\c!default=]        % mkiv only == text
308% \setupxml[\c!default=\v!none] % mkiv only, undefined -> hidden
309% \setupxml[\c!default=\v!text] % mkiv only, undefined -> text
310
311% \def\xmlctxdirective#1#2#3{\doif{#1}{clue}{\doif{#2}{page}}{\page[#3]}}
312
313\newconstant\xmlprocessingmode % 0=unset, 1=text, 2=hidden
314
315\installcorenamespace{xml}
316\installcorenamespace{xmldefaults}
317\installcorenamespace{xmlmapvalue}
318
319\installdirectcommandhandler \??xml {xml}
320
321\letcsname\??xmldefaults\v!normal\endcsname\zerocount
322\letcsname\??xmldefaults\v!none  \endcsname\zerocount
323\letcsname\??xmldefaults\v!text  \endcsname\plusone
324\letcsname\??xmldefaults\v!hidden\endcsname\plustwo
325
326\permanent\protected\def\xmldefaulttotext
327  {\ifcase\xmlprocessingmode
328     \expandafter\gobbleoneargument       % 0 (none)
329   \or
330     \expandafter\clf_xmlsetcommandtotext % 1 (normal)
331   \or
332     \expandafter\clf_xmlsetcommandtonone % 2 (hidden)
333   \else
334     \expandafter\gobbleoneargument       %   (none)
335   \fi}
336
337\appendtoks
338    \xmlprocessingmode\executeifdefined{\??xmldefaults\directxmlparameter\c!default}\plusone
339\to \everysetupxml
340
341\setupxml
342  [\c!default=,          % flush all
343   \c!compress=\v!no,    % strip comment
344   \c!entities=\v!no]    % load big entity file
345
346\appendtoks
347    \ifcstok{\directxmlparameter\c!entities}\v!yes
348        \clf_xmlloadentities
349    \fi
350\to \everysetupxml
351
352\permanent\def\xmlmapvalue    #1#2#3{\defcsname\??xmlmapvalue#1:#2\endcsname{#3}} % keep #3 to grab spaces
353\permanent\def\xmldoifelsevalue #1#2{\ifcsname\??xmlmapvalue#1:#2\endcsname\expandafter\firstoftwoarguments\else\expandafter\secondoftwoarguments\fi}
354
355\permanent\def\xmlvalue#1#2% #3
356  {\ifcsname\??xmlmapvalue#1:#2\endcsname
357     \expandafter\expandafter\expandafter\lastnamedcs\expandafter\gobbleoneargument
358   \else
359     \expandafter\firstofoneargument
360   \fi}
361
362\permanent\def\xmlmappedvalue#1#2#3%
363  {\ifcsname\??xmlmapvalue#1:#2\endcsname
364     \expandafter\lastnamedcs
365   \else
366     \csname\??xmlmapvalue#1:#3\expandafter\endcsname
367   \fi}
368
369\aliased\let\xmldoifvalueelse\xmldoifelsevalue
370
371\aliased\let\xmlmapval\xmlmapvalue
372\aliased\let\xmlval   \xmlvalue
373
374%D Experimental:
375
376\permanent\def\xmlgetindex    #1{\clf_xmlgetindex {\xmldocument}{#1}}
377\permanent\def\xmlwithindex #1#2{\clf_xmlwithindex{\xmldocument}{#1}{#2}}
378\permanent\def\xmlreference #1#2{\string\xmlwithindex{#1}{#2}}
379
380%D Entities:
381%D
382%D \starttyping
383%D \xmlsetentity{tex}{\TEX{}} % {} needed
384%D \stoptyping
385
386\permanent\protected\def\xmlsetentity#1#2{\clf_xmlsetentity{#1}{\detokenize{#2}}}
387\permanent\protected\def\xmltexentity#1#2{\clf_xmltexentity{#1}{\detokenize{#2}}}
388
389%D The following might change (or even disappear) so we keep it undocumented.
390
391\setnewconstant\xmlautoentities\plusone % 0=off, 1=upper, 2=upper,lower
392
393\permanent\protected\def\xmle
394  {\ifcase\xmlautoentities
395     \expandafter\lxml_e_none
396   \or
397     \expandafter\lxml_e_upper
398   \or
399     \expandafter\lxml_e_upperlower
400   \else
401     \expandafter\lxml_e_none
402   \fi}
403
404\def\lxml_e_none#1#2% safe
405  {#1}
406
407\def\lxml_e_upper#1#2% can be abbreviation
408  {\ifcsname#2\endcsname
409     \csname#2\expandafter\endcsname
410   \else
411     #1%
412   \fi}
413
414\def\lxml_e_upperlower#1#2% can be anything, so unsafe
415  {\ifcsname#2\endcsname
416     \csname#2\expandafter\endcsname
417   \orelse\ifcsname#1\endcsname
418     \csname#1\expandafter\endcsname
419   \else
420     #1%
421   \fi}
422
423%D We keep these around as there are also MP, LUA and TEX variants but they are not
424%D the same as in \MKII.
425
426\permanent\tolerant\protected\def\processXMLbuffer[#1]{\xmlprocessbuffer{temp}{#1}{}}
427\permanent         \protected\def\processXMLfile    #1{\xmlprocessfile  {temp}{#1}{}}
428\permanent         \protected\def\XMLdata           #1{\xmlprocessdata  {temp}{#1}{}}
429
430\aliased\let\processxmlbuffer\processXMLbuffer
431\aliased\let\processxmlfile  \processXMLfile
432\aliased\let\xmldata         \XMLdata
433
434\permanent\protected\def\xmlsetinjectors  [#1]{\clf_xmlsetinjectors{#1}}
435\permanent\protected\def\xmlresetinjectors    {\clf_xmlresetinjectors{}}
436
437% \def\xmlinjector#1{\executeifdefined{#1}\donothing}
438
439\permanent\def\xmlinjector#1{\fastsetup{xml:directive:injector:#1}}
440
441\startsetups xml:directive:injector:page
442    \page
443\stopsetups
444
445\startsetups xml:directive:injector:column
446    \column
447\stopsetups
448
449\startsetups xml:directive:injector:blank
450    \blank
451\stopsetups
452
453\startsetups xml:directive:injector:noline
454    \vskip-\lineheight
455\stopsetups
456
457\aliased\let\xmlapplyselectors\clf_xmlapplyselectors
458
459% In the\MKIV\ file there is some commented code wrt handing entities with active
460% characters ... very old and obsolete.
461
462\protect \endinput
463
464