Module:Parallel pages sections

local p = {} --p stands for package
local getArgs = require('Module:Arguments').getArgs

--[=[
Convert the positional parameters into a list of pagenum/sec nums for transclusion
=]=]
function p.get_section_list( args )

    local i = 1

    local out = {}

    while args[ i ] do
    	
    	-- check for a "hoisted" section at the end
    	local parts = mw.text.split( args[ i ], ';' )
    	local page_num
    	local cont_section
	
        local init_parts = mw.text.split( parts[ 1 ], '/' )
        page_num = init_parts[ 1 ]
        
        local first_sec
        local last_sec
        if #init_parts > 1 then
        	local range_parts = mw.text.split( init_parts[ 2 ], '-' )
        	
        	if #range_parts == 1 then
        		first_sec = 1
        		last_sec = range_parts[ 1 ]
        	else
        		first_sec = range_parts[ 1 ]
        		last_sec = range_parts[ 2 ]
        	end
        end
        
        if first_sec and last_sec then
	        for sec_num = first_sec, last_sec do
	            table.insert( out, {
	                pagenums = { page_num },
	                secnum = tostring( sec_num )
	            } )
	        end
		end
        
		local cont_pages = {}
        
        if #parts > 1 then
        	-- split off the section name
        	local cont_parts = mw.text.split( parts[ 2 ], '/' )	
        	
        	-- pages are comma-separated
        	cont_pages  = mw.text.split( cont_parts[1], ',')
        	
        	-- the section name is at the end
        	cont_section = cont_parts[2]
        end

        if #cont_pages > 0 then
        	-- also include the cont section on the first page
        	table.insert( cont_pages, 1, page_num )
        	
            table.insert( out, {
                pagenums = cont_pages,
                secnum = cont_section
            } )
        end
        
        i = i + 1
    end
    return out
end

--[=[
Render a single section with the <pages> tag
]=]
function render_section( frame, index, pagenums, offset, lang, section )
	
	local adj_pagenums = {}
	for _, v in pairs( pagenums ) do
		table.insert( adj_pagenums, tonumber( v ) + offset )
	end
	
    return frame:callParserFunction{
        name = '#tag:pages',
        args = {
            index=index,
            lang=lang,
            include=table.concat( adj_pagenums,  ',' ),
            onlysection=section,
            header = 0
        }
    }
end

--[=[
Main entry point
]=]
function p.main(frame)
    mw.logObject(getArgs)
    local args = getArgs(frame)

    local pref_l = args.prefix_l
    local pref_r = args.prefix_r
    local lang_l = args.lang_l
    local lang_r = args.lang_r
    local offset = tonumber(args.offset or '0')
    
    local cont_section = args.cont_section or 'cont'

    local secs = p.get_section_list( args, cont_section )
    
    mw.logObject(secs)

    local out = ''
    for _, sec in pairs(secs) do
        out = out .. frame:expandTemplate{
            title='tt2',
            args = {
                [1] = render_section(frame, args.index, sec.pagenums, 0,
                	                 lang_l, pref_l .. sec.secnum ),
                [2] = render_section(frame, args.index, sec.pagenums, offset,
                	                 lang_r, pref_r .. sec.secnum )
            }
        }
    end

    return out
end

return p