Module:Get short description and Module:Get short description/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
imported>Qwerfjkl
 
imported>Trappist the monk
lightweight rewrite of the current (2024-08-15) module;
 
Line 1: Line 1:
local p = {}
require ('strict')
 
--[[--------------------------< P A T T E R N S _ T >----------------------------------------------------------
 
a sequence of patterns to match canonical template name and its redirects by transclusion counts as of 2024-08-15:
'Short description', -- 6,090,442 (canonical form)
'Short Description', -- 124
'Des', -- 20
'Shortdesc', -- 10
'Short desc', -- 8
'Shortdescription', -- 6
 
]]
 
local patterns_t = {
'[Ss]hort [Dd]escription',
'[Dd]es',
'[Ss]hort *desc',
'[Ss]hortdescription',
}
 
 
--[[-----------------------------< G E T C O N T E N T >-------------------------------------------------------
 
fetch the article's raw wikitext
]]


local function getContent(title)
local function getContent(title)
local success, titleObj = pcall(mw.title.new, title)
local success, titleObj = pcall(mw.title.new, title) -- get a title object for title
if not success then return nil end
if not success then return nil end -- return nil if sommat went wron
return titleObj:getContent()
return titleObj:getContent() -- return wikitext else
end
end


function p.main(frame, title)
 
--[[-----------------------------< M A I N >-------------------------------------------------------------------
]]
 
local function main(frame, title) -- do we really need <title> here?
local title = frame.args[1]
local title = frame.args[1]
     local wikitext = getContent(title)
     local wikitext = getContent(title)
if wikitext == nil then return "" end
if wikitext == nil then return "" end -- sommat went wrong, abandon
wikitext = frame:preprocess(wikitext)
 
    local startIndex, endIndex = string.find(wikitext, "<div class=\"shortdescription nomobile noexcerpt noprint searchaux\" style=\"display:none\">")
for _, pattern in ipairs (patterns_t) do -- for each pattern in the sequence of redirect patterns
    if startIndex == nil then
local description = wikitext:match ('{{%s*' .. pattern .. '%s*|%s*([^|}]+)%s*'); -- try to find a match
        return nil
if description then
    end
return description; -- found one, stop looking and done
    local descriptionStart = endIndex + 1
end
    local descriptionEnd = string.find(wikitext, "</div>", descriptionStart)
end
    if descriptionEnd == nil then
        return nil
    end
    return string.sub(wikitext, descriptionStart, descriptionEnd - 1)
end
end


return p
 
--[[-----------------------------< E X P O R T S >-------------------------------------------------------------
]]
 
return {
main = main,
}