Mô đun:Excerpt slideshow

local p = {}
local excerptModule = require('Module:Excerpt/portals')
local slideshowModule = require('Module:Random slideshow')
local randomModule = require('Module:Random')

local DEFAULT_LIMIT = 25 -- max number of excerpts to be shown in the slideshow
local SOURCE_PAGES_LIMIT = 10 -- max number of pages to check for links / list items

-- begin BHG addition for tracking source pages
local sourcepgagesused = {};
local sourcepgagesusedcounter = 0;
local articlelistcount = -1;
local usesEmbeddedList = false;
-- end BHG addition for tracking source pages

function cleanupArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s(.-)%s$')
if val ~= then
cleanArgs[key] = val
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end

function isDeclined(val)
if not val then return false end
local declinedWords = " decline declined exclude excluded false none not no n off omit omitted remove removed "
return string.find(declinedWords , ' '..val..' ', 1, true ) and true or false
end

--
@param {String} wikitext: Wikitext of just the list (i.e. each line is a list item)
@param {String} symbol: Special character used in the wikitext markup for the list, e.g. '' or ''
@param {String} outerTag: Text portion of the tag for each list or sublist, e.g. 'ul' or 'ol'
@param {String} innerTag: Text portion of the tag for each list item, e.g. 'li'

local wikitextToHtmlList = function(wikitext, symbol, outerTag, innerTag)
local listParts = {}
for level, item in mw.ustring.gmatch('\n'..wikitext..'\n', '\n(%'..symbol..'+)(.-)%f[\n]') do
table.insert(listParts, {level=level, item=item})
end
table.insert(listParts, {level=, item=})

local htmlList = {}
for i, this in ipairs( listParts ) do
local isFirstItem = (i == 1)
local isLastItem = (i == listParts)
local lastLevel = isFirstItem and or listParts[i-1]['level']
local tags
if lastLevel == this.level then
tags =
elseif this.level > lastLevel then
tags = string.rep(, this.level - lastLevel)
elseif isLastItem then
tags = string.rep(, lastLevel)
else -- ( this.level ', lastLevel - this.level ) ..
end
table.insert(htmlList, tags .. this.item)
end
return table.concat(htmlList)
end

--
@param {String} wikitext: Wikitext excertp containg zero or more lists
@param {String} symbol: Special character used in the wikitext markup for the list, e.g. '' or ''
@param {String} outerTag: Text portion of the tag for each list or sublist, e.g. 'ul' or 'ol'
@param {String} innerTag: Text portion of the tag for each list item, e.g. 'li'

local gsubWikitextLists = function(wikitext, symbol, outerTag, innerTag)
-- temporarily remove list linebreaks...
wikitext = mw.ustring.gsub(wikitext..'\n', '\n%'..symbol, '¿¿¿'..symbol)
-- ...so we can grab the whole list (and just the list)...
return mw.ustring.gsub(
wikitext,
'¿¿¿%'..symbol..'[^\n]+',
function(listWikitext)
-- ...and then reinstate linebreaks...
listWikitext = mw.ustring.gsub(listWikitext, '¿¿¿%'..symbol, '\n'..symbol)
-- ...and finally do the conversion
return wikitextToHtmlList(listWikitext, symbol, outerTag, innerTag)
end
)
end

-- help gsub strip tables and templates that aren't part of the prose,
and remove linebreaks from within other templates,
and preprocess parser functions
local processBraces = function(t)
local isTable = mw.ustring.sub(mw.text.trim(t), 2, 2) == '|'
if isTable then
return
end
-- else it's a template or parser function
local first = mw.ustring.sub(t, 1, 1)
local last = mw.ustring.sub(t, -1)
local isNotPartOfProse = first '\n' and last '\n'
if isNotPartOfProse then
return
end
local isParserFunction = mw.ustring.sub(mw.text.trim(t), 3, 3) == ''
if isParserFunction then
local frame = mw.getCurrentFrame()
return frame:preprocess(t)
end
-- else replace pipes and remove internal linebreaks
return mw.ustring.gsub(t, '\n', )
end

local cleanUpExcerpt = function(excerpt)
-- strip galleries
excerpt = mw.ustring.gsub(excerpt, ".-", "")
-- strip tables and block templates; strip newlines and replace pipes within inline templates
excerpt = mw.ustring.gsub(excerpt..'\n', '\n?%b{}\n?', processBraces)
-- replace wikitext bulleted lists with html bulleted lists
excerpt = gsubWikitextLists(excerpt, '', 'ul', 'li')
-- replace wikitext numbered lists with html numbered lists
excerpt = gsubWikitextLists(excerpt, '', 'ol', 'li')
excerpt = mw.text.trim(excerpt)
return excerpt
end

function makeGalleryArgs(titles, options, limit, nonRandom)
local galleryArgs = {}
local titlesSequence = {}
local i = 1
while titles[i] do
titlesSequence[i] = titles[i]
i = i + 1
end
local sortedTitles = nonRandom and titlesSequence or randomModule.main('array', {t=titlesSequence, limit=limit})
for _i, title in ipairs(sortedTitles) do
if (galleryArgs / 2) 10 then -- check again in case we had a few characters plus (Full article...)
excerpt = cleanUpExcerpt(excerpt)
if options.more then
excerpt = excerpt .. " (" .. options.more .. ")"
end
local text = .. mw.ustring.gsub(excerpt, '%c', ) ..
table.insert(galleryArgs, 'File:Blank.png')
table.insert(galleryArgs, text)
end
end
end
if nonRandom then
galleryArgs.random = 'false'
end
if galleryArgs == 0 and options.nostubs then
-- try again, this time including stubs
options.nostubs = false
return makeGalleryArgs(titles, options, limit, nonRandom)
else
return galleryArgs
end
end

local makeOptions = function(args)
local options = args -- pick up miscellaneous options: more, errors, fileargs
options.paraflags = excerptModule.numberFlags(args.paragraphs or "") -- parse paragraphs, e.g. "1,3-5" → {"1","3-5"}
options.fileflags = excerptModule.numberFlags(args.files or "") -- parse file numbers
if args.nostubs and isDeclined(args.nostubs) then
options.nostubs = false
else
options.nostubs = true
end
return options
end

local isArticle = function(pagetitle)
local titleObject = mw.title.new(pagetitle)
return ( titleObject and titleObject.namespace == 0 ) and true or false
end

local getLinkedTitles = function(args, method, limit)
local pagenames = {}
local ii = 1
local isNotCategory
while args[ii] and ii \n----\n'
retval = retval .. "sourcepgagesusedcounter: " .. sourcepgagesusedcounter .. "\n----\n"
retval = retval .. "pages used:"
end
local apage
for apage in arrayvalues(sourcepgagesused) do
if debugging then
retval = retval .. "\n :" .. apage .. ""
retval = retval .. " — " .. "First 999 = /" .. string.sub(apage, 1, 999) .. "/"
end
if (string.find(apage, "^[tT]emplate ?:") == 1) then
templatecount = templatecount + 1;
end
if (string.find(apage, "^[oO]utline +of ") == 1) then
outlinecount = outlinecount + 1;
end
if (apage == thisPageBareName) then
usesEponymousArticle = true;
end
pagecounter = pagecounter + 1
end
if debugging then
retval = retval .. "\nTổng số trang: " .. pagecounter
retval = retval .. "\ntemplatecount: " .. templatecount
retval = retval .. ""
end
-- first do a sanity check that both counting methods have produced the same result
if (sourcepgagesusedcounter pagecounter) then
-- if all pages are templates, then populate tracking categories
if (pagecounter templatecount) then
if (templatecount == 1) then
retval = retval .. ""
elseif (templatecount == 2) then
retval = retval .. ""
elseif (templatecount == 3) then
retval = retval .. ""
elseif (templatecount > 3) then
retval = retval .. ""
end
elseif (templatecount > 0) then
retval = retval .. ""
end
end
if (outlinecount >= 1) then
retval = retval .. ""
end
if (articlelistcount 1000) then
retval = retval .. ""
end
if usesEmbeddedList then
retval = retval .. ""
end
if usesEponymousArticle then
retval = retval .. ""
end
return retval
end

function arrayvalues(t)
local i = 0
return function() i = i + 1; return t[i] end
end

-- end BHG addition for tracking source pages
return p