Module:Cite Almanac FS
Appearance
require ('strict')
local get_args = require ('Module:Arguments').getArgs;
local data = mw.loadData ('Module:Cite Almanac FS/data');
--[[--------------------------< 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
--[[--------------------------< E R R _ M E S S A G I N G >----------------------------------------------------
create a formatted error message using Module:Citation/CS1/styles.css for styling
]]
local function err_messaging (frame, message)
return substitute (data.error_messages_t.msg_format, {
frame:extensionTag ('templatestyles', '', {src=data.error_messages_t.templatestyles}),
data.error_messages_t.template_name,
message
});
end
--[[--------------------------< U N K N O W N _ P A R A M S _ C H E C K >--------------------------------------
looks at all parameters in the template (parent) frame; returns first unrecognized parameter name; nil else
]]
local function unknown_params_check (frame)
local args_t = frame:getParent().args; -- get parameters in the calling template frame
for param, _ in pairs (args_t) do
if not data.known_parameters_t[param] then -- do we recognize <param>?
return param; -- no; return unknown <param> for use in an error message
end
end
end
--[[--------------------------< A L I A S _ G E T >------------------------------------------------------------
extracts the first alias listed in <aliases_t> from <args_t>; if present, returns the parameter alias and the
assigned value; nil else. Only applies to the parameters that are unique to this template (the parameters listed
in <unique_parameters_t>).
]]
local function alias_get (aliases_t, args_t)
for _, param in ipairs (aliases_t) do -- for each parameter in the <aliases_t> sequence
if args_t[param] then -- if this parameter is in the template call (<args_t>)
return param, args_t[param]; -- return the parameter name and its value
end
end
end
--[[--------------------------< M A I N >----------------------------------------------------------------------
{{#invoke:Cite Almanac FS|main|section=women|subsection=c}}
|section= accepts these keywords (case insensitive):
men
women
coed -- no subsections
men-inactive -- no subsections
women-inactive -- no subsections
institutions
closed-institutions -- no subsections
founding-chronological -- no subsections
founding-of-fs-system --no subsections
none -- suppress {{cite book}} section rendering (displays only {{cite book}} title)
|subsection= -- required for §men, §women, §institutions; accepts a single letter (case insensitive)
|organization= -- optional organization name; concatenated with |section= and |subsection= values;
ignored when |section=none
|last-first= --optional; accepts one value; when set to yes uses:
|editor-last=Lurding and |editor-first=Carroll and
|editor2-last=Becque and |editor2-first=Fran;
uses |editor=Carroll Lurding and |editor2=Fran Becque else
|access-date= -- supported cs1|2 parameters
|archive-url=
|archive-date=
|date=
|mode=
|page=
|pages=
|ref=
|url-status=
any other parameters are rejected with an error message.
]]
local function main (frame)
local error_msg = unknown_params_check (frame); -- look at all parameters
if error_msg then
error_msg = substitute (data.error_messages_t.unknown_parameter, {error_msg}); -- at least one parameter is not recognized so
return err_messaging (frame, error_msg); -- emit an error message and abandon
end
local args_t = get_args (frame); -- get a proper table of parameters with assigned values
local Section_origin, Section = alias_get (data.unique_aliases_t.section_t, args_t);
local Subsection_origin, Subsection = alias_get (data.unique_aliases_t.subsection_t, args_t);
local Last_first_origin, Last_first = alias_get (data.unique_aliases_t.last_first_t, args_t);
local Organization_origin, Organization = alias_get (data.unique_aliases_t.organization_t, args_t);
local section_index = Section and data.keywords_t[Section:lower()] or nil; -- nil when |section= empty/omitted or unknown
local subsection_index = Subsection and Subsection:upper() or nil;
if not section_index and not Section then
return err_messaging (frame, data.error_messages_t.section_required); -- emit an error message and abandon
elseif 'none' ~= section_index then
if not data.sections_t[section_index] then -- error when section does not exist
error_msg = substitute (data.error_messages_t.unknown_section, {Section});
return err_messaging (frame, error_msg); -- emit an error message and abandon
end
end
if ('none' ~= section_index) then -- if section not suppressed
if data.sections_t[section_index]['subsection url'] and not Subsection then -- no subsection error when no subsection url
return err_messaging (frame, data.error_messages_t.subsection_required); -- emit an error message and abandon
elseif Subsection then
if not data.sections_t[section_index][subsection_index] then -- error when subsection does not exist
error_msg = substitute (data.error_messages_t.unknown_subsection, {Subsection});
return err_messaging (frame, error_msg); -- emit an error message and abandon
end
end
end
local last_first = data.affirmative_t[Last_first]; -- boolean for editor name-order TODO: add 'yes' 'aliasing mechanism'
local cite_args_t = {}; -- initialize a {{cite book}} template
for param, value in pairs (data.static_param_data_t) do
cite_args_t[param] = value;
end
if last_first then
cite_args_t['editor-last'] = data.static_editor_data_t['editor-last'];
cite_args_t['editor-first'] = data.static_editor_data_t['editor-first'];
cite_args_t['editor2-last'] = data.static_editor_data_t['editor2-last'];
cite_args_t['editor2-first'] = data.static_editor_data_t['editor2-first'];
else
cite_args_t.editor = data.static_editor_data_t.editor;
cite_args_t.editor2 = data.static_editor_data_t.editor2;
end
if 'none' ~= section_index then
if data.sections_t[section_index]['subsection url'] then -- if we have a base subsection url
if data.keywords_t.index == subsection_index then -- are we rendering a link to the index page?
cite_args_t['section-url'] = data.sections_t[section_index]['section url']; -- create |section-url=
cite_args_t.section = substitute (data.section_title_formating_t.index, { -- create |section=
data.sections_t[section_index]['section title'],
data.section_title_formating_t.index_label
});
else
cite_args_t['section-url'] = substitute ('$1$2', {data.sections_t[section_index]['subsection url'], data.sections_t[section_index][subsection_index]}); -- create |section-url=
if Organization then
cite_args_t.section = substitute (data.section_title_formating_t.org, { -- create |section= with organization name
data.sections_t[section_index]['section title'], -- the section title
subsection_index, -- the subsection index letter
Organization -- organization name
});
else -- organization not named
cite_args_t.section = substitute (data.section_title_formating_t.plain, {data.sections_t[section_index]['section title'], subsection_index}); -- append the subsection index letter
end
end
else -- here when no base subsection url
cite_args_t['section-url'] = data.sections_t[section_index]['section url']; -- use the section url
if Organization then
cite_args_t.section = substitute (data.section_title_formating_t.org_no_subsection, {
data.sections_t[section_index]['section title'],
Organization -- append organization name
});
else -- organization not named
cite_args_t.section = data.sections_t[section_index]['section title'];
end
end
cite_args_t['section-format'] = 'PDF'; -- mark |section= as a pdf document
end
for _, param in ipairs (data.cs1_parameters_t) do -- add supported cs1|2 parameters to <cite_args_t>
if args_t[param] then -- if this <param> was present in the template call and has a value
cite_args_t[param] = args_t[param]; -- add param / value pair to the {{cite book}} template
end
end
return require ('Module:Citation/CS1')._citation (nil, cite_args_t, {CitationClass='book'}); -- render the {{cite book}} template
end
--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]
return {
main = main,
}