local cleanArgs = {}
	for key, val in pairs(argsTable) do
		if type(val) == 'string' then
			val = val:match('^%s*(.-)%s*$')
			if val ~= '' then
				cleanArgs[key] = val
			end
		else
			cleanArgs[key] = val
		end
	end
	return cleanArgs
end

function isAffirmed(val)
	if not(val) then return false end
	local affirmedWords = ' add added affirm affirmed include included on true yes y '
	return string.find(affirmedWords, ' '..string.lower(val)..' ', 1, true ) and true or false
end

function makeOutput
	local output = ''
	local itemIndex = 1
	local maxCount = math.min
	while itemIndex <= maxCount do
		output = output .. allItems[itemIndex] .. '\n'
		itemIndex = itemIndex + 1
	end
	if more then
		output = output .. more
	end
	return mw.text.trim(output)
end

function cleanForPatternMatching(wikitext)
	-- remove wikilink brackets
	local cleaned = mw.ustring.gsub(wikitext, "%[%[(.-)%]%]","%1")
	-- remove pipes that would have been in piped links
	cleaned = mw.ustring.gsub
	-- remove external links
	cleaned = mw.ustring.gsub
	return cleaned
end

function formatDateString
	if mdyDates then
		formattedDatePattern = "%2 %3, %1"
	else
		formattedDatePattern = "%1 %2 %3 "
	end
	return ''
end

function makeCollapsed
	return "" .. innerText .. ""
end

-- Get current events for a "YYYY Month D" date. Returns a table of list items.
function getCurrentEvents
	local title = mw.title.new("틀:요즘 화제 처음/" .. date)
	local raw = title:getContent
	if (not raw) or raw == '' then
		return {}
	end
	local lines = mw.text.split
	local items = {}
	local itemHeading = ''
	local cleanItemHeading = ''
	local previousItemPrefix = ''

	local formattedDate = formatDateString

	for i, v in ipairs(lines) do
		local keep = false
		local skip = false
		local isSublistItem = ( string.sub == '**' )
		local isListItem = not isSublistItem and ( string.sub == '*' )
		local hasSublistItem = isListItem and i < #lines and ( string.sub == '**' )

		if hasSublistItem then
			itemHeading = mw.text.trim(mw.ustring.gsub)
			cleanItemHeading = cleanForPatternMatching(itemHeading)
		elseif isListItem then
			itemHeading = ""
			cleanItemHeading = ""
		end

		if (isListItem and not hasSublistItem) or isSublistItem then
			local text = cleanForPatternMatching(v)
			for ii, keepPatt in pairs(keepPatterns) do
				if not keep and ( mw.ustring.find or mw.ustring.find ) then
					keep = true
				end
			end
			if #skipPatterns > 0 then
				for iii, skipPatt in pairs(skipPatterns) do
					if not skip and ( mw.ustring.find or mw.ustring.find ) then
						skip = true			
					end
				end
			end
		end

		if keep and not skip then
			local itemPrefix = ";" .. formattedDate
			if itemHeading ~= "" then itemPrefix = itemPrefix .. " **"..itemHeading.."**" end
			itemPrefix = itemPrefix .. "\n:"
			if previousItemPrefix == itemPrefix then
				itemPrefix = ':'
			else
				previousItemPrefix = itemPrefix
			end
			local item = mw.ustring.gsub
			if showWikitext then
				-- remove html comments
				local itemWikitext = mw.ustring.gsub
				-- remove prefix from wikitext
				itemWikitext = mw.ustring.gsub(itemWikitext, ";(.-)\n", "")
				itemWikitext = ""
				-- remove prefix from item
				itemWithoutPrexix = mw.ustring.gsub
				item = itemPrefix .. makeCollapsed
			end
			table.insert
		end
	end
	return items
end

function getItems
	local allItems = {}
	local lang = mw.language.new('ko')
	local daysAgo = 0
	while daysAgo < maxDays do
		local day = lang:formatDate
		local dailyItems = getCurrentEvents
		for i, item in ipairs(dailyItems) do
			table.insert
		end
		daysAgo = daysAgo + 1
	end
	return allItems
end

function getPatterns
	local patterns = {}
	local ii = 1
	while args[prefix and prefix..ii or ii] do
		patterns[ii] = args[prefix and prefix..ii or ii]
		ii = ii + 1
	end
	return patterns
end

local p = {}

p.main = function(frame)
	local parent = frame.getParent(frame)
	local parentArgs = parent.args
	local args = cleanupArgs(parentArgs)

	if args['not'] and not args['not1'] then
		args['not1'] = args['not']
	end
	
	local patterns = getPatterns(args)
	if #patterns < 1 then
		return error("검색 패턴이 설정되지 않음")
	end

	local skipPatterns = getPatterns

	local days = tonumber(args.days or args['일'] or args['일수']) or 30

	local mdyDates = args.dates or args['날짜'] and string.lower(args.dates or args['날짜']) == 'mdy'
	
	local showWikitext = isAffirmed(args.wikitext or args['위키문법'])

	local allItems = getItems
	if #allItems < 1 then
		return args.header or args['윗글'] or args['머리말'] and '' or args.none or '최근 소식 없음'
	end

	local maxItems = tonumber(args.max or args['최대']) or 6

	local more = args.more or args['더보기']
	if isAffirmed(args.more or args['더보기']) then
		more = "**요즘 화제 더보기...**"
	end

	local output = makeOutput
	if args.header or args['윗글'] or args['머리말'] then
		output = (args.header or args['윗글'] or args['머리말']) .. '\n' .. output .. '\n' .. (args.footer or args['아랫글'] or '')
	end
	local needsExpansion = mw.ustring.find	
	if needsExpansion then
		return frame:preprocess(output)
	else 
		return output
	end

end