Idi na sadržaj

Modul:Hatnote

S Wikipedije, slobodne enciklopedije

Dokumentaciju za ovaj modul možete napraviti na straniciModul:Hatnote/dok

--------------------------------------------------------------------------------
-- Module:Hatnote --
-- --
-- This module produces hatnote links and links to related articles. It --
-- implements the {{hatnote}} and {{format link}} meta-templates and includes --
-- helper functions for other Lua hatnote modules. --
--------------------------------------------------------------------------------

locallibraryUtil=require('libraryUtil')
localcheckType=libraryUtil.checkType
localmArguments-- lazily initialise [[Modul:Arguments]]
localyesno-- lazily initialise [[Modul:Yesno]]

localp={}

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

localfunctiongetArgs(frame)
-- Fetches the arguments from the parent frame. Whitespace is trimmed and
-- blanks are removed.
mArguments=require('Modul:Arguments')
returnmArguments.getArgs(frame,{parentOnly=true})
end

localfunctionremoveInitialColon(s)
-- Removes the initial colon from a string, if present.
returns:match('^:?(.*)')
end

functionp.findNamespaceId(link,removeColon)
-- Finds the namespace id (namespace number) of a link or a pagename. This
-- function will not work if the link is enclosed in double brackets. Colons
-- are trimmed from the start of the link by default. To skip colon
-- trimming, set the removeColon parameter to false.
checkType('findNamespaceId',1,link,'string')
checkType('findNamespaceId',2,removeColon,'boolean',true)
ifremoveColon~=falsethen
link=removeInitialColon(link)
end
localnamespace=link:match('^(.-):')
ifnamespacethen
localnsTable=mw.site.namespaces[namespace]
ifnsTablethen
returnnsTable.id
end
end
return0
end

functionp.formatPages(...)
-- Formats a list of pages using formatLink and returns it as an array. Nil
-- values are not allowed.
localpages={...}
localret={}
fori,pageinipairs(pages)do
ret[i]=p._formatLink(page)
end
returnret
end

functionp.formatPageTables(...)
-- Takes a list of page/display tables and returns it as a list of
-- formatted links. Nil values are not allowed.
localpages={...}
locallinks={}
fori,tinipairs(pages)do
checkType('formatPageTables',i,t,'table')
locallink=t[1]
localdisplay=t[2]
links[i]=p._formatLink(link,display)
end
returnlinks
end

functionp.makeWikitextError(msg,helpLink,addTrackingCategory,title)
-- Formats an error message to be returned to wikitext. If
-- addTrackingCategory is not false after being returned from
-- [[Module:Yesno]], and if we are not on a talk page, a tracking category
-- is added.
checkType('makeWikitextError',1,msg,'string')
checkType('makeWikitextError',2,helpLink,'string',true)
yesno=require('Modul:Yesno')
title=titleormw.title.getCurrentTitle()
-- Make the help link text.
localhelpText
ifhelpLinkthen
helpText=' ([['..helpLink..'|pomoć]])'
else
helpText=''
end
-- Make the category text.
localcategory
ifnottitle.isTalkPageandyesno(addTrackingCategory)~=falsethen
category='Šabloni koji koriste hatnote sa greškama'
category=string.format(
'[[%s:%s]]',
mw.site.namespaces[14].name,
category
)
else
category=''
end
returnstring.format(
'<strong class= "error" >Greška: %s%s.</strong>%s',
msg,
helpText,
category
)
end

functionp.disambiguate(page,disambiguator)
-- Formats a page title with a disambiguation parenthetical,
-- i.e. "Example" → "Example (disambiguation)".
checkType('disambiguate',1,page,'string')
checkType('disambiguate',2,disambiguator,'string',true)
disambiguator=disambiguatoror'čvor'

-- Check if the page with "višeznačna odrednica" exists
localtitleObj=mw.title.new(page..' (višeznačna odrednica)')
localexists=titleObjandtitleObj.exists

ifexiststhen
returnstring.format('%s (%s)',page,'višeznačna odrednica')
else
returnstring.format('%s (%s)',page,disambiguator)
end
end


--------------------------------------------------------------------------------
-- Format link
--
-- Makes a wikilink from the given link and display values. Links are escaped
-- with colons if necessary, and links to sections are detected and displayed
-- with "§" as a separator rather than the standard MediaWiki "#". Used in
-- the {{format hatnote link}} template.
--------------------------------------------------------------------------------

functionp.formatLink(frame)
localargs=getArgs(frame)
locallink=args[1]
localdisplay=args[2]
ifnotlinkthen
returnp.makeWikitextError(
'niste naveli link',
'Šablon:Formatiranje linka u hatnoteu#Greške',
args.category
)
end
returnp._formatLink(link,display)
end

functionp._formatLink(link,display)
checkType('_formatLink',1,link,'string')
checkType('_formatLink',2,display,'string',true)

-- Remove the initial colon for links where it was specified manually.
link=removeInitialColon(link)

-- Find whether a faux display value has been added with the {{!}} magic
-- word.
ifnotdisplaythen
localprePipe,postPipe=link:match('^(.-)|(.*)$')
link=prePipeorlink
display=postPipe
end

-- Find the display value.
ifnotdisplaythen
localpage,section=link:match('^(.-)#(.*)$')
ifpagethen
display=page..' §&nbsp;'..section
end
end

-- Assemble the link.
ifdisplaythen
returnstring.format(
'[[:%s|%s]]',
string.gsub(link,'|(.*)$',''),--display overwrites manual piping
display
)
else
returnstring.format('[[:%s]]',link)
end
end

--------------------------------------------------------------------------------
-- Hatnote
--
-- Produces standard hatnote text. Implements the {{hatnote}} template.
--------------------------------------------------------------------------------

functionp.hatnote(frame)
localargs=getArgs(frame)
locals=args[1]
localoptions={}
ifnotsthen
returnp.makeWikitextError(
'niste naveli tekst',
'Šablon:Hatnote#Greške',
args.category
)
end
options.extraclasses=args.extraclasses
options.selfref=args.selfref
returnp._hatnote(s,options)
end

functionp._hatnote(s,options)
checkType('_hatnote',1,s,'string')
checkType('_hatnote',2,options,'table',true)
options=optionsor{}
localclasses={'hatnote','navigation-not-searchable'}
localextraclasses=options.extraclasses
localselfref=options.selfref
iftype(extraclasses)=='string'then
classes[#classes+1]=extraclasses
end
ifselfrefthen
classes[#classes+1]='selfref'
end
returnstring.format(
'<div role= "note" class= "%s" >%s</div>',
table.concat(classes,' '),
s
)
end

returnp