Home
Random
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Word count by section/sandbox
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
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 '<!%-%-.-%-%->', -- 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>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 function p.getWordcountsForSectionsModified( 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>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[header] = wordCounts[i] -- first capture group end i = i+1 end return resultTable end function p.getByUsername( 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 sectionName = frame.args["sectionname"] local wordcountTable = p.getWordcountsForSectionsModified( targetPage, sectionLevel ) mw.log(sectionName) for section, count in pairs(wordcountTable) do section = mw.ustring.match(section, "^%s*(.-)%s*$") mw.log(section) if sectionName == section then return count end end return "ERRRROROROR" end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Template used on this page:
Module:Word count by section/sandbox/doc
(
edit
)