Dokumentaci tohoto modulu lze vytvořit na stránce Nápověda:Modul:CommonsLink

require 'Modul:No globals'

local WD = require 'Modul:Wikidata-WP'
local lib = require 'Modul:Wikidata/lib'
local getArgs = (require 'Modul:Arguments').getArgs

local function gallery(args, of)
	args.property = 'P935'
	args.of = of
	local link = WD.getRawValueFromLua(args)
	if not link then
		args.site = 'commonswiki'
		link = WD.getSitelinkFromLua(args)
		if not link or mw.ustring.sub(link, 1, #'Category:') == 'Category:' then
			return nil
		end
	end
	if lib.IsOptionTrue(args, 'link') then
		return mw.ustring.format('[[c:%s|%s]]', link, link)
	end
	return link
end

local function category(args, of)
	local args = args --mw.clone(args)
	args.property = 'P373'
	args.of = of
	local link = WD.getRawValueFromLua(args)
	if not link then
		args.site = 'commonswiki'
		link = WD.getSitelinkFromLua(args)
		if not link or mw.ustring.sub(link, 1, #'Category:') ~= 'Category:' then
			return nil
		else
			link = mw.ustring.sub(link, #'Category:' + 1)
		end
	end
	if lib.IsOptionTrue(args, 'link') then
		return mw.ustring.format('[[c:Category:%s|%s]]', link, link)
	end
	if lib.IsOptionTrue(args, 'prefix') then
		return 'Category:' .. link
	end
	return link
end

local p = {}

function p._gallery(args)
	return gallery(args) or gallery(args, 'P910')
end

function p.gallery(frame)
	return p._gallery(getArgs(frame, { noOverwrite = false, frameOnly = true }))
end

function p._category(args)
	return category(args) or category(args, 'P910')
end

function p.category(frame)
	return p._category(getArgs(frame, { noOverwrite = false, frameOnly = true }))
end

function p._any(args)
	return p._category(args) or p._gallery(args) 
end

function p.any(frame)
	return p._any(getArgs(frame, { noOverwrite = false, frameOnly = true }))
end

return p