このモジュールはブール値文字列の入力を処理するために必要な統一インターフェースを使用することに用います。Luaではtrueとfalseといったブール値がありますが、ウィキテキストにおけるテンプレートでは"yes"、"no"など文字列でしか処理できません。このモジュールはこれらの文字列を処理して、Luaで使用するためのブール値に変換します。また、nilとfalseを区別できるよう、nilの入力はnilとして返すことができます。このモジュールはブール値、数字、テーブル、関数の入力も受け付けています。
この解説は、モジュール:Yesno/docから呼び出されています。 (編集 | 履歴) 編集者は、このモジュールをサンドボックス (作成 | 複製)とテストケース (作成)で試すことができます。(解説) このモジュールのサブページ一覧。 |
-- 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 tonumber(val) == 1 then return true elseif val == false or val == 'no' or val == 'n' or val == 'false' or val == 'f' or tonumber(val) == 0 then return false else return default end end