Jump to content

Module:Cite/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 16:00, 23 April 2025 (If we're not grabbing classes, this is no longer needed). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require ('strict');

local cfg = mw.loadData ('Module:Cite/config/sandbox');							-- Sandbox includes aliases with alternative capitalization (e.g. arXiv)


--[[--------------------------< 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.

TODO: this function goes away if positinal parameter use goes away
]]

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.

TODO: this function goes away if positinal parameter use goes away
]]

local function make_error_msg (frame, msg, nocat)
	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 not nocat) and substitute ('[[Category:$1]]', {cfg.settings_t.err_category}) or '';
	return substitute ('<span style="color:#d33">Error: &#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 (main namespace only)
		})
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|cite|book|title=Title}}

]]

local function cite (frame, t)													-- if positional parameter use goes away, <t> renames to <template>
	local args_t = require ('Module:Arguments').getArgs (frame, {frameOnly=true});	-- simplify if positional parameter not used?

-- <start> all of this goes away if positional parameter not used; after fixes to various templates that use this module
-- this goes away because we can't get here without we already know that the calling function exists
	if t then args_t[1] = t end                                                 -- override args_t[1] if called using template name as function
	if not args_t[1] then														-- this is the template name; we must have a template name
		return make_error_msg (frame, cfg.error_messages_t.missing);			-- no template name; abandon with error message
	end
	
	local template = args_t[1]:lower();											-- lowercase for table indexes
	args_t[1] = nil;															-- unset, no longer needed (and would break the cs1|2 template)
	
	if not cfg.known_templates_lower_case_t[template] then						-- do we recognize this template name?
		return make_error_msg (frame, substitute (cfg.error_messages_t.unknown, {template}));	-- nope; abandon with error message
	end
-- <end>all of  this goes away if positional parameter not used	
--	template = template:lower();								-- add this line if positional parameter use goes away	-- lowercase for table indexing
	local config_t = {['CitationClass'] = cfg.citation_classes_t[template] or template};
	return require ('Module:Citation/CS1')._citation (nil, args_t, config_t);	-- go render the citation
end


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

local p = {
	cite = cite,																-- these go away if we adopt the code below !after necessary
	Cite = cite,																-- edits to the various templates that already use this module
	}

for _, templates_t in ipairs ({cfg.known_templates_lower_case_t, cfg.known_templates_mixed_case_t}) do
	for template, _ in pairs (templates_t) do									-- iterate over known template names and aliases
		p[template] = function (frame) return cite (frame, template) end;		-- create a function for each template (e.g. {{#invoke:cite|book|title=Title}})
		p[template:gsub("^%l", string.upper)] = function (frame) return cite (frame, template) end;	-- create a function for first letter capitalized (e.g. {{#invoke:cite|Book|title=Title}})
	end
end

return p