Module:Ikiusu: Difference between revisions

No edit summary
fix brackets inside definition
 
(25 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local replacements = {
["{"] = "{",
["}"] = "}",
["|"] = "|",
["="] = "{{=}}",
}
local function escape(s)
return (s:gsub(".", replacements))
end


function p.main(frame)
function p.main(frame)
Line 5: Line 16:
local word = args[1]
local word = args[1]
local options = {}
local options = {}
for i = 2, #args do
for i = 1, math.huge do
options[args[i]] = true
if not args[i] then
break
end
options[mw.text.trim(args[i])] = true
end
end
options.image = args.image
options.image = args.image
Line 21: Line 35:
end
end
if tonumber(param) then
if tonumber(param) then
langs[lang][tonumber(param)] = v
langs[lang][tonumber(param)] = escape(v)
else
else
langs[lang][param] = v
langs[lang][param] = escape(v)
end
end
end
end
end
end
end
end
table.sort(lang_list)
local result = {}
local result = {}
table.insert(result, "[[Ikiusu:lemma::"..word.."|]]")
table.insert(result, "{{langs|")
table.insert(result, "{{langs|")
for _, lang in ipairs(lang_list) do
for _, lang in ipairs(lang_list) do
Line 35: Line 52:
local caption = langs[lang].caption or ""
local caption = langs[lang].caption or ""
table.insert(result,
table.insert(result,
"[["..options.image.."|200px|thumb|"..caption.."]]"
"[[File:"..options.image.."|200px|thumb|"..caption.."]]"
)
)
end
end
table.insert(result, "'''"..word.."'''")
local entry_start = "'''"..mw.text.trim(word).."'''"
table.insert(result, "")
if langs[lang].pos then
entry_start = entry_start.." (''"..mw.text.trim(langs[lang].pos).."'')"
end
table.insert(result, entry_start)
for _, defn in ipairs(langs[lang]) do
for _, defn in ipairs(langs[lang]) do
table.insert(result, "#"..defn)
table.insert(result, "# {{#set:Ikiusu:definition="..defn.."@"..lang.."}}"..defn)
end
end
if options.inflect then
if options.inflect then

Latest revision as of 11:31, 29 September 2025

Documentation for this module may be created at Module:Ikiusu/doc

local p = {}

local replacements = {
	["{"] = "{",
	["}"] = "}",
	["|"] = "|",
	["="] = "{{=}}",
}

local function escape(s)
	return (s:gsub(".", replacements))
end

function p.main(frame)
	local args = frame:getParent().args
	local word = args[1]
	local options = {}
	for i = 1, math.huge do
		if not args[i] then
			break
		end
		options[mw.text.trim(args[i])] = true
	end
	options.image = args.image
	
	local langs = {}
	local lang_list = {}
	for k, v in pairs(args) do
		if type(k) == "string" then
			local lang, param = k:match "([^-]*)-(.*)"
			if lang then
				if not langs[lang] then
					table.insert(lang_list, lang)
					langs[lang] = {}	
				end
				if tonumber(param) then
					langs[lang][tonumber(param)] = escape(v)
				else
					langs[lang][param] = escape(v)
				end
			end
		end
	end
	table.sort(lang_list)
	
	local result = {}
	table.insert(result, "[[Ikiusu:lemma::"..word.."|]]")
	table.insert(result, "{{langs|")
	for _, lang in ipairs(lang_list) do
		table.insert(result, "{{langs/in|"..lang.."|")
		if options.image then
			local caption = langs[lang].caption or ""
			table.insert(result,
				"[[File:"..options.image.."|200px|thumb|"..caption.."]]"
			)
		end
		local entry_start = "'''"..mw.text.trim(word).."'''"
		if langs[lang].pos then
			entry_start = entry_start.." (''"..mw.text.trim(langs[lang].pos).."'')"
		end
		table.insert(result, entry_start)
		for _, defn in ipairs(langs[lang]) do
			table.insert(result, "# {{#set:Ikiusu:definition="..defn.."@"..lang.."}}"..defn)
		end
		if options.inflect then
			table.insert(result, "{{inflect senra|"..lang.."|"..word.."}}")
		end
		table.insert(result, langs[lang].extra or "")
		table.insert(result, "}}")
	end
	table.insert(result, "}}")
	return frame:preprocess(table.concat(result, "\n"))
end

return p