localu={}localz={error_categories={};-- for categorizing citations that contain errorserror_ids={};message_tail={};maintenance_cats={};-- for categorizing citations that aren't erroneous per se, but could use a little workproperties_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 selected 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.]]localfunctionis_set(var)returnnot(var==nilorvar=='');end--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------Whether needle is in haystack]]localfunctionin_array(needle,haystack)ifneedle==nilthenreturnfalse;endforn,vinipairs(haystack)doifv==needlethenreturnn;endendreturnfalse;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--[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------Makes a wikilink; when bot link and display text is provided, returns a wikilink in the form [[L|D]]; if onlylink is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns anempty string.]=]localfunctionmake_wikilink(link,display)ifis_set(link)thenifis_set(display)thenreturntable.concat({'[[',link,'|',display,']]'});elsereturntable.concat({'[[',link,']]'});endelsereturn'';endend--[[--------------------------< 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 isthe 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==nilthenerror(cfg.messages['undefined_error']);-- because missing error handler in Module:Citation/CS1/Configurationelseifis_set(error_state.category)thentable.insert(z.error_categories,error_state.category);endlocalmessage=substitute(error_state.message,arguments);message=table.concat({message,' (',make_wikilink(table.concat({cfg.messages['help page link'],'#',error_state.anchor}),cfg.messages['help page label']),')'});--message = table.concat ({message, ' (', substitute (cfg.presentation['wikilink'],--{cfg.messages['help page link'] .. '#' .. error_state.anchor, cfg.messages['help page label']}), ')'});--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']thenreturn'',false;endmessage=table.concat({prefix,message,suffix});ifraw==truethenreturnmessage,error_state.hidden;endreturnerror_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 listprovided by the template.Input:args – pointer to the arguments table from calling templatealias – one of the list of possible aliases in the aliases lists from Module:Citation/CS1/Configurationindex – for enumerated parameters, identifies which oneenumerated – true/false flag used choose how enumerated aliases are examinedvalue – value associated with an alias that has previously been selected; nil if not yet selectedselected – the alias that has previously been selected; nil if not yet selectederror_list – list of aliases that are duplicates of the alias already selectedReturns:value – value associated with alias we selected or that was previously selected or nil if an alias not yet selectedselected – 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 indexelsealias=alias:gsub('#','');-- remove '#' if it existsendifis_set(args[alias])then-- alias is in the template's argument listifvalue~=nilandselected~=aliasthen-- if we have already selected one of the aliaseslocalskip;for_,vinipairs(error_list)do-- spin through the error list to see if we've added this aliasifv==aliasthenskip=true;break;-- has been added so stop lookingendendifnotskipthen-- has not been added sotable.insert(error_list,alias);-- add error alias to the error listendelsevalue=args[alias];-- not yet selected an alias, so select this oneselected=alias;endendreturnvalue,selected;-- return newly selected alias, or previously selected aliasend--[[--------------------------< 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_catslocalfunctionadd_maint_cat(key,arguments)ifnotadded_maint_cats[key]thenadded_maint_cats[key]=true;-- note that we've added this categorytable.insert(z.maintenance_cats,substitute(cfg.maint_cats[key],arguments));-- make name then add to tableendend--[[--------------------------< 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 thatthey will be inverted (i.e. unitalicized) in the resulting references. In addition, <i> and '' tend to interactpoorly under Mediawiki's HTML tidy.]]localfunctionsafe_for_italics(str)ifnotis_set(str)thenreturnstr;elseifstr:sub(1,1)=="'"thenstr="<span></span>"..str;endifstr:sub(-1,-1)=="'"thenstr=str.."<span></span>";end-- Remove newlines as they break italics.returnstr:gsub('\n',' ');endend--[[--------------------------< W R A P _ S T Y L E >----------------------------------------------------------Applies styling to various parameters. Supplied string is wrapped using a message_list configuration taking oneargument; protects italic styled parameters. Additional text taken from citation_config.presentation - the reasonthis function is similar to but separate from wrap_msg().]]localfunctionwrap_style(key,str)ifnotis_set(str)thenreturn"";elseifin_array(key,{'italic-title','trans-italic-title'})thenstr=safe_for_italics(str);endreturnsubstitute(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 justnames. For parameters that may be enumerated, the position of the numerator in the parameter name is identifiedby 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 parameterlocalselected='';-- the name of the parameter we have chosenlocalerror_list={};ifindex~=nilthenindex=tostring(index);endfor_,aliasinipairs(aliases_list)do-- for each alias in the aliases listifalias:match('#')then-- if this alias can be enumeratedif'1'==indexthen-- when index is 1 test for enumerated and non-enumerated aliasesvalue,selected=is_alias_used(args,alias,index,false,value,selected,error_list);-- first test for non-enumerated aliasendvalue,selected=is_alias_used(args,alias,index,true,value,selected,error_list);-- test for enumerated aliaselsevalue,selected=is_alias_used(args,alias,index,false,value,selected,error_list);--test for non-enumerated aliasendendif#error_list>0and'none'~=error_conditionthen-- for cases where this code is used outside of extract_names()localerror_str="";for_,kinipairs(error_list)doiferror_str~=""thenerror_str=error_str..cfg.messages['parameter-separator']enderror_str=error_str..wrap_style('parameter',k);endif#error_list>1thenerror_str=error_str..cfg.messages['parameter-final-separator'];elseerror_str=error_str..cfg.messages['parameter-pair-separator'];enderror_str=error_str..wrap_style('parameter',selected);table.insert(z.message_tail,{set_error(error_condition,{error_str},true)});endreturnvalue,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 BThe str:gsub() returns either A|B froma [[A|B]] or B from [[B]] or B from B (no wikilink markup).In l(), l:gsub() removes the link and pipe (if they exist); the second :gsub() trims white space from the labelif str was wrapped in wikilink markup. Presumably, this is because without wikimarkup in str, there is no matchin the initial gsub, the replacement function l() doesn't get called.]=]localfunctionremove_wiki_link(str)return(str:gsub("%[%[([^%[%]]*)%]%]",function(l)returnl:gsub("^[^|]*|(.*)$","%1"):gsub("^%s*(.-)%s*$","%1");end));end--[=[-------------------------< I S _ W I K I L I N K >--------------------------------------------------------Determines if str is a wikilink, extracts, and returns the the wikilink type, link text, and display text parts.If str is a complex wikilink ([[L|D]]):returns wl_type 2 and D and L from [[L|D]];if str is a simple wikilink ([[D]])returns wl_type 1 and D from [[D]] and L as empty string;if not a wikilink:returns wl_type 0, str as D, and L as empty string.trims leading and trailing white space and pipes from L and D ([[L|]] and [[|D]] are accepted by MediaWiki andtreated like [[D]]; while [[|D|]] is not accepted by MediaWiki, here, we accept it and return D without the pipes).]=]localfunctionis_wikilink(str)localD,Llocalwl_type=2;-- assume that str is a complex wikilink [[L|D]]L,D=str:match('%[%[([^|]+)|([^%]]+)%]%]');-- get L and D from [[L|D]]ifnotis_set(D)then-- if no separate linkD=str:match('%[%[([^%]]*)|*%]%]');-- get D from [[D]]wl_type=1;endifnotis_set(D)then-- no wikilink markupD=str;-- return the string as Dwl_type=0;-- but say that it is not a wikilinkendD=mw.text.trim(D,'%s|');-- trim white space and pipe charactersL=Landmw.text.trim(L,'%s|');returnwl_type,D,Lor'';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;endreturn{-- return exported functions and tablesis_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,is_wikilink=is_wikilink,make_wikilink=make_wikilink,set_selected_modules=set_selected_modules,z=z,}