Module:Yesno: Difference between revisions

From PT National Labs
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
Line 1: Line 1:
-- <nowiki>
--[[
{{Helper module|name=Yesno
|fname1=(arg)
|ftype1=Any value
|fuse1=Reads arg for yes/no and returns the appropriate boolean or nil
|fname2=(arg1,arg2)
|ftype2=Any value, Any value
|fuse2=Reads arg1 for yes/no and returns the appropriate boolean; returns arg2 if arg1 was not an applicable value
}}
--]]
-- <pre>
-- Used to evaluate args to booleans where applicable
--
--
-- Implements {{tnavbar}} and variants
-- Based on <https://en.wikipedia.org/wiki/Module:Yesno>
-- see page history there for contributors
--
--
-- @todo move the hardcoded css to [[MediaWiki:Common.css]] given how many pages it's found on
--
require('strict')
local p = {}
local yesno = require( 'Module:Yesno' )
function p._navbar( args )
local navbarstyle = args.style or ''
local fontstyle = args.fontstyle or ''
local view, talk, edit = (args.view or true), (args.talk or true), (args.edit or true)
local desc = {
view = 'view',
talk = 'talk',
edit = 'edit'
}
local tag = mw.html.create( 'div' )
:addClass( 'navbar' )
:addClass( 'plainlinks' )
:addClass( 'noprint' )
:css( {
['white-space'] = 'nowrap',
['font-weight'] = 'normal',
['font-size'] = 'x-small'
} )
:cssText( navbarstyle )
if yesno( args.mini ) then
desc = {
view = 'v',
talk = 't',
edit = 'e'
}
tag:addClass( 'navbar-mini' )
end
local viewSpan = mw.html.create( 'span' )
:attr( 'title', 'View this template' )
:cssText( fontstyle )
:wikitext( desc.view )
local talkSpan = mw.html.create( 'span' )
:attr( 'title', 'Discussion about this template' )
:cssText( fontstyle )
:wikitext( desc.talk )
local editSpan = mw.html.create( 'span' )
:attr( 'title', 'Edit this template' )
:cssText( fontstyle )
:wikitext( desc.edit )


local title = args[1] and mw.text.trim( args[1] ) or error('No page title given')
return function( arg, default )
local ns, titleTbl, pagelink, talklink
arg = type( arg ) == 'string' and arg:lower() or arg


if mw.ustring.sub( title, 1, 1 ) == ':' then
if arg == nil then
-- mainspace
return nil
title = mw.ustring.sub( title, 2 )
pagelink = title
talklink = 'Talk:' .. title
 
elseif mw.ustring.match( title, ':' ) then
-- split title to see if it has a valid namespace
titleTbl = mw.text.split( title, ':' )
ns = mw.site.namespaces[titleTbl[1]]
 
if ns ~= nil then
pagelink = ns.name .. ':' .. table.concat( titleTbl, '', 2 )
 
if ns.isTalk then
talklink = page
else
talklink = ns.talk.name .. ':' .. table.concat( titleTbl, '', 2 )
end
end
end
end


-- this happens if there's no semi-colons in title
if
-- or if there is semi-colons but it didn't have valid ns name
arg == true or
if not pagelink then
arg == 'yes' or
pagelink = 'Template:' .. title
arg == 'y' or
talklink = 'Template talk:' .. title
arg == 'true' or
tonumber( arg ) ==  1
then
return true
end
end


tag:wikitext( '[[' .. pagelink .. '|' .. tostring( viewSpan ) .. ']]' )
if
:wikitext( '&nbsp;' )
arg == false or
:tag( 'span' )
arg == 'no' or
:css( 'font-size', '80%' )
arg == 'n' or
:wikitext( '&bull;' )
arg == 'false' or
:done()
tonumber( arg ) == 0
:wikitext( '&nbsp;' )
then
 
return false
if talk == 'autoconfirmed' then
tag
:tag( 'span' )
:addClass( 'autoconfirmed-show' )
:css( 'display', 'none' )
:wikitext( '[' .. tostring( mw.uri.fullUrl( talklink ) ) .. ' ' .. tostring( talkSpan ) .. ']' )
:wikitext( '&nbsp;' )
:tag( 'span' )
:css( 'font-size', '80%' )
:wikitext( '&bull;' )
:done()
:wikitext( '&nbsp;' )
:done()
elseif yesno(talk) then
tag:wikitext( '[' .. tostring( mw.uri.fullUrl( talklink ) ) .. ' ' .. tostring( talkSpan ) .. ']' )
:wikitext( '&nbsp;' )
:tag( 'span' )
:css( 'font-size', '80%' )
:wikitext( '&bull;' )
:done()
:wikitext( '&nbsp;' )
:done()
end
end


tag:wikitext( '[' .. tostring( mw.uri.fullUrl( pagelink, 'action=edit' ) ) .. ' ' .. tostring( editSpan ) .. ']' )
return default
 
return tostring( tag )
end
 
function p.navbar( frame )
return p._navbar( frame:getParent().args )
end
end
return p

Latest revision as of 10:23, 26 June 2025

Documentation for this module may be created at Module:Yesno/doc

--[[
{{Helper module|name=Yesno
|fname1=(arg)
|ftype1=Any value
|fuse1=Reads arg for yes/no and returns the appropriate boolean or nil
|fname2=(arg1,arg2)
|ftype2=Any value, Any value
|fuse2=Reads arg1 for yes/no and returns the appropriate boolean; returns arg2 if arg1 was not an applicable value
}}
--]]
-- <pre>
-- Used to evaluate args to booleans where applicable
--
-- Based on <https://en.wikipedia.org/wiki/Module:Yesno>
-- see page history there for contributors
--

return function( arg, default )
	arg = type( arg ) == 'string' and arg:lower() or arg

	if arg == nil then
		return nil
	end

	if
		arg == true or
		arg == 'yes' or
		arg == 'y' or
		arg == 'true' or
		tonumber( arg ) ==  1
	then
		return true
	end

	if
		arg == false or
		arg == 'no' or
		arg == 'n' or
		arg == 'false' or
		tonumber( arg ) == 0
	then
		return false
	end

	return default
end