Module:Excerpt: Difference between revisions

Content deleted Content added
Update to 1.2.3, fixes lint errors (see talk page), global scope issue and code style changes
Use infobox CSS class if it exists (mainly for dark mode compatibility)
 
(6 intermediate revisions by 2 users not shown)
Line 1:
-- Module:Excerpt implements the Excerpt template
-- Documentation and master version: https://wwwen.mediawikiwikipedia.org/wiki/Module:Excerpt
-- ByAuthors: User:Sophivorus, User:Certes, User:Aidan9382 & others
-- License: CC-BY-SA-43.0
-- Version 1.2.3
-- License CC-BY-SA-4.0
 
local Transcluder = require( 'Module:Transcluder' )
Line 16 ⟶ 15:
-- Helper function to get arguments
local args
local function getArg( key, default )
local value = args[ key ]
if value and mw.text.trim( value ) ~= '' then
Line 25 ⟶ 24:
 
-- Helper function to handle errors
local function getError( message, value )
if type( message ) == 'string' then
message = Transcluder.getError( message, value )
Line 36 ⟶ 35:
 
-- Helper function to get localized messages
local function getMessage( key )
local ok, TNT = pcall( require, 'Module:TNT' )
if not ok then return key end
Line 42 ⟶ 41:
end
 
-- Main entry point for templates
function p.main( frame )
args = Transcluder.parseArgs( frame )
Line 47:
-- Make sure the requested page exists
local page = getArg( 1 )
if not page or page == '{{{1}}}' then return getError( 'no-page' ) end
local title = mw.title.new(page)
if not title then return getError( 'noinvalid-pagetitle', page ) end
if title.isRedirect then title = title.redirectTarget end
if not title.exists then return getError( 'page-not-found', page ) end
Line 67:
local references = getArg( 'references' )
local subsections = not yesno( getArg( 'subsections' ) )
local noLinks = not yesno( getArg( 'links', true ) )
local noBold = not yesno( getArg( 'bold' ) )
local freefilesonlyFreeFiles = yesno( getArg( 'freefilesonlyfreefiles', true ) )
local briefDates = yesno( getArg( 'briefdates', false ) )
local inline = yesno( getArg( 'inline' ) )
local quote = yesno( getArg( 'quote' ) )
local more = yesno( getArg( 'more' ) )
local class = getArg( 'class' )
local displaytitle = getArg( 'displaytitle' ) or page
 
-- Build the hatnote
Line 86 ⟶ 89:
end
hat = hat .. ' ' .. getMessage( 'excerpt' ) .. ' '
if section and not fragment then
hat = hat .. '[[:' .. page .. '#' .. mw.uri.anchorEncode( section ) .. '|' .. pagedisplaytitle
.. ' § ' .. mw.ustring.gsub( section, '%[%[([^]|]+)|?[^]]*%]%]', '%1' ) .. ']].' -- remove nested links
else
hat = hat .. '[[:' .. page .. '|' .. pagedisplaytitle .. ']].'
end
if edit then
Line 125 ⟶ 128:
references = references,
only = only and mw.text.trim( only, 's' ) .. 's',
noLinks = noLinks,
noBold = noBold,
noSelfLinks = true,
noNonFreeFiles = freefilesonlyFreeFiles,
noBehaviorSwitches = true,
fixReferences = true,
Line 139 ⟶ 143:
if mw.text.trim( excerpt ) == '' and not only then
if section then return getError( 'section-empty', section ) else return getError( 'lead-empty' ) end
end
 
-- Fix birth and death dates, but only in the first paragraph
if quotebriefDates then
local startpos = 1 -- skip initial templates
local s
local tag1e = 'div'0
repeat
startpos = e + 1
s, e = mw.ustring.find( excerpt, "%s*%b{}%s*", startpos )
until not s or s > startpos
s, e = mw.ustring.find( excerpt, "%b()", startpos ) -- get (...), which may be (year–year)
if s and s < startpos + 100 then -- look only near the start
local year1, conjunction, year2 = mw.ustring.match( mw.ustring.sub( excerpt, s, e ), '(%d%d%d+)(.-)(%d%d%d+)' )
if year1 and year2 and (mw.ustring.match( conjunction, '[%-–—]' ) or mw.ustring.match( conjunction, '{{%s*[sS]nd%s*}}' )) then
local y1 = tonumber(year1)
local y2 = tonumber(year2)
if y2 > y1 and y2 < y1 + 125 and y1 <= tonumber( os.date( "%Y" )) then
excerpt = mw.ustring.sub( excerpt, 1, s ) .. year1 .. "–" .. year2 .. mw.ustring.sub( excerpt, e )
end
end
end
end
 
Line 151 ⟶ 177:
infobox = table.concat( infobox )
local parameters = Transcluder.getParameters( infobox )
local file, captions, caption, cssclasses, cssclass
for _, pair in pairs( config.captions ) do
file = pair[1]
Line 161 ⟶ 187:
if parameters[ p ] then caption = parameters[ p ] break end
end
excerpt = '[[File:' .. file .. '|thumb|' .. ( caption or '' ) .. ']]' .. excerpt
if-- (Check freefilesfor )CSS thenclasses
-- We opt to use skin-invert-image instead of skin-invert
-- in all other cases, the CSS provided in the infobox is used
if pair[3] then
cssclasses = pair[3]
for _, p in pairs(cssclasses) do
if parameters[p] then
cssclass = ((parameters[p] == 'skin-invert') and 'skin-invert-image' or parameters[p])
break
end
end
end
excerpt = '[[File:' .. file ..
(cssclass and ('|class=' .. cssclass) or '') ..
excerpt = '[[File:' .. file .. '|thumb|' .. ( caption or '' ) .. ']]' .. excerpt
if ( onlyFreeFiles ) then
excerpt = Transcluder.removeNonFreeFiles( excerpt )
end
Line 217 ⟶ 259:
 
-- Combine and return the elements
local tag1 = 'div'
local tag2 = 'div'
if quote then
tag2 = 'blockquote'
end
excerpt = mw.html.create( tag1 ):addClass( 'excerpt' ):wikitext( excerpt )
if inline then
excerptreturn =mw.text.trim( excerpt:css( 'display', 'inline' )
end
local tag2tag = 'div'
local block = mw.html.create( tag2 ):addClass( 'excerpt-block' ):addClass( class )
if inlinequote then
tag2tag = 'blockquote'
block = block:css( 'display', 'inline' )
end
excerpt = mw.html.create( tag1'div' ):addClass( 'excerpt' ):wikitext( excerpt )
local block = mw.html.create( tag2tag ):addClass( 'excerpt-block' ):addClass( class )
return block:node( styles ):node( hat ):node( excerpt ):node( more )
end