Module:CppStd

From emmtrix Wiki
Jump to navigation Jump to search
local p = {}

local function getIndexData(stdid)
	return require('Module:CppStd/Index' .. stdid)
end

local function createIndex2(index, id, level)
	local wiki = ''	

	if level == 1 then
        wiki = wiki .. '<ul class="index-enum-root">\n'
	elseif level <= 2 then
        wiki = wiki .. '<ul class="index-enum">\n'
	elseif level <= 3 then
        --wiki = wiki .. '<ul class="index-enum mw-collapsible">\n'
        wiki = wiki .. '<ul class="index-enum">\n'
	else
        wiki = wiki .. '<ul class="index-enum mw-collapsible mw-collapsed">\n'
	end

    repeat 
        local entry = index[id]
        -- wiki = wiki .. string.rep(':', level-1)
        wiki = wiki .. '<li class"head' .. level .. '">'
        if id == '' then
            wiki = wiki .. entry.text .. '\n'
        else
            wiki = wiki .. '<span class="namespace">';
	        if entry.section then
	            wiki = wiki .. '<span class="section">' .. entry.section .. '</span>'
	        end
            --wiki = wiki .. string.format(' <span class="secname%d";">', level)
            wiki = wiki .. string.format(' <span class="secname%d";">[[CppStd:%s|%s]]</span></span>', level, id, entry.text)
            wiki = wiki .. '<span class="secno">[' .. id .. ']</span>\n'
        end

        if entry.child then
            wiki = wiki .. createIndex2(index, entry.child, level + 1)
        end

        id = entry.next
    until id == nil

    wiki = wiki .. '</ul>\n'

    return wiki
end

-- Function to output the number of diagnostics
function p.createIndex(frame)
	local stdid = frame.args.stdid
	local indexData = getIndexData(stdid)

	local wiki = ''
    wiki = wiki .. createIndex2(indexData, '', 1)
    return wiki
end

local function pageIndexFormat(id, entry)
	local wiki = ''

    if entry.section ~= '' then
    	wiki = wiki .. entry.section .. ' '
    end
    wiki = wiki .. '[[CppStd:' .. id .. ' |' .. entry.text .. ']] [' .. id .. ']'
    wiki = wiki .. '<br>\n'

	return wiki
end

local function pageIndexBefore(indexData, page)
	local wiki = ''	
	local parent = indexData[page].parent
	
	if parent then
		local id = indexData[parent].child
	
		wiki = wiki .. pageIndexBefore(indexData, parent)
	
	    while id ~= page do 
	        local entry = indexData[id]
	        wiki = wiki .. pageIndexFormat(id, entry)
	        id = entry.next
	    end
			
        wiki = wiki .. pageIndexFormat(id, indexData[id])
	end
	
	return wiki
end

-- Function to output the number of diagnostics
function p.createPageIndex(frame)
	local stdid = frame.args.stdid
	local page = frame.args.page
	local indexData = getIndexData(stdid)

	local wiki = '<div class="cppstd-toc">\n'
	
	wiki = wiki .. pageIndexBefore(indexData, page)

	local id = indexData[page].child
	while id do
        local entry = indexData[id]
        wiki = wiki .. pageIndexFormat(id, entry)
        id = entry.next
	end

	id = indexData[page].next
	while id do
        local entry = indexData[id]
        wiki = wiki .. pageIndexFormat(id, entry)
        id = entry.next
	end

	wiki = wiki .. '</div>\n'

    return wiki
end

return p