wp2txt 2.4.0 → 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.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +16 -1
  3. data/.github/workflows/publish-image.yml +8 -4
  4. data/.gitignore +0 -1
  5. data/CHANGELOG.md +23 -0
  6. data/DEVELOPMENT.md +62 -92
  7. data/DEVELOPMENT_ja.md +60 -92
  8. data/Dockerfile +1 -1
  9. data/README.md +54 -8
  10. data/README_ja.md +54 -8
  11. data/bin/wp2txt +23 -6
  12. data/lib/wp2txt/article.rb +1 -2
  13. data/lib/wp2txt/bz2_stream.rb +63 -0
  14. data/lib/wp2txt/cli.rb +48 -26
  15. data/lib/wp2txt/config.rb +2 -11
  16. data/lib/wp2txt/constants.rb +0 -1
  17. data/lib/wp2txt/corpus.rb +1 -21
  18. data/lib/wp2txt/data/template_aliases.json +3 -0
  19. data/lib/wp2txt/data/template_patterns.json +61643 -0
  20. data/lib/wp2txt/dump_snapshot.rb +99 -0
  21. data/lib/wp2txt/extraction_run.rb +272 -0
  22. data/lib/wp2txt/extractor.rb +476 -325
  23. data/lib/wp2txt/fts_index.rb +1 -1
  24. data/lib/wp2txt/index_cache.rb +144 -55
  25. data/lib/wp2txt/index_commands.rb +22 -19
  26. data/lib/wp2txt/link_counter.rb +1 -1
  27. data/lib/wp2txt/magic_words.rb +44 -28
  28. data/lib/wp2txt/metadata_index.rb +6 -13
  29. data/lib/wp2txt/multistream.rb +890 -824
  30. data/lib/wp2txt/output_writer.rb +3 -1
  31. data/lib/wp2txt/parser_functions.rb +23 -5
  32. data/lib/wp2txt/ractor_worker.rb +6 -4
  33. data/lib/wp2txt/regex.rb +24 -4
  34. data/lib/wp2txt/site_info.rb +123 -0
  35. data/lib/wp2txt/stream_store.rb +91 -0
  36. data/lib/wp2txt/template_args.rb +111 -0
  37. data/lib/wp2txt/template_expander.rb +92 -125
  38. data/lib/wp2txt/template_patterns.rb +182 -0
  39. data/lib/wp2txt/utils.rb +160 -125
  40. data/lib/wp2txt/version.rb +1 -1
  41. data/spec/auto_download_spec.rb +9 -6
  42. data/spec/category_fetcher_spec.rb +135 -463
  43. data/spec/cli_targeted_spec.rb +60 -0
  44. data/spec/config_spec.rb +2 -15
  45. data/spec/constants_spec.rb +0 -4
  46. data/spec/dump_fetch_spec.rb +293 -0
  47. data/spec/extraction_run_spec.rb +520 -0
  48. data/spec/index_build_spec.rb +113 -0
  49. data/spec/index_commands_version_spec.rb +17 -0
  50. data/spec/multistream_reader_spec.rb +140 -0
  51. data/spec/multistream_spec.rb +8 -214
  52. data/spec/output_integrity_spec.rb +45 -2
  53. data/spec/regex_spec.rb +30 -0
  54. data/spec/spec_helper.rb +3 -3
  55. data/spec/support/dump_status.rb +34 -0
  56. data/spec/support/multistream_fixture.rb +26 -8
  57. data/spec/template_args_spec.rb +133 -0
  58. data/spec/template_expander_spec.rb +29 -111
  59. data/spec/template_passthrough_spec.rb +17 -2
  60. data/spec/template_patterns_spec.rb +253 -0
  61. data/spec/tracked_paths_spec.rb +168 -0
  62. data/spec/utils_spec.rb +7 -1
  63. metadata +30 -5
  64. data/lib/wp2txt/category_cache.rb +0 -313
  65. data/spec/category_cache_spec.rb +0 -226
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "wp2txt/template_args"
5
+ require "wp2txt/dump_snapshot"
6
+
7
+ RSpec.describe Wp2txt::TemplateArgs do
8
+ def args(*parts)
9
+ described_class.parse(parts)
10
+ end
11
+
12
+ it "numbers the unnamed arguments from 1" do
13
+ expect(args("de", "Wort").positional).to eq(%w[de Wort])
14
+ end
15
+
16
+ it "takes explicit numbers as the numbered arguments they name" do
17
+ expect(args("1=de", "2=Wort").positional).to eq(%w[de Wort])
18
+ end
19
+
20
+ it "lets a later argument replace an earlier one of the same number" do
21
+ expect(args("1=a", "b")[1]).to eq("b")
22
+ expect(args("a", "1=b")[1]).to eq("b")
23
+ end
24
+
25
+ it "keeps names in any script and their case" do
26
+ a = args("表示=語", "Title=One", "title=Two")
27
+ expect(a["表示"]).to eq("語")
28
+ expect(a["Title"]).to eq("One")
29
+ expect(a["title"]).to eq("Two")
30
+ expect(a.positional).to be_empty
31
+ end
32
+
33
+ it "does not split at an equals sign inside nested markup" do
34
+ a = args("[[a|b=c]]", "{{x|y=z}}")
35
+ expect(a.positional).to eq(["[[a|b=c]]", "{{x|y=z}}"])
36
+ end
37
+
38
+ it "tells an empty argument from a missing one" do
39
+ a = args("", "label=")
40
+ expect(a[1]).to eq("")
41
+ expect(a["label"]).to eq("")
42
+ expect(a[2]).to be_nil
43
+ expect(a.key?("label")).to be(true)
44
+ end
45
+
46
+ it "keeps unnamed arguments as written and trims named ones" do
47
+ a = args(" X ", " name = v ")
48
+ expect(a[1]).to eq(" X ")
49
+ expect(a["name"]).to eq("v")
50
+ expect(a.to_params[:positional]).to eq(["X"])
51
+ end
52
+
53
+ it "splits at an equals sign inside single brackets, as MediaWiki does" do
54
+ expect(args("x[y=z")["x[y"]).to eq("z")
55
+ end
56
+
57
+ it "does not make a list as long as a large explicit number" do
58
+ a = args("a", "100000=x")
59
+ expect(a.positional).to eq(["a"])
60
+ expect(a.positional_count).to eq(1)
61
+ expect(a["100000"]).to eq("x")
62
+ end
63
+
64
+ it "holds the equals signs a template shows, but not those of a template still to be expanded" do
65
+ expect(described_class.literal_equals("a=b {{x|c=d}} e=f", "#")).to eq("a#b {{x|c=d}} e#f")
66
+ expect(described_class.literal_equals("a=b", nil)).to eq("a=b")
67
+ end
68
+
69
+ it "reads a skipped number as empty in the handlers' shape" do
70
+ expect(args("3=c").to_params[:positional]).to eq(["", "", "c"])
71
+ end
72
+ end
73
+
74
+ RSpec.describe "templates in text" do
75
+ let(:renderer) { Object.new.extend(Wp2txt) }
76
+
77
+ it "reads {{!}} and {{=}} as the characters they stand for, after splitting arguments" do
78
+ expect(renderer.format_wiki("a {{=}} b, {{lang|ja|x{{=}}y}}, c{{!}}d", expand_templates: true, title: "T"))
79
+ .to eq("a = b, x=y, c|d")
80
+ end
81
+
82
+ it "leaves characters of the input that look like its own placeholders as they are" do
83
+ expect(renderer.format_wiki("A\u{E0A1}B\u{E0A2}C", expand_templates: true, title: "T")).to eq("A\u{E0A1}B\u{E0A2}C")
84
+ end
85
+
86
+ it "does not take noncharacters in the input for its own placeholders" do
87
+ expect(renderer.format_wiki("A\u{FDD0}B\u{FDD1}C", expand_templates: true, title: "T")).to eq("A\u{FFFD}B\u{FFFD}C")
88
+ end
89
+
90
+ it "does not take character references to noncharacters for its own placeholders" do
91
+ expect(renderer.format_wiki("ABC", expand_templates: true, title: "T")).to eq("A\u{FFFD}B\u{FFFD}C")
92
+ end
93
+
94
+ it "keeps an equals sign that a template, function, or page name shows inside another template" do
95
+ config = { expand_templates: true, title: "A=B" }
96
+ expect(renderer.format_wiki("x{{Unknown template|{{PAGENAME}}}}y", config)).to eq("xA=By")
97
+ expect(renderer.format_wiki("x{{Unknown template|{{#if:1|C=D|E}}}}y", config)).to eq("xC=Dy")
98
+ expect(renderer.format_wiki("x{{Unknown template|{{uc:c=d}}}}y", config)).to eq("xC=Dy")
99
+ expect(renderer.format_wiki("x{{Unknown template|{{plural:1|C=D|E}}}}y", config)).to eq("xC=Dy")
100
+ end
101
+
102
+ it "keeps an equals sign a page name shows in a branch that a function chooses" do
103
+ config = { expand_templates: true, title: "A=B" }
104
+ expect(renderer.format_wiki("x{{#if:1|{{Unknown template|{{PAGENAME}}}}|C}}y", config)).to eq("xA=By")
105
+ expect(renderer.format_wiki("x{{#switch:z|z={{Unknown template|{{PAGENAME}}}}}}y", config)).to eq("xA=By")
106
+ end
107
+
108
+ it "compares and encodes a page name with its equals sign as written" do
109
+ config = { expand_templates: true, title: "A=B" }
110
+ expect(renderer.format_wiki("{{#ifeq:{{PAGENAME}}|A=B|yes|no}}", config)).to eq("yes")
111
+ expect(renderer.format_wiki("{{#switch:{{PAGENAME}}|A=B|x=no|#default=yes}}", config)).to eq("yes")
112
+ expect(renderer.format_wiki("{{urlencode:{{PAGENAME}}}}", config)).to eq("A%3DB")
113
+ end
114
+
115
+ it "reads the arguments of a template left inside another as that template's own" do
116
+ config = { expand_templates: true, title: "T" }
117
+ expect(renderer.format_wiki("{{lang|en|{{Unknown template|mode=quiet|answer}}}}", config)).to eq("answer")
118
+ end
119
+
120
+ it "counts ages from the dump's date, not from the day of the run" do
121
+ config = { expand_templates: true, title: "T", dump_date: Time.utc(2026, 11, 1) }
122
+ expect(renderer.format_wiki("{{age|2000|1|1}}", config)).to eq("26")
123
+ end
124
+ end
125
+
126
+ RSpec.describe Wp2txt::DumpSnapshot do
127
+ it "reads the wiki and the date from a Wikimedia dump file's name" do
128
+ expect(described_class.name_info("/x/jawiki-20261101-pages-articles-multistream.xml.bz2")).to eq(%w[ja 20261101])
129
+ expect(described_class.name_info("zh_yuewiki-20261101-pages-articles.xml.bz2")).to eq(%w[zh-yue 20261101])
130
+ expect(described_class.name_info("articles.xml")).to be_nil
131
+ expect(described_class.time_of("20261101")).to eq(Time.utc(2026, 11, 1))
132
+ end
133
+ end
@@ -9,6 +9,17 @@ RSpec.describe Wp2txt::TemplateExpander do
9
9
  let(:expander_with_date) { described_class.new(reference_date: reference_date) }
10
10
 
11
11
  describe "date templates" do
12
+ it "keeps a date written in words as written" do
13
+ expect(expander.expand("{{date|5|mars|1986}}")).to eq("5 mars 1986")
14
+ expect(expander.expand("{{birth date|5 octobre 1979}}")).to eq("5 octobre 1979")
15
+ expect(expander_with_date.expand("{{birth date and age|1990|May|15}}")).to eq("1990 May 15")
16
+ expect(expander_with_date.expand("{{death date and age|2020|May|3|2000|May|4}}")).to eq("2020 May 3")
17
+ end
18
+
19
+ it "shows an equals sign in what the cleaning stage renders as the character itself" do
20
+ expect(expander.expand("{{ruby|a=b|c}}")).to eq("a=b(c)")
21
+ end
22
+
12
23
  describe "{{birth date}}" do
13
24
  it "expands {{birth date|1990|5|15}} to formatted date" do
14
25
  expect(expander.expand("{{birth date|1990|5|15}}")).to eq("May 15, 1990")
@@ -134,115 +145,23 @@ RSpec.describe Wp2txt::TemplateExpander do
134
145
  end
135
146
 
136
147
  describe "convert templates" do
137
- describe "length conversions" do
138
- it "converts km to mi" do
139
- result = expander.expand("{{convert|100|km|mi}}")
140
- expect(result).to match(/100 km \(6[0-9](\.[0-9])? mi\)/)
141
- end
142
-
143
- it "converts mi to km" do
144
- result = expander.expand("{{convert|100|mi|km}}")
145
- expect(result).to match(/100 mi \(16[0-9](\.[0-9])? km\)/)
146
- end
147
-
148
- it "converts m to ft" do
149
- result = expander.expand("{{convert|100|m|ft}}")
150
- expect(result).to match(/100 m \(32[0-9](\.[0-9])? ft\)/)
151
- end
152
-
153
- it "converts ft to m" do
154
- result = expander.expand("{{convert|100|ft|m}}")
155
- expect(result).to match(/100 ft \(30(\.[0-9])? m\)/)
156
- end
157
-
158
- it "converts cm to in" do
159
- result = expander.expand("{{convert|100|cm|in}}")
160
- expect(result).to match(/100 cm \(39(\.[0-9])? in\)/)
161
- end
162
-
163
- it "converts in to cm" do
164
- result = expander.expand("{{convert|10|in|cm}}")
165
- expect(result).to match(/10 in \(25(\.[0-9])? cm\)/)
166
- end
167
- end
168
-
169
- describe "weight conversions" do
170
- it "converts kg to lb" do
171
- result = expander.expand("{{convert|100|kg|lb}}")
172
- expect(result).to match(/100 kg \(22[0-9](\.[0-9])? lb\)/)
173
- end
174
-
175
- it "converts lb to kg" do
176
- result = expander.expand("{{convert|100|lb|kg}}")
177
- expect(result).to match(/100 lb \(4[0-9](\.[0-9])? kg\)/)
178
- end
179
-
180
- it "converts g to oz" do
181
- result = expander.expand("{{convert|100|g|oz}}")
182
- expect(result).to match(/100 g \(3\.[0-9] oz\)/)
183
- end
184
- end
185
-
186
- describe "temperature conversions" do
187
- it "converts °C to °F" do
188
- expect(expander.expand("{{convert|0|°C|°F}}")).to eq("0 °C (32 °F)")
189
- end
190
-
191
- it "converts C to F (without degree symbol)" do
192
- expect(expander.expand("{{convert|100|C|F}}")).to eq("100 °C (212 °F)")
193
- end
194
-
195
- it "converts °F to °C" do
196
- expect(expander.expand("{{convert|32|°F|°C}}")).to eq("32 °F (0 °C)")
197
- end
198
-
199
- it "converts F to C (without degree symbol)" do
200
- expect(expander.expand("{{convert|212|F|C}}")).to eq("212 °F (100 °C)")
201
- end
202
- end
203
-
204
- describe "area conversions" do
205
- it "converts km2 to sqmi" do
206
- result = expander.expand("{{convert|100|km2|sqmi}}")
207
- expect(result).to match(/100 km² \(3[0-9](\.[0-9])? sq mi\)/)
208
- end
209
-
210
- it "converts sqmi to km2" do
211
- result = expander.expand("{{convert|100|sqmi|km2}}")
212
- expect(result).to match(/100 sq mi \(25[0-9](\.[0-9])? km²\)/)
213
- end
214
-
215
- it "converts ha to acre" do
216
- result = expander.expand("{{convert|100|ha|acre}}")
217
- expect(result).to match(/100 ha \(24[0-9](\.[0-9])? acres\)/)
218
- end
219
- end
220
-
221
- describe "speed conversions" do
222
- it "converts km/h to mph" do
223
- result = expander.expand("{{convert|100|km/h|mph}}")
224
- expect(result).to match(/100 km\/h \(6[0-9](\.[0-9])? mph\)/)
225
- end
226
-
227
- it "converts mph to km/h" do
228
- result = expander.expand("{{convert|60|mph|km/h}}")
229
- expect(result).to match(/60 mph \(9[0-9](\.[0-9])? km\/h\)/)
230
- end
231
- end
232
-
233
- describe "edge cases" do
234
- it "handles decimal values" do
235
- result = expander.expand("{{convert|3.5|km|mi}}")
236
- expect(result).to match(/3\.5 km \(2\.[0-9] mi\)/)
237
- end
238
-
239
- it "handles unknown units gracefully" do
240
- expect(expander.expand("{{convert|100|foo|bar}}")).to eq("100 foo")
241
- end
242
-
243
- it "handles abbr=on parameter" do
244
- result = expander.expand("{{convert|100|km|mi|abbr=on}}")
245
- expect(result).to match(/100 km \(6[0-9](\.[0-9])? mi\)/)
148
+ # The template computes the conversion; the values and unit as written are kept
149
+ {
150
+ "{{convert|100|km|mi}}" => "100 km",
151
+ "{{convert|3.5|km|mi}}" => "3.5 km",
152
+ "{{convert|0|°C|°F}}" => "0 °C",
153
+ "{{convert|100|C|F}}" => "100 °C",
154
+ "{{convert|100|km2|sqmi}}" => "100 km²",
155
+ "{{convert|60|mph|km/h|abbr=on}}" => "60 mph",
156
+ "{{convert|100|foo|bar}}" => "100 foo",
157
+ "{{convert|5|-|10|mi|km}}" => "5–10 mi",
158
+ "{{convert|16|to|17|cm|in}}" => "16 to 17 cm",
159
+ "{{convert|2|x|4|m|ft}}" => "2 × 4 m",
160
+ "{{cvt|1=12|2=kg}}" => "12 kg",
161
+ "{{convert|1,200}}" => "1,200"
162
+ }.each do |call, written|
163
+ it "renders #{call} as #{written}" do
164
+ expect(expander.expand(call)).to eq(written)
246
165
  end
247
166
  end
248
167
  end
@@ -465,8 +384,7 @@ RSpec.describe Wp2txt::TemplateExpander do
465
384
  it "expands convert templates" do
466
385
  input = "The mountain is {{convert|8848|m|ft}} tall."
467
386
  result = format_wiki(input, title: "Test", expand_templates: true)
468
- expect(result).to include("8848 m")
469
- expect(result).to include("ft")
387
+ expect(result).to eq("The mountain is 8848 m tall.")
470
388
  end
471
389
  end
472
390
  end
@@ -33,8 +33,23 @@ RSpec.describe "templates rendered by the cleaning stage" do
33
33
  expect(summary_of(source)).to include("ナラム・シン。画像は戦勝記念碑。")
34
34
  end
35
35
 
36
- it "still removes templates nothing downstream knows" do
37
- expect(summary_of("前{{Otheruses|x|y}}後")).to eq("前後")
36
+ it "removes templates that show nothing inside a sentence" do
37
+ expect(summary_of("前{{Reflist}}{{Efn|注}}後")).to eq("前後")
38
+ expect(summary_of("前{{Harvnp|Collins|1999|pp=376}}{{Sfnm|1a1=Smith|1y=2001}}{{ref+|Сноска}}後")).to eq("前後")
39
+ end
40
+
41
+ it "keeps the first argument of a template it does not know, as the text it was given to show" do
42
+ expect(summary_of("前{{Unknown template|x|y}}後")).to eq("前x後")
43
+ expect(summary_of("前{{Unknown template|de|x}}後")).to eq("前x後")
44
+ expect(summary_of("前{{Unknown template|red|blue}}後")).to eq("前red後")
45
+ expect(summary_of("前{{Unknown template|simple|words}}後")).to eq("前words後")
46
+ expect(summary_of("前{{Unknown template|mode=a}}後")).to eq("前後")
47
+ expect(summary_of("前{{Unknown template|3=x}}後")).to eq("前x後")
48
+ expect(summary_of("前{{Unknown template|in}}後")).to eq("前in後")
49
+ end
50
+
51
+ it "does not read an equals sign shown by an inner template as naming an argument" do
52
+ expect(summary_of("前{{Unknown template|{{lang|en|a=b}}}}後")).to eq("前a=b後")
38
53
  end
39
54
 
40
55
  it "adds no empty brackets for an empty reading" do
@@ -0,0 +1,253 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "wp2txt/template_patterns"
5
+ require "wp2txt/template_expander"
6
+
7
+ RSpec.describe Wp2txt::TemplatePatterns do
8
+ let(:data) do
9
+ {
10
+ "wikis" => {
11
+ "xx" => {
12
+ "templates" => {
13
+ "Lang-de" => { "p" => "Deutsch: {1}" },
14
+ "Langx" => { "code_arg" => 1, "p" => "{2}", "by_code" => { "de" => "Deutsch: {2}", "fr" => "Französisch: {2}" } },
15
+ "Link" => { "code_arg" => 2, "p" => "{1}", "by_code" => { "de" => "{1} (deutsch)" }, "replaces" => { "label" => "1" } },
16
+ "Cite web" => { "p" => "{1}" },
17
+ "Circa" => { "p" => "c. {1} – c. {2}", "p1" => "c. {1}" },
18
+ "Reading" => { "p" => "{1} ({2})" },
19
+ "Iw" => { "code_arg" => 3, "p" => "{2}", "by_code" => { "en" => "{2} [en]" }, "fallback" => { "2" => "1" } },
20
+ "Ill" => { "p" => "{2}", "fallback" => { "2" => "1" }, "replaces" => { "label" => "2" } },
21
+ "Count" => { "p" => "THREE {1}", "p1" => "ONE {1}", "p2" => "TWO {1}" },
22
+ "Known" => { "p" => "{1}" },
23
+ "Height" => { "p" => "{1} m", "n" => 1 },
24
+ "Start date" => { "p" => "{1}", "n" => 1 },
25
+ "Lang" => { "code_arg" => 1, "p" => "{2}", "n" => 2 },
26
+ "Some langs" => { "code_arg" => 1, "p" => "{2}", "n" => 2, "codes" => %w[de grc] },
27
+ "Zh" => { "p" => "Chinese: {1}", "replaces" => { "s" => "1", "t" => "1" }, "ignores" => %w[link],
28
+ "with" => { "lit" => "Chinese: {1}, lit. {lit}", "lc" => "chinese: {1}", "sm" => "Chinese: {1}" },
29
+ "off" => { "lc" => %w[no NO] }, "shows" => %w[s t lit rm sep] },
30
+ "Auy" => { "p" => "{1}, {2}", "with" => { "bio" => "{1}" } },
31
+ "Enlink" => { "p" => "({3}:{2})", "p2" => "({2})", "with" => { "p" => "{3}:{2}" } },
32
+ "Infobox thing" => { "p" => "" },
33
+ "Infobox thing/link" => { "p" => "{1}" },
34
+ "Box" => { "p" => "" },
35
+ "Range" => { "p" => "{1} {2}{3}", "p2" => "{1} {2}", "with" => { "to" => "{1} to {to} {2}{3}", "alt" => "{alt}" },
36
+ "shows" => %w[to alt] }
37
+ },
38
+ "aliases" => { "Lde" => "Lang-de" }
39
+ }
40
+ }
41
+ }
42
+ end
43
+
44
+ around do |example|
45
+ saved = described_class.instance_variable_get(:@data)
46
+ described_class.instance_variable_set(:@data, data)
47
+ example.run
48
+ ensure
49
+ described_class.instance_variable_set(:@data, saved)
50
+ end
51
+
52
+ def render(lang, name, *parts)
53
+ definition = described_class.lookup(lang, name)
54
+ definition && described_class.render(definition, Wp2txt::TemplateArgs.parse(parts))
55
+ end
56
+
57
+ it "finds a template by its name as MediaWiki matches it, or by a name that redirects to it" do
58
+ expect(described_class.lookup("xx", "lang-de")).to eq(data.dig("wikis", "xx", "templates", "Lang-de"))
59
+ expect(described_class.lookup("xx", " cite_web ")).to eq(data.dig("wikis", "xx", "templates", "Cite web"))
60
+ expect(described_class.lookup("xx", "lde")).to eq(data.dig("wikis", "xx", "templates", "Lang-de"))
61
+ expect(described_class.lookup("xx", "Unknown")).to be_nil
62
+ expect(described_class.lookup("yy", "Lang-de")).to be_nil
63
+ end
64
+
65
+ it "puts the arguments in the pattern's slots" do
66
+ expect(render("xx", "Lang-de", "Wort")).to eq("Deutsch: Wort")
67
+ end
68
+
69
+ it "uses the pattern for the language code given, and the plain one for other codes" do
70
+ expect(render("xx", "Langx", "fr", "mot")).to eq("Französisch: mot")
71
+ expect(render("xx", "Langx", "DE", "Wort")).to eq("Deutsch: Wort")
72
+ expect(render("xx", "Langx", "qq", "x")).to eq("x")
73
+ end
74
+
75
+ it "uses the pattern for as many arguments as the call gives" do
76
+ expect(render("xx", "Circa", "1550")).to eq("c. 1550")
77
+ expect(render("xx", "Circa", "1550", "1560")).to eq("c. 1550 – c. 1560")
78
+ end
79
+
80
+ it "does not render a call with more arguments than the pattern was learned with" do
81
+ expect(render("xx", "Height", "185")).to eq("185 m")
82
+ expect(render("xx", "Height", "29", "4", "2000")).to be_nil
83
+ expect(render("xx", "Height", "Ada")).to be_nil
84
+ expect(render("xx", "Lang", "grc", "λόγος")).to eq("λόγος")
85
+ expect(render("xx", "Lang", "grc", "λόγος", "logos")).to be_nil
86
+ end
87
+
88
+ it "renders a code pattern learned from n arguments only for calls of that shape and its codes" do
89
+ expect(render("xx", "Lang", "grc")).to be_nil
90
+ expect(render("xx", "Lang", "grc", "λόγος", "italic=no")).to be_nil
91
+ expect(render("xx", "Lang", "grc", "")).to be_nil
92
+ expect(render("xx", "Lang", "2=λόγος")).to be_nil
93
+ expect(render("xx", "Some langs", "GRC", "λόγος")).to eq("λόγος")
94
+ expect(render("xx", "Some langs", "fr", "mot")).to be_nil
95
+ end
96
+
97
+ describe "named arguments" do
98
+ it "uses the pattern where the named arguments change nothing, stand in for an argument, or are not known" do
99
+ expect(render("xx", "Zh", "字", "link=no")).to eq("Chinese: 字")
100
+ expect(render("xx", "Zh", "s=字")).to eq("Chinese: 字")
101
+ expect(render("xx", "Zh", "字", "lit=")).to eq("Chinese: 字")
102
+ expect(render("xx", "Zh", "字", "mode=x")).to eq("Chinese: 字")
103
+ end
104
+
105
+ it "uses the pattern learned with a named argument where the call gives no other, its last value" do
106
+ expect(render("xx", "Zh", "字", "lit=word")).to eq("Chinese: 字, lit. word")
107
+ expect(render("xx", "Zh", "字", "lit=old", "lit=new", "link=no")).to eq("Chinese: 字, lit. new")
108
+ expect(render("xx", "Zh", "字", "lit=word", "mode=x")).to eq("Chinese: 字, lit. word")
109
+ end
110
+
111
+ it "uses the pattern learned with a named argument for a call of fewer arguments, where it has their slots" do
112
+ expect(render("xx", "Range", "252", "to=381", "m")).to eq("252 to 381 m")
113
+ expect(render("xx", "Range", "2018", "alt=in 2018")).to eq("in 2018")
114
+ end
115
+
116
+ it "reads a setting given a value the template reads as off as off" do
117
+ expect(render("xx", "Zh", "字", "lc=no")).to eq("Chinese: 字")
118
+ expect(render("xx", "Zh", "字", "lc=NO")).to eq("Chinese: 字")
119
+ expect(render("xx", "Zh", "字", "lc=yes")).to eq("chinese: 字")
120
+ end
121
+
122
+ it "follows the pattern with the values shown, each once, where no pattern has the named arguments given" do
123
+ expect(render("xx", "Zh", "字", "rm=zi", "lit=word")).to eq("Chinese: 字 zi word")
124
+ expect(render("xx", "Zh", "s=字", "lit=word")).to eq("Chinese: 字 word")
125
+ expect(render("xx", "Zh", "s=字", "rm=字")).to eq("Chinese: 字")
126
+ end
127
+
128
+ it "keeps each value given in place of the same argument" do
129
+ expect(render("xx", "Zh", "s=字", "t=體")).to eq("Chinese: 字 體")
130
+ expect(render("xx", "Zh", "s=字", "t=體", "rm=zi")).to eq("Chinese: 字 體 zi")
131
+ expect(render("xx", "Zh", "s=字", "t=[[字]]")).to eq("Chinese: 字")
132
+ end
133
+
134
+ it "does not add a value that holds no word or number" do
135
+ expect(render("xx", "Zh", "字", "rm=zi", "sep=、")).to eq("Chinese: 字 zi")
136
+ end
137
+
138
+ it "does not use a pattern learned with a named argument that leaves out an argument without showing its own" do
139
+ expect(render("xx", "Auy", "Vent.", "1807", "bio=bot")).to eq("Vent., 1807")
140
+ end
141
+
142
+ it "does not use a pattern learned with a named argument where an argument the call leaves empty is joined to its text" do
143
+ expect(render("xx", "Enlink", "Resident (title)", "Resident", "p=off")).to eq("(Resident)")
144
+ expect(render("xx", "Enlink", "Handwerksordnung", "Gesetz", "de", "p=off")).to eq("de:Gesetz")
145
+ end
146
+
147
+ it "uses the pattern for settings given together that show no value of their own" do
148
+ expect(render("xx", "Zh", "字", "lc=yes", "sm=y")).to eq("Chinese: 字")
149
+ end
150
+
151
+ it "does not render a code pattern learned from n arguments for a named argument not known to change nothing" do
152
+ expect(render("xx", "Lang", "grc", "λόγος", "italic=no")).to be_nil
153
+ end
154
+
155
+ it "shows nothing for a template shown only as blocks, whatever it is given" do
156
+ expect(render("xx", "Box", "x", "style=y")).to eq("")
157
+ end
158
+ end
159
+
160
+ it "does not take a number for a language code" do
161
+ expect(render("xx", "Langx", "2000", "5")).to be_nil
162
+ expect(render("xx", "Langx", "de", "Wort")).to eq("Deutsch: Wort")
163
+ end
164
+
165
+ it "leaves out brackets that hold no argument" do
166
+ expect(render("xx", "Reading", "東京")).to eq("東京")
167
+ expect(render("xx", "Reading", "東京", "とうきょう")).to eq("東京 (とうきょう)")
168
+ end
169
+
170
+ it "shows a named label in place of the argument it replaces" do
171
+ expect(render("xx", "Link", "Title", "de", "Titel")).to eq("Title (deutsch)")
172
+ expect(render("xx", "Link", "Title", "de", "Titel", "label=Shown")).to eq("Shown (deutsch)")
173
+ expect(render("xx", "Link", "Title", "de", "Titel", "label=")).to eq("Title (deutsch)")
174
+ end
175
+
176
+ it "shows another argument in place of one left empty, as the template does" do
177
+ expect(render("xx", "Iw", "Title", "Label", "en", "Other")).to eq("Label [en]")
178
+ expect(render("xx", "Iw", "Title", "", "en", "Other")).to eq("Title [en]")
179
+ expect(render("xx", "Iw", "Title")).to eq("Title")
180
+ end
181
+
182
+ it "prefers a named label to the argument it replaces, even where that argument is left empty" do
183
+ expect(render("xx", "Ill", "Title", "", "label=Shown")).to eq("Shown")
184
+ expect(render("xx", "Ill", "Title", "")).to eq("Title")
185
+ end
186
+
187
+ it "keeps brackets and spaces that an argument holds" do
188
+ expect(render("xx", "Known", "()")).to eq("()")
189
+ expect(render("xx", "Reading", "a b", "")).to eq("a b")
190
+ end
191
+
192
+ it "keeps the spaces around an unnamed argument" do
193
+ expect(render("xx", "Lang-de", " Wort ")).to eq("Deutsch: Wort ")
194
+ end
195
+
196
+ describe "with templates inside arguments" do
197
+ it "keeps an equals sign an inner template shows as text" do
198
+ expect(Wp2txt::TemplateExpander.new(lang: "xx", literal_equals: "\u{FDD1}").expand("{{Known|{{Known|1=a=b}}}}"))
199
+ .to eq("a\u{FDD1}b")
200
+ end
201
+ end
202
+
203
+ it "reads the data the same way from any Ractor" do
204
+ expect(Ractor.shareable?(described_class::DATA)).to be(true)
205
+ end
206
+
207
+ describe "in the template expander" do
208
+ it "counts an empty last argument as given" do
209
+ expect(Wp2txt::TemplateExpander.new(lang: "xx").expand("{{Count|x|}}")).to eq("TWO x")
210
+ end
211
+
212
+ it "chooses the pattern for a language code given by a template in the argument" do
213
+ expect(Wp2txt::TemplateExpander.new(lang: "xx").expand("{{Langx|{{Known|de}}|Wort}}")).to eq("Deutsch: Wort")
214
+ end
215
+
216
+ it "renders known templates of the dump's edition" do
217
+ expect(Wp2txt::TemplateExpander.new(lang: "xx").expand("a {{lang-de|Wort}} b")).to eq("a Deutsch: Wort b")
218
+ end
219
+
220
+ it "keeps citations removed, whatever the patterns say" do
221
+ expect(Wp2txt::TemplateExpander.new(lang: "xx").expand("a{{Cite web|x}}b")).to eq("ab")
222
+ end
223
+
224
+ it "leaves a template shown as a block marker to the markers" do
225
+ expect(Wp2txt::TemplateExpander.new(lang: "xx", preserve_unknown: true).expand("a{{Infobox thing|name=x}}b"))
226
+ .to eq("a{{Infobox thing|name=x}}b")
227
+ end
228
+
229
+ it "uses the pattern of a template named like a block that shows text, and of one holding a block" do
230
+ expander = Wp2txt::TemplateExpander.new(lang: "xx", preserve_unknown: true)
231
+ expect(expander.expand("a{{Infobox thing/link|Album}}b")).to eq("aAlbumb")
232
+ expect(expander.expand("a{{Auy|Vent.|1807|note={{Infobox thing}}}}b")).to eq("aVent., 1807b")
233
+ end
234
+
235
+ it "leaves a call the pattern does not tell to the other rules" do
236
+ expander = Wp2txt::TemplateExpander.new(lang: "xx", preserve_unknown: true)
237
+ expect(expander.expand("a {{Height|29|4|2000}} b")).to eq("a {{Height|29|4|2000}} b")
238
+ end
239
+
240
+ it "gives the cleaner the arguments as expanded" do
241
+ expect(Wp2txt::TemplateExpander.new(lang: "xx", preserve_unknown: true).expand("{{Height|{{Known|29}}|x}}"))
242
+ .to eq("{{Height|29|x}}")
243
+ end
244
+
245
+ it "gives the other rules the arguments as expanded" do
246
+ expect(Wp2txt::TemplateExpander.new(lang: "xx").expand("{{Start date|{{Known|2000}}|5|3}}")).to eq("May 3, 2000")
247
+ end
248
+
249
+ it "uses its own handlers when the edition is not known" do
250
+ expect(Wp2txt::TemplateExpander.new.expand("{{lang-de|Wort}}")).to eq("German: Wort")
251
+ end
252
+ end
253
+ end