Module:GetShortDescription/sandbox: Difference between revisions

Content deleted Content added
use latest version
use preprocessing with safesubst to avoid trying to parse wikitext
Line 45:
return nil
end
-- Preprocess the short description template so that we don't have to parse
-- The following mess is an alternative to preprocessing, which is expensive and
-- the wikitext ourselves. We have to use safesubst here so that the
-- causes empty strings to be returned if the invocation is substituted.
-- template is processed correctly when both transcluding and when
-- Why does it look like this though?
-- substituting.
-- Short descriptions defined by template {{short description}} may contain:
local preprocessed_short_description_template = mw.getCurrentFrame():preprocess(
-- a numbered or unnumbered 1st param for the description
'{{safesubst:short description|' .. short_description_content .. '}}'
-- a numbered or unnumbered 2nd param for noreplace
)
-- the pagetype named param
-- anGet emptythe short description from the preprocessed template.
local short_description = mw.ustring.match(
-- we might therefore need to understand the content of:
preprocessed_short_description_template,
-- {{short description|2=noreplace|pagetype=Redirect|Redirect page}} or
'>%s*(.-)%s*<'
-- {{short description|2=noreplace|1=Redirect page|pagetype=Redirect}} or
)
-- {{short description|pagetype=Redirect|Redirect page|noreplace}} etc.
if mw.ustring.match(not short_description, '=' ) then
-- all with a variety white space.
return nil
-- Split the param content into trimmed param expressions.
local short_description_params = mw.text.split( short_description_content, '%s*|%s*' )
local short_description
-- For each param expession:
for i, param in ipairs( short_description_params ) do
-- ignore 'pagetype=<anything>'
-- ignore 'noreplace' if unnumbered or numbered as '2='
if not mw.ustring.match( param, '^pagetype%s*=' )
and not mw.ustring.match( param, '^noreplace$' )
and not mw.ustring.match( param, '^2%s*=' ) then
-- Whatever remains is the short description expression.
short_description = param
break
end
end
-- The short descrition expression might be in the form
-- 'short description' or
-- '1=short description'
-- If the latter; trim the leading '1='.
if mw.ustring.match( short_description, '=' ) then
short_description = mw.ustring.gsub( short_description, '^1%s*=%s*', '' )
end
-- Now we know what this template's short description is,
-- check if it's in use and return nil if not.
if mw.ustring.match( short_description, '^[Nn]one$' ) then
short_description =return nil
end
-- It could still be a completely useless string e.g.
-- {{short description| &nbsp; &nbsp; &nbsp; &nbsp; }}
-- creates an 8 character short description of entirely whitespace.
-- Trim the result before return.
return mw.ustring.match( short_description, '^%s*([^%s]-)%s*$' )