Module:JSONView: Difference between revisions

From Guild of Archivists
No edit summary
 
m (1 revision imported)
 

Latest revision as of 07:26, 30 October 2015

Documentation for this module may be created at Module:JSONView/doc

json = require('Module:JSON')

function make_table (t)
    local rows = ''
    for k, v in pairs(t) do
        rows = rows .. make_row(k, v)
    end
    return '<table class="mw-json-schema"><tbody>' .. rows .. '</tbody></table>'
end

function make_row (k, v)
    th = '<th>' .. k .. '</th>'
    if type(v) == 'table' then
        td = '<td>' .. make_table(v) .. '</td>'
    else
        if type(v) == 'string' then
            v = '"' .. v .. '"'
        else
            v = json.encode(v)
        end
        td = '<td class="value">' .. v .. '</td>'
    end
    return '<tr>' .. th .. td .. '</tr>'
end

function render ( frame )
    return '<nowiki>' .. make_table(json.decode(frame.args[1])) .. '</nowiki>'
end

function renderAll ( frame )
	-- for show all json
	local p = json.decode(frame.args[1])
    return '<nowiki>' .. json.encode(p) .. '</nowiki>'
end

return { render=render, renderAll=renderAll }