Module:News

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"

function isNewsPage return mw.title.getCurrentTitle == 'Hitler Parody Wiki:News' end

-- no fuss way to generate date table function makeDate(y, m, d)  return os.date('!*t', lang:formatDate( 'U', y..'-'..m..'-'..d ) ) end

-- returns date table function parseDate(s) return os.date('!*t', lang:formatDate( 'U', s ) ) end

function monthDiff(d1, d2) local baseyear = 2000 local m1 = (d1.year - baseyear) * 12 + d1.month local m2 = (d2.year - baseyear) * 12 + d2.month return m2 - m1 end

-- compares the months and determine if news item is old function isOld(d1) return monthDiff(d1, today)>maxMonthAge end

function h2(s) return ' ' ..s.. ' ' end

function h3(s) return ' ' ..s.. ' ' end

-- takes a table of strings and returns a HTML ul -- NOTE: only iterates the numbered arguments in the table function itable2ul(t) --if tablelength(t) == 0 then return '' end --if type(t) == 'string' then return t end local s = {} table.insert(s, '') for _,v in ipairs(t) do       table.insert(s, '' .. v .. '') end table.insert(s, '') return table.concat(s) end

-- MODULE METHODS -- year header function p.h2(frame) local d = makeDate(frame.args[1], 12, 31) -- take as end of year if not isOld(d) or isNewsPage then return h2(frame.args[1]) end end

-- month header function p.h3(frame) local d = parseDate(frame.args[1]) if not isOld(d) or isNewsPage then return h3(frame.args[1]) end end

-- the main method -- args[date]: the news date -- args[n]: items in the news for the above date function p.item(frame) local dateStr = frame.args[date] local dateTbl = os.date( '!*t', dateStr ) if not isOld(dateTbl) or isNewsPage then local sDate = lang:formatDate(dateDispFmt, dateStr) local htmDate = ''..sDate..' ' local htmNews = itable2ul(frame.args) return htmDate .. htmNews end end

return p