Jump to content

Module:Table row counter

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 08:31, 21 June 2014 (rewrite using the absolute number of wikitables on a page rather than using error-prone section searching). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module counts table rows in wikitext.

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {parentOnly = true})
	local tableNo = tonumber(args[1])
	local ignoreRows = tonumber(args.ignore)
	local page = args.page
	local success, titleObj = pcall(mw.title.new, page)
	if not success then
		titleObj = nil
	end
	return p.luaMain(tableNo, titleObj) or ''
end

function p.luaMain(tableNo, ignoreRows, titleObj)
	titleObj = titleObj or mw.title.getCurrentTitle()
	local content = titleObj:getContent()
	if not content then
		return nil
	end

	local wikitables
	do
		wikitables = {}
		local iWikitable = 0
		for s in content:gmatch('\n({|.-\n|})') do
			iWikitable = iWikitable + 1
			wikitables[iWikitable] = s
		end
	end

	tableNo = tableNo or 1
	local wikitable = wikitables[tableNo]

	if not wikitable then
		return nil
	end

	local iRow
	wikitable, iRow = wikitable:gsub('\n|%-', '\n|-')
	return iRow - ignoreRows
end

return p