跳转到内容

模組:Cite

本页使用了标题或全文手工转换
维基百科,自由的百科全书

-- 源自[[:en:Module:Cite]]的本地化版本。当前基于 https://en.wikipedia.org/w/index.php?title=Module:Cite&oldid=1290436560

-- 本地[[Module:Citation/CS1]]的入口函数citation为全局变量,因此不兼容strict
-- require ('strict');

local cfg = mw.loadData ('Module:Cite/config');


--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------

Substitutes $1, $2, etc in <message> with data from <data_t>. Returns plain-text substituted string when
<data_t> not nil; returns <message> else.

]]

local function substitute (message, data_t)
	return data_t and mw.message.newRawMessage (message, data_t):plain() or message;
end


--[[--------------------------< M A K E _ E R R O R _ M S G >--------------------------------------------------

Assembles an error message from module name, message text, help link, and error category.

]]

local function make_error_msg (frame, msg)
	local module_name = frame:getTitle();										-- get the module name for prefix and help-link label
	local namespace = mw.title.getCurrentTitle().namespace;						-- used for categorization

	local category_link = (0 == namespace) and substitute ('[[Category:$1]]', {cfg.settings_t.err_category}) or '';
	return substitute ('<span style="color:#d33">&#x7B;{[[$1|#invoke:$2]]}}错误:$3 ([[:$4|$5]])</span>$6',
		{
		module_name,															-- the module name with namespace
		module_name:gsub ('Module:', ''),										-- the module name without namespace
		msg,																	-- the error message
		cfg.settings_t.help_text_link,											-- help wikilink to text at help page
		cfg.settings_t.help,													-- help wikilink display text
		category_link															-- link to error category (for main namespace errors only)
		})
end


-- 本地化改动。方法来自[[Module:Cite web]]

local function patch_frame (original_frame, new_args)
	local proxyFrame = {
		getParent = function(self)
			return original_frame
		end,
		getTitle = function(self)
			return 'Module:Citation/CS1'
		end,
		args = new_args
	}

	setmetatable(proxyFrame, {
		__index = function(t, k)
			if type(original_frame[k]) == 'function' then
				return function(...)
					return original_frame[k](original_frame, select(2, ...))
				end
			else
				return original_frame[k]
			end
		end
	})

	return proxyFrame
end


--[[--------------------------< C I T E >---------------------------------------------------------------------

Function to call Module:Citation/CS1/sandbox with appropriate parameters.  For use when an article exceeds the
post-expand include size limit.

	{{#invoke:cite|book|title=Title}}

]]

local function cite (frame, template)
	-- 目前类别参数为英语。若含有非ASCII字符则需改用mw.ustring.lower()
	template = template:lower();												-- lowercase for table indexes
	
	if not cfg.known_templates_t[template] then									-- do we recognize this template name?
		return make_error_msg (frame, substitute (cfg.settings_t.unknown_name, {template}));	-- nope; abandon with error message
	end

	local config_t = {['CitationClass'] = cfg.citation_classes_t[template] or template};	-- set CitationClass value

	-- 本地[[Module:Citation/CS1]]与英文维基百科结构不同,只有citation一个入口,且要求传入frame。
	-- 它的citation函数会将本层frame的args参数表当作已解析的参数,直接存入内部的config和args变量;
	-- 随后再解析并校验parent frame的args参数表(即条目页传给{{cite web}}模板的参数),结果将并入内部的args变量。
	-- 考虑到本模块会被用户直接在条目中调用,参数不规范的机会远多于模板作者在模板中预设CS1参数的情形,
	-- 因此应当使用Module:Citation/CS1对于{{cite web}}的校验逻辑。
	-- 故选择修改frame变量,将本模块的传入参数伪装成parent frame的参数,以复用相关逻辑。
	local newFrame = patch_frame(frame, config_t)
	return require ('Module:Citation/CS1').citation (newFrame);	-- go render the citation
end


--[[--------------------------< E X P O R T S >---------------------------------------------------------------
]]

return setmetatable({}, {__index =												-- returns an empty TABLE whose metatable has the __index set so that, for any given KEY, it returns
	function(_, template)														-- this anonymous function called as function(TABLE, KEY)
		return function (frame) return cite (frame, template) end;				-- which in turn returns a function that calls cite() with the KEY name
	end
})