See Global Lua Modules/Feature page
--[[ <pre> Displays content from randomly featured live article from a list or lua table (e.g. Module:Top/data)
Syntax :
{{#invoke:feature page|page1|page2|page3}}
{{#invoke:feature page|#table=datamodule}}
{{#invoke:feature page|#table=Module:Holocron/data}}
{{#invoke:feature page|#chars=500|#table=Module:Holocron/data}}
{{#invoke:feature page|#chars=inf|#table=Module:Holocron/data}}
{{#invoke:feature page|#sanitize=0|#table:Module:Holocron/data}}
--]]
local p = {}
local utility = require("Dev:Utility")
function p.main(frame)
local tArgs = utility.getArgs(frame)
local extTable
if not (tArgs and type(tArgs) == "table") then
return "Nothing to display..."
end
extTable = mw.clone(tArgs)
chars = tArgs["#chars"] or 300
if tArgs["#table"] then
extTable = require(tArgs["#table"])
end
math.randomseed(os.time())
math.random()
local iRand = math.random(#extTable)
if not extTable[iRand] or extTable[iRand] == "" then
return
end
local pagename = extTable[iRand]
local titleFeatured = mw.title.new(pagename)
-- Follow redirect once
if not (titleFeatured and titleFeatured.exists) then
return
end
if titleFeatured.isRedirect then
local pageContents = mw.clone(titleFeatured:getContent())
pageContents = pageContents:match("%[%[(.-)%]%]")
titleFeatured = mw.title.new(pageContents)
end
-- Sanitize content and display
if not (titleFeatured and titleFeatured.exists) then
return
end
pageContents = mw.clone(titleFeatured:getContent())
if not tArgs["#sanitize"] or tArgs["#sanitize"] ~= "0" then
if not (tArgs["#notable"] and tArgs["#notables"] == "1") then
pageContents = pageContents:gsub("%{%s*%|.-%|%s*%}", "")
end
pageContents = pageContents:gsub("%<%s*ref.-%>.-%<%/%s*ref%s*>", "")
pageContents = pageContents:gsub("%<%s*ref.-%/%>", "")
pageContents = pageContents:gsub("%[%[File%:.-%]%]", "")
pageContents = pageContents:gsub("%=%=.-%=%=.*", "")
end
if not (tArgs["#notemplate"] and tArgs["#notemplate"] == "1") then
pageContents = pageContents:gsub("%{%{.-%}%}", "")
end
if chars == "inf" then
return frame:preprocess(pageContents)
end
-- Don't break on a link!
local inlink = false
local intemp = false
local i = 2
while i < tonumber(chars) or inlink or intemp do
local substr = pageContents:sub(i - 1, i)
if substr == "[[" then inlink = true
elseif substr == "{{" then intemp = true
elseif substr == "]]" then inlink = false
elseif substr == "}}" then intemp = false
end
i = i + 1
end
-- Truncation
pageContents = mw.text.truncate(pageContents, i)
pageContents = mw.text.trim(pageContents)
-- Return value
local ret = "[[" .. mw.text.trim(pagename) .. "|(more)]]"
ret = (pageContents ~= "" and pageContents .. " " or "") .. ret
ret = frame:preprocess(ret)
return ret
end
return p