Module:example: Difference between revisions
Jump to navigation
Jump to search
(Created page with "local export = {} local m_formatting = require("Module:formatting") local m_languages = require("Module:languages") function export.show(frame) local args = frame:getParent().args local language = m_languages.get_by_code(args[1]) if not args[2] then error("a sentence must be provided to {{example}}") end local translation = args[3] and ('<dl><dd>' .. args[3] .. '</dd></dl>') or "" return '<dl><dd>' .. m_formatting.wrap_face(args[2], language, "example") .. transla...") |
TheNightAvl (talk | contribs) No edit summary |
||
(11 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
local m_formatting = require("Module:formatting") | local m_formatting = require("Module:formatting") | ||
local m_languages = require("Module:languages") | local m_languages = require("Module:languages") | ||
local m_languages_oow = require("Module:languages-oow") | |||
local params = { | |||
[1] = {required = true}, | |||
[2] = {required = true}, | |||
[3] = {list = true}, | |||
["inline"] = {type = "boolean"}, | |||
["nocat"] = {type = "boolean"}, | |||
} | |||
local params_inline = { | |||
[1] = {required = true}, | |||
[2] = {required = true}, | |||
[3] = {list = true}, | |||
["inline"] = {type = "boolean", default = true}, | |||
["nocat"] = {type = "boolean"}, | |||
} | |||
function export.make_example(language, sentence, glosses, inline, nocat) | |||
local translation = "" | |||
local cats = nocat and "" or ("[[Category:" .. language.name .. " terms with usage examples]]") | |||
if #glosses > 0 then | |||
for _, gloss in ipairs(glosses) do | |||
if mw.ustring.match(gloss, "^([a-z]+)%:(.+)$") then | |||
local code, gloss_temp = mw.ustring.match(gloss, "^([a-z]+)%:(.+)$") | |||
if m_languages_oow.is_valid_code(code) then | |||
gloss = m_languages_oow.get_flag(code) .. " " .. gloss_temp | |||
cats = cats .. (nocat and "" or ("[[Category:Terms with " .. m_languages_oow.get_name(code) .. " translations]]")) | |||
end | |||
end | |||
translation = translation .. (inline and (" ― " .. gloss) or ("<dl><dd>" .. gloss .. "</dd></dl>")) | |||
end | |||
translation = mw.ustring.gsub(translation, "</dd></dl><dl><dd>", " — ") | |||
end | |||
return '<div class="h-example">' .. m_formatting.wrap_face(mw.ustring.gsub(sentence,"%*([^%*]+)*","'''%1'''"), language, "example") .. mw.ustring.gsub(translation,"%*([^%*]+)*","'''%1'''") .. '</div>' .. cats | |||
end | |||
function export.show(frame) | function export.show(frame) | ||
local args = frame:getParent().args | local args = require("Module:parameters").process(frame:getParent().args, params) | ||
local language = m_languages.get_by_code(args[1]) | |||
return export.make_example(language, args[2], args[3], args["inline"], args["nocat"]) | |||
end | |||
function export.show_inline(frame) | |||
local args = require("Module:parameters").process(frame:getParent().args, params_inline) | |||
local language = m_languages.get_by_code(args[1]) | local language = m_languages.get_by_code(args[1]) | ||
return export.make_example(language, args[2], args[3], args["inline"], args["nocat"]) | |||
end | end | ||
return export |
Latest revision as of 23:26, 25 December 2024
Implements {{example}}
.
local export = {}
local m_formatting = require("Module:formatting")
local m_languages = require("Module:languages")
local m_languages_oow = require("Module:languages-oow")
local params = {
[1] = {required = true},
[2] = {required = true},
[3] = {list = true},
["inline"] = {type = "boolean"},
["nocat"] = {type = "boolean"},
}
local params_inline = {
[1] = {required = true},
[2] = {required = true},
[3] = {list = true},
["inline"] = {type = "boolean", default = true},
["nocat"] = {type = "boolean"},
}
function export.make_example(language, sentence, glosses, inline, nocat)
local translation = ""
local cats = nocat and "" or ("[[Category:" .. language.name .. " terms with usage examples]]")
if #glosses > 0 then
for _, gloss in ipairs(glosses) do
if mw.ustring.match(gloss, "^([a-z]+)%:(.+)$") then
local code, gloss_temp = mw.ustring.match(gloss, "^([a-z]+)%:(.+)$")
if m_languages_oow.is_valid_code(code) then
gloss = m_languages_oow.get_flag(code) .. " " .. gloss_temp
cats = cats .. (nocat and "" or ("[[Category:Terms with " .. m_languages_oow.get_name(code) .. " translations]]"))
end
end
translation = translation .. (inline and (" ― " .. gloss) or ("<dl><dd>" .. gloss .. "</dd></dl>"))
end
translation = mw.ustring.gsub(translation, "</dd></dl><dl><dd>", " — ")
end
return '<div class="h-example">' .. m_formatting.wrap_face(mw.ustring.gsub(sentence,"%*([^%*]+)*","'''%1'''"), language, "example") .. mw.ustring.gsub(translation,"%*([^%*]+)*","'''%1'''") .. '</div>' .. cats
end
function export.show(frame)
local args = require("Module:parameters").process(frame:getParent().args, params)
local language = m_languages.get_by_code(args[1])
return export.make_example(language, args[2], args[3], args["inline"], args["nocat"])
end
function export.show_inline(frame)
local args = require("Module:parameters").process(frame:getParent().args, params_inline)
local language = m_languages.get_by_code(args[1])
return export.make_example(language, args[2], args[3], args["inline"], args["nocat"])
end
return export