Module:Selected current events

function cleanupArgs(argsTable)local cleanArgs = {}for key, val in pairs(argsTable) doif type(val) == 'string' thenval = val:match('^%s*(.-)%s*$')if val ~= '' thencleanArgs[key] = valendelsecleanArgs[key] = valendendreturn cleanArgsendfunction isAffirmed(val)if not(val) then return false endlocal affirmedWords = ' add added affirm affirmed include included on true yes y 'return string.find(affirmedWords, ' '..string.lower(val)..' ', 1, true ) and true or falseendfunction makeOutput(allItems, maxItems, more)local output = ''local itemIndex = 1local maxCount = math.min(#allItems, maxItems)while itemIndex <= maxCount dooutput = output .. allItems[itemIndex] .. '\n'itemIndex = itemIndex + 1endif more thenoutput = output .. moreendreturn mw.text.trim(output)endfunction cleanForPatternMatching(wikitext)-- remove wikilink bracketslocal cleaned = mw.ustring.gsub(wikitext, "%[%[(.-)%]%]","%1")-- remove pipes that would have been in piped linkscleaned = mw.ustring.gsub(cleaned, "%|"," ")-- remove external linkscleaned = mw.ustring.gsub(cleaned, "%[.-%]"," ")return cleanedendfunction formatDateString(dateString, mdyDates)if mdyDates thenformattedDatePattern = "%2 %3, %1"elseformattedDatePattern = "%3 %2 %1"endreturn '<span style="font-weight:normal;">' .. string.gsub(dateString, "(.*) (.*) (.*)", formattedDatePattern) .. ' –</span>'endfunction makeCollapsed(outerText, innerText)return "{{Hidden begin | titlestyle = font-weight:normal | title = " .. outerText .. "}}" .. innerText .. "{{Hidden end}}"end-- Get current events for a "YYYY Month D" date. Returns a table of list items.function getCurrentEvents(date, mdyDates, keepPatterns, skipPatterns, showWikitext)local title = mw.title.new("Portal:Current events/" .. date)local raw = title:getContent()if (not raw) or raw == '' thenreturn {}endlocal lines = mw.text.split( raw , '\n')local items = {}local itemHeading = ''local cleanItemHeading = ''local previousItemPrefix = ''local formattedDate = formatDateString(date, mdyDates)for i, v in ipairs(lines) dolocal keep = falselocal skip = falselocal isSublistItem = ( string.sub( v, 0, 2 ) == '**' )local isListItem = not isSublistItem and ( string.sub( v, 0, 1) == '*' )local hasSublistItem = isListItem and i < #lines and ( string.sub( lines[i+1], 0, 2 ) == '**' )if hasSublistItem thenitemHeading = mw.text.trim(mw.ustring.gsub(v, '%*', '', 1))cleanItemHeading = cleanForPatternMatching(itemHeading)elseif isListItem thenitemHeading = ""cleanItemHeading = ""endif (isListItem and not hasSublistItem) or isSublistItem thenlocal text = cleanForPatternMatching(v)for ii, keepPatt in pairs(keepPatterns) doif not keep and ( mw.ustring.find(text, keepPatt) or mw.ustring.find(cleanItemHeading, keepPatt) ) thenkeep = trueendendif #skipPatterns > 0 thenfor iii, skipPatt in pairs(skipPatterns) doif not skip and ( mw.ustring.find(text, skipPatt) or mw.ustring.find(cleanItemHeading, skipPatt) ) thenskip = trueendendendendif keep and not skip thenlocal itemPrefix = ";" .. formattedDateif itemHeading ~= "" then itemPrefix = itemPrefix .. " '''"..itemHeading.."'''" enditemPrefix = itemPrefix .. "\n:"if previousItemPrefix == itemPrefix thenitemPrefix = ':'elsepreviousItemPrefix = itemPrefixendlocal item = mw.ustring.gsub(v, '%*+', itemPrefix)if showWikitext then-- remove html commentslocal itemWikitext = mw.ustring.gsub(item, "%<%!%-%-(.-)%-%-%>", "")-- remove prefix from wikitextitemWikitext = mw.ustring.gsub(itemWikitext, ";(.-)\n", "")itemWikitext = "<pre>" .. mw.text.nowiki( itemWikitext ) .. "</pre>"-- remove prefix from itemitemWithoutPrexix = mw.ustring.gsub(v, '%*+', '')item = itemPrefix .. makeCollapsed(itemWithoutPrexix, itemWikitext)endtable.insert(items, item)endendreturn itemsendfunction getItems(maxDays, mdyDates, patterns, skipPatterns, showWikitext)local allItems = {}local lang = mw.language.new('en')local daysAgo = 0while daysAgo < maxDays dolocal day = lang:formatDate('Y F j', 'now - '..daysAgo..' days')local dailyItems = getCurrentEvents(day, mdyDates, patterns, skipPatterns, showWikitext)for i, item in ipairs(dailyItems) dotable.insert(allItems, item)enddaysAgo = daysAgo + 1endreturn allItemsendfunction getPatterns(args, prefix)local patterns = {}local ii = 1while args[prefix and prefix..ii or ii] dopatterns[ii] = args[prefix and prefix..ii or ii]ii = ii + 1endreturn patternsendlocal p = {}p.main = function(frame)local parent = frame.getParent(frame)local parentArgs = parent.argslocal args = cleanupArgs(parentArgs)if args['not'] and not args['not1'] thenargs['not1'] = args['not']endlocal patterns = getPatterns(args)if #patterns < 1 thenreturn error("Search pattern not set")endlocal skipPatterns = getPatterns(args, 'not')local days = tonumber(args.days) or 30local mdyDates = args.dates and string.lower(args.dates) == 'mdy'local showWikitext = isAffirmed(args.wikitext)local allItems = getItems(days, mdyDates, patterns, skipPatterns, showWikitext)if #allItems < 1 thenreturn args.header and '' or args.none or 'No recent news'endlocal maxItems = tonumber(args.max) or 6local more = args.moreif isAffirmed(args.more) thenmore = "'''[[Portal:Current events|More current events...]]'''"endlocal output = makeOutput(allItems, maxItems, more)if args.header thenoutput = args.header .. '\n' .. output .. '\n' .. (args.footer or '{{Box-footer}}')endlocal needsExpansion = mw.ustring.find(output, '{{', 0, true)if needsExpansion thenreturn frame:preprocess(output)else return outputendendreturn p