Jump to content
Wikipedia The Free Encyclopedia

Module:Italic title

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 1,210,000 pages, or roughly 2% of all pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
This module depends on the following other modules:

This module implements {{italic title }} and {{italic dab }}. Please see the template pages for documentation.

The above documentation is transcluded from Module:Italic title/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (edit | run) pages.
Subpages of this module.

 -- This module implements {{italic title}}.

 require('strict')
 locallibraryUtil=require('libraryUtil')
 localcheckType=libraryUtil.checkType
 localcheckTypeForNamedArg=libraryUtil.checkTypeForNamedArg
 localyesno=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]then
 error(string.format(
 "bad argument #%d to '%s' ('%s' is not a valid key)",
 argId,
 name,
 key
 ),3)
 end
 end

 -- Set up object structure.
 localparsed=false
 localcategories={}
 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 text
 localprefix,parentheses
 ifnotoptions.ignoreDabthen
 prefix,parentheses=mw.ustring.match(
 title.text,
 '^(.+) %(([^%(%)]+)%)$'
 )
 end
 ifprefixandparenthesesthen
 self.title=prefix
 self.dab=parentheses
 else
 self.title=title.text
 end

 -- Namespace
 localnamespace=mw.site.namespaces[title.namespace].name
 ifnamespaceand#namespace>=1then
 self.namespace=namespace
 end

 -- Register the object as having parsed a title.
 parsed=true

 returnself
 end

 -- 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]=true
 returnself
 end

 -- 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]=nil
 returnself
 end

 -- 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]=true
 returnself
 end

 -- 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]=nil
 returnself
 end

 -- Renders the object into a page name. If no title has yet been parsed,
 -- the current title is used.
 -- Returns string
 functionobj: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.
 ifnotparsedthen
 self: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)do
 ifself[key]then
 ifitalicizedKeys[key]then
 titleParts[key]=italicize(self[key])
 else
 titleParts[key]=self[key]
 end
 end
 end

 -- 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)then
 titleParts.title=self.title
 forsinpairs(italicizedSubstrings)do
 localpattern=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<1then
 categories['Pages using italic title with no matching string']=true
 end
 end
 end

 -- Assemble the title together from the parts.
 localret=''
 iftitleParts.namespacethen
 ret=ret..titleParts.namespace..':'
 end
 ret=ret..titleParts.title
 iftitleParts.dabthen
 ret=ret..' ('..titleParts.dab..')'
 end

 returnret
 end

 -- Returns an expanded DISPLAYTITLE parser function called with the
 -- result of obj:renderTitle, plus any other optional arguments.
 -- Returns string
 functionobj: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 string
 functionobj:render(...)
 checkSelf(self,'render')
 localret=self:renderDisplayTitle(...)
 forcatinpairs(categories)do
 ret=ret..string.format(
 '[[Category:%s]]',
 cat
 )
 end
 returnret
 end

 returnobj
 end
 end

 --------------------------------------------------------------------------------
 -- 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.stringthen
 italicTitle:italicizeSubstring(args.string)
 else
 italicTitle:italicize('title')
 end
 returnitalicTitle:render(args[1])
 end

 functionp.main(frame)
 returnp._main(getArgs(frame,'Template:Italic title'))
 end

 functionp._dabonly(args)
 returnItalicTitle.new()
 :italicize('dab')
 :render(args[1])
 end

 functionp.dabonly(frame)
 returnp._dabonly(getArgs(frame,'Template:Italic dab'))
 end


 returnp

AltStyle によって変換されたページ (->オリジナル) /