Module:mg-noun: Difference between revisions
Jump to navigation
Jump to search
This module implements the noun inflection-table template
No edit summary |
No edit summary |
||
(76 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local export = {} | |||
local m_u = require('Module:utilities') | |||
local m_data = require('Module:mg-noun/data') | |||
local sub = mw.ustring.sub | local sub = mw.ustring.sub | ||
local find = mw.ustring.find | local find = mw.ustring.find | ||
local match = mw.ustring.match | |||
local gmatch = mw.ustring.gmatch | local gmatch = mw.ustring.gmatch | ||
local gsub = mw.ustring.gsub | |||
local u = mw.ustring.char | |||
local split = mw.text.split | |||
local gsplit = mw.text.gsplit | |||
local PAGENAME = gsub(mw.title.getCurrentTitle().text, "%s", " ") | local PAGENAME = gsub(mw.title.getCurrentTitle().text, "%s", " ") | ||
local NAMESPACE = mw.title.getCurrentTitle().nsText | local NAMESPACE = mw.title.getCurrentTitle().nsText | ||
local | local genders = {["m"] = "masculine", ["f"] = "feminine", ["?"] = ""} | ||
local function detect_decl(word) | |||
if word:match("[aeoá]$") then | |||
local function detect_decl(word | return sub(word, -1, -1) -- last letter | ||
if | |||
return | |||
elseif word:match("n$") then | elseif word:match("n$") then | ||
return " | return "nt" | ||
else | |||
return "cons" | |||
end | end | ||
end | end | ||
Line 31: | Line 32: | ||
local parent_args = frame:getParent().args | local parent_args = frame:getParent().args | ||
local numbers = | local numbers = {} | ||
local decl = {} | local decl = {} | ||
local word = NAMESPACE == "Template" and "mazer" or parent_args | local g = NAMESPACE == "Template" and "f" or parent_args[1] or parent_args["g"] | ||
local word = NAMESPACE == "Template" and "mazer" or parent_args["word"] or PAGENAME | |||
local args = {} | local args = {} | ||
local decl_type = "" | local decl_type = NAMESPACE == "Template" and "r" or parent_args[2] or parent_args["decl"] or detect_decl(word) | ||
if g ~= "m" and g ~= "f" and g ~= "?" then error("Unknown gender: it must be either ‘m’ or ‘f’") end | |||
if not m_data[word] then | if not m_data[word] then | ||
if not decl_type then | if not decl_type then | ||
Line 64: | Line 58: | ||
local data = {forms = {}, categories = {}} | local data = {forms = {}, categories = {}} | ||
data.head = parent_args["head"] or | data.head = parent_args["head"] or word | ||
data.proper = parent_args["proper"] and true or false | data.proper = parent_args["proper"] and true or false | ||
data.nocat = parent_args["nocat"] and true or false | data.nocat = parent_args["nocat"] and true or false | ||
data.g = genders[g] | |||
data.nopl = parent_args["nopl"] and true or false | |||
if data.proper then data.nopl = true end | |||
data.decl_type = decl_type | |||
-- Generate the forms | -- Generate the forms | ||
if | if m_data[word] then | ||
m_data[word](parent_args, data) | m_data[word](parent_args, data) | ||
else | else | ||
m_data[decl_type](args, data) | m_data[decl_type](args, data) | ||
end | end | ||
-- | -- make the table | ||
return make_table(data) | return make_table(data, parent_args) | ||
end | end | ||
function make_table(data) | function make_table(data, args) | ||
local function show_form(form) | local function show_form(form) | ||
Line 98: | Line 96: | ||
local links = {} | local links = {} | ||
for alt in gmatch(term, "([^%s,]+)") do | for alt in gmatch(term, "([^%s,]+)") do | ||
alt = "[[Contionary:" .. alt .. "|" .. alt .. "]]" | alt = term == "—" and term or "[[Contionary:" .. alt .. "|" .. alt .. "]]" | ||
table.insert(links, alt) | table.insert(links, alt) | ||
end | end | ||
Line 108: | Line 106: | ||
return data.decl_type | return data.decl_type | ||
elseif param == "title" then | elseif param == "title" then | ||
return data.forms. | return data.forms.di_s[1] | ||
elseif param == "pagename" and NAMESPACE == "Template" then | elseif param == "pagename" and NAMESPACE == "Template" then | ||
return " | return "mazer" | ||
elseif param == "pagename" then | elseif param == "pagename" then | ||
return PAGENAME | return PAGENAME | ||
elseif param == "gender" then | |||
return data.g | |||
else | else | ||
return show_form(data.forms[param]) | return show_form(data.forms[param]) | ||
Line 119: | Line 119: | ||
local function make_cases(data) | local function make_cases(data) | ||
local cases = {" | local cases = {"direct", "dative", "locative"} | ||
local ret = {} | local ret = {} | ||
for _, case in ipairs(cases) do | for _, case in ipairs(cases) do | ||
local case_short = sub(case, 1, | local case_short = sub(case, 1, 2) | ||
local form_s = args[case_short .. '_s'] and {args[case_short .. '_s']} or data.forms[case_short .. '_s'] or nil | |||
local form_p = args[case_short .. '_p'] and {args[case_short .. '_p']} or data.forms[case_short .. '_p'] or nil | |||
table.insert(ret, '|- \n! ' .. case .. '\n') | |||
table.insert(ret, '| ' .. link(show_form(form_s)) .. '\n') | |||
if not data.nopl then | |||
table.insert(ret, | table.insert(ret, '| ' .. link(show_form(form_p)) .. '\n') | ||
end | end | ||
end | end | ||
Line 135: | Line 135: | ||
end | end | ||
local | local navframe = [=[ | ||
<div class="mw-collapsible" style="border-collapse: collapse; margin: 0px 0px -1px 0px; padding: 2px; border: 1px solid #aaaaaa; text-align: center; font-size: 95%; overflow: auto; width: 55%;"> | |||
<div style="min-height: 1.6em; font-weight:bold; font-size: 100%; text-align: left; background-color:#efefef; padding-left: 10px; background-image: -webkit-gradient(linear, left top, left bottom, from(#EFEFEF), to(#DFDFDF), color-stop(0.6, #E3E3E3)); background-image: -moz-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF); background-image: -o-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF);">''{{{title}}}'' — {{{gender}}} noun, {{{decl_type}}}-declension</div> | |||
<div class="mw-collapsible-content" style="font-size: 100%;"> | |||
]=] | |||
local wikicode = [=[ | local wikicode = [=[ | ||
{| class="bluetable" border="1px solid #000000" style="border-collapse:collapse; background:#fafafa; text-align:center; width:100%" | |||
{| class=" | |||
|- | |- | ||
! | ! | ||
! | ! Singular | ||
]=] .. (data.nopl and "\n" or [=[ | |||
! | ! Plural | ||
]=]) .. make_cases(data) .. [=[ | ]=]) .. make_cases(data) .. [=[ | ||
|}]=] | |}</div></div>]=] | ||
return gsub(navframe .. wikicode, "{{{([a-z0-9_]+)}}}", repl) | |||
return gsub(wikicode, "{{{([a-z0-9_]+)}}}", repl) .. (not data.nocat and | .. (not data.nocat and m_u.format_categories(data.categories, lang) or "") | ||
end | end | ||
return export | return export |
Latest revision as of 14:04, 11 December 2022
- The following documentation is located at Module:mg-noun/doc.[edit]
- Useful links: subpage list • links • transclusions • testcases • sandbox
This module implements the noun inflection-table template
{{mg-decl-noun}}
.
local export = {}
local m_u = require('Module:utilities')
local m_data = require('Module:mg-noun/data')
local sub = mw.ustring.sub
local find = mw.ustring.find
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local gsub = mw.ustring.gsub
local u = mw.ustring.char
local split = mw.text.split
local gsplit = mw.text.gsplit
local PAGENAME = gsub(mw.title.getCurrentTitle().text, "%s", " ")
local NAMESPACE = mw.title.getCurrentTitle().nsText
local genders = {["m"] = "masculine", ["f"] = "feminine", ["?"] = ""}
local function detect_decl(word)
if word:match("[aeoá]$") then
return sub(word, -1, -1) -- last letter
elseif word:match("n$") then
return "nt"
else
return "cons"
end
end
-- The main entry point.
function export.show(frame)
local parent_args = frame:getParent().args
local numbers = {}
local decl = {}
local g = NAMESPACE == "Template" and "f" or parent_args[1] or parent_args["g"]
local word = NAMESPACE == "Template" and "mazer" or parent_args["word"] or PAGENAME
local args = {}
local decl_type = NAMESPACE == "Template" and "r" or parent_args[2] or parent_args["decl"] or detect_decl(word)
if g ~= "m" and g ~= "f" and g ~= "?" then error("Unknown gender: it must be either ‘m’ or ‘f’") end
if not m_data[word] then
if not decl_type then
error("Unknown declension '" .. decl_type .. "'")
end
args = require("Module:parameters").process(parent_args, m_data[decl_type].params, true)
if numbers then
for i, number in ipairs(numbers) do
args[i] = number
end
end
end
local data = {forms = {}, categories = {}}
data.head = parent_args["head"] or word
data.proper = parent_args["proper"] and true or false
data.nocat = parent_args["nocat"] and true or false
data.g = genders[g]
data.nopl = parent_args["nopl"] and true or false
if data.proper then data.nopl = true end
data.decl_type = decl_type
-- Generate the forms
if m_data[word] then
m_data[word](parent_args, data)
else
m_data[decl_type](args, data)
end
-- make the table
return make_table(data, parent_args)
end
function make_table(data, args)
local function show_form(form)
if not form then
return "—"
end
local ret = {}
for key, subform in ipairs(form) do
table.insert(ret, subform)
end
return table.concat(ret, ", ")
end
local function link(term)
local links = {}
for alt in gmatch(term, "([^%s,]+)") do
alt = term == "—" and term or "[[Contionary:" .. alt .. "|" .. alt .. "]]"
table.insert(links, alt)
end
return table.concat(links, ", ")
end
local function repl(param)
if param == "decl_type" then
return data.decl_type
elseif param == "title" then
return data.forms.di_s[1]
elseif param == "pagename" and NAMESPACE == "Template" then
return "mazer"
elseif param == "pagename" then
return PAGENAME
elseif param == "gender" then
return data.g
else
return show_form(data.forms[param])
end
end
local function make_cases(data)
local cases = {"direct", "dative", "locative"}
local ret = {}
for _, case in ipairs(cases) do
local case_short = sub(case, 1, 2)
local form_s = args[case_short .. '_s'] and {args[case_short .. '_s']} or data.forms[case_short .. '_s'] or nil
local form_p = args[case_short .. '_p'] and {args[case_short .. '_p']} or data.forms[case_short .. '_p'] or nil
table.insert(ret, '|- \n! ' .. case .. '\n')
table.insert(ret, '| ' .. link(show_form(form_s)) .. '\n')
if not data.nopl then
table.insert(ret, '| ' .. link(show_form(form_p)) .. '\n')
end
end
return table.concat(ret)
end
local navframe = [=[
<div class="mw-collapsible" style="border-collapse: collapse; margin: 0px 0px -1px 0px; padding: 2px; border: 1px solid #aaaaaa; text-align: center; font-size: 95%; overflow: auto; width: 55%;">
<div style="min-height: 1.6em; font-weight:bold; font-size: 100%; text-align: left; background-color:#efefef; padding-left: 10px; background-image: -webkit-gradient(linear, left top, left bottom, from(#EFEFEF), to(#DFDFDF), color-stop(0.6, #E3E3E3)); background-image: -moz-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF); background-image: -o-linear-gradient(top, #EFEFEF, #E3E3E3 60%, #DFDFDF);">''{{{title}}}'' — {{{gender}}} noun, {{{decl_type}}}-declension</div>
<div class="mw-collapsible-content" style="font-size: 100%;">
]=]
local wikicode = [=[
{| class="bluetable" border="1px solid #000000" style="border-collapse:collapse; background:#fafafa; text-align:center; width:100%"
|-
!
! Singular
]=] .. (data.nopl and "\n" or [=[
! Plural
]=]) .. make_cases(data) .. [=[
|}</div></div>]=]
return gsub(navframe .. wikicode, "{{{([a-z0-9_]+)}}}", repl)
.. (not data.nocat and m_u.format_categories(data.categories, lang) or "")
end
return export