Module:Etymology
Appearance
See Template:Etymology.
require('strict')
local p = {}
local ERROR_MESSAGE = '{{[[Template:Etymology#Examples|etymology]]}} requires 1st or 2nd parameter in each triplet!'
local function trim(s)
return s:match('^[ \t\n\r%z\v]*(.-)[ \t\n\r%z\v]*$')
end
local function language(code)
local iso639 = require('Module:ISO 639 name')
if iso639.iso_639_code_exists({code}) then
return trim(iso639.iso_639_code_to_name({code, link = 'yes'}))
end
return '[[' .. code .. ' language|' .. code .. ']]'
end
local function language_tag(lang)
local iso639 = require('Module:ISO 639 name')
if iso639.iso_639_code_exists({lang}) then
return lang
end
if iso639.iso_639_name_exists({lang}) then
return iso639.iso_639_name_to_code({lang})
end
end
local function triplet(frame, lang, word, meaning)
lang = trim(lang)
local parts = {}
if lang ~= '' then
table.insert(parts, ' ' .. language(lang))
end
if trim(word) ~= '' then
local italic = "'' " .. word .. "''"
local tag = lang ~= '' and language_tag(lang)
if tag then
italic = tostring(mw.html.create('span'):attr('lang', tag):wikitext(italic))
end
table.insert(parts, ' ' .. italic)
end
if trim(meaning) ~= '' then
table.insert(parts, trim(' ' .. frame:expandTemplate{title = 'Gloss', args = {trim(meaning)}}))
end
return table.concat(parts)
end
function p.main(frame)
local args = frame:getParent().args
if trim((args[1] or '') .. (args[2] or '')) == '' then
return require('Module:Error')._error{message = ERROR_MESSAGE}
end
local values = {}
for _, value in ipairs(args) do
table.insert(values, value)
end
local items = {}
for i = 1, #values, 3 do
table.insert(items, triplet(frame, values[i], values[i + 1] or '', values[i + 2] or ''))
end
return 'from' .. mw.text.listToText(items, ', ', ' and')
end
return p