Jump to content

Module:Archive index

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Pppery (talk | contribs) at 23:26, 12 March 2019 (Optimize). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
--[[
Derived from https://github.com/legoktm/hbcai/blob/master/index_help.py, which is

Copyright (C) 2012 Legoktm
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
]]--
local p = {}
local plaintext = require("Module:Plain text") -- Module
local function split_into_threads(text, level3)
	local threads = {}
	local current_thread
	local header_pat 
	text = "\n" .. text
	if level3 then
		header_pat = '\n=== *([^=].-) * === *\n' 
	else
		header_pat = '\n== *([^=].-) *== *\n' 
	end
	local last = 1
	while true do 
		local start, endindex, topic = text:find(header_pat, last)
		if current_thread then 
			current_thread.content = text:sub(last, start)
			threads[#threads + 1] = current_thread
			current_thread = {topic = header}
		end
		if start == nil then
			-- hit end of page
			break
		end
		last = endindex + 1
	end
	if not threads and not level3 then
		return split_into_threads(text, true)
	end
	return threads
end
local function parse_archive(title, out)
	local frame = mw.getCurrentFrame()
	if title.isRedirect then
		title = title.redirectTarget
	end
	local threads = split_into_threads(title:getContent())
	for _, thread in ipairs(threads) do
		local __, reply_count = thread.content:gsub("%(UTC%)","") 
		local link = string.format("[[%s#%s]]", title.prefixedText, frame:callParserFunction("anchorencode", {thread.topic}))
		out = out .. string.format("\n|-\n| %s || %s || %s", thread.topic, tostring(reply_count), link)
	end
	return out
end	
-- End of copied code
function p._main(prefix)
	--local prefix = frame.args.titleprefix
	local archivecount = 0
	local out = mw.getCurrentFrame():extensionTag("templatestyles","",{src="Module:Sandbox/pppery/archive index/styles.css"})
	while true do
		archivecount = archivecount + 1
		local title = mw.title.new(prefix .. archivecount)
		if not title.exists then
			return out
		end
		out = parse_archive(title, out)
	end
end
function p.main(frame)
	return p._main(frame.args[1])
end
return p