-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcard-filter.tex
324 lines (297 loc) · 9.28 KB
/
card-filter.tex
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
% Definition of the filter language
% Helper function used to define the semantics of the filter language
% for mathematical expression used with \pgfmath<XXX>
\def\mathoperator#1#2{
{#1}{%
{\unexpanded{##1}}\unexpanded{\toks0=\expandafter{\intResult}%
{##2}\toks2=\expandafter{\intResult}%
\edef\res{\noexpand#2{\the\toks0}{\the\toks2}}%
\res\xdef\intResult{\pgfmathresult}}%
}%
}
% other helpers
\pgfkeys{
/language/.cd, precedence/.is choice, operator/.is choice, value/.is choice,
/handlers/.precedence/.code={%
\edef\pgfkeyscurrentkey{\pgfkeyscurrentpath}\pgfkeyssplitpath{}%
\pgfkeysalso{\pgfkeyscurrentpath/precedence/\pgfkeyscurrentname/.style={current precedence=#1}}%
},
/handlers/.operator expanded/.code={%
\edef\pgfkeyscurrentkey{\pgfkeyscurrentpath}\pgfkeyssplitpath{}%
\pgfkeysalso{%
\pgfkeyscurrentpath/operator/\pgfkeyscurrentname/.style={current operator/.expanded=#1}
}
},
/handlers/.operator/.code={%
\edef\pgfkeyscurrentkey{\pgfkeyscurrentpath}\pgfkeyssplitpath{}%
\pgfkeysalso{%
\pgfkeyscurrentpath/operator/\pgfkeyscurrentname/.style={current operator=#1}
}
},
/handlers/.store intResult/.code 2 args={\pgfkeysalso{%
\pgfkeyscurrentpath/.style={current value={#1}{\xdef\intResult{#2}}}%
}},
/handlers/.value/.code 2 args={\pgfkeysalso{%
\pgfkeyscurrentpath/.style={current value={#1}{#2}}
}},
/handlers/.same value/.code={%
\edef\pgfkeyscurrentkey{\pgfkeyscurrentpath}\pgfkeyssplitpath{}%
\edef\helper{current value={#1}{\pgfkeyscurrentname}}%
\pgfkeysalso{%
\pgfkeyscurrentpath/\pgfkeyscurrentname/.style/.expand once=\helper
}
}
}
% Definition of syntax and semantics of the filter language for cards
\pgfkeys{
/language/.cd,
% Define operator precedences
or/.precedence=2,
and/.precedence=3,
</.precedence=4,
>/.precedence=4,
eq/.precedence=4,
<>/.precedence=4,
+/.precedence=5,
*/.precedence=6,
-/.precedence=5,
% Define types and semantics of operators
bool and bool/.operator expanded=\mathoperator{bool}{\pgfmathand},
bool or bool/.operator expanded=\mathoperator{bool}{\pgfmathor},
%
number + number/.operator expanded=\mathoperator{number}{\pgfmathadd},
number * number/.operator expanded=\mathoperator{number}{\pgfmathmultiply},
number - number/.operator expanded=\mathoperator{number}{\pgfmathsubtract},
number < number/.operator expanded=\mathoperator{bool}{\pgfmathless},
number > number/.operator expanded=\mathoperator{bool}{\pgfmathgreater},
number eq number/.operator expanded=\mathoperator{bool}{\pgfmathgreater},
%
type eq type/.operator={bool}{\xdef\intResult{\ifnum\pdfstrcmp{##1}{##2}=0 1 \else 0 \fi}},
type <> type/.operator={bool}{\xdef\intResult{\ifnum\pdfstrcmp{##1}{##2}=0 0 \else 1 \fi}},
%
title eq title/.operator={bool}{\xdef\intResult{\ifnum\pdfstrcmp{##1}{##2}=0 1 \else 0 \fi}},
title <> title/.operator={bool}{\xdef\intResult{\ifnum\pdfstrcmp{##1}{##2}=0 0 \else 1 \fi}},
%
% Define types and semantics of values
value/true/.store intResult={bool}{1},
value/false/.store intResult={bool}{0},
%
value/0/.store intResult={number}0,
value/1/.store intResult={number}1,
value/2/.store intResult={number}2,
value/3/.store intResult={number}3,
value/4/.store intResult={number}4,
value/4+/.store intResult={number}4.5,
value/5/.store intResult={number}5,
value/6/.store intResult={number}6,
value/7/.store intResult={number}7,
value/8/.store intResult={number}8,
value/9/.store intResult={number}9,
%
value/level/.store intResult={number}{\cardLevel},
%
value/type/.value={type}{\cardType},
value/Item/.same value=type,
value/Action/.same value=type,
value/Spell/.same value=type,
value/Cantrip/.same value=type,
value/Focus/.same value=type,
value/Rule/.same value=type,
value/Condition/.same value=type,
%
value/title/.value={title}{\cardTitle},
value/title/.unknown/.code={
\edef\currentname{\pgfkeyscurrentname}
\pgfkeysalso{current value/.expanded={title}{\currentname}}
}
}
% Convenience layer for filtering
\pgfkeys{
/card attributes/.cd,
type/.estore in=\cardType,
level/.estore in=\cardLevel,
level/.append code={\ifnum\pdfstrcmp{\cardLevel}{}=0 \edef\cardLevel{-1}\fi},
level/.append code={\ifnum\pdfstrcmp{\cardLevel}{4+}=0 \edef\cardLevel{4.5}\fi},
title/.estore in=\cardTitle,
source book/.estore in=\cardSourceBook,
collection value/.estore in=\cardCollectionValue
}
\def\allowedControlSequencesForFilteringCards{
\def\and{and}%
\def\or{or}%
\def\title{title}%
\def\level{level}%
\def\type{type}%
\def\ne{<>}%
}
\NewDocumentEnvironment{filterBy}{}{%
\let\allowedControlSequences=\allowedControlSequencesForFilteringCards
\startExpression
}{%
\ifnum \pdfstrcmp{\currentType}{bool}=0 \else
\errmessage{Wrong type of filter expression, expected bool but got \currentType}
\fi
\global\let\cardFilter=\currentCalc
}
\NewDocumentCommand\ifVisible{o}{%
\pgfqkeys{/card attributes}{#1}%
\cardFilter
\ifnum\intResult=1
\clearpage
\writeMetadata
}
% Write metadata information about some file
\immediate\newwrite\metadataFile
\immediate\openout\metadataFile=metadata.txt
\pgfkeys{
/collections/all/.style={},
/collections/by name/.code={%
\pgfkeysalso{/collections/#1}%
\ifnum\intResult=1
\immediate\write\metadataFile{ collection=#1}
\fi
}
}
\NewDocumentEnvironment{collection}{m}{%
\let\allowedControlSequences=\allowedControlSequencesForFilteringCards
\startExpression
}{%
\ifnum \pdfstrcmp{\currentType}{bool}=0 \else
\errmessage{Wrong type of collection expression, expected bool but got \currentType}
\fi
\global\let\collectionCurrentCalc=\currentCalc
\gdef\evalAfterGroup{
\pgfkeys{
/collections/#1/.code/.expand once=\collectionCurrentCalc,
/collections/all/.append style={/collections/by name=#1}
}
}
\aftergroup\evalAfterGroup
}
\def\writeMetadata{
\immediate\write\metadataFile{page=\thepage}%
\immediate\write\metadataFile{ title=\cardTitle}%
\immediate\write\metadataFile{ level=\cardLevel}%
\immediate\write\metadataFile{ type=\cardType}%
\immediate\write\metadataFile{ source book=\cardSourceBook}%
\immediate\write\metadataFile{ source file=\CurrentFilePath/\CurrentFile}%
\ifnum\pdfstrcmp{\cardCollectionValue}{}=0\else
\immediate\write\metadataFile{ collection value=\cardCollectionValue}%
\fi
\pgfkeys{/collections/all}%
}
% Implementation of semantics and type-checking
% Set up pgfkeys to forward values to the correct macros
\pgfkeys{
/language/current precedence/.code={%
\pgfmathparse{\basePrecedence + #1}
\xdef\nextPrecedence{\pgfmathresult}%
},
/language/current value/.code 2 args={%
\edef\currentType{#1}%
\def\currentCalc{#2}%
},
/language/current operator/.code 2 args={%
\edef\currentType{#1}
\def\helper##1##2{#2}
\toks0=\expandafter{\currentCalc}
\toks2=\expandafter{\nextCalc}
\edef\currentCalc{%
\noexpand\helper{\the\toks0}{\the\toks2}%
}%
\edef\currentCalc{%
\unexpanded\expandafter\expandafter\expandafter{\currentCalc}%
}%
}
}
% entrypoint
\def\startExpression{
\xdef\basePrecedence{0}%
\edef\currentPrecedence{0}%
\edef\currentOperator{}%
\continueWithValue
}
\def\continue#1{
\def\currentToken{#1}
\def\a{\end}\def\b{)}%
\ifx\a\currentToken
% finish
\closeLevels
\expandafter\end
\else\ifx\b\currentToken
\pgfmathparse{int(\basePrecedence-1000)}%
\xdef\basePrecedence{\pgfmathresult}%
\expandafter\expandafter\expandafter\continue
\else
\expandafter\expandafter\expandafter\continueWithOperator
\fi
\fi
}
\def\continueWithOperator{
\parseOperator
\pgfqkeys{/language}{precedence/.expanded=\nextOperator}
\endgroupsPrecedence
\edef\currentOperator{\nextOperator}
\begingroup
\edef\currentOperator{}
\edef\currentPrecedence{\nextPrecedence}
\continueWithValue
}
\def\continueWithValue#1{
\def\a{(}
\def\b{#1}
\ifx\a\b
\pgfmathparse{\basePrecedence+1000}%
\xdef\basePrecedence{\pgfmathresult}%
\expandafter\continueWithValue
\else
\parseValue{#1}
\pgfqkeys{/language}{value/.expanded=\currentValue}
\expandafter\continue
\fi
}
\def\allowedControlSequences{}
\def\parseOperator{
{
\allowedControlSequences
\xdef\nextOperator{\currentToken}
}
\xdef\eqToken{=}
% convert = to eq to support pgfkeys better
\ifx\nextOperator\eqToken \xdef\nextOperator{eq} \fi
}
\def\parseValue#1{%
{%
\allowedControlSequences
\xdef\currentValue{#1}%
}%
}
\def\promoteCurrentType{
\def\empty{}%
\ifx\empty\currentOperator
\edef\currentType{\nextType}
\edef\currentCalc{\unexpanded\expandafter{\nextCalc}}
\else
\pgfqkeys{/language}{operator/.expanded=\currentType\space\currentOperator\space\nextType}
\fi
}
\def\endgroupsPrecedence{
\pgfmathparse{\nextPrecedence<\currentPrecedence} %
\ifnum\pgfmathresult=1
\xdef\nextType{\currentType}
\xdef\nextCalc{\unexpanded\expandafter{\currentCalc}}
\endgroup
\promoteCurrentType
\endgroupsPrecedence
\fi
}
\def\closeLevels{
\xdef\initialBasePrecedence{0}
\ifx\basePrecedence\initialBasePrecedence
\xdef\nextPrecedence{0}
\endgroupsPrecedence
\else
\pgfmathparse{\basePrecedence / 1000}%
\errmessage{Not enough closing brackets at end of expression, \pgfmathresult\space brackets missing}%
\fi
}