summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/lxml-sor.mkiv
blob: 29882861111134f830379c4991c15791aaa03ed2 (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
%D \module
%D   [       file=lxml-sor,
%D        version=2009.08.24,
%D          title=\CONTEXT\ \XML\ Support,
%D       subtitle=Sorting,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D THIS IS VERY EXPERIMENTAL!

\writestatus{loading}{ConTeXt XML Support / Sorting}

\registerctxluafile{lxml-sor}{}

\unprotect

% the flusher is unexpandable so that it can be used in tables (noalign
% interferences)

\unexpanded\def\xmlresetsorter     #1{\ctxlxml{sorters.reset("#1")}}
\unexpanded\def\xmladdsortentry#1#2#3{\ctxlxml{sorters.add("#1","#2",\!!bs#3\!!es)}}
\unexpanded\def\xmlshowsorter      #1{\ctxlxml{sorters.show("#1")}}
           \def\xmlflushsorter   #1#2{\ctxlxml{sorters.flush("#1","#2")}}
\unexpanded\def\xmlsortentries     #1{\ctxlxml{sorters.sort("#1")}}

\protect \endinput

\startbuffer[test]
<demo>
  <entry>
    <category>one</category>
    <key>alpha</key>
    <content>alpha indeed</content>
  </entry>
  <entry>
    <category>one</category>
    <key>gamma</key>
    <content>gamma indeed</content>
  </entry>
  <entry>
    <category>one</category>
    <key>beta</key>
    <content>beta indeed</content>
  </entry>
  <entry>
    <category>two</category>
    <key>alpha</key>
    <content>alpha again</content>
  </entry>
  <entry>
    <category>two</category>
    <key>gamma</key>
    <content>gamma again</content>
  </entry>
  <entry>
    <category>two</category>
    <key>beta</key>
    <content>beta again</content>
  </entry>
</demo>
\stopbuffer

\startxmlsetups xml:mysetups
    \xmlsetsetup{\xmldocument}{demo|entry|content}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:mysetups}

\startxmlsetups xml:demo
    \xmlresetsorter{demo}
    \xmlfilter{#1}{entry/command(xml:entry:getkeys)}
    \blank sortkeys: \blank\xmlshowsorter{demo}\blank
    \xmlsortentries{demo}
    \xmlflushsorter{demo}{xml:entry:flush}
\stopxmlsetups

\startxmlsetups xml:entry:getkeys
    \xmladdsortentry{demo}{#1}{\xmltext{#1}{category}}
    \xmladdsortentry{demo}{#1}{\xmltext{#1}{key|entry}}
\stopxmlsetups

\startxmlsetups xml:entry:flush
    \xmltext{#1}{content}\par
\stopxmlsetups

\startxmlsetups xml:entry
    \xmltext{#1}{content}\par
\stopxmlsetups

\starttext
    \xmlprocessbuffer{main}{test}{}
\stoptext