Module:Str2wikitable
Jump to navigation
Jump to search
This module creates a wikitable based on a string input.
Parameters
The following parameters are available.
Input parameter | Description | Format |
---|---|---|
header |
Headers for the table. | Rank, Account name, Points |
link |
Link the interesting info to a page, currently supports: poe | nil, poe, nil |
pattern |
The pattern to find in the string and extract information from. | "rank":(%d*), "account":{"name":"(.-)", "points":(%d*) |
string |
The string to search information in. | string |
Implemented templates
The above documentation is transcluded from Module:Str2wikitable/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
local util = require('Module:Util')
local getArgs = require('Module:Arguments').getArgs
local g_args, g_frame
local p = {}
function p.main(frame)
g_args = getArgs(frame, {
parentFirst = true
})
g_frame = util.misc.get_frame(frame)
-- Inputs from template are always strings.
-- g_args["header"] = [[Rank, Account name, Points]]
-- g_args["link"] = [[nil, poe, nil]]
-- g_args["pattern"] = [["rank":(%d*), "account":{"name":"(.-)", "points":(%d*)]]
-- g_args["string"] = [[<nowiki>
-- c.resetPaginator({"limit":20,"seasonId":"Winterheart Race Season","total":15000,"disableAccountNames":false,"entries":[{"account":{"name":"Krame_","challenges":{"total":8},"twitch":{"name":"krame_420"}},"points":2525,"rank":1},{"account":{"name":"bottlehead93","challenges":{"total":3}},"points":2100,"rank":2},{"account":{"name":"Tetoz","challenges":{"total":16},"twitch":{"name":"tetozlol"}},"points":1994,"rank":3},{"account":{"name":"pasta777","challenges":{"total":0},"twitch":{"name":"boon777"}},"points":1959,"rank":4},{"account":{"name":"Goratha","challenges":{"total":0},"twitch":{"name":"goratha"}},"points":1924,"rank":5},{"account":{"name":"Fightgarr","challenges":{"total":36}},"points":1825,"rank":6},{"account":{"name":"Robit","challenges":{"total":35},"twitch":{"name":"therobitlaser"}},"points":1789,"rank":7},{"account":{"name":"Steelmage","challenges":{"total":39},"twitch":{"name":"steelmage202"}},"points":1760,"rank":8},{"account":{"name":"D84","challenges":{"total":37}},"points":1748,"rank":9},{"account":{"name":"Cataract","challenges":{"total":25}},"points":1714,"rank":10},{"account":{"name":"Ghbeats","challenges":{"total":40},"twitch":{"name":"ghbeats"}},"points":1712,"rank":11},{"account":{"name":"feelgewd","challenges":{"total":24}},"points":1703,"rank":12},{"account":{"name":"Johnny_Annihilation","challenges":{"total":28},"twitch":{"name":"yor0"}},"points":1669,"rank":13},{"account":{"name":"MewsicalPulse","challenges":{"total":28},"twitch":{"name":"ilbistarz"}},"points":1637,"rank":14},{"account":{"name":"D_bak","challenges":{"total":40}},"points":1627,"rank":15},{"account":{"name":"Whirlwarian","challenges":{"total":32}},"points":1610,"rank":16},{"account":{"name":"Morsexier","challenges":{"total":2},"twitch":{"name":"morsrageng"}},"points":1587,"rank":17},{"account":{"name":"Karvarousku","challenges":{"total":36},"twitch":{"name":"karvarouskugaming"}},"points":1563,"rank":18},{"account":{"name":"Mirato","challenges":{"total":16}},"points":1557,"rank":19},{"account":{"name":"passofexhale","challenges":{"total":38},"twitch":{"name":"glaum"}},"points":1520,"rank":20}]});
-- </nowiki>]]
-- Remove <nowiki> if string requires it.
g_args["string"] = mw.text.unstripNoWiki( g_args["string"] )
-- String to table
g_args["header"] = str2tbl( mw.text.unstripNoWiki(g_args["header"]) )
g_args["link"] = str2tbl( mw.text.unstripNoWiki(g_args["link"]) )
g_args["pattern"] = str2tbl( mw.text.unstripNoWiki(g_args["pattern"]) )
-- Extract interesting information.
for i,v in ipairs(g_args["header"]) do
g_args["header"][v] = gmatch( g_args["string"], g_args["pattern"][i] )
end
-- Create table for easy copy/pasting.
sep_header = " !! "
sep_col = " || "
-- Wikitable intro
g_args["tbl"] = [[{| class="wikitable sortable" style="text-align: center;"]] .. "\n ! "
-- Wikitable headers
for i=1,#g_args["header"] do
g_args["tbl"] = g_args["tbl"] .. g_args["header"][i] .. sep_header
end
g_args["tbl"] = g_args["tbl"]:sub(1,-#sep_header) -- Remove the empty header column.
-- Wikitable content
for i=1,#g_args["header"][g_args["header"][1]] do
g_args["tbl"] = g_args["tbl"] .. "\n|-\n | "
for j,v in ipairs(g_args["header"]) do
if g_args["link"][j] == "poe" then
g_args["tbl"] = g_args["tbl"] .. link_poe(tostring(g_args["header"][v][i])) .. sep_col
else
g_args["tbl"] = g_args["tbl"] .. tostring(g_args["header"][v][i]) .. sep_col
end
end
g_args["tbl"] = g_args["tbl"]:sub(1,-#sep_col) -- Remove the empty column.
end
-- Wikitable outro
g_args["tbl"] = g_args["tbl"] .. "\n|}"
-- Output
return g_args["tbl"]
end
function gmatch(s, pattern)
words = {}
i=1
for word in string.gmatch(s, pattern) do
words[i]=word
i=i+1
end
return words
end
function link_poe(account)
return "[http://www.pathofexile.com/account/view-profile/" .. account .. " " .. account .. "]"
end
function str2tbl(string)
return mw.text.split(string,"%s*,%s*")
end
return p