Module:Cite: Difference between revisions

Content deleted Content added
create;
 
simplify
 
(9 intermediate revisions by 2 users not shown)
Line 1:
require ('strict');
 
local cfg = mw.loadData ('Module:Cite/config');
--[[--------------------------< K N O W N _ T E M P L A T E S _ T >-------------------------------------------
 
list of all known cs1|2 templates by their canonical names
 
--[[--------------------------< KS NU OB WS NT _I T E M P L AU T E S _ T >----------------------------------------------------------
key is lowercase canonical template name
 
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)
local known_templates_t = {
return data_t and mw.message.newRawMessage (message, data_t):plain() or message;
['arxiv'] = true,
end
['av media'] = true,
['av media notes'] = true,
['biorxiv'] = true,
['book'] = true,
['citation'] = true,
['citeseerx'] = true,
['conference'] = true,
['document'] = true,
['encyclopedia'] = true,
['episode'] = true,
['interview'] = true,
['journal'] = true,
['magazine'] = true,
['mailing list'] = true,
['map'] = true,
['medrxiv'] = true,
['news'] = true,
['newsgroup'] = true,
['podcast'] = true,
['press release'] = true,
['report'] = true,
['serial'] = true,
['sign'] = true,
['speech'] = true,
['SSRN'] = true,
['tech report'] = true,
['thesis'] = true,
['web'] = true,
}
 
 
--[[--------------------------< C I TM A TK IE O_ NE _R CR LO AR S_ S EM S _ TG >--------------------------------------------------
 
Assembles an error message from module name, message text, help link, and error category.
|CitationClass= in the cs1|2 templates gets the lowercase template name except for these
 
key is lowercase canonical template name
 
]]
 
local function make_error_msg (frame, msg)
local citation_classes_t = { --
local module_name = frame:getTitle(); -- get the module name for prefix and help-link label
['av media'] = 'audio-visual',
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization
['av media notes'] = 'AV-media-notes',
['encyclopedia'] = 'encyclopaedia',
['mailing list'] = 'mailinglist',
['press release'] = 'pressrelease',
['tech report'] = 'techreport',
}
 
local category_link = (0 == namespace) 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 (for main namespace errors only)
})
end
}
 
--[[--------------------------< C I T E >---------------------------------------------------------------------
 
testFunction to prove thatcall Module:Citation/CS1/sandbox canwith beappropriate calledparameters. from anotherFor Moduleuse when an article exceeds the
post-expand include size limit.
 
{{#invoke:Sandbox/trappist the monk/cite|cite|book|title=Title}}
 
TODO: needs better error handling
 
]]
 
local function cite (frame, template)
local args_t = require ('Module:Arguments').getArgs (frame, {frameOnly=true});
 
if not args_t[1] then -- this is the template name; we must have a template name
local template = args_t[1]template:lower(); -- lowercase for table indexes
return error ('missing template name as first positional parameter'); -- 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_t[template] then -- do we recognize this template name?
return errormake_error_msg ('unknownframe, template name: '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
return require ('Module:Citation/CS1/sandbox')._citation (nil, args_t, config_t); -- go render the citation
end
 
Line 91 ⟶ 65:
]]
 
return setmetatable({}, {__index = -- returns an empty TABLE whose metatable has the __index set so that, for any given KEY, it returns
return {
function(_, template) -- this anonymous function called as function(TABLE, KEY)
cite = cite,
return function (frame) return cite (frame, template) end; -- which in turn returns a function that calls cite() with the KEY name
}
end
})