Module:User:Erutuon/sort test
Appearance
- The following documentation is located at Module:User:Erutuon/sort test/documentation. [edit]
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
Lua error at line 44: attempt to call field 'shallowcopy' (a nil value)
local export = {}
local function parse_replacements(replacements_string)
local replacements = {}
local i = 1
for line in replacements_string:gmatch "[^\n]+" do
local from, to = line:match("^(%S+)%s+(.+)$")
i = i + 1
if not from then
error("Line " .. i .. " didn't consist of a string and a replacement separated by a tab")
end
replacements[from] = to
end
return replacements
end
local default_sort_key_replacements = {}
default_sort_key_replacements.hu = parse_replacements [[
a a①
e e①
i i①
o o①
u u①
á a②
é e②
í i②
ó o②
ú u②
ö o③
ü u③
ő ö④
ű ü④
]]
local function make_sort_key(replacements, str)
for k, v in pairs(replacements) do
str = mw.ustring.gsub(str, k, v)
end
return str
end
function export.main(frame)
local words = require "Module:table".shallowcopy(frame.args)
local lang = frame.args.lang and mw.text.trim(frame.args.lang)
local sc = frame.args.sc and mw.text.trim(frame.args.sc)
if not (lang and sc) then
error("Provide language and script in |lang= and |sc= parameters")
end
local replacements = parse_replacements(frame.args.sort_key)
if not sort_key then
replacements = default_sort_key_replacements[lang]
if not replacements then
error("No replacements for " .. lang)
end
end
local Array = require "Module:array"
return Array(words)
-- Can't use mw.text.trim directly because it has a second argument.
:map(function (word) return mw.text.trim(word) end)
:sort(
function(a, b)
return make_sort_key(replacements, a) < make_sort_key(replacements, b)
end)
:map(
function(word)
return '* <span class="' .. sc .. '" lang="' .. lang .. '">'
.. word .. '</span>'
end)
:concat("\n")
end
return export