模組:PatternedCandidateUtils
外觀
![]() | 此模組被引用於系統介面中。 任何對此模組的修改會立即反映於維基百科的使用者介面。 為了避免造成大規模的影響,編者可以在本模組的沙盒 和測試樣例 頁面進行實驗。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 |
本模塊用於生成頁面的標題列表。
例子
以Wikipedia:優良條目評選/列表為例,介紹如何從Wikipedia:優良條目評選/提名區原始碼中抽取二級標題(== ××× ==
)。
{{#invoke:PatternedCandidateUtils|list
| title = Wikipedia:優良條目評選/提名區
| pattern = ==%s*(.-)%s*==%f[^=]
| blackregex = ^=.*=$
| linkprefix = Wikipedia:優良條目評選#
}}
參數解說:
|title=Wikipedia:優良條目評選/提名區
— 從「Wikipedia:優良條目評選/提名區」抽取標題|pattern===%s*(.-)%s*==%f[^=]
— Lua的模式字符串,用於捕獲提取二級標題。|blackregex=^=.*=$
— 上方模式串會過度提取三級標題,如=== Jumbo的意見 ===
會得到= Jumbo的意見 =
。可用本參數屏蔽開頭和結尾是等號的捕獲內容。|linkprefix=Wikipedia:優良條目評選#
— 給提取的標題加上前綴,以供連結到Wikipedia:優良條目評選頁對應章節。
參見
local z = {}
function getCandidates( frame )
local page = mw.title.new( frame.args.title ):getContent()
local matches = {}
local black = {}
if frame.args.black then
for b in mw.text.gsplit( frame.args.black, '|', true ) do
black[b] = true
end
end
for m in mw.ustring.gmatch( page, frame.args.pattern ) do
if not black[m] and not ( frame.args.blackregex and mw.ustring.match( m, frame.args.blackregex ) ) then
table.insert( matches, m )
end
end
return matches
end
function z.count( frame )
return #getCandidates( frame )
end
function z.list( frame )
local list = getCandidates( frame )
local linkprefix = frame.args.linkprefix
for i = 1, #list do
if linkprefix then
list[i] = '[[:' .. linkprefix .. list[i] .. '|' .. string.gsub(list[i], "_", " ") .. ']]'
else
list[i] = '[[:' .. string.gsub(list[i], "_", " ") .. ']]'
end
end
if #list > 0 then
return table.concat( list, '-' )
else
return '暂无'
end
end
return z