Module:Sandbox/Firefly
local p = {}
local function prepareText( inputString )
local retVal = inputString
local cleanupPatterns = {}
-- Note, these are invoked in order, which may be important!
cleanupPatterns[1] = '=+[^=]+=+[ ]-\n' -- no headers
cleanupPatterns[2] = '[^[]%[[^[ ]+[]%a%d]' -- no external link URLs
cleanupPatterns[3] = '{{[^|]-|' -- no template invocations
cleanupPatterns[4] = '<!%-%-.-%-%->' -- no HTML comments
cleanupPatterns[5] = '\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
local function makeWordcountTable( args )
return
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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.