<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://dogcraft.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3AJson2table</id>
	<title>Module:Json2table - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://dogcraft.net/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3AJson2table"/>
	<link rel="alternate" type="text/html" href="https://dogcraft.net/wiki/index.php?title=Module:Json2table&amp;action=history"/>
	<updated>2026-04-11T12:15:15Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.2</generator>
	<entry>
		<id>https://dogcraft.net/wiki/index.php?title=Module:Json2table&amp;diff=28282&amp;oldid=prev</id>
		<title>Domino: Created page with &quot;--[[     __  __           _       _           _                 ____  _        _     _        |  \/  | ___   __| |_   _| | ___ _   | |___  ___  _ __ |___ \| |_ __ _| |__ | | _...&quot;</title>
		<link rel="alternate" type="text/html" href="https://dogcraft.net/wiki/index.php?title=Module:Json2table&amp;diff=28282&amp;oldid=prev"/>
		<updated>2022-12-15T05:09:14Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;--[[     __  __           _       _           _                 ____  _        _     _        |  \/  | ___   __| |_   _| | ___ _   | |___  ___  _ __ |___ \| |_ __ _| |__ | | _...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;--[[  &lt;br /&gt;
  __  __           _       _           _                 ____  _        _     _      &lt;br /&gt;
 |  \/  | ___   __| |_   _| | ___ _   | |___  ___  _ __ |___ \| |_ __ _| |__ | | ___ &lt;br /&gt;
 | |\/| |/ _ \ / _` | | | | |/ _ (_)  | / __|/ _ \| '_ \  __) | __/ _` | '_ \| |/ _ \&lt;br /&gt;
 | |  | | (_) | (_| | |_| | |  __/| |_| \__ \ (_) | | | |/ __/| || (_| | |_) | |  __/&lt;br /&gt;
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)___/|___/\___/|_| |_|_____|\__\__,_|_.__/|_|\___|&lt;br /&gt;
                                                                                     &lt;br /&gt;
This module is intended for converting tabular data pages on commons to wikitables.&lt;br /&gt;
&lt;br /&gt;
Authors and maintainers:&lt;br /&gt;
* User:Jarekt - original version in response to https://phabricator.wikimedia.org/T252307&lt;br /&gt;
]]&lt;br /&gt;
&lt;br /&gt;
require('Module:No globals')                        -- see Q16748603&lt;br /&gt;
local ISOdate = require('Module:ISOdate')._ISOdate  -- see Q20962109&lt;br /&gt;
local getArgs = require('Module:SvCore').getArgs      -- see Q93865060&lt;br /&gt;
local yesno   = require('Module:SvCore').yesno        -- see Q93865060&lt;br /&gt;
&lt;br /&gt;
-- ==================================================&lt;br /&gt;
-- === External functions ===========================&lt;br /&gt;
-- ==================================================&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
-- ========================================================================================&lt;br /&gt;
-- === INPUTS:                                                                          ===&lt;br /&gt;
-- ===  * dataset - name of a page on Commons in &amp;quot;Data:&amp;quot; namespace                      ===&lt;br /&gt;
-- ===  * lang    - language code, in not provided, it will default to Wiki's language  ===&lt;br /&gt;
-- ===            (on monolingual wikis) or the user's language (on multilingual Wikis) ===                                                    ===&lt;br /&gt;
-- ===  * args    - additional optional arguments:                                      ===&lt;br /&gt;
-- ===    * row_header - (true) should first column be in bold                          ===&lt;br /&gt;
-- ===    * col_header - (true) should we look up and use column names                  ===&lt;br /&gt;
-- ===    * caption    - (true) should we add table description as caption              ===&lt;br /&gt;
-- ===    * empty_cell - ([space]) value to use if &amp;quot;nil&amp;quot; value in the table             ===&lt;br /&gt;
-- ===    * format_table - (default class=&amp;quot;Wikitable&amp;quot;) change table formating           ===&lt;br /&gt;
-- ===    * bare       - (false) show only table's cells (needs to be placed within     ===&lt;br /&gt;
-- ===                    table wikitext                                                ===&lt;br /&gt;
-- ===    * nowiki     - (false) show table's wikicode (used for debugging)             ===&lt;br /&gt;
-- ========================================================================================&lt;br /&gt;
function p._json2table(dataset, lang, args)	&lt;br /&gt;
	-- make sure inputs are in the right format&lt;br /&gt;
	if not lang or not mw.language.isValidCode( lang ) then&lt;br /&gt;
		_, lang = mw.wikibase.getLabelWithLang( 'Q2' ) -- get user's chosen language&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local rowHeader = yesno(args.row_header, true ) -- row header? Or should first column be highlighted?&lt;br /&gt;
	rowHeader = (rowHeader and '!') or '|'&lt;br /&gt;
	local bare      = yesno(args.bare, false)&lt;br /&gt;
	local endRow = '\n|-\n'&lt;br /&gt;
	local useCol = {}&lt;br /&gt;
	&lt;br /&gt;
	-- allow some columns to be skipped&lt;br /&gt;
	local useAllCol = (args.columns==nil)&lt;br /&gt;
	if args.columns then&lt;br /&gt;
		local str = args.columns..&amp;quot;/&amp;quot;&lt;br /&gt;
		local columns = {str:match((str:gsub(&amp;quot;[^/]*/&amp;quot;, &amp;quot;([^/]*)/&amp;quot;)))} -- split string&lt;br /&gt;
		for _, col in ipairs(columns) do&lt;br /&gt;
			useCol[mw.ustring.lower(col)] = true&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	-- make sure detaset is provided and has the right extension&lt;br /&gt;
	assert(dataset, 'Commons dataset variable not provided')&lt;br /&gt;
	if string.sub(dataset,-4) ~= '.tab' then&lt;br /&gt;
		dataset = dataset .. '.tab'&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local data = mw.ext.data.get(dataset, lang)&lt;br /&gt;
	assert(data, 'Can not access Commons dataset [[c:Data:' .. dataset..'|'..dataset..']]')&lt;br /&gt;
	local dataTable, colIdx, colDate, colTable = {}, {}, {}, {}&lt;br /&gt;
	if yesno(args.caption, true) and not bare then -- add table caption&lt;br /&gt;
		local linkback = &amp;quot; [[File:Blue pencil.svg |frameless |text-top |10px |link=c:Data:&amp;quot;..dataset .. &amp;quot;]]&amp;quot;&lt;br /&gt;
		table.insert(dataTable, '|+'..data.description..linkback..'\n')&lt;br /&gt;
	end&lt;br /&gt;
	-- add column names &lt;br /&gt;
	for iCol, field in pairs(data.schema.fields) do -- go over field names&lt;br /&gt;
		local key = mw.ustring.lower(field.name)&lt;br /&gt;
		if useAllCol or useCol[key] then &lt;br /&gt;
			table.insert(colTable, '!'.. (field.title or field.name))&lt;br /&gt;
			table.insert(colIdx, iCol)&lt;br /&gt;
			-- if field.name hase a word &amp;quot;date&amp;quot; in it than we will trreat it as  &lt;br /&gt;
			-- a date and translate to project's language&lt;br /&gt;
			table.insert(colDate, mw.ustring.find(key, 'date')~=nil)&lt;br /&gt;
		end&lt;br /&gt;
	end	&lt;br /&gt;
	local nCol = #colTable&lt;br /&gt;
	if yesno(args.col_header, true) then&lt;br /&gt;
		table.insert(dataTable, table.concat(colTable, '\n'))&lt;br /&gt;
	end&lt;br /&gt;
	--add table cells&lt;br /&gt;
	for _, row in pairs(data.data) do -- go over rows&lt;br /&gt;
		colTable = {}&lt;br /&gt;
		for _, i in ipairs(colIdx) do            -- go over columns&lt;br /&gt;
			local val = (row[i] or args.empty_cell or ' ')&lt;br /&gt;
			if colDate[i] then&lt;br /&gt;
				val = ISOdate(val, lang) -- convert ISO date YYYY-MM-DD to I18n date &lt;br /&gt;
			end&lt;br /&gt;
			table.insert(colTable, val)&lt;br /&gt;
		end&lt;br /&gt;
		table.insert(dataTable, rowHeader..table.concat(colTable, '\n|'))&lt;br /&gt;
	end&lt;br /&gt;
	-- finish formating the table&lt;br /&gt;
	local wikitable = endRow .. table.concat(dataTable, endRow)&lt;br /&gt;
	if not yesno(args.bare, false) then&lt;br /&gt;
		local Format = args.table_format or 'class=&amp;quot;wikitable&amp;quot;'&lt;br /&gt;
		wikitable = '{| ' .. Format .. '\n' .. wikitable .. '\n|}'&lt;br /&gt;
	end&lt;br /&gt;
	if yesno(args.nowiki, false) then&lt;br /&gt;
		wikitable = '&amp;lt;pre&amp;gt;'..wikitable..'&amp;lt;/pre&amp;gt;'&lt;br /&gt;
	end&lt;br /&gt;
	return wikitable &lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[ ===========================================================================&lt;br /&gt;
Call through a template with code:&lt;br /&gt;
&lt;br /&gt;
{{#invoke:Json2table|json2table |lang={{{lang|xx}}} }}&lt;br /&gt;
&lt;br /&gt;
where xx is a  project's language ( leave blank on multilanguage projects )&lt;br /&gt;
&lt;br /&gt;
Template can be called with variety of parameters:&lt;br /&gt;
                                                                    &lt;br /&gt;
 * dataset - name of a page on Commons in &amp;quot;Data:&amp;quot; namespace                     &lt;br /&gt;
 * lang    - language code, in not provided, it will default to Wiki's          &lt;br /&gt;
			 language (on monolingual wikis) or the user's language              &lt;br /&gt;
			 (on multilingual Wikis)                                            &lt;br /&gt;
 * row_header - (true) should first column be in bold                           &lt;br /&gt;
 * col_header - (true) should we look up and use column names                   &lt;br /&gt;
 * caption    - (true) should we add table description as caption               &lt;br /&gt;
 * empty_cell - ([space]) value to use if &amp;quot;nil&amp;quot; value in the table              &lt;br /&gt;
 * format_table - (default class=&amp;quot;Wikitable&amp;quot;) change table formating            &lt;br /&gt;
 * bare       - (false) show only table's cells (needs to be placed within      &lt;br /&gt;
				 table wikitext                                                 &lt;br /&gt;
 * nowiki     - (false) show table's wikicode (used for debugging)              &lt;br /&gt;
]]&lt;br /&gt;
function p.json2table(frame)&lt;br /&gt;
	local args = getArgs(frame)&lt;br /&gt;
	if args.dataset then &lt;br /&gt;
		return p._json2table(args.dataset, args.lang, args)	&lt;br /&gt;
	else&lt;br /&gt;
		return ''&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Domino</name></author>
	</entry>
</feed>