This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
This module depends on the following other modules:
This module implements {{italic title}} and {{italic dab}}. Please see the template pages for documentation.
-- This module implements {{italic title}}.require('strict')locallibraryUtil=require('libraryUtil')localcheckType=libraryUtil.checkTypelocalcheckTypeForNamedArg=libraryUtil.checkTypeForNamedArglocalyesno=require('Module:Yesno')---------------------------------------------------------------------------------- ItalicTitle class--------------------------------------------------------------------------------localItalicTitle={}do------------------------------------------------------------------------------ Class attributes and functions-- Things that belong to the class are here. Things that belong to each-- object are in the constructor.------------------------------------------------------------------------------ Keys of title parts that can be italicized.localitalicizableKeys={namespace=true,title=true,dab=true,}------------------------------------------------------------------------------ ItalicTitle constructor-- This contains all the dynamic attributes and methods.----------------------------------------------------------------------------functionItalicTitle.new()localobj={}-- Function for checking self variable in methods.localcheckSelf=libraryUtil.makeCheckSelfFunction('ItalicTitle','obj',obj,'ItalicTitle object')-- Checks a key is present in a lookup table.-- Param: name - the function name.-- Param: argId - integer position of the key in the argument list.-- Param: key - the key.-- Param: lookupTable - the table to look the key up in.localfunctioncheckKey(name,argId,key,lookupTable)ifnotlookupTable[key]thenerror(string.format("bad argument #%d to '%s' ('%s' is not a valid key)",argId,name,key),3)endend-- Set up object structure.localparsed=falselocalcategories={}localitalicizedKeys={}localitalicizedSubstrings={}-- Parses a title object into its namespace text, title, and-- disambiguation text.-- Param: options - a table of options with the following keys:-- title - the title object to parse-- ignoreDab - ignore any disambiguation parentheses-- Returns the current object.functionobj:parseTitle(options)checkSelf(self,'parseTitle')checkType('parseTitle',1,options,'table')checkTypeForNamedArg('parseTitle','title',options.title,'table')localtitle=options.title-- Title and dab textlocalprefix,parenthesesifnotoptions.ignoreDabthenprefix,parentheses=mw.ustring.match(title.text,'^(.+) %(([^%(%)]+)%)$')endifprefixandparenthesesthenself.title=prefixself.dab=parentheseselseself.title=title.textend-- Namespacelocalnamespace=mw.site.namespaces[title.namespace].nameifnamespaceand#namespace>=1thenself.namespace=namespaceend-- Register the object as having parsed a title.parsed=truereturnselfend-- Italicizes part of the title.-- Param: key - the key of the title part to be italicized. Possible-- keys are contained in the italicizableKeys table.-- Returns the current object.functionobj:italicize(key)checkSelf(self,'italicize')checkType('italicize',1,key,'string')checkKey('italicize',1,key,italicizableKeys)italicizedKeys[key]=truereturnselfend-- Un-italicizes part of the title.-- Param: key - the key of the title part to be un-italicized. Possible-- keys are contained in the italicizableKeys table.-- Returns the current object.functionobj:unitalicize(key)checkSelf(self,'unitalicize')checkType('unitalicize',1,key,'string')checkKey('unitalicize',1,key,italicizableKeys)italicizedKeys[key]=nilreturnselfend-- Italicizes a substring in the title. This only affects the main part-- of the title, not the namespace or the disambiguation text.-- Param: s - the substring to be italicized.-- Returns the current object.functionobj:italicizeSubstring(s)checkSelf(self,'italicizeSubstring')checkType('italicizeSubstring',1,s,'string')italicizedSubstrings[s]=truereturnselfend-- Un-italicizes a substring in the title. This only affects the main-- part of the title, not the namespace or the disambiguation text.-- Param: s - the substring to be un-italicized.-- Returns the current object.functionobj:unitalicizeSubstring(s)checkSelf(self,'unitalicizeSubstring')checkType('unitalicizeSubstring',1,s,'string')italicizedSubstrings[s]=nilreturnselfend-- Renders the object into a page name. If no title has yet been parsed,-- the current title is used.-- Returns stringfunctionobj:renderTitle()checkSelf(self,'renderTitle')-- Italicizes a string-- Param: s - the string to italicize-- Returns string.localfunctionitalicize(s)assert(type(s)=='string','s was not a string')assert(s~='','s was the empty string')returnstring.format('<i>%s</i>',s)end-- Escape characters in a string that are magic in Lua patterns.-- Param: pattern - the pattern to escape-- Returns string.localfunctionescapeMagicCharacters(s)assert(type(s)=='string','s was not a string')returns:gsub('%p','%%%0')end-- If a title hasn't been parsed yet, parse the current title.ifnotparsedthenself:parseTitle{title=mw.title.getCurrentTitle()}end-- Italicize the different parts of the title and store them in a-- titleParts table to be joined together later.localtitleParts={}-- Italicize the italicizable keys.forkeyinpairs(italicizableKeys)doifself[key]thenifitalicizedKeys[key]thentitleParts[key]=italicize(self[key])elsetitleParts[key]=self[key]endendend-- Italicize substrings. If there are any substrings to be-- italicized then start from the raw title, as this overrides any-- italicization of the main part of the title.ifnext(italicizedSubstrings)thentitleParts.title=self.titleforsinpairs(italicizedSubstrings)dolocalpattern=escapeMagicCharacters(s)localitalicizedTitle,nReplacements=titleParts.title:gsub(pattern,italicize)titleParts.title=italicizedTitle-- If we didn't make any replacements then it means that we-- have been passed a bad substring or that the page has-- been moved to a bad title, so add a tracking category.ifnReplacements<1thencategories['Pages using italic title with no matching string']=trueendendend-- Assemble the title together from the parts.localret=''iftitleParts.namespacethenret=ret..titleParts.namespace..':'endret=ret..titleParts.titleiftitleParts.dabthenret=ret..' ('..titleParts.dab..')'endreturnretend-- Returns an expanded DISPLAYTITLE parser function called with the-- result of obj:renderTitle, plus any other optional arguments.-- Returns stringfunctionobj:renderDisplayTitle(...)checkSelf(self,'renderDisplayTitle')returnmw.getCurrentFrame():callParserFunction('DISPLAYTITLE',self:renderTitle(),...)end-- Returns an expanded DISPLAYTITLE parser function called with the-- result of obj:renderTitle, plus any other optional arguments, plus-- any tracking categories.-- Returns stringfunctionobj:render(...)checkSelf(self,'render')localret=self:renderDisplayTitle(...)forcatinpairs(categories)doret=ret..string.format('[[Category:%s]]',cat)endreturnretendreturnobjendend---------------------------------------------------------------------------------- Exports--------------------------------------------------------------------------------localp={}localfunctiongetArgs(frame,wrapper)assert(type(wrapper)=='string','wrapper was not a string')returnrequire('Module:Arguments').getArgs(frame,{wrappers=wrapper})end-- Main function for {{italic title}}functionp._main(args)checkType('_main',1,args,'table')localitalicTitle=ItalicTitle.new()italicTitle:parseTitle{title=mw.title.getCurrentTitle(),ignoreDab=yesno(args.all,false)}ifargs.stringthenitalicTitle:italicizeSubstring(args.string)elseitalicTitle:italicize('title')endreturnitalicTitle:render(args[1])endfunctionp.main(frame)returnp._main(getArgs(frame,'Template:Italic title'))endfunctionp._dabonly(args)returnItalicTitle.new():italicize('dab'):render(args[1])endfunctionp.dabonly(frame)returnp._dabonly(getArgs(frame,'Template:Italic dab'))endreturnp