Module:Contents topic list

require('strict')

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

-- Close  and  enclosing deeper depths local function closePending(toClose, depth) local out = '' while #toClose > 0 and toClose[#toClose][1] >= depth do out = out .. toClose[#toClose][2] table.remove(toClose, #toClose) end return out end

function p.main(frame) local args = getArgs(frame) local wikitext = frame:preprocess(args[1]) local items = {} local out = '' for indent, item in wikitext:gmatch("(**) *([^\n]+)") do 	    items[#items + 1] = {#indent + 1, item} end -- List of { depth, closing tag [, rest block-level (for s) ] } local toClose = {} -- Lowest depth of parent that forces all s to be inline local forcedInline = math.huge

for i, item in pairs(items) do out = out .. closePending(toClose, item[1]) -- Create a new list if needed if #toClose == 0 or toClose[#toClose][2] ~= '' then out = out .. (forcedInline <= item[1] and '' or '') toClose[#toClose + 1] = { item[1] - 1, '', false } end if item[1] <= forcedInline then forcedInline = math.huge end -- Added by 		local keepInline = item[2]:match('KEEP%-INLINE$') ~= nil if keepInline then out = out .. '' .. item[2]:gsub(' *KEEP%-INLINE$', '') forcedInline = math.min(item[1], forcedInline) elseif forcedInline < item[1] then out = out .. '' .. item[2] elseif i < #items and items[i + 1][1] > item[1] then -- Remove ":" if first item is block-level out = out:gsub("&#58; $", "") -- Treat remaining items in this last as block-level toClose[#toClose][3] = true out = out .. '' .. "'" .. item[2] .. "&#58; '"		elseif toClose[#toClose][3] then out = out .. '' .. "'" .. item[2] .. "'"		else out = out .. '' .. item[2] end toClose[#toClose + 1] = { item[1], '' } end out = out .. closePending(toClose, 0) return ' \n' .. out .. ' ' .. frame:extensionTag{ name = 'templatestyles', args = { src = 'Template:Contents topic list/styles.css' } } end

return p