Jump to content

Module:News

From WhiskerWiki

Documentation for this module may be created at Module:News/doc

-- ================================================================================
-- Module dependencies
-- ================================================================================





-- ================================================================================
-- Main table
-- ================================================================================
local p = {}
local json_decode = require("mw.text").jsonDecode


-- ========================================
-- Fetch the news items from the data page
-- ========================================
-- Function to read JSON data from a wiki data page
local function fetch_news_data(data_page)
    
    local raw_content = mw.title.new(data_page):getContent()

    if not raw_content then return {} end  -- Return empty if no data found

    -- Remove <pre> and </pre> tags from raw_content
    raw_content = raw_content:gsub("^<pre>%s*", ""):gsub("%s*</pre>$", "")

    -- Decode JSON into a Lua table
    local success, news_data = pcall(json_decode, raw_content)
    return success and news_data or {}
end







-- ================================================================================
-- WhiskerWiki update news feed
-- ================================================================================

-- Function to render the WhiskerWiki updates news feed
function p.render_whiskerwiki_news(frame)
    
    local data_page = "Data:WhiskerWiki news"
    local news_items = fetch_news_data(data_page)
    
    local output = {}

    table.insert(output, '<div class="news-feed">')
    table.insert(output, '<ul>')  -- Start unordered list

    for i, news_item in ipairs(news_items) do
        if i > 5 then break end  -- Show only the latest 5 news items
        
        local title_display
        if news_item.link and news_item.link ~= "" then
            title_display = string.format('<b>[%s %s]</b>', news_item.link, news_item.title)
        else
            title_display = string.format('<b>%s</b>', news_item.title)
        end

        local posted_date = news_item.date_posted and string.format(
            '<br><span style="font-size:90%%; color:#666;">Posted: %s</span>', 
            news_item.date_posted
        ) or ""

        table.insert(output, string.format('<li>%s - %s%s</li>', title_display, news_item.summary, posted_date))
    end

    table.insert(output, '</ul>')  -- End unordered list
    table.insert(output, '</div>')

    -- Ensure the output is processed correctly by MediaWiki
    return frame:preprocess(table.concat(output, "\n"))
end









return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.