Module:Fling Messages
Appearance
This module is intended for mapping Phone Fling messages into a table. Used on {{Fling Messages }}.
local p = {}
local wrapperTemplate = "Template:Fling Messages"
--Function to determine if string is empty value
local function isEmpty(s)
return s == nil or s == ''
end
local function starts_with(str, start)
return type(str) == 'string' and str:sub(1, #start) == start
end
local function constructTableWithHeader()
local root = mw.html.create('table')
:attr('class', 'mw-collapsible mw-collapsed fling-table')
root:tag('tr')
:attr('style', 'background:#CC6EDE; color:white;')
:tag('th')
:attr('width', '10%')
:wikitext('Progress')
:done()
:tag('th')
:attr('colspan', '2')
:wikitext('Messages')
:done()
return root
end
local function buildMessageBlocks(args, keys)
local blocks = {}
local current = { rows = {}, total = 0, terminated = false }
local i = 1
while i <= #keys do
local kind = ''
local isPhoto = false
for s in string.gmatch(args[keys[i]], '%a+') do
if s == 'photo' then
isPhoto = true
else
kind = s
end
end
local msg = args[keys[i + 1]]
i = i + 2
if not isEmpty(msg) then
current.rows[#current.rows + 1] = { kind = kind, msg = msg }
if isPhoto then
current.total = current.total + 1
current.terminated = true
blocks[#blocks + 1] = current
current = { rows = {}, total = 0, terminated = false }
elseif kind == 'girl' or kind == 'boy' then
current.total = current.total + 1
end
end
end
if #current.rows > 0 then
blocks[#blocks + 1] = current -- terminated stays false
end
return blocks
end
--uses the template as a wrapper, grabbing any arguments passed to the template
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrapper=wrapperTemplate
})
return p._main(args, frame)
end
function p._main(args, frame)
frame = frame or mw.getCurrentFrame()
local keys = {}
for k in pairs(args) do
local n = tonumber(k)
if n and n >= 1 then keys[#keys + 1] = n end
end
table.sort(keys)
local blocks = buildMessageBlocks(args, keys)
local outputTable = constructTableWithHeader()
for _, block in ipairs(blocks) do
local counter = 0
for _, entry in ipairs(block.rows) do
local kind = entry.kind
local msg = entry.msg
local isYou = (kind == 'you')
local isPause = (kind == 'pause')
local fromThem = not isYou and not isPause
local textSplit = mw.text.split(msg, '`', true)
local B = textSplit[2]
local row = outputTable:tag('tr')
row:attr('class', 'message-'..kind)
if fromThem then
counter = counter + 1
end
if isYou then
row:tag('td'):wikitext('(Response)'):done()
elseif isPause then
row:tag('td'):wikitext("Pause"):done()
elseif not block.terminated then
row:tag('td'):wikitext(counter .. '/??'):done()
else
row:tag('td'):wikitext(counter .. '/' .. block.total):done()
end
if isEmpty(B) then
row:tag('td')
:attr('colspan', '2')
:wikitext(textSplit[1] or '')
:done()
else
row:tag('td'):attr('width', '45%'):wikitext(textSplit[1] or ''):done()
row:tag('td'):attr('width', '45%'):wikitext(B):done()
end
end
end
return outputTable
end
return p