summaryrefslogtreecommitdiff
path: root/tex/context/base/xtag-xsl.tex
blob: 3ad096c280a39b587105e445843008d251d9d6dc (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
%D \module
%D   [       file=xtag-xsl,
%D        version=2002.03.07,
%D          title=\CONTEXT\ XML Support,
%D       subtitle=XSLT processing,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

% FOR THIS TO WORK YOU NEED A PROPER cp8bit VECTOR
%
% 8 bit support has been messed up in a couple of distributions
%
% 0x00   0x00
% 0x01   0x01
% 0x02   0x02
% etc

%D This module is rather experimental so users may expect
%D changes in the interface.

\unprotect

\definesystemvariable {xl}

\newif\iftraceXSLTprocessing % \traceXSLTprocessingtrue

\def\XSLTscriptfile{\bufferprefix xlscript}

% to be sped up:

\def\presetXSLTcharacters
  {\let\par\empty
   \def\{{\rawcharacter{`\{}}%  \string^^..
   \def\}{\rawcharacter{`\}}}%  \string^^..
   \def\\{\rawcharacter{`\\}}}% \string^^..

\def\obeyXSLTcharacters
  {\setnaturalcatcodes
   \catcode`\\=\@@escape}

\long\def\startXSLTscript #1 #2 %
  {\bgroup
   \obeyXSLTcharacters
   \doifelse{#2}{capsule}\dostartXSLTscript\nostartXSLTscript#1 #2 }

\long\def\dostartXSLTscript#1 #2 #3 %
  {\obeylines
   \dodostartXSLTscript#1 #2 #3 }

\long\def\dodostartXSLTscript#1 #2 #3 #4\stopXSLTscript
  {\egroup
   \long\setvalue{\??xl#1}{\doXSLTscript{#3}{#4}}}

\long\def\nostartXSLTscript
  {\obeylines
   \nonostartXSLTscript}

\long\def\nonostartXSLTscript#1 #2\stopXSLTscript
  {\egroup
   \long\setvalue{\??xl#1}{\doXSLTscript\empty{#2}}}

\long\def\donormalXSLTscript#1#2%
  {\includeXSLTscript{b-capsule-#1}%
   #2%
   \includeXSLTscript{e-capsule-#1}}

\def\includeXSLTscript#1%
  {\csname\ifcsname\??xl#1\endcsname\??xl#1\else\s!empty\fi\endcsname}

\startXSLTscript b-capsule-1.0
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
\stopXSLTscript

\startXSLTscript e-capsule-1.0
</xsl:stylesheet>
\stopXSLTscript

\def\applyXSLTscript
  {\dotripleempty\doapplyXSLTscript}

\long\def\doapplyXSLTscript[#1][#2][#3]%
  {\ifthirdargument
     \bgroup
     \let\doXSLTscript\donormalXSLTscript
     \presetXSLTcharacters
     \donefalse
     \def\docommand##1{\dodoapplyXSLTscript{##1}{\ifdone#3\else#2\fi}{#3}\donetrue}%
     \processcommalist[#1]\docommand
     \egroup
   \else
     \applyXSLTscript[#1][#2][#2]%
   \fi}

% Since a \XML\ file is loaded into memory as a tree, we
% can safely use the same input and output filename, thereby
% avoiding temporary files.

\def\dodoapplyXSLTscript#1#2#3%
  {\bgroup
   \doifundefined{\??xl#1}%
     {\writestatus{XSLT}{unknown script #1}}
     {\writestatus{XSLT}{convert #2 into #3 using #1}%
      \edef\par{\outputnewlinechar}%
      \edef\!!stringc{\getvalue{\??xl#1}}%
      \immediate\openout\scratchwrite=\XSLTscriptfile.xsl
      \immediate\write\scratchwrite{\!!stringc}%
      \immediate\closeout\scratchwrite
      \iftraceXSLTprocessing\wait\fi
      \executeXSLT{\XSLTscriptfile.xsl}{#2}{#3}%
      \iftraceXSLTprocessing\wait\fi}%
   \egroup}

\def\executeXSLT#1#2#3% script input output
  {\executesystemcommand{xsltproc -o #3 #1 #2}}

\long\def\applyXSLTtobuffer
  {\dodoubleempty\doapplyXSLTtobuffer}

\long\def\doapplyXSLTtobuffer[#1][#2]%
  {\expanded{\applyXSLTscript[#1][\TEXbufferfile{\ifsecondargument#2\else\jobname\fi}]}}

\def\resetXSLTscripting#1%
  {\setgvalue{\??xl\s!dummy}{\doXSLTscript{#1}{}}}

\resetXSLTscripting{1.0}

\def\startXSLTscripting
  {\bgroup
   \obeyXSLTcharacters
   \obeylines
   \dostartXSLTscripting}

\long\def\dostartXSLTscripting#1\stopXSLTscripting
  {\egroup
   \let\doXSLTscript\doextendXSLTscript
   \getvalue{\??xl\s!dummy}{#1}}

\long\def\doextendXSLTscript#1#2#3%
  {\setgvalue{\??xl\s!dummy}{\doXSLTscript{#1}{#2#3}}}

\def\applyXSLTscripting
  {\applyXSLTscript[\s!dummy]}

\protect

\doifnotmode{demo}{\endinput}

% \def\Whow{Whow It Works}
%
% \startXSLTscript test capsule 1.0
% <xsl:output method="text"/>
%
% <xsl:template match="/figurelibrary/figure">
%   \Whow \\ #<xsl:apply-templates/>
% </xsl:template>
%
% <xsl:template match="/">
%   <xsl:apply-templates/>
% </xsl:template>
% \stopXSLTscript
%
% \applyXSLTscript [test] [figtest.xml] [crap.tmp] \typefile{crap.tmp}
%
% \startbuffer
% <hello>world</hello>
% \stopbuffer
%
% \applyXSLTtobuffer[test] \typebuffer
%
% \startXSLTscripting
% <xsl:output method="text"/>
% \stopXSLTscripting
%
% \startXSLTscripting
% <xsl:template match="/figurelibrary/figure">
%   <xsl:apply-templates/>
% </xsl:template>
% \stopXSLTscripting
%
% \startXSLTscripting
% <xsl:template match="/">
%   <xsl:apply-templates/>
% </xsl:template>
% \stopXSLTscripting
%
% \applyXSLTscripting [figtest.xml] [crap.tmp]  \typefile{crap.tmp}

\starttext

\startXSLTscript another
<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:cd="http://www.pragma-ade.com/commands"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="cd:interface">
  <xsl:apply-templates><xsl:sort order="descending" select="@name"/></xsl:apply-templates>
</xsl:template>

<xsl:template match="cd:command">
  name: <xsl:value-of select="@name"/>\\par
</xsl:template>

</xsl:stylesheet>
\stopXSLTscript

\applyXSLTscript [another] [cont-en.xml] [whatever.tmp]

\readfile{whatever.tmp}{}{}

\stoptext