Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Module:CS1 identifiers

Permanently protected module
From Wikipedia, the free encyclopedia
The abovedocumentation istranscluded fromModule:CS1 identifiers/doc.(edit |history)
Editors can experiment in this module's sandbox(create |mirror) and testcases(create) pages.
Subpages of this module.

require('strict');localget_args=require('Module:Arguments').getArgs;localidentifiers=require('Module:Citation/CS1/Identifiers');localutilities=require('Module:Citation/CS1/Utilities');localhas_accept_as_written=utilities.has_accept_as_written;-- import functions from Module:Citation/CS1/Utilitieslocalis_set=utilities.is_set;localmake_wikilink=utilities.make_wikilink;localset_message=utilities.set_message;localsubstitute=utilities.substitute;localcfg=mw.loadData('Module:Citation/CS1/Configuration');utilities.set_selected_modules(cfg);-- so that functions in Utilities can see the selected cfg tablesidentifiers.set_selected_modules(cfg,utilities);-- so that functions in Identifiers can see the selected cfg tables and selected Utilities modulelocalFrame;-- local copy of <frame> from main(); nil elselocalthis_page=mw.title.getCurrentTitle();-- used to limit categorization to certain namepsaceslocalno_cat;-- used to limit categorization to certain namespacesifcfg.uncategorized_namespaces[this_page.namespace]then-- is this page's namespace id one of the uncategorized namespace ids?no_cat=true;-- set no_cat; this page will not be categorizedendfor_,vinipairs(cfg.uncategorized_subpages)do-- cycle through page name patternsifthis_page.text:match(v)then-- test page name against each patternno_cat=true;-- set no_cat; this page will not be categorizedbreak;-- bail out if one is foundendend--[[--------------------------< E R R _ M E S S A G E _ C O N V E R T >----------------------------------------converts cs1|2 error message to a message suitable for this module.converted error messages do not name a parameter as is done in cs1|2.  The help link links to the template pagenot to a help-namespace page.  The prefix is rewritten to name the offending template; not a cs1|2 template.adds template specific category.done this way because the identifier functions in Module:Citation/CS1/Identifiers create properly formattedmessages with correct html for styling.]]localfunctionerr_message_convert(message_prefix,message,_template,no_cat)message=message:gsub('&#124;([^=]+)=','%1');-- remove parameter pipe and assignment operatormessage=message:gsub('Help:CS1 errors#[^%]]+',substitute('Template:$1|help',_template));-- rewrite help textmessage=message:gsub('(%b<>)',substitute('$1$2: ',{'%1',message_prefix}),1);-- %1 is the opening span tag; insert <message_prefix>localcategory=no_catand''orsubstitute('[[Category:Pages with $1 errors]]',_template:upper());-- limited to certain namespacesreturnsubstitute('$1$2',{message,category});-- make a big string and doneend--[[--------------------------< M A I N T _ M E S S A G E _ C O N V E R T >------------------------------------converts cs1|2 maintenance message to a message suitable for this module.converted maintenance messages have a prefix suitable for the rendered template.  The 'link' text links to thean appropriate maintenance category]]localfunctionmaint_message_convert(message_prefix,message_raw,_template,no_cat)message_raw=message_raw:gsub('CS1 maint: ','');-- strip cs1-specific prefix from cat namelocalmessage=substitute('$1: $2',{message_prefix,message_raw});-- add the template prefixmessage=substitute('$1$2 ($3)',{message,no_catand''orsubstitute(cfg.messages['cat wikilink'],message_raw),-- the category link; limited to certain namespacessubstitute(cfg.messages[':cat wikilink'],message_raw)}-- links to the maint cat, just as cs1|2 links to its maint cats);returnsubstitute(cfg.presentation['hidden-maint'],message);-- the maint message textend--[[--------------------------< P A R A M S _ G E T >----------------------------------------------------------extract enumerated parameters from <args_t> where the enumerator is <i>.  enumerator is always the last characterof the parameter name (doi-broken-date1 not doi1-broken-date)special case the enumerator is 1: prefer non-enumerated parametersreturns a table of same-enumerator parameters (without enumerator)note: <i> is a number]]localfunctionparams_get(args_t,i)--mw.logObject (args_t, 'args_t')localparams_t={};-- selected parameters go in this tableif1==ithen-- special case when enumerator (<i>) is 1fork,vinpairs(args_t)do-- for each parameterif'number'==type(k)then-- if this is a positional parameterif1==kthen-- and its the first positional parameterparams_t[1]=v;-- save itendelse-- here for named parameterslocalenum=k:match('%d+$');-- extract the enumerator from the parameter's name; nil elseif(notenum)or(1==tonumber(enum))then-- when not enumerated or when enumerator is 1k=k:gsub('%d+$','');-- remove the enumerator from parameter nameparams_t[k]=v;-- and save this parameterendendendelsefork,vinpairs(args_t)do-- for each parameterif'number'==type(k)then-- if this is a positional parameterifi==kthen-- and is the desired positional parameterparams_t[1]=v;-- save it (as index number 1; not as index <i>)endelse-- here for named parameterslocalenum=k:match('%d+$');-- extract the parameter's enumerator; nil elseifenumand(i==tonumber(enum))then-- when enumerated and the enumerator is same as requestedk=k:gsub('%d+$','');-- remove the enumerator from parameter nameparams_t[k]=v;-- and save this parameterendendendend--mw.logObject (params_t, 'params_t')returnparams_t;-- and doneend--[[--------------------------< R E N D E R _ F I N A L >------------------------------------------------------this function applies cs1|2 template style sheet to a rendered identifier or error message]]localfunctionrender_final(output)ifnotFramethen-- not set when this module called from another moduleFrame=mw.getCurrentFrame();-- get the calling module's frame so that we can call extensionTag()endreturnsubstitute('$1$2',{Frame:extensionTag('templatestyles','',{src='Module:Citation/CS1/styles.css'}),-- apply templatestylesoutput-- to the rendered identifier or error message});end--[[--------------------------< _ M A I N >--------------------------------------------------------------------entry point when called from another module; example:local rendered_identifier = require ('Module:CS1 identifiers')._main ({'10.4231/sommat', _template = 'doi', ['doi-access'] = 'free', ['doi-broken-date'] = 'June 2025'});supported identifier templates are:{{arxiv}}{{asin}}{{bibcode}}{{biorxiv}}{{citeseerx}}{{doi}}{{hdl}}{{isbn}}{{ismn}}{{issn}}{{jfm}}{{jstor}}{{medrxiv}}{{mr}}{{oclc}}{{ol}}{{osti}}{{pmc}}{{pmid}}{{sbn}}{{ssrn}}{{s2cid}}{{zbl}}<args_t> is a table of all parameters needed to properly render the identifier]]localfunction_main(args_t)localID_list_t={};-- sequence table of rendered identifierslocalID_list_coins_t={};-- table of identifiers and their values from args; key is same as cfg.id_handlers's key; COinS not supported in this modulelocaltemplate_name=args_t._template;localmessage_prefix;ifnot(template_nameandcfg.id_handlers[template_name:upper()])thenerror('|_template= requires valid value');-- a message for template writers; not seen by usersendtemplate_name=template_name:lower();-- force lower casemessage_prefix=substitute('<code class="cs1-code">&#x7B;{[[Template:$1|$1]]}}</code>',template_name);localrendered_id;-- a single rendered id worked on herelocalrendered_ids_t={};-- individual rendered ids go herelocali=1;-- initialize the indexer for the repeat looprepeat-- loop until nolocalparams_t=params_get(args_t,i);-- get the parameters associated with enumerator <i>params_t[template_name]=params_t[1]orparams_t['id'];-- assign value from {{{1}}} or |id= to |<identifier>=params_t._template=nil;-- unset as no longer neededifnotparams_t[template_name]then-- in case params_t[1] and params_t.id are nilparams_t[template_name]='';-- set <id> to empty stringreturnrender_final(-- make an error message and donesubstitute('<span class="cs1-visible-error citation-comment">$1: required identifier missing ($2)</span>$3',{message_prefix,substitute('[[Template:$1|help]]',template_name),no_catand''orsubstitute('[[Category:Pages with $1 errors]]',template_name:upper())}));endparams_t[i]=nil;-- unset these as no longer neededparams_t.id=nil;localDoiBroken=params_t['doi-broken-date'];-- {{doi}} onlylocalEmbargo=params_t['pmc-embargo-date'];-- {{pmc}} onlylocalClass=params_t['class'];-- {{arxiv}} onlylocalAsinTLD=params_t['asin-tld'];-- {{asin}} onlyID_list_t,ID_list_coins_t=identifiers.identifier_lists_get(params_t,{DoiBroken=DoiBroken,Embargo=Embargo,Class=Class,ASINTLD=AsinTLD},{});-- {} is a placeholder for unused ID_support{}rendered_id=ID_list_t[1];ifutilities.z.error_msgs_t[1]then-- only one error message consideredrendered_id=substitute('$1 $2',{rendered_id,err_message_convert(message_prefix,utilities.z.error_msgs_t[1],template_name,no_cat),});elseifutilities.z.maint_cats_t[1]then-- only one maint message considered per renderingrendered_id=substitute('$1 $2',{rendered_id,maint_message_convert(message_prefix,utilities.z.maint_cats_t[1],template_name,no_cat),});endutilities.z.error_msgs_t={};-- reset theseutilities.z.maint_cats_t={};if(1~=i)or((1==i)and('yes'==args_t.plainlink))then-- no label for 2nd... identifiers; when |plainlink=yes then no label for first identifier;localseparator=cfg.id_handlers[template_name:upper()].separator;-- get the identifier label separatorrendered_id=rendered_id:gsub('^%[%[.-|.-%]%]','');-- strip cs1-supplied labelrendered_id=rendered_id:gsub('^'..separator,'');-- and strip the label separatorelseif1==ithen-- here for first identifier; |plainlink= not setif'no'==args_t.linkthen-- |link=no then do not link identifier labelrendered_id=rendered_id:gsub('^%[%[.-|(.-)%]%]','%1');-- strip wikilink markup from cs1-supplied labelendendtable.insert(rendered_ids_t,rendered_id);-- save the rendered identifieri=i+1;-- bump the indexeruntilnot(args_t[i]orargs_t['id'..i]);-- end of repeat looplocallist_separator=', ';-- identifier separator for a list of identifierslocalleadout=args_t.leadoutandmw.ustring.gsub(args_t.leadout,'^(%a.*)',' %1 ')ornil;-- insert leading space if first character is a letter; add trailing spaceifleadoutthen-- extra text goes between last two identifiers in the listreturnrender_final(mw.text.listToText(rendered_ids_t,list_separator,leadout));-- make a list, add templatestyles, and doneendreturnrender_final(table.concat(rendered_ids_t,list_separator));-- make a list, add templatestyles, and doneend--[[--------------------------< M A I N >----------------------------------------------------------------------entry point from an invoke{{#invoke:CS1 identifiers|main|_template=<identifier name>}}]]localfunctionmain(frame)Frame=frame;-- make a copy for rendering final outputlocalargs_t=get_args(frame);-- extract the argumentsreturn_main(args_t);-- render the identifier and doneend--[[--------------------------< E X P O R T S >----------------------------------------------------------------]]return{main=main,-- entry point from an invoke (in a template usually)_main=_main-- entry point when called from another module}
Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:CS1_identifiers&oldid=1304026881"
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp