wp2txt 2.3.4 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.dockerignore +0 -4
- data/.github/workflows/ci.yml +16 -1
- data/.github/workflows/publish-image.yml +8 -4
- data/.gitignore +3 -6
- data/CHANGELOG.md +32 -0
- data/DEVELOPMENT.md +63 -93
- data/DEVELOPMENT_ja.md +61 -93
- data/Dockerfile +1 -1
- data/README.md +93 -8
- data/README_ja.md +84 -8
- data/Rakefile +10 -21
- data/bin/wp2txt +84 -15
- data/bin/wp2txt-mcp +1 -1
- data/docs/INDEXES.md +61 -1
- data/lib/wp2txt/article.rb +2 -3
- data/lib/wp2txt/bz2_stream.rb +63 -0
- data/lib/wp2txt/cli.rb +90 -26
- data/lib/wp2txt/config.rb +2 -11
- data/lib/wp2txt/constants.rb +13 -1
- data/lib/wp2txt/corpus.rb +10 -23
- data/lib/wp2txt/data/template_aliases.json +4 -1
- data/lib/wp2txt/data/template_patterns.json +61643 -0
- data/lib/wp2txt/dump_snapshot.rb +99 -0
- data/lib/wp2txt/extraction_run.rb +272 -0
- data/lib/wp2txt/extractor.rb +481 -327
- data/lib/wp2txt/formatter.rb +14 -12
- data/lib/wp2txt/fts_index.rb +1 -1
- data/lib/wp2txt/index_cache.rb +144 -55
- data/lib/wp2txt/index_commands.rb +109 -18
- data/lib/wp2txt/langlinks_importer.rb +19 -35
- data/lib/wp2txt/lead_terms.rb +228 -0
- data/lib/wp2txt/link_counter.rb +171 -0
- data/lib/wp2txt/magic_words.rb +44 -28
- data/lib/wp2txt/metadata_index.rb +82 -13
- data/lib/wp2txt/multistream.rb +911 -824
- data/lib/wp2txt/output_writer.rb +3 -1
- data/lib/wp2txt/page_props_importer.rb +170 -0
- data/lib/wp2txt/parser_functions.rb +23 -5
- data/lib/wp2txt/ractor_worker.rb +6 -4
- data/lib/wp2txt/regex.rb +24 -4
- data/lib/wp2txt/site_info.rb +123 -0
- data/lib/wp2txt/sql_dump_reader.rb +57 -0
- data/lib/wp2txt/stream_processor.rb +9 -2
- data/lib/wp2txt/stream_store.rb +91 -0
- data/lib/wp2txt/template_args.rb +111 -0
- data/lib/wp2txt/template_expander.rb +110 -124
- data/lib/wp2txt/template_patterns.rb +182 -0
- data/lib/wp2txt/utils.rb +164 -127
- data/lib/wp2txt/version.rb +1 -1
- data/lib/wp2txt/wikitext_regions.rb +66 -0
- data/spec/auto_download_spec.rb +9 -6
- data/spec/category_fetcher_spec.rb +135 -463
- data/spec/cli_targeted_spec.rb +60 -0
- data/spec/config_spec.rb +2 -15
- data/spec/constants_spec.rb +0 -4
- data/spec/docs_sync_spec.rb +3 -3
- data/spec/dump_fetch_spec.rb +293 -0
- data/spec/extraction_run_spec.rb +520 -0
- data/spec/index_build_spec.rb +113 -0
- data/spec/index_commands_version_spec.rb +17 -0
- data/spec/langlinks_importer_spec.rb +27 -0
- data/spec/lead_terms_edge_cases_spec.rb +194 -0
- data/spec/lead_terms_links_qids_spec.rb +204 -0
- data/spec/multistream_reader_spec.rb +140 -0
- data/spec/multistream_spec.rb +8 -214
- data/spec/output_integrity_spec.rb +45 -2
- data/spec/p1_correctness_spec.rb +12 -9
- data/spec/page_properties_spec.rb +161 -0
- data/spec/regex_spec.rb +30 -0
- data/spec/region_semantics_spec.rb +71 -0
- data/spec/spec_helper.rb +3 -3
- data/spec/support/dump_status.rb +34 -0
- data/spec/support/multistream_fixture.rb +26 -8
- data/spec/template_args_spec.rb +133 -0
- data/spec/template_expander_spec.rb +29 -111
- data/spec/template_passthrough_spec.rb +58 -0
- data/spec/template_patterns_spec.rb +253 -0
- data/spec/tracked_paths_spec.rb +168 -0
- data/spec/utils_spec.rb +7 -1
- metadata +45 -5
- data/lib/wp2txt/category_cache.rb +0 -313
- data/spec/category_cache_spec.rb +0 -226
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Wp2txt
|
|
4
|
+
# The arguments of a template call, read the way MediaWiki reads them. An
|
|
5
|
+
# argument is named when it holds an "=" outside nested markup; its name is
|
|
6
|
+
# the text before that "=" (any characters, case kept), and both name and
|
|
7
|
+
# value are trimmed. The other arguments are numbered from 1 in order and
|
|
8
|
+
# kept as written. A later argument with the same name or number replaces
|
|
9
|
+
# an earlier one, so in {{x|1=a|b}} the first argument is "b".
|
|
10
|
+
class TemplateArgs
|
|
11
|
+
# Highest number read as a position, so that a short call never makes a
|
|
12
|
+
# long list. An argument numbered above it (only an explicit "100000=x"
|
|
13
|
+
# can be) is read only by its number, args["100000"]
|
|
14
|
+
MAX_POSITION = 1000
|
|
15
|
+
|
|
16
|
+
# @param parts [Array<String>] the call's arguments, split at top-level pipes
|
|
17
|
+
def self.parse(parts)
|
|
18
|
+
values = {}
|
|
19
|
+
number = 0
|
|
20
|
+
parts.each do |part|
|
|
21
|
+
eq = top_level_equals(part)
|
|
22
|
+
if eq
|
|
23
|
+
values[part[0...eq].strip] = part[(eq + 1)..].strip
|
|
24
|
+
else
|
|
25
|
+
number += 1
|
|
26
|
+
values[number.to_s] = part
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
new(values)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Index of the first "=" not inside {{...}} or [[...]]
|
|
33
|
+
def self.top_level_equals(part)
|
|
34
|
+
depth = 0
|
|
35
|
+
i = 0
|
|
36
|
+
while i < part.length
|
|
37
|
+
pair = part[i, 2]
|
|
38
|
+
if pair == "{{" || pair == "[["
|
|
39
|
+
depth += 1
|
|
40
|
+
i += 2
|
|
41
|
+
elsif pair == "}}" || pair == "]]"
|
|
42
|
+
depth -= 1
|
|
43
|
+
i += 2
|
|
44
|
+
else
|
|
45
|
+
return i if part[i] == "=" && depth <= 0
|
|
46
|
+
|
|
47
|
+
i += 1
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
private_class_method :top_level_equals
|
|
53
|
+
|
|
54
|
+
# What a template or function shows, with each "=" held as `literal`, so
|
|
55
|
+
# that a template around it does not read the "=" as naming an argument.
|
|
56
|
+
# Templates in it still to be expanded keep their own "=".
|
|
57
|
+
def self.literal_equals(text, literal)
|
|
58
|
+
return text unless literal && text.include?("=")
|
|
59
|
+
|
|
60
|
+
depth = 0
|
|
61
|
+
out = +""
|
|
62
|
+
i = 0
|
|
63
|
+
while i < text.length
|
|
64
|
+
pair = text[i, 2]
|
|
65
|
+
if pair == "{{" || (pair == "}}" && depth.positive?)
|
|
66
|
+
depth += pair == "{{" ? 1 : -1
|
|
67
|
+
out << pair
|
|
68
|
+
i += 2
|
|
69
|
+
else
|
|
70
|
+
out << (text[i] == "=" && depth.zero? ? literal : text[i])
|
|
71
|
+
i += 1
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
out
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def initialize(values)
|
|
78
|
+
@values = values
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# @param key [String, Integer] a name, or the number of an argument
|
|
82
|
+
def [](key)
|
|
83
|
+
@values[key.to_s]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def key?(key)
|
|
87
|
+
@values.key?(key.to_s)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Numbered arguments from 1 up to the highest given (nil where one is missing)
|
|
91
|
+
def positional
|
|
92
|
+
(1..positional_count).map { |n| @values[n.to_s] }
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The highest position given
|
|
96
|
+
def positional_count
|
|
97
|
+
@positional_count ||= @values.keys.grep(/\A[1-9]\d{0,3}\z/).map(&:to_i).select { |n| n <= MAX_POSITION }.max || 0
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Arguments with names that are not numbers
|
|
101
|
+
def named
|
|
102
|
+
@values.reject { |key, _| key.match?(/\A[1-9]\d*\z/) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The shape the expander's handlers read: {positional: [...], "name" => value}.
|
|
106
|
+
# A numbered argument skipped in the call reads as empty there.
|
|
107
|
+
def to_params
|
|
108
|
+
{ positional: positional.map { |value| value.to_s.strip } }.merge(named)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "template_args"
|
|
4
|
+
require_relative "template_patterns"
|
|
5
|
+
|
|
3
6
|
module Wp2txt
|
|
4
7
|
# Expands common MediaWiki templates to their text representation
|
|
5
8
|
# Handles date templates, convert templates, and other common patterns
|
|
@@ -9,48 +12,10 @@ module Wp2txt
|
|
|
9
12
|
July August September October November December
|
|
10
13
|
].freeze
|
|
11
14
|
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
["mi", "km"] => 1.60934,
|
|
17
|
-
["m", "ft"] => 3.28084,
|
|
18
|
-
["ft", "m"] => 0.3048,
|
|
19
|
-
["cm", "in"] => 0.393701,
|
|
20
|
-
["in", "cm"] => 2.54,
|
|
21
|
-
["mm", "in"] => 0.0393701,
|
|
22
|
-
["in", "mm"] => 25.4,
|
|
23
|
-
["yd", "m"] => 0.9144,
|
|
24
|
-
["m", "yd"] => 1.09361,
|
|
25
|
-
# Weight
|
|
26
|
-
["kg", "lb"] => 2.20462,
|
|
27
|
-
["lb", "kg"] => 0.453592,
|
|
28
|
-
["g", "oz"] => 0.035274,
|
|
29
|
-
["oz", "g"] => 28.3495,
|
|
30
|
-
["t", "lb"] => 2204.62,
|
|
31
|
-
["lb", "t"] => 0.000453592,
|
|
32
|
-
# Temperature (special handling)
|
|
33
|
-
["C", "F"] => :celsius_to_fahrenheit,
|
|
34
|
-
["°C", "°F"] => :celsius_to_fahrenheit,
|
|
35
|
-
["F", "C"] => :fahrenheit_to_celsius,
|
|
36
|
-
["°F", "°C"] => :fahrenheit_to_celsius,
|
|
37
|
-
# Area
|
|
38
|
-
["km2", "sqmi"] => 0.386102,
|
|
39
|
-
["sqmi", "km2"] => 2.58999,
|
|
40
|
-
["ha", "acre"] => 2.47105,
|
|
41
|
-
["acre", "ha"] => 0.404686,
|
|
42
|
-
["m2", "sqft"] => 10.7639,
|
|
43
|
-
["sqft", "m2"] => 0.092903,
|
|
44
|
-
# Speed
|
|
45
|
-
["km/h", "mph"] => 0.621371,
|
|
46
|
-
["mph", "km/h"] => 1.60934,
|
|
47
|
-
["m/s", "km/h"] => 3.6,
|
|
48
|
-
["km/h", "m/s"] => 0.277778,
|
|
49
|
-
# Volume
|
|
50
|
-
["l", "gal"] => 0.264172,
|
|
51
|
-
["gal", "l"] => 3.78541,
|
|
52
|
-
["ml", "floz"] => 0.033814,
|
|
53
|
-
["floz", "ml"] => 29.5735
|
|
15
|
+
# Words {{convert}} takes between the values of a range (5|-|10, 16|to|17)
|
|
16
|
+
CONVERT_RANGE_WORDS = {
|
|
17
|
+
"-" => "–", "–" => "–", "to" => "to", "to(-)" => "to", "and" => "and", "and(-)" => "and",
|
|
18
|
+
"or" => "or", "x" => "×", "by" => "by", "+/-" => "±", "±" => "±", "+" => "+", "~" => "~"
|
|
54
19
|
}.freeze
|
|
55
20
|
|
|
56
21
|
# Unit display names
|
|
@@ -87,9 +52,17 @@ module Wp2txt
|
|
|
87
52
|
"floz" => "fl oz"
|
|
88
53
|
}.freeze
|
|
89
54
|
|
|
90
|
-
|
|
55
|
+
# @param reference_date [Time, nil] "now" for templates that count from
|
|
56
|
+
# today (ages): the dump's date, so a dump always gives the same text
|
|
57
|
+
# @param lang [String, nil] the wiki's language code
|
|
58
|
+
# @param literal_equals [String, nil] put in place of "=" in what a
|
|
59
|
+
# template shows, so that a template around it does not take it for
|
|
60
|
+
# the start of an argument's value (changed back by the caller)
|
|
61
|
+
def initialize(reference_date: nil, preserve_unknown: false, lang: nil, literal_equals: nil)
|
|
91
62
|
@reference_date = reference_date || Time.now
|
|
92
63
|
@preserve_unknown = preserve_unknown
|
|
64
|
+
@lang = lang&.to_s
|
|
65
|
+
@literal_equals = literal_equals
|
|
93
66
|
end
|
|
94
67
|
|
|
95
68
|
# Main expansion method
|
|
@@ -143,6 +116,8 @@ module Wp2txt
|
|
|
143
116
|
|
|
144
117
|
template_content = text[(start_idx + 2)...end_idx]
|
|
145
118
|
expanded = expand_single_template(template_content)
|
|
119
|
+
# A template left for later stages keeps its own syntax
|
|
120
|
+
expanded = TemplateArgs.literal_equals(expanded, @literal_equals) unless expanded == "{{#{template_content}}}"
|
|
146
121
|
result << expanded
|
|
147
122
|
|
|
148
123
|
pos = end_idx + 2
|
|
@@ -171,9 +146,50 @@ module Wp2txt
|
|
|
171
146
|
nil
|
|
172
147
|
end
|
|
173
148
|
|
|
149
|
+
# Templates whose rendering the text-cleaning stage defines (see
|
|
150
|
+
# Wp2txt#correct_inline_template), keyed by normalized name. Limited to
|
|
151
|
+
# kinds whose output does not depend on marker settings.
|
|
152
|
+
def rendered_by_cleaner?(template_name)
|
|
153
|
+
@rendered_by_cleaner ||= [Wp2txt::RUBY_TEXT_TEMPLATES, Wp2txt::INTERWIKI_LINK_TEMPLATES]
|
|
154
|
+
.flatten.to_set { |name| name.to_s.tr("_", " ").strip.downcase }
|
|
155
|
+
@rendered_by_cleaner.include?(template_name.tr("_", " "))
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def removed_by_rule?(content)
|
|
159
|
+
Wp2txt::CITATION_TEMPLATE_REGEX.match?(content) || Wp2txt::REMOVE_TEMPLATES_REGEX.match?(content)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# A template shown as a block marker ([INFOBOX], [REFERENCES]) that shows
|
|
163
|
+
# no text of its own is left to the markers
|
|
164
|
+
def left_to_marker?(name, definition)
|
|
165
|
+
return false unless definition["p"].to_s.empty?
|
|
166
|
+
|
|
167
|
+
@block_marker_templates ||= Wp2txt::BLOCK_MARKERS.flat_map { |type| Wp2txt::MARKER_PATTERNS.dig(type, :templates) || [] }
|
|
168
|
+
@block_marker_templates.any? { |regex| regex.match?("{{#{name.strip}") }
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def text_renderer
|
|
172
|
+
@text_renderer ||= Object.new.extend(Wp2txt)
|
|
173
|
+
end
|
|
174
|
+
|
|
174
175
|
def expand_single_template(content)
|
|
175
176
|
parts = split_template_parts(content)
|
|
176
|
-
return "" if parts.empty?
|
|
177
|
+
return "" if parts[0].strip.empty?
|
|
178
|
+
|
|
179
|
+
# The edition's own way of showing the template, where it is known;
|
|
180
|
+
# citations and templates removed by rule stay removed. Templates in
|
|
181
|
+
# the arguments are expanded first, once the arguments are split, as
|
|
182
|
+
# the pattern may depend on their values (a language code)
|
|
183
|
+
if @lang && (definition = TemplatePatterns.lookup(@lang, parts[0])) && !removed_by_rule?(content) &&
|
|
184
|
+
!left_to_marker?(parts[0], definition)
|
|
185
|
+
args = parts[1..].map { |part| part.include?("{{") ? expand(part) : part }
|
|
186
|
+
text = TemplatePatterns.render(definition, TemplateArgs.parse(args))
|
|
187
|
+
return text if text
|
|
188
|
+
|
|
189
|
+
# The arguments as expanded, for the rules below and for the cleaner
|
|
190
|
+
parts = [parts[0], *args]
|
|
191
|
+
content = parts.join("|")
|
|
192
|
+
end
|
|
177
193
|
|
|
178
194
|
template_name = parts[0].strip.downcase
|
|
179
195
|
params = parse_template_params(parts[1..])
|
|
@@ -263,62 +279,53 @@ module Wp2txt
|
|
|
263
279
|
# Handle lang-xx templates (e.g., lang-fr, lang-de, lang-ja)
|
|
264
280
|
if template_name.start_with?("lang-")
|
|
265
281
|
expand_lang_xx(template_name, params)
|
|
282
|
+
elsif rendered_by_cleaner?(template_name)
|
|
283
|
+
# These carry words the article needs (a reading, a link's display
|
|
284
|
+
# text). Deleting them dropped those words; leaving the raw template
|
|
285
|
+
# for later confused links that contain it (an image caption with a
|
|
286
|
+
# "|" inside). Render them now, with the cleaner's own rules.
|
|
287
|
+
text_renderer.correct_inline_template("{{#{expand(content)}}}").tr(Wp2txt::LITERAL_EQUALS, @literal_equals || "=")
|
|
266
288
|
else
|
|
267
289
|
@preserve_unknown ? "{{#{content}}}" : ""
|
|
268
290
|
end
|
|
269
291
|
end
|
|
270
292
|
end
|
|
271
293
|
|
|
294
|
+
# The name and the arguments, split at pipes outside {{...}} and [[...]];
|
|
295
|
+
# an empty last argument ({{x|a|}}) is an argument given
|
|
272
296
|
def split_template_parts(content)
|
|
273
297
|
parts = []
|
|
274
298
|
current = +""
|
|
275
299
|
depth = 0
|
|
300
|
+
i = 0
|
|
276
301
|
|
|
277
|
-
content.
|
|
278
|
-
|
|
302
|
+
while i < content.length
|
|
303
|
+
pair = content[i, 2]
|
|
304
|
+
if pair == "{{" || pair == "[["
|
|
279
305
|
depth += 1
|
|
280
|
-
current <<
|
|
281
|
-
|
|
306
|
+
current << pair
|
|
307
|
+
i += 2
|
|
308
|
+
elsif pair == "}}" || pair == "]]"
|
|
282
309
|
depth -= 1
|
|
283
|
-
current <<
|
|
284
|
-
|
|
285
|
-
parts << current
|
|
286
|
-
current = +""
|
|
310
|
+
current << pair
|
|
311
|
+
i += 2
|
|
287
312
|
else
|
|
288
|
-
|
|
313
|
+
c = content[i]
|
|
314
|
+
if c == "|" && depth <= 0
|
|
315
|
+
parts << current
|
|
316
|
+
current = +""
|
|
317
|
+
else
|
|
318
|
+
current << c
|
|
319
|
+
end
|
|
320
|
+
i += 1
|
|
289
321
|
end
|
|
290
322
|
end
|
|
291
323
|
|
|
292
|
-
parts << current
|
|
293
|
-
parts
|
|
324
|
+
parts << current
|
|
294
325
|
end
|
|
295
326
|
|
|
296
327
|
def parse_template_params(parts)
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
parts.each do |part|
|
|
300
|
-
# Check for named parameter (key=value)
|
|
301
|
-
# Only treat as named parameter if:
|
|
302
|
-
# 1. Contains '='
|
|
303
|
-
# 2. The key part looks like a valid parameter name (alphanumeric/underscore only)
|
|
304
|
-
# 3. Key doesn't contain HTML tags or other special chars
|
|
305
|
-
if part.include?("=")
|
|
306
|
-
key, value = part.split("=", 2)
|
|
307
|
-
key_stripped = key.strip
|
|
308
|
-
# Valid param name: only letters, digits, underscore, space
|
|
309
|
-
# Should NOT contain < > { } or other markup
|
|
310
|
-
if key_stripped.match?(/\A[\w\s]+\z/) && !key_stripped.match?(/[<>{}\[\]]/)
|
|
311
|
-
params[key_stripped.downcase] = value&.strip
|
|
312
|
-
else
|
|
313
|
-
# Treat as positional if key doesn't look valid
|
|
314
|
-
params[:positional] << part.strip
|
|
315
|
-
end
|
|
316
|
-
else
|
|
317
|
-
params[:positional] << part.strip
|
|
318
|
-
end
|
|
319
|
-
end
|
|
320
|
-
|
|
321
|
-
params
|
|
328
|
+
TemplateArgs.parse(parts).to_params
|
|
322
329
|
end
|
|
323
330
|
|
|
324
331
|
# Date formatting methods
|
|
@@ -326,6 +333,7 @@ module Wp2txt
|
|
|
326
333
|
def format_date(params, style: :mdy)
|
|
327
334
|
pos = params[:positional]
|
|
328
335
|
return "" if pos.empty?
|
|
336
|
+
return written_date(pos) if written_date(pos)
|
|
329
337
|
|
|
330
338
|
year = pos[0].to_i
|
|
331
339
|
month = pos[1]&.to_i
|
|
@@ -351,9 +359,18 @@ module Wp2txt
|
|
|
351
359
|
end
|
|
352
360
|
end
|
|
353
361
|
|
|
362
|
+
# A date written other than in numbers (a month name, the whole date in one
|
|
363
|
+
# argument) is kept as written, as its parts cannot be read as numbers
|
|
364
|
+
# @return [String, nil] nil when the date is in numbers
|
|
365
|
+
def written_date(pos)
|
|
366
|
+
parts = pos.first(3).map { |v| v.to_s.strip }.reject(&:empty?)
|
|
367
|
+
parts.join(" ") unless parts.all? { |v| v.match?(/\A\d+\z/) }
|
|
368
|
+
end
|
|
369
|
+
|
|
354
370
|
def format_simple_date(params)
|
|
355
371
|
pos = params[:positional]
|
|
356
372
|
return "" if pos.empty?
|
|
373
|
+
return written_date(pos) if written_date(pos)
|
|
357
374
|
|
|
358
375
|
year = pos[0].to_i
|
|
359
376
|
month = pos[1]&.to_i
|
|
@@ -365,6 +382,7 @@ module Wp2txt
|
|
|
365
382
|
def format_date_with_age(params, style: :mdy, age_label: "age")
|
|
366
383
|
pos = params[:positional]
|
|
367
384
|
return "" if pos.empty?
|
|
385
|
+
return written_date(pos) if written_date(pos)
|
|
368
386
|
|
|
369
387
|
year = pos[0].to_i
|
|
370
388
|
month = pos[1]&.to_i || 1
|
|
@@ -380,6 +398,7 @@ module Wp2txt
|
|
|
380
398
|
|
|
381
399
|
def format_death_date_with_age(params)
|
|
382
400
|
pos = params[:positional]
|
|
401
|
+
return written_date(pos) if written_date(pos)
|
|
383
402
|
return "" if pos.length < 6
|
|
384
403
|
|
|
385
404
|
death_year = pos[0].to_i
|
|
@@ -527,36 +546,21 @@ module Wp2txt
|
|
|
527
546
|
|
|
528
547
|
# Convert template
|
|
529
548
|
|
|
549
|
+
# {{convert}}: the values and the unit as written. The converted figures
|
|
550
|
+
# are computed by the template, and not reproduced.
|
|
530
551
|
def expand_convert(params)
|
|
531
|
-
pos = params[:positional]
|
|
552
|
+
pos = params[:positional].map(&:strip)
|
|
532
553
|
return "" if pos.empty?
|
|
533
554
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
# Normalize units
|
|
541
|
-
from_normalized = normalize_unit(from_unit)
|
|
542
|
-
to_normalized = normalize_unit(to_unit)
|
|
543
|
-
|
|
544
|
-
conversion = CONVERSIONS[[from_normalized, to_normalized]]
|
|
545
|
-
|
|
546
|
-
if conversion.nil?
|
|
547
|
-
"#{format_number(value)} #{UNIT_DISPLAY[from_normalized] || from_unit}"
|
|
548
|
-
elsif conversion.is_a?(Symbol)
|
|
549
|
-
# Special conversion (temperature)
|
|
550
|
-
converted = send(conversion, value)
|
|
551
|
-
from_display = UNIT_DISPLAY[from_normalized] || from_unit
|
|
552
|
-
to_display = UNIT_DISPLAY[to_normalized] || to_unit
|
|
553
|
-
"#{format_number(value)} #{from_display} (#{format_number(converted)} #{to_display})"
|
|
554
|
-
else
|
|
555
|
-
converted = value * conversion
|
|
556
|
-
from_display = UNIT_DISPLAY[from_normalized] || from_unit
|
|
557
|
-
to_display = UNIT_DISPLAY[to_normalized] || to_unit
|
|
558
|
-
"#{format_number(value)} #{from_display} (#{format_number(converted)} #{to_display})"
|
|
555
|
+
parts = [pos[0]]
|
|
556
|
+
i = 1
|
|
557
|
+
while (word = CONVERT_RANGE_WORDS[pos[i]]) && !pos[i + 1].to_s.empty?
|
|
558
|
+
parts << word << pos[i + 1]
|
|
559
|
+
i += 2
|
|
559
560
|
end
|
|
561
|
+
text = parts.join(" ").gsub(" – ", "–")
|
|
562
|
+
unit = pos[i].to_s
|
|
563
|
+
unit.empty? ? text : "#{text} #{UNIT_DISPLAY[normalize_unit(unit)] || unit}"
|
|
560
564
|
end
|
|
561
565
|
|
|
562
566
|
def normalize_unit(unit)
|
|
@@ -564,24 +568,6 @@ module Wp2txt
|
|
|
564
568
|
unit.gsub(/\s+/, "")
|
|
565
569
|
end
|
|
566
570
|
|
|
567
|
-
def format_number(value)
|
|
568
|
-
# Format number, removing unnecessary decimals
|
|
569
|
-
rounded = value.round(1)
|
|
570
|
-
if rounded == rounded.to_i
|
|
571
|
-
rounded.to_i.to_s
|
|
572
|
-
else
|
|
573
|
-
format("%.1f", rounded)
|
|
574
|
-
end
|
|
575
|
-
end
|
|
576
|
-
|
|
577
|
-
def celsius_to_fahrenheit(c)
|
|
578
|
-
(c * 9.0 / 5.0 + 32).round
|
|
579
|
-
end
|
|
580
|
-
|
|
581
|
-
def fahrenheit_to_celsius(f)
|
|
582
|
-
((f - 32) * 5.0 / 9.0).round
|
|
583
|
-
end
|
|
584
|
-
|
|
585
571
|
# Common template expansions
|
|
586
572
|
|
|
587
573
|
def expand_circa(params)
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "template_args"
|
|
5
|
+
|
|
6
|
+
module Wp2txt
|
|
7
|
+
# How each Wikipedia edition displays its most used inline templates, as
|
|
8
|
+
# learned from the edition's own parser (scripts/fetch_template_patterns.rb):
|
|
9
|
+
# a pattern of the displayed text with slots for the arguments, such as
|
|
10
|
+
# "ドイツ語: {1}" for {{Lang-de|...}} on the Japanese edition.
|
|
11
|
+
module TemplatePatterns
|
|
12
|
+
DATA_PATH = File.join(__dir__, "data", "template_patterns.json")
|
|
13
|
+
|
|
14
|
+
# Loaded once and made shareable, so that Ractors read the same data
|
|
15
|
+
DATA = Ractor.make_shareable(File.exist?(DATA_PATH) ? JSON.parse(File.read(DATA_PATH)) : { "wikis" => {} })
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def data
|
|
20
|
+
@data || DATA
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Identifies the patterns in use, for the record of an extraction
|
|
24
|
+
def digest
|
|
25
|
+
data.dig("meta", "digest")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The definition of a template on an edition, under its own name or another
|
|
29
|
+
# name that redirects to it
|
|
30
|
+
# @return [Hash, nil]
|
|
31
|
+
def lookup(lang, name)
|
|
32
|
+
wiki = data["wikis"][lang.to_s]
|
|
33
|
+
return nil unless wiki
|
|
34
|
+
|
|
35
|
+
key = normalize(name)
|
|
36
|
+
wiki["templates"][key] || wiki["templates"][wiki["aliases"][key]]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The displayed text of a call, or nil when the pattern does not tell how
|
|
40
|
+
# the call shows: one learned from a number (n arguments, the first a
|
|
41
|
+
# number) for a call with more arguments or other text first, and one
|
|
42
|
+
# learned for a language code from n arguments for a call of another shape
|
|
43
|
+
# (an argument missing or empty) or with a code not among those it was
|
|
44
|
+
# learned from. A call with a named argument the template is known to show
|
|
45
|
+
# or to be shown otherwise with is shown by the pattern learned with that
|
|
46
|
+
# argument, where the call gives no other and the pattern fits it, or else
|
|
47
|
+
# by its pattern followed by the values of those the template shows, each once
|
|
48
|
+
# @param definition [Hash] from lookup
|
|
49
|
+
# @param args [TemplateArgs]
|
|
50
|
+
# @return [String, nil]
|
|
51
|
+
def render(definition, args)
|
|
52
|
+
count = args.positional_count
|
|
53
|
+
# No language code is a number: a year in its place is a date's
|
|
54
|
+
return nil if (code_arg = definition["code_arg"]) && args[code_arg].to_s.strip.match?(/\A\p{Nd}+\z/)
|
|
55
|
+
|
|
56
|
+
if (limit = definition["n"])
|
|
57
|
+
if code_arg
|
|
58
|
+
return nil if count != limit || args.positional.any? { |v| v.to_s.strip.empty? }
|
|
59
|
+
return nil if definition["codes"] && !definition["codes"].include?(args[code_arg].to_s.strip.downcase)
|
|
60
|
+
# Learned with no named argument: one not known to change nothing is not told
|
|
61
|
+
return nil unless (given_named(definition, args) - known_named(definition)).empty?
|
|
62
|
+
elsif count > limit || !args[1].to_s.strip.match?(NUMBER)
|
|
63
|
+
return nil
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# A template given one or two arguments may show other text than with three
|
|
68
|
+
pattern = (count < 3 && definition["p#{count}"]) || definition["p"]
|
|
69
|
+
if code_arg
|
|
70
|
+
pattern = definition.dig("by_code", args[code_arg].to_s.strip.downcase) || pattern
|
|
71
|
+
end
|
|
72
|
+
return fill(definition, args, pattern) if pattern.empty?
|
|
73
|
+
|
|
74
|
+
replaces = definition.fetch("replaces", {})
|
|
75
|
+
# Those not known to change the text are not told, as with no named argument
|
|
76
|
+
given = given_named(definition, args) & (known_named(definition) + replaces.keys)
|
|
77
|
+
# Those standing in for an argument are shown by the pattern, one for each
|
|
78
|
+
changing = (given - standing_in(definition, args)) & known_named(definition)
|
|
79
|
+
return fill(definition, args, pattern) if changing.empty?
|
|
80
|
+
|
|
81
|
+
# Learned with the arguments the pattern shows, those not given left empty
|
|
82
|
+
with = definition.dig("with", given[0]) if given.size == 1
|
|
83
|
+
return fill(definition, args, with) if with && with_fits?(definition, args, pattern, with, given[0])
|
|
84
|
+
|
|
85
|
+
# Otherwise the pattern, followed by the values of those it shows that
|
|
86
|
+
# hold a word or a number (a separator such as "、" holds neither)
|
|
87
|
+
text = fill(definition, args, pattern)
|
|
88
|
+
shown = definition.fetch("shows", [])
|
|
89
|
+
values = args.named.select { |name, _| changing.include?(name) && shown.include?(name) }.values
|
|
90
|
+
values.map { |value| value.to_s.strip }.select { |value| value.match?(/[\p{L}\p{N}]/) }.each do |value|
|
|
91
|
+
text = "#{text} #{value}".strip unless link_text(text).include?(link_text(value))
|
|
92
|
+
end
|
|
93
|
+
text
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Text with each link as the text it shows, to tell a value already shown
|
|
97
|
+
def link_text(text)
|
|
98
|
+
text.gsub(/\[\[(?:[^\[\]|]*\|)?([^\[\]|]*)\]\]/, '\1')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Whether the pattern learned with a named argument shows the call: it
|
|
102
|
+
# keeps the arguments of the call's pattern unless it shows the named one
|
|
103
|
+
# in their place (for a call of fewer arguments, it keeps them whatever it
|
|
104
|
+
# shows), and an argument the call leaves empty is not joined to its text
|
|
105
|
+
def with_fits?(definition, args, pattern, with, name)
|
|
106
|
+
slots = with.scan(/\{(\d+)\}/).flatten
|
|
107
|
+
keeps = (pattern.scan(/\{(\d+)\}/).flatten - slots).empty?
|
|
108
|
+
return false unless keeps || (pattern == definition["p"] && with.include?("{#{name}}"))
|
|
109
|
+
|
|
110
|
+
slots.none? do |slot|
|
|
111
|
+
next false unless slot_value(definition, args, slot).strip.empty?
|
|
112
|
+
|
|
113
|
+
with.scan(/(.)?\{#{slot}\}(.)?/).flatten.compact.any? { |c| !c.match?(/[\s{}()\[\]()「」【】]/) }
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# The named arguments a call gives in place of its arguments: for each
|
|
118
|
+
# argument, the first of those standing in for it that has a value
|
|
119
|
+
def standing_in(definition, args)
|
|
120
|
+
definition.fetch("replaces", {}).reject { |param, _| args[param].to_s.empty? }
|
|
121
|
+
.group_by { |_, slot| slot }.map { |_, params| params[0][0] }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Named arguments of a call, given a value, other than those that change
|
|
125
|
+
# nothing and those given a value the template reads as off
|
|
126
|
+
def given_named(definition, args)
|
|
127
|
+
ignores = definition.fetch("ignores", [])
|
|
128
|
+
off = definition.fetch("off", {})
|
|
129
|
+
args.named.reject do |name, value|
|
|
130
|
+
value = value.to_s.strip
|
|
131
|
+
value.empty? || ignores.include?(name) || off.fetch(name, []).include?(value)
|
|
132
|
+
end.keys
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Named arguments known to be shown or to change the text
|
|
136
|
+
def known_named(definition)
|
|
137
|
+
definition.fetch("shows", []) + definition.fetch("with", {}).keys
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def fill(definition, args, pattern)
|
|
141
|
+
# Each slot's value is put in as a token first, so that cleaning the
|
|
142
|
+
# pattern's own brackets and spaces never touches what an argument holds
|
|
143
|
+
values = []
|
|
144
|
+
text = pattern.gsub(/\{([^{}]+)\}/) do
|
|
145
|
+
value = slot_value(definition, args, ::Regexp.last_match(1))
|
|
146
|
+
next "" if value.strip.empty?
|
|
147
|
+
|
|
148
|
+
values << value
|
|
149
|
+
"#{TOKEN_OPEN}#{values.size - 1}#{TOKEN_CLOSE}"
|
|
150
|
+
end
|
|
151
|
+
# Brackets left with nothing in them, where an argument was not given
|
|
152
|
+
text = text.gsub(EMPTY_BRACKETS, "").gsub(/ {2,}/, " ").strip
|
|
153
|
+
text.gsub(TOKEN) { values[::Regexp.last_match(1).to_i] }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# The value shown in a slot: a named argument given in its place, else the
|
|
157
|
+
# argument itself, else, when that is empty, the one the template shows instead
|
|
158
|
+
def slot_value(definition, args, slot, seen = [])
|
|
159
|
+
return "" if seen.include?(slot)
|
|
160
|
+
|
|
161
|
+
named = definition.fetch("replaces", {}).find { |param, target| target == slot && !args[param].to_s.empty? }
|
|
162
|
+
return args[named[0]].to_s if named
|
|
163
|
+
|
|
164
|
+
value = args[slot].to_s
|
|
165
|
+
other = definition.fetch("fallback", {})[slot]
|
|
166
|
+
value.strip.empty? && other ? slot_value(definition, args, other, seen + [slot]) : value
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
NUMBER = /\A[+\-−]?\d[\d.,\s ']*\z/.freeze
|
|
170
|
+
EMPTY_BRACKETS = /\s*(?:(\s*)|\(\s*\)|\[\s*\]|「\s*」|【\s*】)/
|
|
171
|
+
TOKEN_OPEN = "\u{FDD2}"
|
|
172
|
+
TOKEN_CLOSE = "\u{FDD3}"
|
|
173
|
+
TOKEN = /#{TOKEN_OPEN}(\d+)#{TOKEN_CLOSE}/.freeze
|
|
174
|
+
|
|
175
|
+
# A template name as MediaWiki matches it: spaces for underscores, single
|
|
176
|
+
# spaces, and a capital first letter
|
|
177
|
+
def normalize(name)
|
|
178
|
+
text = name.to_s.tr("_", " ").gsub(/\s+/, " ").strip
|
|
179
|
+
text.empty? ? text : text[0].upcase + text[1..]
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|