FANDOM


local p = {}
local today = os.date('!*t')
local lang = mw.language.new('en')
local maxMonthAge = 3 -- older than this will not be displayed
local dateDispFmt = 'j F Y' -- "1 January 2017"
local parserFooter = "----\n''Parsed using HPW's News Module. [[Thread:23415|Info/Feedback]]''"
local markerData = mw.loadData('Module:News/markerdata')
 
-- no fuss way to generate date table
local function makeDate(y, m, d)
   return os.date('!*t', lang:formatDate( 'U', y..'-'..m..'-'..d ) )
end
 
-- returns date table
local function parseDate(s)
    return os.date('!*t', lang:formatDate( 'U', s ) )
end
 
-- return a date object a specified months before given date
local function targetDate(d1,dmo)
    local baseyear = 2000
    local monthsI = (d1.year-baseyear) * 12 + d1.month
    local monthsT = monthsI - dmo
    local tYr = baseyear + math.floor(monthsT/12)
    local tMo = monthsT % 12
    return makeDate(tYr, tMo, d1.day)
end
 
--[[
THE NEW PARSE APPROACH - WIP
what it does:
    1) calculate target month
    2) find target month header in text
    3) split text into two
    4) take first half, preprocess and display
arguments:
    ns: the namespace. defaults to Project
    pagename: pagename to have parsed
    maxmonthage: max age in months
    full: don't parse, return full page
]]--
function p.parse2(frame)
    -- extract arguments
    local dataPageNS = frame.args.ns or 'Project'
    local dataPageName = frame.args.pagename or 'News/content'
    local age = frame.args.maxmonthage or maxMonthAge
 
    local dataPage = mw.title.new(dataPageName, dataPageNS)
    local dataStr = dataPage:getContent()
 
    if not frame.args.full then
        local cutoffDate = targetDate(today,age)
        local cutoffDateStr = os.date('%Y-%m-%d', os.time(cutoffDate))
        local cutoffStr, pattern
        if cutoffDate.month==12 then
            -- find the year header instead
            cutoffStr = lang:formatDate('Y',cutoffDateStr)
            pattern = '==\'*' .. cutoffStr .. '\'*=='
        else
            cutoffStr = lang:formatDate('F Y',cutoffDateStr)
            pattern = '===\'*' .. cutoffStr .. '\'*==='
        end
        local cutoffPos = mw.ustring.find(dataStr, pattern)
        dataStr = mw.ustring.sub(dataStr, 1, cutoffPos-1) .. parserFooter
        --mw.log(dataStr)
    end
    return '<div class="news-container">' .. frame:preprocess(dataStr) .. '</div>'
end
 
-- lua implementation of the news date template
local function dateHead(frame, dateStr)
    local dateFmt = lang:formatDate('Y-m-d',dateStr)
    local todayFmt = os.date('%Y-%m-%d')
    if dateFmt == todayFmt then
        return "<span class=\"today\">'''Today'''</span>"
    else
        -- formats date according to user preference
        dateStr = frame:preprocess("{{#formatdate:"..dateStr.."|dmy}}")
        return "'''" .. dateStr .. "'''"
    end
end
function p.date(frame)
    return dateHead(frame, frame.args[1])
end
 
-- lua implementation of the news marker template, WIP
--[[note: uses the parent's arguments so template:news marker 
   should not pass any argument]]
local function markers(args)
    local outTbl = {}
    local classTbl = {} -- classes to append to container
    for _, v in pairs(args) do
        if v == "BN/UU" then v="BNUU" end
        if markerData[v] ~= nil then 
            table.insert(outTbl, markerData[v]) 
            table.insert(classTbl, v)
        end
    end
    return '<div class="news-marker pull-right right ' 
    ..table.concat(classTbl,' ').. 
    '" style="float:right; padding-left:4px; position:relative">' 
    ..table.concat(outTbl, "&nbsp;").. '</div>'
end
function p.marker(frame)
    local args = frame:getParent().args
    return markers(args)
end
 
-- news of the day (ported from module:notd)
-- I genuinely don't remember when and why I made this
function p.notd(frame)
    -- extract arguments
    local dataPageNS = frame.args.ns or 'Project'
    local dataPageName = frame.args.pagename or 'News/content'
 
    local so = mw.title.new(dataPageName, dataPageNS)
    local sm = dataPage:getContent()
    --local lang = mw.language.new('en')
    local sDate = lang:formatDate('j F Y')
    local sFind = '{{news date|' .. sDate .. '}}'
    local i = mw.ustring.find(sm,sFind,1,true)
    if i ~= nil then
        local j = mw.ustring.find(sm, '{{news date|', i+mw.ustring.len(sFind), true)
        sx = mw.ustring.sub(sm,i+mw.ustring.len(sFind),j-1)
        local st = frame:preprocess(sx)
        return st
    else
        return ''
    end
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.