Module:Toc2row

-- Create a TOC with two rows per chapter and with tabular data for chapter numbers, page numbers, and descriptions --

local getArgs = require('Module:Arguments').getArgs local p = {};

p.buildNestedTable = function (frame, cols) -- Build a nested array without named properties local args = getArgs(frame) local output = {} local tabData = {} local iterator = 1 -- Get number of unnamed arguments while args[iterator] do --Check if item is last argument by checking if it exists tabData[#tabData+1] = args[iterator] -- Add item to new table iterator = iterator + 1 -- Iterate end

--Loop through args, take every third item for i=1, #tabData, cols do		local row = {} for j=1, cols, 1 do			row[#row+1] = tabData[i+j-1] end output[#output+1] = row end

return output end

-- Match pattern for an whole line of text ending in a newline -- for item in frame.args.tabularData:gmatch("([^\n]+)") -- Match pattern for 3 items in a CSV row -- local chapter, page, description = v:match('%s*"(.-)",%s-"(.-)",%s*"(.-)"%s*$')

p.buildAssociativeTable = function(frame, fields) -- Add property names to "column" items in nested table local output = {} local input = p.buildNestedTable(frame, #fields)

for i, row in ipairs(input) do		output[#output+1] = {} for j, item in ipairs(fields) do			output[#output][item] = row[j] end end

return output end

p.content = { [=[  CHAPTER ',	-- Between 3 & 4: chapterRoman	'. ', -- Between 4 & 5: description [=[ ]=], -- Between 5 & 6: page ' ' }

concatEntry = function(frame, row) local roman = require('Module:Roman')

local url = frame.args.baseURL local description = row.description local page = row.page local chapter = row.chapter local chaptRoman = roman._Numeral(chapter) return p.content[1] .. url .. p.content[2] .. chapter .. p.content[3] .. chaptRoman .. p.content[4] .. description .. p.content[5] .. page .. p.content[6] end

p.outerWrapper = { -- additional HTML tags to wrap around each row which may require more detailed formatting [=[ ' }

p.transclude = function(frame) local fields = {'chapter', 'page', 'description'} local output = {} local input = p.buildAssociativeTable(frame, fields) for i, v in ipairs(input) do v = p.outerWrapper[1] .. concatEntry(frame, v) .. p.outerWrapper[2] table.insert(output, v)	end return table.concat(output, "\n") end

return p