Module:government: Difference between revisions
Jump to navigation
Jump to search
TheNightAvl (talk | contribs) No edit summary |
TheNightAvl (talk | contribs) (Added auto decatting of accusative with verbs) |
||
(25 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
-- COLOUR DATA | -- COLOUR DATA | ||
local data = mw.loadData("Module:government/data") | local data = mw.loadData("Module:government/data") | ||
local function pluralize(word) | local function pluralize(word) | ||
Line 18: | Line 17: | ||
end | end | ||
function export. | function export.show(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local | local language, pos | ||
if mw.ustring.find(args[1], "^[a-z]+%s+[a-z]+$") then | |||
language, pos = mw.ustring.match(args[1], "^([a-z]+)%s+([a-z]+)$") | |||
if | language = m_languages.find_by_code(language) | ||
pos = ( language ~= nil and (data["pos aliases"][pos] or pos) ) or nil | |||
else | else | ||
language = m_languages.find_by_code(args[1]) | |||
pos = data["pos aliases"][args["pos"]] or args["pos"] | |||
end | end | ||
if (language and args[2] == nil) or args[1] == nil then error("Entry parameter is missing") end | |||
if language == nil and pos then error("The |pos parameter requires the language code to be specified in the first parameter") end | |||
if | |||
local show = "[" | local show = "[" | ||
Line 71: | Line 39: | ||
local i = 2 | local i = 2 | ||
if language == nil then i = 1 end | |||
while args[i] do | while args[i] do | ||
if | if #show > 1 then | ||
append("; ''or'' ") | append("; ''or'' ") | ||
end | end | ||
local temp = args[i] | local temp = args[i] | ||
local with_needed = true | |||
if mw.ustring.sub(temp, 1, 1) == "&" then | if mw.ustring.sub(temp, 1, 1) == "&" then | ||
Line 83: | Line 54: | ||
else | else | ||
append("''with'' ") | append("''with'' ") | ||
with_needed = false | |||
end | end | ||
while #temp > 0 do | while #temp > 0 do | ||
mw.log(temp) | mw.log(show, temp) | ||
local function remove_temp(regex) | local function remove_temp(regex) | ||
temp = mw.ustring.gsub(temp, regex, "") | temp = mw.ustring.gsub(temp, regex, "") | ||
end | end | ||
local conjunction = mw.ustring.match(temp, "^%s*([%/%+])%s*") | |||
append(" ''or'' ") | if conjunction then | ||
remove_temp("^%s*%/%s*") | if conjunction == "/" then | ||
append(" ''or'' ") | |||
elseif conjunction == "+" and not with_needed then | |||
append(" ''and'' ") | |||
end | |||
remove_temp("^%s*[%/%+]%s*") | |||
if mw.ustring.sub(temp, 1, 1) == "&" then | |||
temp = mw.ustring.sub(temp, 2) | |||
elseif with_needed then | |||
append(" ''with'' ") | |||
with_needed = false | |||
end | |||
else | else | ||
local function glossary(form, colour) | local function glossary(form, colour) | ||
if data[form] then | form = mw.ustring.gsub(form, "^%*", "") | ||
if mw.ustring.sub(form, 1, 1) == "_" then | |||
return "''" .. mw.ustring.sub(form, 2) .. "''" | |||
else | |||
if data[form] then | |||
form = data[form].label | |||
if colour == nil then colour = data[form].colour end | |||
end | |||
local to_Return = "''[[Appendix:Glossary#" .. form .. "|" .. form .. "]]''" | |||
if colour then | |||
to_Return = "<span style=\"background-color: #" .. colour .. ";\">" .. to_Return .. "</span>" | |||
end | |||
return to_Return | |||
end | end | ||
end | end | ||
local temp_main = "" | local temp_main = "" | ||
Line 116: | Line 98: | ||
end | end | ||
local form, colour | local form, colour | ||
if mw.ustring.find(temp, "^%:[^%(%<%[%+%/]+") then | if mw.ustring.find(temp, "^%([^%)]+%)%:") then -- for postpositions | ||
local link = mw.ustring.match(temp, "^%:([^%(%<%[%+%/]+)") | form = mw.ustring.match(temp, "^%(([^%)]+)%)%:") | ||
remove_temp("^%([^%)]+%)%:") | |||
if mw.ustring.find(temp, "^%[[^%]]+%]") then | |||
colour = mw.ustring.match(temp, "^%[([^%]]+)%]") | |||
remove_temp("^%[[^%]]+%]") | |||
end | |||
temp_append("(" .. glossary(form, colour) .. " +) ") | |||
end | |||
if mw.ustring.find(temp, "^%:[^%(%<%[%+%/%:]+") then | |||
if language == nil then error("A language code must be specified in the first parameter for the link function to work") end | |||
temp_append("'''") | |||
while mw.ustring.find(temp, "^%:[^%(%<%[%+%/%:]+") do | |||
local link = mw.ustring.match(temp, "^%:([^%(%<%[%+%/%:]+)") | |||
local spacing = mw.ustring.match(link, "%s+$") or "" | |||
link = mw.ustring.gsub(link, "%s+$", "") | |||
temp_append(m_links.full_link{language = language, term = link} .. spacing) | |||
remove_temp("^%:[^%(%<%[%+%/%:]+") | |||
end | |||
temp_append("'''") | |||
if mw.ustring.find(temp, "^%([^%)]+%)") then | if mw.ustring.find(temp, "^%([^%)]+%)") then | ||
form = mw.ustring.match(temp, "^%(([^%)]+)%)") | form = mw.ustring.match(temp, "^%(([^%)]+)%)") | ||
Line 139: | Line 136: | ||
end | end | ||
temp_append(glossary(form, colour)) | temp_append(glossary(form, colour)) | ||
if | if language and pos and data[form] then | ||
if | if not (data[form].label == "accusative" and pos == "verb") then | ||
temp_append("[[Category:" .. language.name .. " " .. pluralize(pos) .. " governing the " .. data[form].label .. "]]") | |||
end | |||
end | end | ||
else | else | ||
Line 154: | Line 152: | ||
qq = " (''" .. mw.ustring.sub(content, 4) .. "'')" | qq = " (''" .. mw.ustring.sub(content, 4) .. "'')" | ||
elseif mw.ustring.sub(content, 1, 3) == "q*:" then | elseif mw.ustring.sub(content, 1, 3) == "q*:" then | ||
show = "[(''" .. mw.ustring.sub(content, 4) .. "'') " mw.ustring.sub(show, 2) | show = "[(''" .. mw.ustring.sub(content, 4) .. "'') " .. mw.ustring.sub(show, 2) | ||
else | else | ||
t = " <small>‘".. content .."’</small>" | t = " <small>‘".. content .."’</small>" | ||
Line 160: | Line 158: | ||
remove_temp("^%<[^%>]+%>") | remove_temp("^%<[^%>]+%>") | ||
end | end | ||
append(q .. temp_main .. | append(q .. temp_main .. t .. qq) | ||
end | end | ||
end | end |
Latest revision as of 12:58, 6 August 2024
See {{with}}
. To add to the recognised argument list, edit Module:government/data.
local export = {}
local getArgs = require('Module:Arguments').getArgs
local m_languages = require('Module:languages')
local m_links = require('Module:links')
-- COLOUR DATA
local data = mw.loadData("Module:government/data")
local function pluralize(word)
local ending = mw.ustring.sub(word, -1)
if ending == "h" and mw.ustring.sub(word, -2, -1) == "s" then ending = "sh" end
if ending == "s" or ending == "z" or ending == "x" or ending == "sh" then
return word .. "es"
end
return word .. "s"
end
function export.show(frame)
local args = getArgs(frame)
local language, pos
if mw.ustring.find(args[1], "^[a-z]+%s+[a-z]+$") then
language, pos = mw.ustring.match(args[1], "^([a-z]+)%s+([a-z]+)$")
language = m_languages.find_by_code(language)
pos = ( language ~= nil and (data["pos aliases"][pos] or pos) ) or nil
else
language = m_languages.find_by_code(args[1])
pos = data["pos aliases"][args["pos"]] or args["pos"]
end
if (language and args[2] == nil) or args[1] == nil then error("Entry parameter is missing") end
if language == nil and pos then error("The |pos parameter requires the language code to be specified in the first parameter") end
local show = "["
local function append(text)
show = show .. text
end
local i = 2
if language == nil then i = 1 end
while args[i] do
if #show > 1 then
append("; ''or'' ")
end
local temp = args[i]
local with_needed = true
if mw.ustring.sub(temp, 1, 1) == "&" then
temp = mw.ustring.sub(temp, 2)
else
append("''with'' ")
with_needed = false
end
while #temp > 0 do
mw.log(show, temp)
local function remove_temp(regex)
temp = mw.ustring.gsub(temp, regex, "")
end
local conjunction = mw.ustring.match(temp, "^%s*([%/%+])%s*")
if conjunction then
if conjunction == "/" then
append(" ''or'' ")
elseif conjunction == "+" and not with_needed then
append(" ''and'' ")
end
remove_temp("^%s*[%/%+]%s*")
if mw.ustring.sub(temp, 1, 1) == "&" then
temp = mw.ustring.sub(temp, 2)
elseif with_needed then
append(" ''with'' ")
with_needed = false
end
else
local function glossary(form, colour)
form = mw.ustring.gsub(form, "^%*", "")
if mw.ustring.sub(form, 1, 1) == "_" then
return "''" .. mw.ustring.sub(form, 2) .. "''"
else
if data[form] then
form = data[form].label
if colour == nil then colour = data[form].colour end
end
local to_Return = "''[[Appendix:Glossary#" .. form .. "|" .. form .. "]]''"
if colour then
to_Return = "<span style=\"background-color: #" .. colour .. ";\">" .. to_Return .. "</span>"
end
return to_Return
end
end
local temp_main = ""
local function temp_append(text)
temp_main = temp_main .. text
end
local form, colour
if mw.ustring.find(temp, "^%([^%)]+%)%:") then -- for postpositions
form = mw.ustring.match(temp, "^%(([^%)]+)%)%:")
remove_temp("^%([^%)]+%)%:")
if mw.ustring.find(temp, "^%[[^%]]+%]") then
colour = mw.ustring.match(temp, "^%[([^%]]+)%]")
remove_temp("^%[[^%]]+%]")
end
temp_append("(" .. glossary(form, colour) .. " +) ")
end
if mw.ustring.find(temp, "^%:[^%(%<%[%+%/%:]+") then
if language == nil then error("A language code must be specified in the first parameter for the link function to work") end
temp_append("'''")
while mw.ustring.find(temp, "^%:[^%(%<%[%+%/%:]+") do
local link = mw.ustring.match(temp, "^%:([^%(%<%[%+%/%:]+)")
local spacing = mw.ustring.match(link, "%s+$") or ""
link = mw.ustring.gsub(link, "%s+$", "")
temp_append(m_links.full_link{language = language, term = link} .. spacing)
remove_temp("^%:[^%(%<%[%+%/%:]+")
end
temp_append("'''")
if mw.ustring.find(temp, "^%([^%)]+%)") then
form = mw.ustring.match(temp, "^%(([^%)]+)%)")
remove_temp("^%([^%)]+%)")
if mw.ustring.find(temp, "^%[[^%]]+%]") then
colour = mw.ustring.match(temp, "^%[([^%]]+)%]")
remove_temp("^%[[^%]]+%]")
end
temp_append(" (+ " .. glossary(form, colour) .. ")")
end
elseif mw.ustring.match(temp, "^[^%(%<%[%+%/]+") then
form = mw.ustring.match(temp, "^([^%(%<%[%+%/]+)")
form = mw.ustring.gsub(form, "%s+$", "")
remove_temp("^([^%(%<%[%+%/]+)")
if mw.ustring.find(temp, "^%[[^%]]+%]") then
colour = mw.ustring.match(temp, "^%[([^%]]+)%]")
remove_temp("^%[[^%]]+%]")
end
temp_append(glossary(form, colour))
if language and pos and data[form] then
if not (data[form].label == "accusative" and pos == "verb") then
temp_append("[[Category:" .. language.name .. " " .. pluralize(pos) .. " governing the " .. data[form].label .. "]]")
end
end
else
error("Invalid parameter format")
end
local t, q, qq = "", "", ""
while mw.ustring.find(temp, "^%<[^%>]+%>") do
local content = mw.ustring.match(temp, "^%<([^%>]+)%>")
if mw.ustring.sub(content, 1, 2) == "q:" then
q = "(''" .. mw.ustring.sub(content, 3) .. "'') "
elseif mw.ustring.sub(content, 1, 3) == "qq:" then
qq = " (''" .. mw.ustring.sub(content, 4) .. "'')"
elseif mw.ustring.sub(content, 1, 3) == "q*:" then
show = "[(''" .. mw.ustring.sub(content, 4) .. "'') " .. mw.ustring.sub(show, 2)
else
t = " <small>‘".. content .."’</small>"
end
remove_temp("^%<[^%>]+%>")
end
append(q .. temp_main .. t .. qq)
end
end
i = i + 1
end
append("]")
return show
end
return export
--[[
Debug console test string:
=p.generate(mw.getCurrentFrame():newChild{title="whatever",args={"accusative"}})
=p.show(mw.getCurrentFrame():newChild{title="whatever",args={"rad", "acc / :la(dat)", ["pos"] = "verb"}})
]]