User:Funa-enpitu/Forum/thread.lua

--

--[[
Forum.jsのスレッド一覧取得用モジュール

@author Funa-enpitu
@source https://www.mediawiki.org/wiki/User:Funa-enpitu/Forum/thread.lua
@license Creative Commons Attribution-ShareAlike License 4.0
@latest ver.1.0 | 23 January 2024 (UTC)
]]
local p = {};

function p.main(frame)
	if(frame.args[3]=='true') then
		return '<table>'..tr(frame.args[1],' class="f-sticky"')..tr(frame.args[2],'')..'</table>'
	else
		return '<table class="wikitable" style="width:100%;"><tr><th style="width:50%;">スレッド</th><th>投稿数</th><th>最終投稿</th></tr>'..tr(frame.args[1],' class="f-sticky"')..tr(frame.args[2],'')..'</table>'
	end
end

function tr(json,attr)
	local tr = ''
	for idx, val in pairs(mw.text.jsonDecode(json)) do
		local i = p.getinfo({args={page=val['title']}})
		tr = tr..string.format('<tr'..attr..'><td>[[%s|%s]]</td><td>%s</td><td>[[%s#post-%s|%s]] by [[User:%s|%s]]</td></tr>',val['title'],mw.title.new(val['title']).subpageText,i['posts'],val['title'],i['posts'],i['date'],i['user'],i['user'])
	end
	return tr
end

function p.getinfo(frame)
	local lastpost = string.gsub(mw.title.new(frame.args['page']):getContent(),'(.*){{post|(.-)}}$','%2')
	local lpinfo = mw.text.split(lastpost,'|')
	if(frame.args['js']=='true') then
		return lpinfo[2]
	else
		return {
			user= lpinfo[1],
			posts= lpinfo[2],
			date= lpinfo[3]
		}
	end
end

return p;
--