モジュール:サンドボックス/Ef3/SNSuty/sandbox

モジュールの解説[作成]
-- id 
-- name 
-- lang
-- idProp
-- formatter
-- formatterProp
-- from
-- account_pre
-- account_post
-- category (no impl.)

local libraryUtil = require('libraryUtil')

local SNS_Utils = {};

local checkType = libraryUtil.checkType

local function v(val)
	if val and #val > 0 then
		return val;
	else
		return nil;
	end
end

local function getWikidataProperty(property, from )
    local entity = nil;
    if v(from) then
    	entity = mw.wikibase.getEntityObject(from);
    else
    	entity = mw.wikibase.getEntityObject();
    end
    if not entity then
        return nil;
    end
    local claims = entity.claims or {};
    local hasProp = claims[property];
    if not hasProp then
        return nil;
    end
    return hasProp[1].mainsnak.datavalue.value;
end	

function SNS_Utils.dump(frame)
	return mw.dumpObject(frame);
end

function SNS_Utils.main(frame)
	local args = frame.args or frame;
	local id = v(args.id) or getWikidataProperty(args.idProp, args.from);
	local formatterURL = v(args.formatter) or mw.text.decode(getWikidataProperty(args.formatterProp, args.idProp));
	local urlF = function(i) return mw.ustring.gsub(formatterURL or "No formatterURL", "$1", i) end
	local nameF = function() return v(args.name) or mw.ustring.gsub(mw.title.getCurrentTitle().text or "No Title", "%s+%b()$", "%1") end
	local langF = function(lang, contents)
		return lang and mw.ustring.format('<span xml:lang="%s" lang="%s">%s</span>', lang, lang, contents) or contents 
	end
--	if v(args.lang) then
--		name = langF(args.lang,nameF())
--	end
	local account = '';
	if args.account_pre or args.account_post then
		account = args.account_pre .. id .. args.account_post;
	end
	if mw.ustring.match(nameF(), account .. '$') then
		account = '';
	end
	
	local argsF = function() return {
		url = urlF(id), 
		urlF = urlF;
		name = langF(args.lang, nameF()), 
		lang = args.lang,
		id = id, 
		account = account,
		getWikidataProperty = getWikidataProperty,
		} end
	if args.proc then
		return args.proc(argsF());
	end
	if args.templ then
		return frame:expandTemplate{ title = args.templ, args = argsF() }
	end
	return mw.ustring.format(args.page and '[%s %s]%s - %s' or '[%s %s]%s',
			urlF(id),
			langF(args.lang, nameF()),
			account,
			args.page);
end

return SNS_Utils;