IANA language subtag registry: make registry download & parsing reusable

Part of the implementation of #2919.
This commit is contained in:
Moritz Bunkus 2020-09-06 19:51:58 +02:00
parent 83988c2eae
commit 19e61a2d2e
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
2 changed files with 71 additions and 63 deletions

View File

@ -870,7 +870,7 @@ namespace :dev do
desc "Create iana_language_subtag_registry_list.cpp from official list"
task :iana_language_subtag_registry_list do
create_iana_language_subtag_registry_list_file
Mtx::IANALanguageSubtagRegistry.create_cpp
end
end

View File

@ -1,11 +1,10 @@
def create_iana_language_subtag_registry_list_file
txt_file = "language-subtag-registry.txt"
module Mtx::IANALanguageSubtagRegistry
def self.with_registry &func
url = "https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry"
cpp_file_name = "src/common/iana_language_subtag_registry_list.cpp"
registry = "language-subtag-registry"
download_registry = !FileTest.exists?(registry)
File.unlink(txt_file) if FileTest.exists?(txt_file)
runq "wget", url, "wget --quiet -O #{txt_file} #{url}"
runq "wget", url, "wget --quiet -O #{registry} #{url}" if download_registry
entry = {}
entries = {}
@ -20,7 +19,7 @@ def create_iana_language_subtag_registry_list_file
entry = {}
end
IO.readlines(txt_file).
IO.readlines(registry).
map(&:chomp).
each do |line|
@ -38,7 +37,15 @@ def create_iana_language_subtag_registry_list_file
process.call
formatter = lambda do |entry|
func.call(entries)
ensure
File.unlink(registry) if download_registry && FileTest.exists?(registry)
end
def self.do_create_cpp entries
cpp_file_name = "src/common/iana_language_subtag_registry_list.cpp"
entry_formatter = lambda do |entry|
if entry[:prefix]
prefix = '{ ' + entry[:prefix].sort.map(&:to_cpp_string).join(', ') + ' }'
else
@ -51,17 +58,18 @@ def create_iana_language_subtag_registry_list_file
]
end
rows = entries["extlang"].map(&formatter)
formatter = lambda do |type, name|
rows = entries[type].map(&entry_formatter)
extlangs = "std::vector<entry_t> const g_extlangs{\n" +
"std::vector<entry_t> const g_#{name}{\n" +
format_table(rows.sort, :column_suffix => ',', :row_prefix => " { ", :row_suffix => " },").join("\n") +
"\n};\n"
end
rows = entries["variant"].map(&formatter)
variants = "std::vector<entry_t> const g_variants{\n" +
format_table(rows.sort, :column_suffix => ',', :row_prefix => " { ", :row_suffix => " },").join("\n") +
"\n};\n"
formatted = [
formatter.call("extlang", "extlangs"),
formatter.call("variant", "variants"),
]
header = <<EOT
/*
@ -95,13 +103,13 @@ EOT
EOT
content = header +
extlangs +
"\n" +
variants +
formatted.join("\n") +
footer
runq("write", cpp_file_name) { IO.write("#{$source_dir}/#{cpp_file_name}", content); 0 }
ensure
File.unlink(txt_file) if FileTest.exists?(txt_file)
end
def self.create_cpp
self.with_registry { |entries| do_create_cpp(entries) }
end
end