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,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "digest"
|
|
5
|
+
|
|
6
|
+
# A dumpstatus.json as Wikimedia publishes it for one dump date, describing
|
|
7
|
+
# the multistream index and dump file (size and SHA-1).
|
|
8
|
+
module DumpStatusFixture
|
|
9
|
+
def dump_status_json(lang, date, index: "index", dump: "dump", status: "done")
|
|
10
|
+
prefix = "#{lang}wiki-#{date}-pages-articles-multistream"
|
|
11
|
+
file = lambda do |name, content|
|
|
12
|
+
{ name => { "size" => content.bytesize, "url" => "/#{lang}wiki/#{date}/#{name}",
|
|
13
|
+
"md5" => Digest::MD5.hexdigest(content), "sha1" => Digest::SHA1.hexdigest(content) } }
|
|
14
|
+
end
|
|
15
|
+
JSON.generate(
|
|
16
|
+
"jobs" => {
|
|
17
|
+
"articlesmultistreamdumprecombine" => {
|
|
18
|
+
"status" => status,
|
|
19
|
+
"files" => file.("#{prefix}-index.txt.bz2", index).merge(file.("#{prefix}.xml.bz2", dump))
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"version" => "0.8"
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Answer every dumpstatus.json request with a finished status for the
|
|
27
|
+
# language and date in its URL
|
|
28
|
+
def stub_dump_status(index: "index", dump: "dump")
|
|
29
|
+
stub_request(:get, %r{dumps\.wikimedia\.org/\w+wiki/\d{8}/dumpstatus\.json}).to_return do |request|
|
|
30
|
+
lang, date = request.uri.path.match(%r{/(\w+)wiki/(\d{8})/})&.captures
|
|
31
|
+
{ status: 200, body: dump_status_json(lang, date, index: index, dump: dump) }
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
require "open3"
|
|
4
4
|
|
|
5
|
-
# Synthetic
|
|
6
|
-
#
|
|
5
|
+
# Synthetic multistream dump used by metadata index / corpus specs, laid out
|
|
6
|
+
# like a Wikimedia dump: a header stream (siteinfo), stream 1 with articles
|
|
7
|
+
# (ns 0), stream 2 with category pages (ns 14), and a footer stream.
|
|
7
8
|
module MultistreamFixture
|
|
8
|
-
def page_xml(id:, ns:, title:, text:)
|
|
9
|
+
def page_xml(id:, ns:, title:, text:, redirect: nil)
|
|
9
10
|
escaped = text.gsub("&", "&").gsub("<", "<").gsub(">", ">")
|
|
11
|
+
redirect_line = redirect ? %(\n <redirect title="#{redirect}" />) : ""
|
|
10
12
|
<<~XML
|
|
11
13
|
<page>
|
|
12
14
|
<title>#{title}</title>
|
|
13
15
|
<ns>#{ns}</ns>
|
|
14
|
-
<id>#{id}</id
|
|
16
|
+
<id>#{id}</id>#{redirect_line}
|
|
15
17
|
<revision>
|
|
16
18
|
<id>#{id * 100}</id>
|
|
17
19
|
<text bytes="#{text.bytesize}">#{escaped}</text>
|
|
@@ -20,6 +22,20 @@ module MultistreamFixture
|
|
|
20
22
|
XML
|
|
21
23
|
end
|
|
22
24
|
|
|
25
|
+
FIXTURE_HEADER = <<~XML
|
|
26
|
+
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.11/" version="0.11" xml:lang="en">
|
|
27
|
+
<siteinfo>
|
|
28
|
+
<sitename>Wikipedia</sitename>
|
|
29
|
+
<dbname>testwiki</dbname>
|
|
30
|
+
<case>first-letter</case>
|
|
31
|
+
<namespaces>
|
|
32
|
+
<namespace key="0" case="first-letter" />
|
|
33
|
+
<namespace key="14" case="first-letter">Category</namespace>
|
|
34
|
+
</namespaces>
|
|
35
|
+
</siteinfo>
|
|
36
|
+
XML
|
|
37
|
+
FIXTURE_FOOTER = "</mediawiki>\n"
|
|
38
|
+
|
|
23
39
|
def bzip2(data)
|
|
24
40
|
out, status = Open3.capture2("bzip2", "-c", stdin_data: data)
|
|
25
41
|
raise "bzip2 failed" unless status.success?
|
|
@@ -37,7 +53,7 @@ module MultistreamFixture
|
|
|
37
53
|
page_xml(id: 3, ns: 0, title: "Person X",
|
|
38
54
|
text: "Bio.\n== Career == <!-- legacy anchor -->\nActing.\n== '''Style''' ==\nDistinct prose.\n<!-- [[Category:Hidden]] -->\n[[Category:Japanese actors]]\n"),
|
|
39
55
|
page_xml(id: 4, ns: 0, title: "Old Film",
|
|
40
|
-
text: "#REDIRECT [[Film A]]\n[[Category:Japanese films]]\n")
|
|
56
|
+
text: "#REDIRECT [[Film A]]\n[[Category:Japanese films]]\n", redirect: "Film A")
|
|
41
57
|
]
|
|
42
58
|
stream2_pages = [
|
|
43
59
|
page_xml(id: 5, ns: 14, title: "Category:Japanese films", text: "[[Category:Films]]\n"),
|
|
@@ -46,15 +62,17 @@ module MultistreamFixture
|
|
|
46
62
|
page_xml(id: 8, ns: 14, title: "Category:Japanese actors", text: "[[Category:People]]\n")
|
|
47
63
|
]
|
|
48
64
|
|
|
65
|
+
header = bzip2(FIXTURE_HEADER)
|
|
49
66
|
stream1 = bzip2(stream1_pages.join)
|
|
50
67
|
stream2 = bzip2(stream2_pages.join)
|
|
51
68
|
|
|
52
69
|
multistream_path = File.join(dir, "testwiki-20260101-pages-articles-multistream.xml.bz2")
|
|
53
|
-
File.binwrite(multistream_path, stream1 + stream2)
|
|
70
|
+
File.binwrite(multistream_path, header + stream1 + stream2 + bzip2(FIXTURE_FOOTER))
|
|
54
71
|
|
|
55
|
-
|
|
72
|
+
offset1 = header.bytesize
|
|
73
|
+
offset2 = offset1 + stream1.bytesize
|
|
56
74
|
index_lines = [
|
|
57
|
-
"
|
|
75
|
+
"#{offset1}:1:Film A", "#{offset1}:2:Film B", "#{offset1}:3:Person X", "#{offset1}:4:Old Film",
|
|
58
76
|
"#{offset2}:5:Category:Japanese films", "#{offset2}:6:Category:French films",
|
|
59
77
|
"#{offset2}:7:Category:Films", "#{offset2}:8:Category:Japanese actors"
|
|
60
78
|
]
|
|
@@ -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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
|
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
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "wp2txt"
|
|
5
|
+
require "wp2txt/formatter"
|
|
6
|
+
|
|
7
|
+
# Templates the cleaning stage renders itself must survive template expansion;
|
|
8
|
+
# deleting them earlier silently dropped readings and link text from articles.
|
|
9
|
+
RSpec.describe "templates rendered by the cleaning stage" do
|
|
10
|
+
include Wp2txt
|
|
11
|
+
include Wp2txt::Formatter
|
|
12
|
+
|
|
13
|
+
def summary_of(wikitext)
|
|
14
|
+
article = Wp2txt::Article.new("#{wikitext}\n\n== 節 ==\n本文\n", "T", true)
|
|
15
|
+
format_article(article, { format: :json, summary_only: true, expand_templates: true,
|
|
16
|
+
markers: [:all] })["text"].strip
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
{
|
|
20
|
+
"'''{{読み仮名|言語|げんご}}'''は記号体系。" => "言語(げんご)は記号体系。",
|
|
21
|
+
"{{読み仮名_ruby不使用|東京|とうきょう}}都" => "東京(とうきょう)都",
|
|
22
|
+
"作曲は{{仮リンク|ジョン・ドウ|en|John Doe}}が担当した。" => "作曲はジョン・ドウが担当した。",
|
|
23
|
+
"{{ruby|漢字|かんじ}}を読む。" => "漢字(かんじ)を読む。",
|
|
24
|
+
"作曲は{{仮リンク|{{lang|en|John Doe}}|en|John Doe}}。" => "作曲はJohn Doe。"
|
|
25
|
+
}.each do |source, expected|
|
|
26
|
+
it "renders #{source.inspect}" do
|
|
27
|
+
expect(summary_of(source)).to eq(expected)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "keeps an image caption that contains a link template" do
|
|
32
|
+
source = "[[ファイル:Stele.jpg|thumb|200px|[[ナラム・シン]]。画像は{{仮リンク|戦勝記念碑|en|Victory Stele}}。]]\n'''紀元前23世紀'''は世紀。"
|
|
33
|
+
expect(summary_of(source)).to include("ナラム・シン。画像は戦勝記念碑。")
|
|
34
|
+
end
|
|
35
|
+
|
|
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後")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "adds no empty brackets for an empty reading" do
|
|
56
|
+
expect(summary_of("{{読み仮名|語|}}は")).to eq("語は")
|
|
57
|
+
end
|
|
58
|
+
end
|