Module:JSONPageList

-- Demo for T332484 require( 'strict' ) local r={} --local lang="en" local site="enwiki" local siteprefix="en:"

local function get_link(qid) local sitelink=mw.wikibase.getSitelink(qid,site) local label=mw.wikibase.getLabel(qid) if sitelink then return ..label.. else return ..label.. end end

local function get_heading(field) if field:sub(1,1)=='$' then local entity_id=field:sub(2):match("([PQpq]%d+)$") return mw.wikibase.getLabel(entity_id) end return 'item' end

local function get_cell(field, item) local qid=item._qid if field=='@link' then return get_link(qid) elseif field:sub(1,1)=='$' then local entity_path=field:sub(2) if entity_path:match("^([PQpq]%d+)$") then return mw.getCurrentFrame:callParserFunction("#statements",{entity_path, from=qid}) end end return '...' end

function r.table(pagename, result_fields) if type(pagename) == "table" then pagename, result_fields = pagename.args[1], pagename.args[2] end local content=mw.title.new(pagename):getContent content=mw.text.jsonDecode(content) result_fields=mw.text.split(result_fields,',') local data_fields=content.fields local items=content.items for i, v in ipairs( items ) do       if type(v) == "string" then items[i]={_qid=v} end end local result=mw.html.create("table"):attr( 'class', 'wikitable' ) local heading=result:tag("tr") for i, field in ipairs( result_fields ) do       field=mw.text.trim(field) result_fields[i]=field heading:tag("th"):wikitext(get_heading(field)) end for _, item in ipairs( items ) do       local row=result:tag("tr") for _, field in ipairs( result_fields ) do           row:tag("td"):wikitext(get_cell(field,item)) end end return tostring( result ) end

return r