Module:Word count by section

local p = {}

local function prepareText( inputString ) local retVal = inputString local cleanupPatterns = { -- Note, these are invoked in order, which may be important! '=+[^=]+=+[ ]-\n', -- no headers '[^[]%[[^[ ]+[]%a%d]', -- no external link URLs		'{{[^|]-|', -- no template invocations		'&#x3c;!%-%-.-%-%-&#x3e;', -- no HTML comments		'\n' -- strip newlines	}	for _, pattern in ipairs(cleanupPatterns) do		retVal = mw.ustring.gsub(retVal, pattern, '')	end

return retVal end

local function getWordcount( text ) return (table.maxn(mw.text.split(prepareText(text), '[ ]+')) + 1) end

local function getWordcountsForSections( targetPage, sectionLevel ) local success, targetTitle = pcall( mw.title.new, targetPage ) if not success or ( success and not targetTitle ) then return nil end local targetContent = targetTitle:getContent local headerMarkup = string.rep( "=", sectionLevel ) local sections = mw.text.split(targetContent, '\n[ ]-'.. headerMarkup ..'[^=]+'.. headerMarkup ..'[ ]-\n') local wordCounts = {} for i, section in ipairs(sections) do		if (i&#x3e;1) then -- Skip preamble section wordCounts[#wordCounts + 1] = getWordcount(section) end end local resultTable = {} local headersIterator = mw.ustring.gmatch(targetContent, '\n[ ]-'.. headerMarkup ..'([^=]+)'.. headerMarkup ..'[ ]-\n') local i = 1 for header in headersIterator do		if mw.ustring.match(header, '{your user name}') == nil then -- we don't care about example sections resultTable[i .. " " .. header] = wordCounts[i] -- first capture group end i = i+1 end return resultTable end

local function getRowsFromWordcountTable( wordcountTable ) local ret = "" for section, count in pairs(wordcountTable) do ret = ret .. string.format("\n|-\n| %s || %s", section, count) end return ret end

function p.main( frame ) local frame = mw.getCurrentFrame local targetPage = frame.args["target"] local sectionLevel = frame.args["sectionlevel"] sectionLevel = tonumber(sectionLevel, 10) if sectionLevel == nil then sectionLevel = 2 end

local tableCss = frame.args["tablecss"] if tableCss == nil then tableCss = '' end local wordcountTable = getWordcountsForSections( targetPage, sectionLevel )

return mw.ustring.format( '\n{| class="wikitable sortable" %s \n|-\n! Section !! Word Count%s\n|-\n|}', tableCss, getRowsFromWordcountTable(wordcountTable) ) end

return p