コンテンツにスキップ
メインメニュー
メインメニュー
サイドバーに移動
非表示
案内
メインページ
最近の更新
カテゴリ一覧
信号灯器
レンズ
アーム
フード(庇)
金具
信号柱
信号機メーカー
警交仕規
制御機
信号制御
設置方法
音響装置
管理番号
周辺物
交差点
その他
俗語集
俗語
編集方針等
信号機Wikiの編集方針
信号機Wikiを二次利用する
ヘルプ
著作権について
お問い合わせ
信号機Wiki
検索
検索
表示
アカウント作成
ログイン
個人用ツール
アカウント作成
ログイン
ログアウトした編集者のページ
もっと詳しく
投稿記録
トーク
「
モジュール:Template invocation
」を編集中
モジュール
議論
English
閲覧
ソースを編集
履歴表示
ツール
ツール
サイドバーに移動
非表示
操作
閲覧
ソースを編集
履歴表示
全般
リンク元
関連ページの更新状況
特別ページ
ページ情報
表示
サイドバーに移動
非表示
警告:
ログインしていません。編集を行うと、あなたの IP アドレスが公開されます。
ログイン
または
アカウントを作成
すれば、あなたの編集はその利用者名とともに表示されるほか、その他の利点もあります。
スパム攻撃防止用のチェックです。 けっして、ここには、値の入力は
しない
でください!
-- This module provides functions for making MediaWiki template invocations. local checkType = require('libraryUtil').checkType local p = {} ------------------------------------------------------------------------ -- Name: p.name -- Purpose: Find a template invocation name from a page name or a -- mw.title object. -- Description: This function detects whether a string or a mw.title -- object has been passed in, and uses that to find a -- template name as it is used in template invocations. -- Parameters: title - full page name or mw.title object for the -- template (string or mw.title object) -- Returns: String ------------------------------------------------------------------------ function p.name(title) if type(title) == 'string' then title = mw.title.new(title) if not title or #title.prefixedText == 0 or #title.interwiki > 0 then error("name関数のtitle引数の値が不正です", 2) end elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then error("name関数の第1引数は文字列かmw.titleオブジェクトでなければなりません", 2) end if title.namespace == 10 then local text = title.text local check = mw.title.new(text, 10) -- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo". return check and mw.title.equals(title, check) and text or title.prefixedText elseif title.namespace == 0 then return ':' .. title.prefixedText else return title.prefixedText end end ------------------------------------------------------------------------ -- Name: p.invocation -- Purpose: Construct a MediaWiki template invocation. -- Description: This function makes a template invocation from the -- name and the arguments given. Note that it isn't -- perfect: we have no way of knowing what whitespace was -- in the original invocation, the named parameters will be -- alphabetically sorted, and any parameters with duplicate keys -- will be removed. -- Parameters: name - the template name, formatted as it will appear -- in the invocation. (string) -- args - a table of template arguments. (table) -- format - formatting options. (string, optional) -- Set to "nowiki" to escape, curly braces, pipes and -- equals signs with their HTML entities. The default -- is unescaped. -- Returns: String ------------------------------------------------------------------------ function p.invocation(name, args, format) checkType('invocation', 1, name, 'string') checkType('invocation', 2, args, 'table') checkType('invocation', 3, format, 'string', true) -- Validate the args table and make a copy to work from. We need to -- make a copy of the table rather than just using the original, as -- some of the values may be erased when building the invocation. local invArgs = {} for k, v in pairs(args) do local typek = type(k) local typev = type(v) if typek ~= 'string' and typek ~= 'number' or typev ~= 'string' and typev ~= 'number' then error("invocationの第2引数の値が不正です(引数名と引数値は文字列か数字しか受け付けません)", 2) end invArgs[k] = v end -- Get the separators to use. local seps = { openb = '{{', closeb = '}}', pipe = '|', equals = '=' } if format == 'nowiki' then for k, v in pairs(seps) do seps[k] = mw.text.nowiki(v) end end -- Build the invocation body with numbered args first, then named. local ret = {} ret[#ret + 1] = seps.openb ret[#ret + 1] = name for k, v in ipairs(invArgs) do if type(v) == 'string' and v:find('=', 1, true) then -- Likely something like 1=foo=bar which needs to be displayed as a named arg. else ret[#ret + 1] = seps.pipe ret[#ret + 1] = v invArgs[k] = nil -- Erase the key so that we don't add the value twice end end local keys = {} -- sort parameter list; better than arbitrary order for k, _ in pairs(invArgs) do keys[#keys + 1] = k end table.sort(keys, function (a, b) -- Sort with keys of type number first, then string. if type(a) == type(b) then return a < b elseif type(a) == 'number' then return true end end ) local maybeSpace = '' -- First named parameter should not be separated by a space for _, v in ipairs(keys) do -- Add named args based on sorted parameter list ret[#ret + 1] = maybeSpace .. seps.pipe ret[#ret + 1] = tostring(v) ret[#ret + 1] = seps.equals ret[#ret + 1] = invArgs[v] maybeSpace = ' ' end ret[#ret + 1] = seps.closeb return table.concat(ret) end return p
編集内容の要約:
信号機Wikiへの投稿はすべて、クリエイティブ・コモンズ 表示-継承 (詳細は
信号機Wiki:著作権
を参照)のもとで公開したと見なされることにご注意ください。
自分が書いたものが他の人に容赦なく編集され、自由に配布されるのを望まない場合は、ここに投稿しないでください。
また、投稿するのは、自分で書いたものか、パブリック ドメインまたはそれに類するフリーな資料からの複製であることを約束してください。
著作権保護されている作品は、許諾なしに投稿しないでください!
ウィキを自動編集スパムから保護するために、下のCAPTCHAを解決してください。
キャンセル
編集の仕方
(新しいウィンドウで開きます)
このページで使用されているテンプレート:
モジュール:Template invocation/doc
(
編集
)