×
Create a new article
Write your page title here:
We currently have 3,602 articles on DC Multiverse Wiki. Type your article name above or create one of the articles listed here!



    DC Multiverse Wiki

    Module:Yesno: Difference between revisions

    Content added Content deleted
    m (2 revisions imported: Importing Template:Tmbox, with templates.)
    (Created page with "-- <nowiki> --- Yesno module for processing of boolean-like wikitext input. -- -- It works similarly to the Yesno Wikipedia -- template. This...")
    Line 1: Line 1:
    -- <nowiki>
    -- Function allowing for consistent treatment of boolean-like wikitext input.
    --- Yesno module for processing of boolean-like wikitext input.
    -- It works similarly to the template {{yesno}}.
    --
    -- 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 (val, default)
    return function(value, default)
    value = type(value) == 'string' and lower(value) or value
    -- 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
    local yes = lower(msg('htmlform-yes'):plain())
    -- following line.
    local no = lower(msg('htmlform-no'):plain())
    val = type(val) == 'string' and val:lower() or val
    local en_yes = lower(msg('htmlform-yes'):inLanguage('en'):plain())
    if val == nil then
    local en_no = lower(msg('htmlform-no'):inLanguage('en'):plain())
    return nil

    elseif val == true
    if value == nil then
    or val == 'yes'
    return nil
    or val == 'y'

    or val == 'true'
    elseif value == true
    or val == 't'
    or val == 'on'
    or value == yes
    or tonumber(val) == 1
    or value == en_yes
    or value == 'y'
    then
    or value == 'true'
    return true
    or value == 't'
    elseif val == false
    or val == 'no'
    or value == 'on'
    or val == 'n'
    or tonumber(value) == 1
    then
    or val == 'false'
    return true
    or val == 'f'

    or val == 'off'
    elseif value == false
    or tonumber(val) == 0
    or value == no
    then
    or value == en_no
    return false
    or value == 'n'
    else
    or value == 'false'
    return default
    or value == 'f'
    end
    or value == 'off'
    or tonumber(value) == 0
    then
    return false

    else
    return default
    end
    end
    end
    -- </nowiki>

    Revision as of 18:21, 27 June 2020


    --  <nowiki>
    --- 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(value, default)
        value = type(value) == 'string' and lower(value) or value
    
        local yes = lower(msg('htmlform-yes'):plain())
        local no = lower(msg('htmlform-no'):plain())
        local en_yes = lower(msg('htmlform-yes'):inLanguage('en'):plain())
        local en_no = lower(msg('htmlform-no'):inLanguage('en'):plain())
    
        if value == nil then
            return nil
    
        elseif value == true
            or value == yes
            or value == en_yes
            or value == 'y'
            or value == 'true'
            or value == 't'
            or value == 'on'
            or tonumber(value) == 1
        then
            return true
    
        elseif value == false
            or value == no
            or value == en_no
            or value == 'n'
            or value == 'false'
            or value == 'f'
            or value == 'off'
            or tonumber(value) == 0
        then
            return false
    
        else
            return default
        end
    end
    --  </nowiki>
    
    Cookies help us deliver our services. By using our services, you agree to our use of cookies.

    Recent changes

  • IC228 • 4 days ago
  • IC228 • 4 days ago
  • IC228 • 5 days ago
  • IC228 • 5 days ago
  • Welcome to the DC Multiverse Wiki


    Cookies help us deliver our services. By using our services, you agree to our use of cookies.