Dokumentation för denna modul finns på /dok (redigera), /test
Se ett grundläggande exempel: Modul:grammar-example, eller någon befintlig grammatikmodul (t.ex. Modul:sv-adj).
getWikitext (exporteras till ärvande modul)
{{sv-adj}}
, som i sin tur anropar sin grammatikmodul {{#invoke|sv-adj|getWikitext}}
.getFormsAndMeta (lokal funktion)
getCategories (lokal funktion)
{{mallnamnet}} (se lista)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
{{mallnamnet}} (se lista)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
{{mallnamnet}} (se lista)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
{{parameterschema|sv-adj|{{sv-adj|autonumrera=}} }}
{{parameterschema|sv-adj|{{sv-adj|grundform=rolig{{!}} }} }}
{{parameterschema|sv-adj-alt|{{sv-adj-alt|grundform=kluv{{!}}en }} }}
.makeArgsTable respektive makeArgsTableFromString
test (exporteras till ärvande modul)
getFormsForExport (exporteras till ärvande modul)
{{#invoke:grammar-export|getJson|pagename=<sidans namn>|template=<hela mallsyntaxen inklusive mallnamnet>}}
{
"expandtemplates": {
"wikitext": "{
\"lang\":\"sv\",
\"h3\":\"adj\",
\"forms\": {
\"x_adv\":\"roligt?\",
\"pred_sup\":\"roligast\",
\"attr_obest_sing_neutrum_pos\":\"roligt\",
\"attr_best_sing_alla_pos\":\"roliga\",
\"attr_plur_pos\":\"roliga\",
\"pred_komp\":\"roligare\",
\"pred_plur_pos\":\"roliga\",
\"attr_best_sing_mask_sup\":\"roligaste\",
\"pred_sing_neutrum_pos\":\"roligt\",
\"pred_sing_utrum_pos\":\"rolig\",
\"attr_obest_sing_utrum_pos\":\"rolig\",
\"attr_komp\":\"roligare\",
\"attr_komp_mask\":\"roligare\",
\"attr_best_sing_mask_pos\":\"rolige\",
\"attr_sup\":\"roligaste\"
}
}"
}
}
local export = {}
local base_param = "grundform"
local quality_param = "fakta"
local meanings_param = "betydelser"
local width_param = "bredd"
local as_first_part_param = "förled"
local note_param = "not"
local auto_number_param = "autonumrera"
local function addGeneralMetaAttributes(pagename, templatename, args, meta)
meta.pagename = pagename
meta.templatename = templatename
meta.quality_notice = args
meta.meanings = args or ""
meta.width = args
meta.as_first_part = args
meta.note = args or "-"
meta.auto_number = not not args
return meta
end
local function fillCellArgumentsWithBrackets(args, numbered_cells_count, named_cells)
for i = 1, numbered_cells_count do
args = args or "{" .. i .. "}"
end
for _, v in ipairs(named_cells) do
args = "{" .. v .. "}"
end
return args
end
local function getFormsAndMeta(pagename, templatename, args, format)
local meta = export._getMeta(pagename, templatename, args)
meta = addGeneralMetaAttributes(pagename, templatename, args, meta)
local parameters_should_be_auto_filled_with_numbers = meta.auto_number
if parameters_should_be_auto_filled_with_numbers then
args = fillCellArgumentsWithBrackets(args, meta.numbered_cells, meta.named_cells)
end
local forms = export._getForms(pagename, templatename, args, meta, format)
local forms_and_meta = {
forms = forms,
meta = meta
}
return forms_and_meta
end
local function makeArgsTable(frame_args, unused_indices)
local args = {}
for k, v in pairs(frame_args) do
if (type(k) ~= "number" or k > unused_indices) then
if type(k) == "number" then
args = v
else
args = v
end
end
end
return args
end
local function makeArgsTableFromString(full_template_call_without_brackets)
local args = {}
local argument_list = mw.text.split(full_template_call_without_brackets, "|")
local is_templatename_index = 1
local function hasExplicitKey(arg)
return not not arg;
end
for i, v in ipairs(argument_list) do
if i ~= is_templatename_index then
local splitArgument = mw.text.split(argument_list, "=")
if hasExplicitKey(splitArgument) then
local key = tonumber(splitArgument) or splitArgument
local value = splitArgument
args = value
else
local value = splitArgument
table.insert(args, value)
end
end
end
return args
end
local function getAcceptedParameters()
local accepted_parameters = {base_param, quality_param, meanings_param, width_param, as_first_part_param, note_param, auto_number_param}
for _,v in ipairs(export._getAcceptedParameters()) do
table.insert(accepted_parameters, v)
end
return accepted_parameters
end
local function getCategories(forms, meta, args, modulename)
local accepted_parameters = getAcceptedParameters()
local unallowed_parameters_are_used = false
local function contains(tbl, val)
for _, v in ipairs(tbl) do
if v == val then
return true
end
end
return false
end
for k,v in pairs(args) do
if type(k) == "number" then
if k > meta.numbered_cells then
unallowed_parameters_are_used = true
end
else
if not contains(accepted_parameters, k) then
unallowed_parameters_are_used = true
end
end
end
local str = export._getCategories(forms, meta)
if unallowed_parameters_are_used then
str = str .. "]"
end
return str
end
function export.getWikitext(frame)
local args = frame:getParent().args
local title = mw.title.getCurrentTitle()
local prefixed_modulename = frame:getTitle()
local prefixed_templatename = frame:getParent():getTitle()
local pagename = args or title.text
local should_categorize = title.namespace == 0
local modulename = mw.ustring.gsub(prefixed_modulename, "^+:", "")
local templatename = mw.ustring.gsub(prefixed_templatename, "^+:", "")
local format = "wikitable"
local fm = getFormsAndMeta(pagename, templatename, args, format)
local wikitable = export._getWikitable(fm.forms, fm.meta)
local categories = should_categorize and getCategories(fm.forms, fm.meta, args, modulename) or ""
local str = wikitable .. categories
return str
end
function export.getJson(frame)
local args = makeArgsTable(frame.args, 0)
local pagename = frame.args
local templatename = mw.text.split(frame.args, "%|")
local format = "export"
local fm = getFormsAndMeta(pagename, templatename, args, format)
local json = mw.text.jsonEncode(fm.forms)
return json
end
function export.test(pagename, templatename_with_args, format, meta_args_to_include_in_result)
local args = makeArgsTableFromString(templatename_with_args)
local templatename = mw.ustring.match(templatename_with_args, "^(+)|?")
local include_meta_in_results = not not meta_args_to_include_in_result
local forms_and_meta = getFormsAndMeta(pagename, templatename, args, format)
local result
if include_meta_in_results then
local new_meta = {}
for i, v in ipairs(meta_args_to_include_in_result) do
new_meta = forms_and_meta.meta
end
forms_and_meta.meta = new_meta
result = forms_and_meta
else
result = forms_and_meta.forms
end
return result
end
return export