-- Module to build tables for standings in Sports
-- See documentation for details  
require('Module:No globals')  
local p = {}  
-- Main function
function p.main
-- Declare locals
local getArgs = require('Module:Arguments').getArgs
local Args = getArgs(, {parentFirst = true})
local ii_start, ii_end, N_rows_res = 0
local text_field_result
local notes_exist = false
local t = {}
local t_footer = {}
local t_return = {}
local team_list = {}
local jj, jjj  
-- Exit early if we are using section transclusion for a different section
if (Args['transcludesection'] and Args['section'])
and Args['transcludesection'] ~= Args['section'] then
return ''
end  
local templatestyles = :extensionTag{
name = 'templatestyles', args = { src = 'Module:Sports table/styles.css' }
}  
-- Edit links if requested
local baselink = :getParent:getTitle
if mw.title.getCurrentTitle.text == baselink then baselink = '' end
local template_name = Args['template_name']
or (baselink ~= '' and (':' .. baselink))
or ''  
-- Get the custom start point for the table (most will start by default at 1)
local top_pos = tonumber(Args['highest_pos']) or 1
-- Get the custom end point for the table (unrestricted if bottom_pos is < top_pos)
local bottom_pos = tonumber(Args['lowest_pos']) or 0
local N_teams = top_pos - 1 -- Default to 0 at start, but higher number needed to skip certain entries  
-- Load modules
local yesno = require('Module:Yesno')
-- Load style and (sub) modules
local style_def = Args['style'] or 'WDL'
-- Historically 'football' exists as style, this is now forwarded to WDL
if style_def == 'football' then style_def = 'WDL' end
local p_style = require('Module:Sports table/'..style_def)
local p_sub = require('Module:Sports table/sub')  
-- Random value used for uniqueness
math.randomseed( os.clock * 10^8 )
local rand_val = math.random  
-- Declare colour scheme
local result_col = {}
result_col = {green1='#BBF3BB', green2='#CCF9CC', green3='#DDFCDD', green4='#EEFFEE',
blue1='#BBF3FF', blue2='#CCF9FF', blue3='#DDFCFF', blue4='#EEFFFF',
yellow1='#FFFFBB', yellow2='#FFFFCC', yellow3='#FFFFDD', yellow4='#FFFFEE',
red1='#FFBBBB', red2='#FFCCCC', red3='#FFDDDD', red4='#FFEEEE',
black1='#BBBBBB', black2='#CCCCCC', black3='#DDDDDD', black4='#EEEEEE',
orange1='#FEDCBA', orange2='#FEEAD5',
white1='inherit',['']='inherit'
}  
-- Show all stats in table or just matches played and points
local full_table = true
local hide_results = yesno(Args['hide_results'] or 'no')
local pld_pts_val = string.lower(Args['only_pld_pts'] or 'no')
local show_class_rules = yesno(Args['show_class_rules'] or 'yes') and true or false
-- True if par doesn't exist, false otherwise
if yesno(pld_pts_val) then
full_table = false
elseif pld_pts_val=='no_hide_class_rules' then
full_table = true
show_class_rules = false
end  
-- Declare results column header
local results_header = {}
results_header = {Q='தகுதி', QR='Qualification or relegation',
P='Promotion', PQR='Promotion, qualification or relegation',
PR='Promotion or relegation', PQ='Promotion or qualification',
R='Relegation'}
local results_defined = false -- Check whether this would be needed
-- Possible prefix for result fields
local respre = (Args['result_prefix'] or '') .. '_'
respre = (respre == '_') and '' or respre
-- Now define line for column header (either option or custom)
local local_res_header = results_header[Args[respre..'res_col_header']] or Args[respre..'res_col_header'] or ''
-- Check whether it includes a note
local res_head_note = Args['note_header_res']
local res_head_note_text = ''
if full_table and res_head_note then
notes_exist = true
res_head_note_text = :expandTemplate{ title = 'efn', args = { group='lower-alpha', res_head_note} }
end
local results_header_txt = '! scope="col" |'..local_res_header..res_head_note_text..'\n'  
-- Get status option
local t_status = p_style.status(Args)  
-- Alternative syntax for team list
if Args['team_order'] and Args['team_order'] ~= '' then
local team_order_offset = (tonumber(Args['team_order_start']) or 1) - 1
local tlist = mw.text.split(Args['team_order'], '%s*[;,]%s*')
for k, tname in ipairs(tlist) do
if tname ~= '' then
Args['team' .. (k + team_order_offset)] = tname
end
end
end  
-- Read in number of consecutive teams (ignore entries after skipping a spot)
while Args['team'..N_teams+1] ~= nil and (bottom_pos < top_pos or N_teams < bottom_pos) do
N_teams = N_teams+1
-- Sneakily add it twice to the team_list parameter, once for the actual
-- ranking, the second for position lookup in sub-tables
-- This is possible because Lua allows both numbers and strings as indices.
team_list[N_teams] = Args['team'..N_teams] -- i^th entry is team X
team_list[Args['team'..N_teams]] = N_teams -- team X entry is position i
end  
-- Optional totals
local total_row_name = 'SPORTS_TABLE_TOTAL'
if yesno(Args['show_totals'] or 'no') then
N_teams = N_teams+1
Args['team' .. N_teams] = total_row_name
Args['name_' .. total_row_name] = 'Total'
Args['result' .. N_teams] = total_row_name
Args['col_' .. total_row_name] = '#eee'
team_list[N_teams] = Args['team' .. N_teams]
team_list[Args['team'..N_teams]] = N_teams
end  
-- Show position
local position_col = yesno(Args['show_positions'] or 'yes') and true or false  
-- Show groups or note
local group_col = yesno(Args['show_groups'] or 'no') and true or false  
-- Show match_table or not
local match_table = yesno(Args['show_matches'] or 'no') and true or false
local p_matches = match_table and
(style_def == 'Chess' and require('Module:Sports results/'..style_def) or require('Module:Sports results'))  
-- Custom position column label or note
local pos_label = Args['postitle'] or 'நிலை'
if position_col == false then pos_label = nil end  
-- Show status or not
local show_status = yesno(Args['show_status'] or 'yes') and true or false  
-- Get VTE button text (but only for non-empty text)
local VTE_text = ''
if (template_name ~= '') then
VTE_text = require('Module:Navbar')._navbar({
template_name,
mini=1,,
brackets=1
})  
-- remove the next part if https://en.wikipedia.org/w/index.php?oldid=832717047#Sortable_link_disables_navbar_links?
-- is ever fixed
if yesno(Args['sortable_table'] or 'no') then
VTE_text = mw.ustring.gsub(VTE_text, '<%/?abbr[^<>]*>', ' ')
end
end  
-- Add source to title if specified and possible
local title_source = false
if Args['title'] and Args['title_source'] then
Args['title'] = Args['title'] .. Args['title_source']
title_source = true
elseif Args['table_header'] and Args['table_header_source'] then
Args['table_header'] = Args['table_header'] .. Args['table_header_source']
title_source = true
end  
-- Write column headers
t_return = p_style.header(t,Args,p_sub,pos_label,group_col,VTE_text,full_table,results_header_txt)
if match_table then
-- Add empty column header
t_return.count = t_return.count+1
table.insert(t_return.tab_text,'! scope="row"| \n')  
-- Add rest of header
t_return = p_matches.header(t_return,Args,p_sub,N_teams,team_list,tonumber(Args['legs']) or 1)
end
t = t_return.tab_text
local N_cols = t_return.count  
-- Determine what entries go into table
-- Find out which team to show (if any)
local ii_show = team_list[Args['showteam'] or nil] -- nil if non-existant
-- Start and end positions to show
local n_to_show = tonumber(Args['show_limit']) or N_teams
-- Check for "legal value", if not legal (or non declared), then show all
local check_n = ((n_to_show>=(N_teams-top_pos+1)) or (n_to_show<=1) or (n_to_show~=math.floor(n_to_show)))
-- Also check whether there is a valid ii_show
if check_n or (not ii_show) then
ii_start = top_pos
ii_end = N_teams
else
-- It's a proper integer between top_pos+1 and N_teams-1
-- If it is in the middle show the same number above and below
-- If it is in the top or bottom, show the exact number
-- How many to show on the side
local n_show_side = math.floor(n_to_show/2)
if (ii_show-top_pos+1)<=n_show_side then
-- Top team
ii_start = top_pos
ii_end = top_pos+n_to_show-1
elseif ii_show>=(N_teams+1-n_show_side) then
-- Bottom team