Module:IgenSub

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code

--[=[
This submodule extends one-letter-code into the subcategory-text for a "SVG created with ...:Topics"

More specific subcategorizing needs 2 or 3 letters - see the templates code.

See also: {{Igen/top}}; {{COAInformation/Imgen}}
]=]
local json
local p = {}

function get(frame, data, key)
	local r -- return
	if not key then
		key = frame.args[1]
	end

	if not key then return "no parameter" end
	if key == "" then return ""	end

	--frame = mw.getCurrentFrame()
	data = frame:expandTemplate{ title = data }
	-- convert data
	json = mw.text.jsonDecode( data )
	-- look for key
	r = json[key]
	-- fallback
	if not r then r = key end
	return r
end


p.sub = function (frame)
	local a = frame.args
	local r = ""
	local data = "Template:Igen/sub.json"

	if not a[1] then
		a = frame:getParent().args
	else
		return get(frame, data, a[1])
	end
	
	if a.sub then
		if a.sub ~= "-" then
			r = a.sub
		end
	elseif a.S then
		if a.S ~= "" then
			r = " "..get(frame, data, a.S)
		end
	elseif a.sc then
		r = ":"..get(frame, data, a.sc)
	elseif a["s:"] then
		r = ":"..get(frame, data, a["s:"]):gsub("^%l", string.upper).."s" -- mw.getContentLanguage():ucfirst( 'string' )
	elseif a["S:"] then
		r = ": "..get(frame, data, a["S:"]):gsub("^%l", string.upper).."s"
	elseif a.sm then
		r = "-"..get(frame, data, a.sm)
	elseif a["s-"] then
		r = "-"..get(frame, data, a["s-"]):gsub("^%l", string.upper).."s"
	elseif a.s then
		r = get(frame, data, a.s)
	elseif a["sub:"] then
		r = ":"..a["sub:"]
	elseif a["Sub:"] and a["Sub:"] ~= "" then
		r = ": "..a["Sub:"]
	elseif a[1] and mw.ustring.lower( a[1] ) == "a$" then
		r = "-Commonist"
	end

	return r
end


p.top = function (frame)
	local langSwitch = require("Module:LangSwitch")

	local args = frame.args
	-- if no expected args provided than check parent template/module args
	if not args.en then
		frame = mw.getCurrentFrame()
		args = frame:getParent().args
	end

	-- resolve lang
	local lang = args.lang
	if not lang or not mw.language.isSupportedLanguage(lang) then
		lang = frame:callParserFunction("int", "lang") -- get user's chosen language
	end

	args = get(frame, "Template:Igen/top.json")
	-- we need table object, resolve data link
	if type( args ) == "string" then
		args = json[ args ]
	end

	frame.args = args
	args = langSwitch._langSwitch(args, lang)
	return args
end

return p


--[=[
function()
		-- local cud = get()
		-- cud ~= nil and #cud > 0
	return get()
end
]=]