Modul:entry

Definition från Wiktionary, den fria ordlistan.
Hoppa till navigering Hoppa till sök

Dokumentation för denna modul finns på /dok (redigera), /test


Syfte[redigera]

Att understödja MediaWiki:Gadget-green links.js‎ (och därmed även MediaWiki:Gadget-translation editor.js) genom att automatiskt generera wikitext baserat på data som tillhandahålls av JavaScriptet.

local export = {}
local lang = require("Modul:lang")
local h3 = require("Modul:h3")

function export.getWikitext(frame)
	local args         = frame.args and mw.text.jsonDecode(frame.args[1]) or {}
	local origin_pagename = args["origin_pagename"]
	local entries      = args["entries"]
	local str          = ""
	local page         = {}
	page.lang_sections = {}

	local function getLangSection(lang_code)
		if not page.lang_sections[lang_code] then
			page.lang_sections[lang_code] = {
				h3_sections = {}
			}
		end
		return page.lang_sections[lang_code]
	end

	local function getH3Section(lang_section, h3_code)
		if not lang_section.h3_sections[h3_code] then
			lang_section.h3_sections[h3_code] = {
				pos_template_line = "",
				bold_title = "",
				translitteration = "",
				genders = {},
				numbers = {},
				aspects = {},
				definitions = {}
			}
		end
		return lang_section.h3_sections[h3_code]
	end

	for _,entry in ipairs(entries) do
		local entry_type       = entry["type"]
		local lang_code        = entry["lang"]
		local h3_code          = entry["h3"]
		local definition       = entry["definition"] and ("#" .. entry["definition"] .. "\n")
		local gender           = entry["gender"]
		local number           = entry["number"]
		local aspect           = entry["aspect"]
		local title            = entry["title"]
		local origin_h3_code   = entry["origin_h3"]
		local translitteration = entry["tr"]

		if gender == "c" then
			gender = "u"
		end

		local lang_section = getLangSection(lang_code)
		local h3_section   = getH3Section(lang_section, h3_code)

		if entry_type == "infl" then
			definition = "#{{böjning|" .. lang_code .. "|" .. h3_code .. "|" .. origin_pagename .. "}}\n"
		elseif entry_type == "translation" then
			definition = "#[[" .. origin_pagename .. "]]\n"
		elseif entry_type == "adv" then
			definition = "#{{avledning|" .. lang_code .. "|" .. origin_pagename .. "|" .. origin_h3_code .. "}}\n"
		elseif entry_type == "prespart" then
			definition = "#{{avledning|" .. lang_code .. "|" .. origin_pagename .. "|ordform=prespart}}\n"
		elseif entry_type == "perfpart" then
			definition = "#{{avledning|" .. lang_code .. "|" .. origin_pagename .. "|ordform=perfpart}}\n"
			--todo: perfpart with particle
		elseif entry_type == "aktpart" then
			definition = "#{{avledning|" .. lang_code .. "|" .. origin_pagename .. "|ordform=aktpart}}\n"
		elseif entry_type == "passpart" then
			definition = "#{{avledning|" .. lang_code .. "|" .. origin_pagename .. "|ordform=passpart}}\n"
		end
		
		local function insertDefinitionToBeginning(defs, def)
			table.insert(defs, 1, def)
		end
		local function insertDefinitionToEnd(defs, def)
			table.insert(defs, def)
		end

		local definition_exists = false
		for _, def in ipairs(h3_section.definitions) do
			if def == definition then
				definition_exists = true
			end
		end
		
		if not definition_exists then
			if entry_type == "infl" then
				insertDefinitionToEnd(h3_section.definitions, definition)
			else
				insertDefinitionToBeginning(h3_section.definitions, definition)
			end
		end

		if entry_type ~= "infl" then
			h3_section.pos_template_line = "{{" .. h3_code .. "|" .. lang_code .. "}}\n"	
		end
		
		if gender then
			h3_section.genders[gender] = true
		end

		if number then
			h3_section.numbers[number] = true
		end

		if aspect then
			h3_section.aspects[aspect] = true
		end

		if h3_section.bold_title == "" then
			h3_section.bold_title = "'''" .. title .. "'''"
		elseif not mw.ustring.find(h3_section.bold_title, "'''" .. title .. "'''", 1, true) then 
			h3_section.bold_title = h3_section.bold_title .. ", " .. "'''" .. title .. "'''"
		end
		--todo: special rules for chinese etc?

		h3_section.translitteration = translitteration and (" (" .. translitteration .. ")") or ""
	end

	local function compareH3Sections(a, b)
		return a.h3_name < b.h3_name
	end

	local function compareLangSections(a, b)
		if a.lang_name == "Svenska" then
			return true
		elseif b.lang_name == "Svenska" then
			return false
		end
	
		return a.lang_name < b.lang_name
	end

	for lang_code, lang_section in pairs(page.lang_sections) do
		for h3_code, h3_section in pairs(lang_section.h3_sections) do
			h3_section.h3_name = h3.getLongSingUCFirst(h3_code)
			lang_section.indexed_h3_sections = lang_section.indexed_h3_sections or {}
			table.insert(lang_section.indexed_h3_sections, h3_section)
		end
		
		table.sort(lang_section.indexed_h3_sections, compareH3Sections)

		lang_section.lang_name = lang.getLanguageUCFirst(lang_code)
		page.indexed_lang_sections = page.indexed_lang_sections or {}
		table.insert(page.indexed_lang_sections, lang_section)
	end
	
	table.sort(page.indexed_lang_sections, compareLangSections)

	local is_first_lang_section = true
	for _1, lang_section in ipairs(page.indexed_lang_sections) do
		if not is_first_lang_section then
			str = str .. "\n"
		end

		str = str .. "==" .. lang_section.lang_name .. "==\n"
		
		local is_first_h3_section = true
		for _2, h3_section in pairs(lang_section.indexed_h3_sections) do
			if not is_first_h3_section then
				str = str .. "\n"
			end

			str = str .. "===" .. h3_section.h3_name .. "===\n"
			str = str .. h3_section.pos_template_line
			
			local bold_line = h3_section.bold_title
			bold_line = bold_line .. h3_section.translitteration
			bold_line = bold_line .. (h3_section.genders.m      and " {{m}}"      or "")
			bold_line = bold_line .. (h3_section.genders.f      and " {{f}}"      or "")
			bold_line = bold_line .. (h3_section.genders.mf     and " {{mf}}"     or "")
			bold_line = bold_line .. (h3_section.genders.u      and " {{u}}"      or "")
			bold_line = bold_line .. (h3_section.genders.n      and " {{n}}"      or "")
			bold_line = bold_line .. (h3_section.numbers.s      and " {{s}}"      or "")
			bold_line = bold_line .. (h3_section.numbers.d      and " {{d}}"      or "")
			bold_line = bold_line .. (h3_section.numbers.p      and " {{p}}"      or "")
			bold_line = bold_line .. (h3_section.aspects.impf   and " {{impf}}"   or "")
			bold_line = bold_line .. (h3_section.aspects.pf     and " {{pf}}"     or "")
			bold_line = bold_line .. (h3_section.aspects.impfpf and " {{impfpf}}" or "")
			bold_line = bold_line .. "\n"
			
			str = str .. bold_line
			
			for _, definition in ipairs(h3_section.definitions) do
				str = str .. definition
			end
	
			is_first_h3_section = false
		end
		
		is_first_lang_section = false
	end

	str = mw.text.trim(str)

	return str
end

return export