Module:User:Kiril kovachev/bg-new

local export = {}

local titleObj = mw.title.getCurrentTitle local pagename = titleObj.text local namespace = titleObj.nsText

local find = mw.ustring.find local length = mw.ustring.len local trim = mw.text.trim local split = mw.text.split local sub, gsub = mw.ustring.sub, mw.ustring.gsub local match, gmatch = mw.ustring.match, mw.ustring.gmatch local to_cp, to_char = mw.ustring.codepoint, mw.ustring.char

function export.new(frame, _args) -- _args is used when called from other Lua code local args = _args or frame:getParent.args

local pos_table = { ["n/noun/"] = { "Noun", "noun" }, ["v/verb"] = { "Verb", "verb" }, ["vform/verb form"] = { "Verb", "verb form" }, ["a/adj/adjective"] = { "Adjective", "adj" }, ["adv/adverb"] = { "Adverb", "pos|adverb" }, ["pron/pronoun"] = { "Pronoun", "pos|pronoun" }, ["pn/propn/proper/proper noun"] = { "Proper noun", "pos|proper" }, ["ph/phrase"] = { "Phrase", "phrase" }, ["intj/interjection"] = { "Interjection", "pos|interjection" }, ["conj"] = { "Conjunction", "pos|conjunction" }, ["part"] = { "Particle", "pos|particle" }, ["prep"] = { "Preposition", "pos|preposition" }, ["suf/suffix"] = { "Suffix", "pos|suffix" }, ["pref/prefix"] = { "Prefix", "pos|prefix" }, ["affix"] = { "Affix", "pos|affix" }, ["prov"] = { "Proverb", "pos|proverb" }, ["id/idiom"] = { "Idiom", "pos|idiom" }, ["adn/adnominal"] = { "Adnominal", "pos|adnominal" }, }	local _pos_table = pos_table; pos_table = {} for k, v in pairs(_pos_table) do for i in mw.text.gsplit(k, "/") do pos_table[i] = v end end

local result = args["en"] and ("===Etymology " .. args["en"] .. "===") or "==Bulgarian==" local hf = args["en"] and "=" or ""

if args["defs"] then result = result .. "\n" end if args["wp"] then result = result .. "\n" end wp_count = 2 while args["wp" .. wp_count] do result = result .. "\n" wp_count = wp_count + 1 end

text = args[1] ~= "" and args[1] or pagename

-- Generate subsection for arguments like alt1, alt2, etc. in a loop. local function other(class, title, args) local code, i = "", 2

if args[class] then code = code .. "\n\n===" .. hf .. title .. hf .. "===\n* "

while args[class .. i] do code = code .. "\n* " i = i + 1 end end

return code end

result = result .. other("alt", "Alternative forms", args)

sortkey = true

--TODO: this etymology section if args["d"] or args["e"] or args["b"] or args["lb"] or args["c"] or args["co1"] or args["bl1"] or args["pr1"] or args["su1"] then result = result .. "\n\n" if not args["en"] then result = result .. hf .. "===Etymology===" .. hf .. "\n" end if args["c"] then result = result .. ""			if args["pr1"] then result = result .. ", equivalent to bg" elseif args["su1"] then result = result .. ", equivalent to " elseif args["co1"] then result = result .. ", equivalent to " elseif args["bl1"] then result = result .. ", equivalent to a " end elseif args["pr1"] then result = result .. "From bg" elseif args["su1"] then result = result .. "From " elseif args["co1"] then result = result .. "From " elseif args["bl1"] then result = result .. ""		elseif args["b"] then result = result .. ""		elseif args["lb"] then result = result .. ""		else result = result .. (args["e"] or				("From ")) end end

-- Add pronunciation result = result .. "\n\n" .. hf .. "===Pronunciation===" .. hf .. "\n* ")	acc_count = 2	while args["acc" .. acc_count] do	   result = result .. "\n* "		acc_count = acc_count + 1	end

if args["hmp"] then result = result .. (args["hmp"] and ""	end	if args["eg"] then		args[3] = args[3] .. "\n#" .. args["eg"]	end	local eg_count = 2	while args["eg" .. eg_count] do		args[3] = args[3] .. "\n#" .. args["eg" .. eg_count]		eg_count = eg_count + 1	end	local function add_pos(header, template_name, def)		result = result .. "\n\n" .. hf .. "===" .. header .. "===" .. hf .. "\n"		result = result .. "\n\n# " .. (def or "")

if header == "Adjective" or header == "Noun" then a_or_n = (header == "Adjective") and "a" or "n" result = result .. "\n\n" .. hf .. "====Declension====" .. hf .. "\n" result = result .. "{{bg-" .. a_or_n .. "decl|" .. text .. "<" .. (args["infl"] or "") .. ">"		end if header == "Verb" then result = result .. "\n\n" .. hf .. "====Conjugation====" .. hf .. "\n" result = result .. "{{bg-conj" .. "|" .. text .. "<" .. (args["infl"] or "") .. ">"		end end if not pos_table[args[2]] then error("The POS code " .. args[2] .. " is not valid. See Module:bg-new for a list of valid POS.") end add_pos(pos_table[args[2]][1], pos_table[args[2]][2], args[3]) if args["use"] then result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args["use"] end result = result .. other("syn", "=Synonyms=", args) result = result .. other("ant", "=Antonyms=", args) result = result .. other("der", "=Derived terms=", args) result = result .. other("rel", "=Related terms=", args) if args[4] then if not pos_table[args[4]] then error("The POS code " .. args[4] .. " is not valid. See Module:ja-new for a list of valid POS.") end add_pos(pos_table[args[4]][1], pos_table[args[4]][2], args[5]) if args["Use"] then result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args["Use"] end result = result .. other("Syn", "=Synonyms=", args) result = result .. other("Ant", "=Antonyms=", args) result = result .. other("Der", "=Derived terms=", args) result = result .. other("Rel", "=Related terms=", args) end if args[6] then if not pos_table[args[6]] then error("The POS code " .. args[6] .. " is not valid. See Module:ja-new for a list of valid POS.") end add_pos(pos_table[args[6]][1], pos_table[args[6]][2], args[7]) if args["USE"] then result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args["Use"] end result = result .. other("SYN", "=Synonyms=", args) result = result .. other("ANT", "=Antonyms=", args) result = result .. other("DER", "=Derived terms=", args) result = result .. other("REL", "=Related terms=", args) end if args[8] then error("this template only supports up to three POS, please ask someone to expand me") end

result = result .. other("ana", "Anagrams", args) result = result .. other("also", "See also", args) if args["ref"] ~= "n" then result = result .. "\n\n===References===\n" if args["ref"] == "1" then result = result .. "* {{R:bg:RBE}}" elseif args["ref"] == "2" then result = result .. "* {{R:bg:RBE2}}" else result = result .. "* {{R:bg:RBE}}\n* {{R:bg:RBE2}}" end end

if args["cn"] then result = result .. "\n\n{{cln|bg|" .. args["cn"] cn_count = 2 while args["cn" .. cn_count] do result = result .. "|" .. args["cn" .. cn_count] cn_count = cn_count + 1 end result = result .. (args["defs"] and "" or args["sort"] and "|sort=" .. args["sort"] or sortkey or "") .. "}}"	end if args["ct"] then result = result .. (args["cn"] and "\n" or "\n\n") .. "{{C|bg|" .. args["ct"] ct_count = 2 while args["ct" .. ct_count] do result = result .. "|" .. args["ct" .. ct_count] ct_count = ct_count + 1 end result = result .. (args["defs"] and "" or args["sort"] and "|sort=" .. args["sort"] or sortkey or "") .. "}}"	end

return result end

return export