/
en.wikipedia.org.rb
376 lines (330 loc) · 9.73 KB
/
en.wikipedia.org.rb
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# frozen_string_literal: true
# rubocop:disable Layout/EmptyLinesAroundArguments
module Infoboxer
MediaWiki::Traits.for('en.wikipedia.org') do
templates do
# https://en.wikipedia.org/wiki/Category:Wikipedia_character-substitution_templates
# ---------------------------------------------------------------------------------
# Extracted semi-automatically
# TODO: fully automatical extraction
literal(
'&',
';',
'=',
'?',
'—',
'1/2',
'1/3',
'1/4',
'2/3',
'3/4',
)
replace(
'!!' => '||',
'!(' => '[',
'!((' => '[[',
'!-' => '|-',
'!:' => ':',
"'" => " '",
"''" => '″',
"'s" => "'s",
'(' => '{',
'((' => '{{',
'(((' => '{{{',
')' => '}',
')!' => ']',
'))' => '}}',
'))!' => ']]',
')))' => '}}}',
'Asterisk' => '*',
'Colon' => ':',
'Em dash' => '—',
'Gc' => '†',
'Ibeam' => 'I',
'Long dash' => ' ——— ',
'Nbhyph' => '‑',
'Number sign' => '#',
'Shy' => '', # soft hyphen
'Single space' => "' ",
'Space single' => " '",
'Spaced ndash' => ' – ',
'Square bracket close' => ']',
'Square bracket open' => '[',
'Zwsp' => '',
'\\' => ' / ',
'`' => "'",
'·' => ' · ',
'‘' => '‘',
'•' => ' • ',
)
# https://en.wikipedia.org/wiki/Category:Line-handling_templates
# ------------------------------------------------------------------
replace(
'-' => "\n",
'Break' => "\n", # FIXME: in fact, break has optional parameter "how many breaks"
'Crlf' => "\n", # FIXME: in fact, alias for break, should have DSL syntax for it!
'Crlf2' => "\n",
)
show(
'Allow wrap',
'Nowrap',
'j', 'nobr', 'nobreak', # aliases for Nowrap
'nowraplinks',
)
# inflow_template('Normalwraplink') # TODO: tricky
# https://en.wikipedia.org/wiki/Category:List_formatting_and_function_templates
# -----------------------------------------------------------------------------
# NB: it's enough for most cases to have all list-representing templates
# just navigable inside and rendered as space-separated list of entries
show(
'Br separated entries',
'Bulleted list',
'Collapsible list',
'Comma separated entries',
'Hlist',
'Flatlist',
'Flowlist',
'Pagelist',
'Ordered list',
'Plainlist',
'Space separated entries',
'Toolbar',
)
# https://en.wikipedia.org/wiki/Category:Wikipedia_XHTML_tag-replacing_templates
# ------------------------------------------------------------------------------
show(
# Font size
'Small',
'Smaller',
'Midsize',
'Larger',
'Big',
'Large',
'Huge',
# Align
'left',
'Center',
'Right',
# Simple style
'Em',
'Kbd',
'Var',
'Varserif',
'Samp',
'Strikethrough',
'Strong',
'Sub',
'Sup',
'Underline',
# FIXME: should do something wiser
'Pre',
'Pre2',
'Code'
)
template 'Abbr' do
def children
fetch('1')
end
end
# TODO: has aliases: {{Define}}, {{Explain}}, {{Tooltip}}
template 'Align' do
def children
fetch('2')
end
end
template 'Dfn' do
def children
fetch('1')
end
end
template 'Resize' do
def children
unnamed_variables.count < 2 ? fetch('1') : fetch('2')
end
end
template 'Font' do
def children
res = fetch('text')
res.empty? ? fetch('1') : res
end
end
# https://en.wikipedia.org/wiki/Category:Text_color_templates
show(
'white', 'silver (color)', 'gray', 'black', 'pink', 'red', 'darkred',
'maroon', 'brown', 'orange (color)', 'gold (color)', 'yellow', 'olive',
'lime', 'green', 'aqua (color)', 'cyan', 'teal', 'blue', 'navy (color)',
'purple', 'fuchsia', 'magenta'
)
# Some most popular templates, without categorical splitting
# https://en.wikipedia.org/wiki/Wikipedia:Database_reports/Templates_transcluded_on_the_most_pages
# ------------------------------------------------------------------------------------------------
# Currently scanned by eyes up to 250-th line, which is used in 130549 articles, according to the
# page - which, though, is dramatically outdated.
template 'Stub', match: /-stub$/ do
def stub?
true
end
end
template 'Infobox', match: /^Infobox/i do
def infobox?
true
end
end
template 'Coord' do
def model
@model ||= begin
npos = lookup_children(text: /^N|S$/).first.index rescue nil # rubocop:disable Style/RescueModifier
case npos
when 1
:decimal
when 2
:min
when 3
:sec
else
:decimal_sign
end
end
end
def lat
case model
when :decimal
'%s°%s′%s' % fetch('1', '2').map(&:text)
when :decimal_sign
fetch('1').text
when :min
'%s°%s′%s' % fetch('1', '2', '3').map(&:text)
when :sec
'%s°%s′%s″%s' % fetch('1', '2', '3', '4').map(&:text)
end
end
def lng
case model
when :decimal, :decimal_sign
fetch('1').text
when :min
'%s°%s′%s' % fetch('1', '2', '3').map(&:text)
when :sec
'%s°%s′%s″%s' % fetch('1', '2', '3', '4').map(&:text)
end
end
end
template 'Convert' do
def value1
fetch('1').text
end
ALLOW_BETWEEN = ['-;', '–',
'and', '&', 'and(-)', ', and',
'or', ', or',
'to', 'to(-)', 'to about',
'+/-', '±', '+',
'by', 'x', '×', 'x',].freeze
def between
ALLOW_BETWEEN.include?(fetch('2').text) ? fetch('2').text : nil
end
def value2
between ? fetch('3').text : nil
end
def measure_from
between ? fetch('4').text : fetch('2').text
end
def measure_to
between ? fetch('5').text : fetch('3').text
end
def text
[value1, between, value2, measure_from].compact.join(' ')
end
end
template 'Age' do
def from
fetch_date('1', '2', '3')
end
def to
fetch_date('4', '5', '6') || Date.today
end
def value
(to - from).to_i / 365 # FIXME: obviously
end
def text
"#{value} years"
end
end
template 'Birth date and age' do
def date
fetch_date('1', '2', '3')
end
def text
date.to_s
end
end
# TODO: aliased as bda
template 'Birth date' do
def date
fetch_date('1', '2', '3')
end
def text
date.to_s
end
end
# TODO: aliased as dob
template 'Time ago' do
def text
str = fetch('1').text
begin
date = Date.parse(str)
"#{(Date.today - date).to_i} days ago" # not trying complext time_distance_in_words formatting here
rescue ArgumentError
str
end
end
end
template 'Flagcountry' do # very popular instead of country name
def children
fetch('1')
end
end
template 'Flag' do # very popular instead of country name
def children
fetch('1')
end
end
show 'Plural'
template 'URL' do
def children
unnamed_variables.count > 1 ? fetch('2') : fetch('1')
end
end
# Prononciation/lang templates - are frequent in article abstracts
# Doint it dirty, but useful, for now:
template 'Lang', match: /^lang-(\w{2,3})$/i do
def children
fetch('1')
end
end
template 'IPAc', match: /^IPAc[12]?-(\w{2,3})$/i do
def text
unnamed_variables.text
end
end
template 'IPA', match: /^IPA-(\w{2,3})$/i do
def text
fetch('1').text
end
end
# TODO: extremely popular:
# Str left - https://en.wikipedia.org/wiki/Category:String_manipulation_templates
# Rnd - https://en.wikipedia.org/wiki/Category:Mathematical_function_templates
# TODO: useful categories
# https://en.wikipedia.org/wiki/Category:Date_mathematics_templates
# https://en.wikipedia.org/wiki/Category:Mathematical_function_templates
# https://en.wikipedia.org/wiki/Category:Wikipedia_formatting_and_function_templates
# https://en.wikipedia.org/wiki/Category:Semantic_markup_templates
# https://en.wikipedia.org/wiki/Category:Quotation_templates
# https://en.wikipedia.org/wiki/Category:Typing-aid_templates
# https://en.wikipedia.org/wiki/Category:Inline_spacing_templates
# https://en.wikipedia.org/wiki/Category:Sorting_templates
# https://en.wikipedia.org/wiki/Wikipedia:Database_reports/Templates_transcluded_on_the_most_pages
end
end
end
# rubocop:enable Layout/EmptyLinesAroundArguments