local p = {}

local function allcases(s)
	return s:gsub('([%^%$%(%)%%%.%[%]%*%+%-])', '%%%1')
		:gsub('%a', function(letter) return '['..letter:upper()..letter:lower()..']' end)
end

local function getParam(content, template, parameter)
	if content == nil then return ''
	else return content:gsub('%b{}',
		function(templated)
			return templated:gsub('^{{%s*'..template..'%s*(|.-})}$',
				function(parameters)
					return parameters:gsub('|%s*'..allcases(parameter)..'%s*=%s*([^|}]+)', '%1')
				end)
		end)
	end
end

function p.main(frame)
    local rowsToGet = tonumber(frame.args[1]) or 200  -- use this to determine number of rows to get (default 200)
    local rowOffset = tonumber(frame.args[2]) or 0   -- use this offset to allow multiple calls to build larger table   
    local source = mw.title.new('Wikipedia:User scripts/Most imported scripts'):getContent()
    local data = {}
	local rows = mw.html.create()
    local count = 0
    for scriptTitle, total, active in source:gmatch('\n| %[%[([^%]]+)%]%] -\n| (%d+) -\n| (%d+)') do
		count = count + 1
		if count > rowOffset then
			local redirectTarget = mw.title.new(scriptTitle).redirectTarget
			if redirectTarget then local scriptTitle = redirectTarget.prefixedText end
			local scriptText = mw.title.new(scriptTitle):getContent()
			-- don't include scripts that have been blanked or redirected as non-functional
			if not scriptText:find("mw.log.warn( 'You installed the userscript", 1, true) then
				data[scriptTitle] = { total = total, active = active }
				local docTitle = scriptTitle:match('(.-)%.[CJcj][Ss][Ss]?$')
				local redirectTarget = mw.title.new(docTitle).redirectTarget
				if redirectTarget then local docTitle = redirectTarget.prefixedText end
				local name = docTitle:match('([^/:]-)$')
				local author = scriptTitle:match('User:([^%/]+)')
				local desc = ''
				local status = ''
				local browsers = ''
				local skins = ''
				local docText = mw.title.new(docTitle):getContent() or ''
				if docText ~= '' then
					name = getParam(docText, '[Ii]nfobox user script', 'name') or getParam(docText, '[Ii]nfobox wikipedia user script', 'name') or docTitle:match('([^/:]-)$')
					author = getParam(docText, '[Ii]nfobox user script', 'author') or getParam(docText, '[Ii]nfobox wikipedia user script', 'author') or script:match('User:([^%/]+)')
					status = getParam(docText, '[Ii]nfobox user script', 'status') or getParam(docText, '[Ii]nfobox wikipedia user script', 'status') or ''
					browsers = getParam(docText, '[Ii]nfobox user script', 'browsers') or getParam(docText, '[Ii]nfobox wikipedia user script', 'browsers') or ''
					skins = getParam(docText, '[Ii]nfobox user script', 'skins') or getParam(docText, '[Ii]nfobox wikipedia user script', 'skins') or ''
					desc = getParam(docText, '[Ii]nfobox user script', 'desc') or getParam(docText, '[Ii]nfobox wikipedia user script', 'desc') or mw.text.truncate(docText
						--keep descriptions from template params						
						:gsub('^{{[Uu]|([^}]+)', '[[User:%1|%1]]') --expand {{u}} at top level
						:gsub("%b{}", '') --remove other templates
						--strip out images, files, media, categories
						:gsub('%b[]',
							function(bracketed)
								return bracketed:gsub('^%[%[%s*(%a+):.-%]%]$',
									function(link_prefix)
										link_prefix = link_prefix:lower()
										if link_prefix == "image" or link_prefix == "file"
										or link_prefix == "media" or link_prefix == "category" then
											return ""
										end -- otherwise leave it alone
									end)
							end)
						--remove spans while keeping text inside
						--strip out remaining tags and the text inside
						:gsub('<(%a+)[^>]+>(.-)</%1>', function(tag, contents)
							if tag:lower() == "span" then
								return contents
							else
								return ""
							end
						end)
						:gsub('%b<>', '') --remove any other tag markup
						:gsub('__[^_]+__', '') --remove __ markups
						:gsub('^=+[^=]+=+', ''):gsub('\n=+[^=]+=+', '') --remove section titles
						:gsub("''+", "") --strip out bold and italic markup
						:gsub('&nbsp;', ' ') --replace nbsp spaces with regular spaces
						:gsub('^[:;%s]+', ''):gsub('\n[:;%s]+', '\n') --strip indents, leading
						:gsub('{|.-\n|}', '') --remove tables
						:gsub('\n|[^\n]*\n', '') --remove table fragments
						:gsub('%s+\n', '\n') --and trailing spaces
						:gsub('(%s)%s+', '%1') --strip redundant spaces
						:gsub(allcases(name)..'(%s)', "'''"..name.."'''%1")
						, 600, ''):gsub('^(.+%.).+$', '%1') --truncate at end of last sentence before 600 chars
						.. '<!-- -->'
					desc = mw.text.trim(desc):gsub('^(.+)%.$', '%1')..'.'  --trim and add last period, if missing
						.. ' [[' .. docTitle .. '|→]]' --add arrow link to full doc
				end
				local row = rows:tag('tr'):attr('id', scriptTitle):attr('style','vertical-align:top')
				if docText == '' then link = scriptTitle else link = docTitle end
				row:tag('td'):wikitext(string.format('\'\'\'[[%s|%s]]\'\'\'<span id="%s" class=scriptInstallerLink></span>\n<p>%s</p>', link, name, scriptTitle, desc))
				row:tag('td'):wikitext(string.format('[[User:%s|%s]]', author, author))
				row:tag('td'):wikitext(frame:callParserFunction('#time', 'j M Y', frame:callParserFunction('REVISIONTIMESTAMP', scriptTitle))):addClass('nowrap'):attr('style','text-align:right')
				row:tag('td'):wikitext(status)
				row:tag('td'):wikitext(skins)
				row:tag('td'):wikitext(browsers)
				row:tag('td'):wikitext(data[scriptTitle].active):attr('style','text-align:right')
				row:tag('td'):wikitext(data[scriptTitle].total):attr('style','text-align:right')
				rows:wikitext('\n')
			end
		end
	    if count >= rowsToGet + rowOffset then break end
    end
    return rows
end

return p