[checked revision] | [checked revision] |
(Created page with "-- TournamentTabs local settings = require('Module:SubpageSettings').tournaments local util = require('Module:Util') local pageinfo = require('Module:GetEventPageInfo') local...") Â |
No edit summary  |
||
(3 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
⚫ | |||
− | -- TournamentTabs |
||
+ | local util_title = require('Module:TitleUtil') |
||
+ | |||
local settings = require('Module:SubpageSettings').tournaments |
local settings = require('Module:SubpageSettings').tournaments |
||
⚫ | |||
local pageinfo = require('Module:GetEventPageInfo') |
local pageinfo = require('Module:GetEventPageInfo') |
||
local tabs = require('Module:AutomatedTabs') |
local tabs = require('Module:AutomatedTabs') |
||
Line 7: | Line 8: | ||
local p = {} |
local p = {} |
||
function p.makeArgsIntoTables(args) |
function p.makeArgsIntoTables(args) |
||
+ | local args2 = mw.clone(args) -- since we might have to modify some keys if they are numbers |
||
− | for key, value in pairs( |
+ | for key, value in pairs(args2) do |
+ | key_str = tostring(key) or key |
||
if not p.doWeSkip(key) then |
if not p.doWeSkip(key) then |
||
− | args[ |
+ | args[key_str] = mw.text.split(value,'%s*%f[,%%],%s*') |
⚫ | |||
+ | args[key_str][k] = args[key_str][k]:gsub('%%,',','):gsub('_',' ') |
||
+ | end |
||
end |
end |
||
end |
end |
||
Line 28: | Line 34: | ||
-- making json |
-- making json |
||
function p.jsonFromArgs(args) |
function p.jsonFromArgs(args) |
||
− | if args.oldMD |
+ | if util_args.castAsBool(args.oldMD) then |
p.oldMD(args) |
p.oldMD(args) |
||
end |
end |
||
Line 56: | Line 62: | ||
tbl.links = links |
tbl.links = links |
||
tbl.names = names |
tbl.names = names |
||
− | if args[thispath .. '_nooverview'] |
+ | if not util_args.castAsBool(args[thispath .. '_nooverview']) then |
+ | tbl.links[1] = 'Overview' |
||
+ | end |
||
for k, v in ipairs(links) do |
for k, v in ipairs(links) do |
||
local newpath = tabs.newIndex(path, v) |
local newpath = tabs.newIndex(path, v) |
||
Line 66: | Line 74: | ||
-- if we don't specify if it's specificaly not an overview page |
-- if we don't specify if it's specificaly not an overview page |
||
-- then we probably don't want any subpage for the first tab |
-- then we probably don't want any subpage for the first tab |
||
− | if args[thispath .. '_nooverview'] |
+ | if not util_args.castAsBool(args[thispath .. '_nooverview']) then |
+ | tbl.links[1] = '' |
||
+ | end |
||
+ | tbl.path = thispath -- for maintenance cargo table to be used when automating things |
||
return tbl |
return tbl |
||
end |
end |
||
Line 88: | Line 99: | ||
local navboxdata = {} |
local navboxdata = {} |
||
for i, navbox in ipairs(args.navboxes) do |
for i, navbox in ipairs(args.navboxes) do |
||
+ | local tm = args['navbox' .. i .. '_titlematch'] and args['navbox' .. i .. '_titlematch'][1] |
||
navboxdata[i] = { |
navboxdata[i] = { |
||
title = navbox, |
title = navbox, |
||
args = {}, |
args = {}, |
||
− | events = args['navbox' .. i .. '_events'] or { showAll = true } |
+ | events = args['navbox' .. i .. '_events'] or ((not tm) and { showAll = true } or {}), |
+ | titlematch = tm |
||
} |
} |
||
for k,v in pairs(args['navbox' .. i .. '_keys'] or {}) do |
for k,v in pairs(args['navbox' .. i .. '_keys'] or {}) do |
||
− | navboxdata.args[v] = args['navbox' .. i .. '_values'][ |
+ | navboxdata[i].args[v] = args['navbox' .. i .. '_values'][k] |
end |
end |
||
end |
end |
||
Line 103: | Line 116: | ||
function p.linkAdjustments(args) |
function p.linkAdjustments(args) |
||
local tbl = {} |
local tbl = {} |
||
− | tbl.fr = p. |
+ | tbl.fr = p.makeFindReplace(args.titlefind, args.titlereplace) |
− | tbl.cd = p. |
+ | tbl.cd = p.makeCurrentdata(args.currentdata_pages, args.currentdata_values, args.currentdata_force) |
return tbl |
return tbl |
||
end |
end |
||
− | function p. |
+ | function p.makeFindReplace(find, replace) |
-- in case the tournament has different base urls for different sections |
-- in case the tournament has different base urls for different sections |
||
− | + | if find and replace then |
|
+ | return { find = find, replace = replace } |
||
⚫ | |||
+ | return { find = {}, replace = {} } |
||
+ | end |
||
end |
end |
||
− | function p. |
+ | function p.makeCurrentdata(pages, values, force) |
-- current data makes a different subpage from the overview focused |
-- current data makes a different subpage from the overview focused |
||
+ | -- typically find-replace is ignored when it's trying to replace the first |
||
⚫ | |||
+ | -- link in a list. but on rare occasions we won't want this, when we're actually |
||
⚫ | |||
+ | -- tricking it into focusing a different thing in the very first line |
||
⚫ | |||
+ | -- specifically this may be the case when there's no overview and it start with qualifiers. |
||
+ | -- in this case we can use the currentdata_force argument, which is a list of booleans. |
||
+ | local data = { force = {} } |
||
+ | if not pages or not values then return data end |
||
+ | force = force or {} |
||
+ | for i, page in ipairs(pages) do |
||
⚫ | |||
+ | data.force[page] = util_args.castAsBool(force[i]) |
||
end |
end |
||
return data |
return data |
||
Line 132: | Line 157: | ||
-- main |
-- main |
||
function p.fromArgs(frame) |
function p.fromArgs(frame) |
||
+ | local args = util_args.merge(true) |
||
− | if frame == mw.getCurrentFrame() then |
||
+ | if not args.basepage then |
||
− | args = require('Module:ProcessArgs').merge(true) |
||
+ | error('Missing basepage parameter') |
||
⚫ | |||
− | frame = mw.getCurrentFrame() |
||
end |
end |
||
-- process data |
-- process data |
||
if not args.events then args.events = args.Overview end |
if not args.events then args.events = args.Overview end |
||
p.makeArgsIntoTables(args) |
p.makeArgsIntoTables(args) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | |||
⚫ | |||
⚫ | |||
− | return tabs.main(json, linkadjustments, navboxdata, args.showCorr and args, args.basepage, after) |
||
⚫ | |||
⚫ | |||
+ | corrdata = args.showCorr and args, |
||
⚫ | |||
+ | basepage = args.basepage, |
||
+ | tabstype = args.tabstype or 'header' |
||
⚫ | |||
⚫ | |||
+ | return tabs.main(json, data), datadiv |
||
end |
end |
||
Latest revision as of 21:29, 4 June 2019
To edit the documentation or categories for this module, click here.
Automated creation of tournament tabs templates without the user needing to use if statements or anything. See {{TournamentTabs}}
for usage.
Dependencies: {{GetEventPageInfo}}m
, {{SubpageSettings}}m
, {{TabsHeader}}m
, {{AutomatedTabs}}m
Eventually {{GetEventPageInfo}}m
should be merged into this but not until it's no longer needed standalone.
local util_args = require('Module:ArgsUtil')
local util_title = require('Module:TitleUtil')
local settings = require('Module:SubpageSettings').tournaments
local pageinfo = require('Module:GetEventPageInfo')
local tabs = require('Module:AutomatedTabs')
local p = {}
function p.makeArgsIntoTables(args)
local args2 = mw.clone(args) -- since we might have to modify some keys if they are numbers
for key, value in pairs(args2) do
key_str = tostring(key) or key
if not p.doWeSkip(key) then
args[key_str] = mw.text.split(value,'%s*%f[,%%],%s*')
for k, v in ipairs(args[key_str]) do
args[key_str][k] = args[key_str][k]:gsub('%%,',','):gsub('_',' ')
end
end
end
end
function p.doWeSkip(key)
if settings.dontconvert[key] then
return true
end
for k, v in pairs(settings.dontconvert_patterns) do
if string.find(key, v, 1, true) then
return true
end
end
return false
end
-- making json
function p.jsonFromArgs(args)
if util_args.castAsBool(args.oldMD) then
p.oldMD(args)
end
local tbl = {}
p.jsonRecursion(tbl, args, nil)
return tbl
end
function p.oldMD(args)
-- oldMD is an arg to make a shortcut for every Match Details artificially becoming named
-- Match Details & VODs
for _, event in ipairs(args.events) do
args[event .. '_names_Match Details'] = 'Match Details'
end
args['names_Match Details'] = 'Match Details'
args['events_names_Match Details'] = 'Match Details'
return
end
function p.jsonRecursion(tbl, args, path)
-- names are made ready for output now but links are kept in canonical form until output
local thispath = path or 'events'
local links = mw.clone(args[thispath])
p.processLinks(links)
local names = mw.clone(args[thispath .. '_names'] or links)
p.processNames(args, thispath, names, links)
tbl.links = links
tbl.names = names
if not util_args.castAsBool(args[thispath .. '_nooverview']) then
tbl.links[1] = 'Overview'
end
for k, v in ipairs(links) do
local newpath = tabs.newIndex(path, v)
if args[newpath] and not tbl[v] then
tbl[v] = {}
p.jsonRecursion(tbl[v],args,newpath)
end
end
-- if we don't specify if it's specificaly not an overview page
-- then we probably don't want any subpage for the first tab
if not util_args.castAsBool(args[thispath .. '_nooverview']) then
tbl.links[1] = ''
end
tbl.path = thispath -- for maintenance cargo table to be used when automating things
return tbl
end
function p.processLinks(links)
for k, link in ipairs(links) do
links[k] = settings.lookup[mw.ustring.lower(link)] or link
end
return
end
function p.processNames(args, path, names, links)
for key, name in ipairs(names) do
names[key] = args[path .. '_names_' .. links[key]] or settings.names[name] or name
end
return
end
-- make navbox args
function p.getNavboxData(args)
local navboxdata = {}
for i, navbox in ipairs(args.navboxes) do
local tm = args['navbox' .. i .. '_titlematch'] and args['navbox' .. i .. '_titlematch'][1]
navboxdata[i] = {
title = navbox,
args = {},
events = args['navbox' .. i .. '_events'] or ((not tm) and { showAll = true } or {}),
titlematch = tm
}
for k,v in pairs(args['navbox' .. i .. '_keys'] or {}) do
navboxdata[i].args[v] = args['navbox' .. i .. '_values'][k]
end
end
return navboxdata
end
-- link manipulation
function p.linkAdjustments(args)
local tbl = {}
tbl.fr = p.makeFindReplace(args.titlefind, args.titlereplace)
tbl.cd = p.makeCurrentdata(args.currentdata_pages, args.currentdata_values, args.currentdata_force)
return tbl
end
function p.makeFindReplace(find, replace)
-- in case the tournament has different base urls for different sections
if find and replace then
return { find = find, replace = replace }
else
return { find = {}, replace = {} }
end
end
function p.makeCurrentdata(pages, values, force)
-- current data makes a different subpage from the overview focused
-- typically find-replace is ignored when it's trying to replace the first
-- link in a list. but on rare occasions we won't want this, when we're actually
-- tricking it into focusing a different thing in the very first line
-- specifically this may be the case when there's no overview and it start with qualifiers.
-- in this case we can use the currentdata_force argument, which is a list of booleans.
local data = { force = {} }
if not pages or not values then return data end
force = force or {}
for i, page in ipairs(pages) do
data[page] = util_title.concatSubpage(page,values[i])
data.force[page] = util_args.castAsBool(force[i])
end
return data
end
function p.getCategories()
local title = mw.title.getCurrentTitle()
if title.nsText == 'Template' then
return '[[Category:Tournament Tabs]]'
end
return
end
-- main
function p.fromArgs(frame)
local args = util_args.merge(true)
if not args.basepage then
error('Missing basepage parameter')
end
-- process data
if not args.events then args.events = args.Overview end
p.makeArgsIntoTables(args)
local datadiv = pageinfo.getEventPageInfo(frame) -- for vardefines
local data = {
linkadjustments = p.linkAdjustments(args),
navboxdata = args.navboxes and p.getNavboxData(args),
corrdata = args.showCorr and args,
after = p.getCategories(),
basepage = args.basepage,
tabstype = args.tabstype or 'header'
}
local json = args.events and p.jsonFromArgs(args)
return tabs.main(json, data), datadiv
end
return p