Zum Inhalt springen

Modul:Citation/CS1/Utilities

Faan Wikipedia

Die Dokumentation für dieses Modul kann unterModul:Citation/CS1/Utilities/Dokuerstellt werden

localu={}

localz={
error_categories={};-- for categorizing citations that contain errors
error_ids={};
message_tail={};
maintenance_cats={};-- for categorizing citations that aren't erroneous per se, but could use a little work
properties_cats={};-- for categorizing citations based on certain properties, language of source for instance
};


--[[--------------------------< F O R W A R D D E C L A R A T I O N S >--------------------------------------
]]

localcfg;-- table of tables imported from slected Module:Citation/CS1/Configuration


--[[--------------------------< I S _ S E T >------------------------------------------------------------------

Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.
This function is global because it is called from both this module and from Date validation

]]
localfunctionis_set(var)
returnnot(var==nilorvar=='');
end


--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------

Whether needle is in haystack

]]

localfunctionin_array(needle,haystack)
ifneedle==nilthen
returnfalse;
end
forn,vinipairs(haystack)do
ifv==needlethen
returnn;
end
end
returnfalse;
end


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

Populates numbered arguments in a message string using an argument table.

]]

localfunctionsubstitute(msg,args)
returnargsandmw.message.newRawMessage(msg,args):plain()ormsg;
end


--[[--------------------------< E R R O R _ C O M M E N T >----------------------------------------------------

Wraps error messages with css markup according to the state of hidden.

]]
localfunctionerror_comment(content,hidden)
returnsubstitute(hiddenandcfg.presentation['hidden-error']orcfg.presentation['visible-error'],content);
end


--[[--------------------------< S E T _ E R R O R >--------------------------------------------------------------

Sets an error condition and returns the appropriate error message. The actual placement of the error message in the output is
the responsibility of the calling function.

]]
localfunctionset_error(error_id,arguments,raw,prefix,suffix)
localerror_state=cfg.error_conditions[error_id];

prefix=prefixor"";
suffix=suffixor"";

iferror_state==nilthen
error(cfg.messages['undefined_error']);
elseifis_set(error_state.category)then
table.insert(z.error_categories,error_state.category);
end

localmessage=substitute(error_state.message,arguments);

message=message.."([["..cfg.messages['help page link']..
"#"..error_state.anchor.."|"..
cfg.messages['help page label'].."]])";

z.error_ids[error_id]=true;
ifin_array(error_id,{'bare_url_missing_title','trans_missing_title'})
andz.error_ids['citation_missing_title']then
return'',false;
end

message=table.concat({prefix,message,suffix});

ifraw==truethen
returnmessage,error_state.hidden;
end

returnerror_comment(message,error_state.hidden);
end


--[[-------------------------< I S _ A L I A S _ U S E D >-----------------------------------------------------

This function is used by select_one() to determine if one of a list of alias parameters is in the argument list
provided by the template.

Input:
args – pointer to the arguments table from calling template
alias – one of the list of possible aliases in the aliases lists from Module:Citation/CS1/Configuration
index – for enumerated parameters, identifies which one
enumerated – true/false flag used choose how enumerated aliases are examined
value – value associated with an alias that has previously been selected; nil if not yet selected
selected – the alias that has previously been selected; nil if not yet selected
error_list – list of aliases that are duplicates of the alias already selected

Returns:
value – value associated with alias we selected or that was previously selected or nil if an alias not yet selected
selected – the alias we selected or the alias that was previously selected or nil if an alias not yet selected

]]

localfunctionis_alias_used(args,alias,index,enumerated,value,selected,error_list)
ifenumeratedthen-- is this a test for an enumerated parameters?
alias=alias:gsub('#',index);-- replace '#' with the value in index
else
alias=alias:gsub('#','');-- remove '#' if it exists
end

ifis_set(args[alias])then-- alias is in the template's argument list
ifvalue~=nilandselected~=aliasthen-- if we have already selected one of the aliases
localskip;
for_,vinipairs(error_list)do-- spin through the error list to see if we've added this alias
ifv==aliasthen
skip=true;
break;-- has been added so stop looking
end
end
ifnotskipthen-- has not been added so
table.insert(error_list,alias);-- add error alias to the error list
end
else
value=args[alias];-- not yet selected an alias, so select this one
selected=alias;
end
end
returnvalue,selected;-- return newly selected alias, or previously selected alias
end


--[[--------------------------< A D D _ M A I N T _ C A T >------------------------------------------------------

Adds a category to z.maintenance_cats using names from the configuration file with additional text if any.
To prevent duplication, the added_maint_cats table lists the categories by key that have been added to z.maintenance_cats.

]]

localadded_maint_cats={}-- list of maintenance categories that have been added to z.maintenance_cats
localfunctionadd_maint_cat(key,arguments)
ifnotadded_maint_cats[key]then
added_maint_cats[key]=true;-- note that we've added this category
table.insert(z.maintenance_cats,substitute(cfg.maint_cats[key],arguments));-- make name then add to table
end
end


--[[--------------------------< S A F E _ F O R _ I T A L I C S >----------------------------------------------

Protects a string that will be wrapped in wiki italic markup ''... ''

Note: We cannot use <i> for italics, as the expected behavior for italics specified by ''...'' in the title is that
they will be inverted (i.e. unitalicized) in the resulting references. In addition, <i> and '' tend to interact
poorly under Mediawiki's HTML tidy.

]]

localfunctionsafe_for_italics(str)
ifnotis_set(str)then
returnstr;
else
ifstr:sub(1,1)=="'"thenstr="<span></span>"..str;end
ifstr:sub(-1,-1)=="'"thenstr=str.."<span></span>";end

-- Remove newlines as they break italics.
returnstr:gsub('\n',' ');
end
end


--[[--------------------------< W R A P _ S T Y L E >----------------------------------------------------------

Applies styling to various parameters. Supplied string is wrapped using a message_list configuration taking one
argument; protects italic styled parameters. Additional text taken from citation_config.presentation - the reason
this function is similar to but separate from wrap_msg().

]]

localfunctionwrap_style(key,str)
ifnotis_set(str)then
return"";
elseifin_array(key,{'italic-title','trans-italic-title'})then
str=safe_for_italics(str);
end

returnsubstitute(cfg.presentation[key],{str});
end


--[[--------------------------< S E L E C T _ O N E >----------------------------------------------------------

Chooses one matching parameter from a list of parameters to consider. The list of parameters to consider is just
names. For parameters that may be enumerated, the position of the numerator in the parameter name is identified
by the '#' so |author-last1= and |author1-last= are represented as 'author-last#' and 'author#-last'.

Because enumerated parameter |<param>1= is an alias of |<param>= we must test for both possibilities.


Generates an error if more than one match is present.

]]

localfunctionselect_one(args,aliases_list,error_condition,index)
localvalue=nil;-- the value assigned to the selected parameter
localselected='';-- the name of the parameter we have chosen
localerror_list={};

ifindex~=nilthenindex=tostring(index);end

for_,aliasinipairs(aliases_list)do-- for each alias in the aliases list
ifalias:match('#')then-- if this alias can be enumerated
if'1'==indexthen-- when index is 1 test for enumerated and non-enumerated aliases
value,selected=is_alias_used(args,alias,index,false,value,selected,error_list);-- first test for non-enumerated alias
end
value,selected=is_alias_used(args,alias,index,true,value,selected,error_list);-- test for enumerated alias
else
value,selected=is_alias_used(args,alias,index,false,value,selected,error_list);--test for non-enumerated alias
end
end

if#error_list>0and'none'~=error_conditionthen-- for cases where this code is used outside of extract_names()
localerror_str="";
for_,kinipairs(error_list)do
iferror_str~=""thenerror_str=error_str..cfg.messages['parameter-separator']end
error_str=error_str..wrap_style('parameter',k);
end
if#error_list>1then
error_str=error_str..cfg.messages['parameter-final-separator'];
else
error_str=error_str..cfg.messages['parameter-pair-separator'];
end
error_str=error_str..wrap_style('parameter',selected);
table.insert(z.message_tail,{set_error(error_condition,{error_str},true)});
end

returnvalue,selected;
end


--[=[-------------------------< R E M O V E _ W I K I _ L I N K >----------------------------------------------

Gets the display text from a wikilink like [[A|B]] or [[B]] gives B

]=]

localfunctionremove_wiki_link(str)
return(str:gsub("%[%[([^%[%]]*)%]%]",function(l)
returnl:gsub("^[^|]*|(.*)$","%1"):gsub("^%s*(.-)%s*$","%1");
end));
end


--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >--------------------------------------

Sets local cfg table to same (live or sandbox) as that used by the other modules.

]]

localfunctionset_selected_modules(cfg_table_ptr)
cfg=cfg_table_ptr;
end



return{-- return exported functions and tables
is_set=is_set,
in_array=in_array,
substitute=substitute,
error_comment=error_comment,
set_error=set_error,
select_one=select_one,
add_maint_cat=add_maint_cat,
wrap_style=wrap_style;
safe_for_italics=safe_for_italics;
remove_wiki_link=remove_wiki_link;
set_selected_modules=set_selected_modules;
z=z,
}