Module:Abstract

local Transcluder = require( 'Module:Transcluder' )

local p = {}

-- Helper function to handle errors function getError( message, value ) if type( message ) == 'string' then message = Transcluder.getError( message, value ) end return message end

function p.main( frame ) local args = Transcluder.parseArgs( frame )

-- Make sure the requested page exists local page = args[1] if not page then return getError( 'no-page' ) end local title = mw.title.new( page ) if not title then return getError( 'no-page' ) end if title.isRedirect then title = title.redirectTarget end if not title.exists then return getError( 'page-not-found', page ) end page = title.prefixedText

-- Get the abstract local ok, abstract = pcall( Transcluder.get, page, {		only = 'parameters',		parameters = 'abstract',		noBold = true,		noLinks = true,		files = 0,		references = 0,		templates = '-efn,efn-la,efn-ua,efn-lr,sfn,refn,notetag,notetag-ua,rp,pp,fig'	} ) if not ok then return getError( 'Abstract not found' ); end

-- Strip the abstract further abstract = frame:preprocess( abstract ) abstract = Transcluder.removeLinks( abstract ) abstract = abstract:gsub( '%^ ]- ([^-)%]', '%1' )

-- If the abstract is too long, collapse the excess local limit = tonumber( args['limit'] ) or 0 if #abstract > limit then local constantContent = abstract:sub( 0, limit ); local remainingContent = abstract:sub( limit ); local constantContent = constantContent .. remainingContent:match( '^(.-%.)' ) local collapsedContent = abstract:sub( #constantContent + 1 ) if #collapsedContent > 0 then abstract = frame:expandTemplate{ title = 'Collapsible toggle', args = { constantContent .. ' [...]',					collapsedContent, collapsed = true, toggle = tonumber( args['toggle'] ) or 0 }			}		end end return abstract end

return p