Content added Content deleted
(Created page with "-- <nowiki> --- Yesno module for processing of boolean-like wikitext input. -- -- It works similarly to the Yesno Wikipedia -- template. This...") |
m (1 revision imported) |
||
Line 1: | Line 1: | ||
-- Function allowing for consistent treatment of boolean-like wikitext input. |
|||
-- <nowiki> |
|||
-- It works similarly to the template {{yesno}}. |
|||
--- Yesno module for processing of boolean-like wikitext input. |
|||
-- |
|||
-- It works similarly to the [[wikipedia:Template:Yesno|Yesno Wikipedia |
|||
-- template]]. This module is a consistent Lua interface for wikitext |
|||
-- input from templates. |
|||
-- |
|||
-- Wikitext markup used by MediaWiki templates only permit |
|||
-- string parameters like `"0"`, `"yes"`, `"no"` etc. As Lua |
|||
-- has a boolean primitive type, Yesno converts this |
|||
-- wikitext into boolean output for Lua to process. |
|||
-- |
|||
-- @script yesno |
|||
-- @release stable |
|||
-- @author [[User:Dessamator|Dessamator]] |
|||
-- @attribution [[wikipedia:User:ATDT|ATDT]] |
|||
-- @attribution [[wikipedia:User:Mr. Stradivarius|Mr. Stradivarius]] |
|||
-- @attribution [[wikipedia:Special:PageHistory/Module:Yesno|Other Wikipedia contributors]] |
|||
-- @see [[wikipedia:Module:Yesno|Original module on |
|||
-- Wikipedia]] |
|||
-- @see [[Module:Yesno/testcases|Test cases for this |
|||
-- module]] |
|||
-- @param {?boolean|string} value Wikitext boolean-style |
|||
-- or Lua boolean input. |
|||
-- * Truthy wikitext input (`'yes'`, `'y'`, `'1'`, |
|||
-- `'t'` or `'on'`) produces `true` as output. |
|||
-- * The string representations of Lua's true |
|||
-- boolean value (`'true'`) also produces `true`. |
|||
-- * Falsy wikitext input (`'no'`, `'n'`, `'0'`, |
|||
-- `'f'` or `'off'`) produces `false` as output. |
|||
-- * The string representation of Lua's false |
|||
-- boolean value (`'false'`) also produces `false`. |
|||
-- * Localised text meaning `'yes'` or `'no'` also |
|||
-- evaluate to `true` or `false` respectively. |
|||
-- @param[opt] {?boolean|string} default Output to return if |
|||
-- the Yesno `value` input is unrecognised. |
|||
-- @return {?boolean} Boolean output corresponding to |
|||
-- `val`: |
|||
-- * The strings documented above produce a |
|||
-- boolean value. |
|||
-- * A `nil` value produces an output of `nil`. |
|||
-- As this is falsy, additional logic may be needed |
|||
-- to treat missing template parameters as truthy. |
|||
-- * Unrecognised values return the `default` |
|||
-- parameter. Blank strings are a key example |
|||
-- of Yesno's unrecognised values and can evaluate |
|||
-- to `true` if there is a default value. |
|||
local lower = mw.ustring.lower |
|||
local msg = mw.message.new |
|||
return function( |
return function (val, default) |
||
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you |
|||
value = type(value) == 'string' and lower(value) or value |
|||
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the |
|||
-- following line. |
|||
local yes = lower(msg('htmlform-yes'):plain()) |
|||
val = type(val) == 'string' and val:lower() or val |
|||
local no = lower(msg('htmlform-no'):plain()) |
|||
if val == nil then |
|||
local en_yes = lower(msg('htmlform-yes'):inLanguage('en'):plain()) |
|||
return nil |
|||
local en_no = lower(msg('htmlform-no'):inLanguage('en'):plain()) |
|||
elseif val == true |
|||
or val == 'yes' |
|||
if value == nil then |
|||
or val == 'y' |
|||
return nil |
|||
or val == 'true' |
|||
or val == 't' |
|||
elseif value == true |
|||
or val == 'on' |
|||
or tonumber(val) == 1 |
|||
then |
|||
or value == 'y' |
|||
return true |
|||
or value == 'true' |
|||
elseif val == false |
|||
or value == 't' |
|||
or val == 'no' |
|||
or val == 'n' |
|||
or val == 'false' |
|||
then |
|||
or val == 'f' |
|||
return true |
|||
or val == 'off' |
|||
or tonumber(val) == 0 |
|||
elseif value == false |
|||
then |
|||
or value == no |
|||
return false |
|||
or value == en_no |
|||
else |
|||
or value == 'n' |
|||
return default |
|||
or value == 'false' |
|||
end |
|||
or value == 'f' |
|||
or value == 'off' |
|||
or tonumber(value) == 0 |
|||
then |
|||
return false |
|||
else |
|||
return default |
|||
end |
|||
end |
end |
||
-- </nowiki> |
Revision as of 09:27, 5 July 2020
-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
return function (val, default)
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
-- following line.
val = type(val) == 'string' and val:lower() or val
if val == nil then
return nil
elseif val == true
or val == 'yes'
or val == 'y'
or val == 'true'
or val == 't'
or val == 'on'
or tonumber(val) == 1
then
return true
elseif val == false
or val == 'no'
or val == 'n'
or val == 'false'
or val == 'f'
or val == 'off'
or tonumber(val) == 0
then
return false
else
return default
end
end