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
ii_start = N_teams+1-n_to_show
ii_end = N_teams
else
-- Normal case
ii_start = ii_show-n_show_side
ii_end = ii_show+n_show_side
end
end  
-- For results column
local new_res_ii = ii_start
-- Pre-check for existence of column
if not hide_results then
for ii = ii_start, ii_end do
if Args[respre..'result'..ii] and Args[respre..'text_' .. Args[respre..'result'..ii]] then results_defined = true end
end
end
-- Remove results header if it is unused
if full_table and not results_defined then
-- First get it as one string, then use string replace to replace that header by empty string
local t_str = tostring(table.concat(t))
t_str = mw.ustring.gsub( t_str, results_header_txt, '' )
N_cols = N_cols-1 -- There is actually one column less
t = {}
table.insert(t, t_str)
end  
-- Write rows
local team_name, team_code_ii, team_code_jj, pos_num, group_txt, note_local
local note_string, note_local, note_local_num, note_id
local note_id_list = {}
local hth_id_list = {}
for ii = ii_start, ii_end do
-- First get code
team_code_ii = team_list[ii]
-- Now read values
pos_num = Args['pos_'..team_code_ii] or ii
group_txt = Args['group_'..team_code_ii] or ' '
team_name = Args['name_'..team_code_ii] or team_code_ii
note_local = Args['note_'..team_code_ii] or nil  
-- Does it need a promotion/qualification/relegation tag
local result_local = Args[respre..'result'..ii] or nil
local bg_col = nil
-- Get local background colour
if result_local then
bg_col = result_col[Args[respre..'col_'..result_local]] or Args[respre..'col_'..result_local] or 'inherit'
bg_col = 'background-color:'..bg_col..';' -- Full style tag
end
if not bg_col then bg_col = 'background-color:transparent;' end -- Becomes default if undefined  
-- Bold this line or not
local ii_fw = ii == ii_show and 'font-weight: bold;' or 'font-weight: normal;'
if yesno(Args['show_totals'] or 'no') and team_code_ii == total_row_name then
ii_fw = 'font-weight: bold;'
end  
-- Check whether there is a note or not, if so get text ready for it
if note_local and full_table then
-- Set global check for notes to true
notes_exist = true
-- There are now 3 options for notes
-- 1) It is a full note
-- 2) It is a referal to another note (i.e. it's just a team code; e.g. note_AAA=Text, note_BBB=AAA) in which the note for BBB should link to the same footnote as AAA, with
-- 2a) The other linked note exist in the part of the table shown
-- 2b) The part of the note does not exist in the part of the table shown
if not Args['note_'..note_local] then
-- Option 1
-- Now define the identifier for this
note_id = '"table_note_'..team_code_ii..rand_val..'"' -- Add random end for unique ID if more tables are present on article (which might otherwise share an ID)
note_id_list[team_code_ii] = note_id  
-- Call refn template
note_string = :expandTemplate{ title = 'efn', args = { group='lower-alpha', name=note_id, note_local} }
else
-- Option 2
-- It is option 2a in either one if either the main note is inside the sub-table
-- or another ref to that note is inside the sub-table
-- Basically when it either has been defined, or the main link will be in the table
note_local_num = team_list[note_local]
if note_id_list[note_local] or ((note_local_num >= ii_start) and (note_local_num <= ii_end)) then
-- Option 2a
note_id = '"table_note_'..note_local..rand_val..'"'
note_string = :extensionTag{ name = 'ref', args = { group = 'lower-alpha', name = note_id} }
else
-- Option 2b
-- Now define the identifier for this
note_id = '"table_note_'..note_local..rand_val..'"' -- Add random end for unique ID
note_id_list[note_local] = note_id  
-- Call refn template
note_string = :expandTemplate{ title = 'efn', args = { group='lower-alpha', name=note_id, Args['note_'..note_local]} }
end
end
else
note_string = '';
end  
-- Insert status when needed
local status_string = ''
local status_local = show_status and Args[respre .. 'status_'..team_code_ii] or nil
local status_let_first = true
local curr_letter
-- Only if it is defined
if status_local then
-- Take it letter by letter
for jjj = 1,mw.ustring.len(status_local) do
curr_letter = mw.ustring.upper(mw.ustring.sub(status_local,jjj,jjj))
-- See whether it exist
if t_status.code[curr_letter] then
-- Depending on whether it is the first letter of not
if status_let_first then
status_string = curr_letter
t_status.called[curr_letter] = true
status_let_first = false
else
status_string = status_string..', '..curr_letter
t_status.called[curr_letter] = true
end
end
end
-- Only add brackets/dash and bolding if it exist
if not status_let_first then
if t_status.position == 'before' then
status_string = '<span>'..string.lower(status_string)..' &ndash;</span> '
else
status_string = ' <span>('..status_string..')</span>'
end
end
end  
-- Now build the rows
if yesno(Args['show_totals'] or 'no') and team_code_ii == total_row_name then
table.insert(t,'|-\n') -- New row
else
table.insert(t,'|- \n') -- New row
end
if position_col then
table.insert(t,'|| '..pos_num..'\n') -- Position number
end
if full_table and group_col then
table.insert(t,'| |'..group_txt..'\n') -- Group number/name
end
-- Build the team string order based on status position
local team_string
if t_status.position == 'before' then
team_string = status_string..team_name..note_string
else
team_string = team_name..note_string..status_string
end
table.insert(t,'! scope="row"| '..team_string..'\n')-- Team (with possible note)
-- Call to subfunction
t_return = p_style.row(,t,Args,p_sub,notes_exist,hth_id_list,full_table,rand_val,team_list,team_code_ii,ii_start,ii_end,ii_fw,bg_col,N_teams,ii,ii_show)
t = t_return.t
notes_exist = t_return.notes_exist
hth_id_list = t_return.hth_id_list  
-- Now check what needs to be added inside the results column
if full_table then
local res_jjj
if ii == new_res_ii then
-- First check how many rows you need for this
N_rows_res = 1
jjj = ii+1
result_local = Args[respre..'result'..ii] or ''
local cont_loop = true
while (jjj<=ii_end) and cont_loop do
if Args['split'..tostring(jjj-1)] then
cont_loop = false
new_res_ii = jjj
else
res_jjj = Args[respre..'result'..jjj] or ''
if result_local == res_jjj then
N_rows_res = N_rows_res+1
else
cont_loop = false
new_res_ii = jjj
end
end
jjj = jjj+1
end
-- Now create this field (reuse ii_fw and bg_col)
-- Bold (if in range) or not
if ii_show and (ii_show>=ii) and (ii_show<=(ii+N_rows_res-1)) then
ii_fw = 'font-weight: bold;'
else
ii_fw = 'font-weight: normal;'
end
-- Get background colour
bg_col = nil
if Args[respre..'result'..ii] then
bg_col = result_col[Args[respre..'col_'..result_local]] or Args[respre..'col_'..result_local] or 'inherit'
bg_col = 'background-color:'..bg_col..';' -- Full style tag
end
if not bg_col then bg_col = 'background-color:transparent;' end -- Becomes default if undefined
-- Check for notes
local note_res_string, note_ref, note_text = *, *, ''
if Args['note_res_'..result_local] then
notes_exist = true
local note_res_local = Args['note_res_'..result_local]  
-- Split the note_res_local into a table if all the entries are valid
local multiref = 1
local note_res_local_table = mw.text.split(note_res_local, '%s*,%s*')
if (#note_res_local_table > 1) then
for k, note_res_loc in ipairs(note_res_local_table) do
multiref = multiref * (Args['note_res_' .. note_res_loc] and 1 or 0)
end
else
multiref = 0
end  
-- Split failed, so make a single entry table with hth_local inside
if multiref < 1 then
note_res_local_table = { note_res_local }
end  
for k,note_res_local in ipairs(note_res_local_table) do
if not Args['note_res_'..note_res_local] then
-- It does not point to another result note
note_ref = respre..'res_'..result_local
note_id = '"table_note_res_'..result_local..rand_val..'"' -- Identifier
note_text = note_res_local
else
-- It does point to another result note
note_ref = respre..'res_'..note_res_local