FANDOM


--[[ <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}}
--]]
local p = {}
local utility = require("Dev:Utility")
 
function p.main(frame)
    local tArgs = utility.getArgs(frame)
    local extTable 
 
    if tArgs and type(tArgs)=="table" then
        extTable = mw.clone(tArgs)
 
        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 titleFeatured  and titleFeatured.exists then 
            if titleFeatured.isRedirect then
                local pageContents = mw.clone(titleFeatured:getContent())
                pageContents = pageContents:match("%[%[(.-)%]%]")
                titleFeatured = mw.title.new(pageContents)
            end
 
            --Sanitize content and display
            if titleFeatured and titleFeatured.exists then
                pageContents  = mw.clone(titleFeatured:getContent())
                local featImg = pageContents:gsub(' ','_'):match("File:(.-\.[^\]%s|]+)");
 
                 if not (tArgs["#notable"] and tArgs["#notables"]=="1") then
                    pageContents = pageContents:gsub("%{%s*%|.-%|%s*%}","")
                end
 
                pageContents = string.gsub(pageContents,"%<%s*ref.-%>.-%<%/%s*ref%s*>","")
                pageContents = string.gsub(pageContents,"%<%s*ref.-%/%>","")
                pageContents = pageContents:gsub("%[%[File%:.-%]%]","")
                pageContents = pageContents:gsub("%=%=.-%=%=.*","")
 
                if not (tArgs["#notemplate"] and tArgs["#notemplate"]=="1") then
                    pageContents = pageContents:gsub("%{%{.-%}%}","")
                end
 
                pageContents = mw.text.truncate(pageContents,300)
                pageContents = mw.text.trim(pageContents)
 
                return frame:preprocess(
                    (featImg and ("[[File:" .. featImg .. "|120px|right|link=]]") or "")
                    .. pageContents
                    .. " <small>([["..pagename.."|more...]])</small>"
                )
            end
        end
    end
    return "Nothing to display..."
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.