Content deleted Content added
Sophivorus (talk | contribs) Recover green status of most test cases |
Try to automatically account for page merge-like redirects |
||
(25 intermediate revisions by 2 users not shown) | |||
Line 7:
local yesno = require( 'Module:Yesno' )
local ok, config = pcall( require, 'Module:Excerpt/config
if not ok then config = {} end
Line 15:
function Excerpt.main( frame )
-- Make sure the requested page exists and get the wikitext
local page = Excerpt.getArg( 1 )
if not page or page == '{{{1}}}' then return Excerpt.getError( 'no-page' ) end
local title = mw.title.new( page )
if not title then return Excerpt.getError( 'invalid-title', page ) end
local fragment = title.fragment -- save for later
if title.isRedirect then
title = title.redirectTarget
if fragment == "" then
fragment = title.fragment -- page merge potential
end
end
if not title.exists then return Excerpt.getError( 'page-not-found', page ) end
page = title.prefixedText
local wikitext = title:getContent()
--
local params = {
hat = yesno( Excerpt.getArg( 'hat', true ) ),
this = Excerpt.getArg( 'this' ),
only = Excerpt.getArg( 'only' ),
files = Excerpt.getArg( 'files', Excerpt.getArg( 'file' ) ),
lists = Excerpt.getArg( 'lists', Excerpt.getArg( 'list' ) ),
tables = Excerpt.getArg( 'tables', Excerpt.getArg( 'table' ) ),
templates = Excerpt.getArg( 'templates', Excerpt.getArg( 'template' ) ),
paragraphs = Excerpt.getArg( 'paragraphs', Excerpt.getArg( 'paragraph' ) ),
references = yesno( Excerpt.getArg( 'references', true ) ),
subsections = yesno( Excerpt.getArg( 'subsections', false ) ),
links = yesno( Excerpt.getArg( 'links', true ) ),
bold = yesno( Excerpt.getArg( 'bold', false ) ),
briefDates = yesno( Excerpt.getArg( 'briefdates', false ) ),
inline = yesno( Excerpt.getArg( 'inline' ) ),
quote = yesno( Excerpt.getArg( 'quote' ) ),
more = yesno( Excerpt.getArg( 'more' ) ),
class = Excerpt.getArg( 'class' ),
displayTitle = Excerpt.getArg( 'displaytitle', page ),
}
-- Make sure the requested section exists and get the excerpt
local excerpt
local section = Excerpt.getArg( 2, fragment )
section = mw.text.trim( section )
if section == '' then section = nil end
if section then
excerpt = parser.getSectionTag( wikitext, section )
if not excerpt then
if params.subsections then
excerpt = parser.getSection( wikitext, section )
else
Line 63 ⟶ 69:
end
if not excerpt then return Excerpt.getError( 'section-not-found', section ) end
if excerpt == ''
else
excerpt = parser.getLead( wikitext )
if excerpt == ''
end
-- Remove noinclude bits
excerpt = excerpt:gsub( '<[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]>.-</[Nn][Oo][Ii][Nn][Cc][Ll][Uu][Dd][Ee]>', '' )
-- Filter various elements from the excerpt
excerpt = Excerpt.filterLists( excerpt, params.lists )
excerpt = Excerpt.filterTables( excerpt, params.tables )
excerpt = Excerpt.filterParagraphs( excerpt, params.paragraphs )
-- If no file
if ( params.only == 'file' or params.only == 'files'
and not section -- and we're in the lead section
and config.captions -- and we have the config option required to try finding files in infoboxes
and #parser.getFiles( excerpt ) == 0 -- and there
then
excerpt = Excerpt.addInfoboxFile( excerpt )
end
-- Filter the templates by appending the templates blacklist to the templates filter
if config.blacklist then
local blacklist = table.concat( config.blacklist, ',' )
if params.templates then
if string.sub( params.templates, 1, 1 ) == '-' then
params.templates = params.templates .. ',' .. blacklist
end
else
params.templates = '-' .. blacklist
end
end
excerpt = Excerpt.filterTemplates( excerpt, params.templates )
-- Leave only the requested elements
if params.only == 'file' or params.only == 'files' then
local files = parser.getFiles( excerpt )
excerpt = params.only == 'file' and files[1] or table.concat( files, '\n\n' )
end
if params.only == 'list' or params.only == 'lists' then
local lists = parser.getLists( excerpt )
excerpt = params.only == 'list' and lists[1] or table.concat( lists, '\n\n' )
end
if params.only == 'table' or params.only == 'tables' then
local tables = parser.getTables( excerpt )
excerpt = params.only == 'table' and tables[1] or table.concat( tables, '\n\n' )
end
if params.only == 'paragraph' or params.only == 'paragraphs' then
local paragraphs = parser.getParagraphs( excerpt )
excerpt = params.only == 'paragraph' and paragraphs[1] or table.concat( paragraphs, '\n\n' )
end
if params.only == 'template' or params.only == 'templates' then
local templates = parser.getTemplates( excerpt )
excerpt = params.only == 'template' and templates[1] or table.concat( templates, '\n\n' )
end
-- @todo Make more robust and move downwards
if params.briefDates then
excerpt = Excerpt.fixDates( excerpt )
end
-- Remove unwanted elements
excerpt = Excerpt.removeComments( excerpt )
excerpt = Excerpt.removeSelfLinks( excerpt )
excerpt = Excerpt.removeNonFreeFiles( excerpt )
excerpt = Excerpt.removeBehaviorSwitches( excerpt )
-- Fix or remove the references
if params.references then
excerpt = Excerpt.fixReferences( excerpt, page, wikitext )
else
excerpt = Excerpt.removeReferences( excerpt )
end
-- Remove wikilinks
if not params.links then
excerpt = Excerpt.removeLinks( excerpt )
end
-- Link the bold text near the start of most leads and then remove it
if not section then
excerpt = Excerpt.linkBold( excerpt, page )
end
if not params.bold then
excerpt = Excerpt.removeBold( excerpt )
end
-- Remove extra line breaks but leave one before and after so the parser interprets lists, tables, etc. correctly
excerpt = excerpt:gsub( '\n\n\n+', '\n\n' )
excerpt = mw.text.trim( excerpt )
excerpt = '\n' .. excerpt .. '\n'
Line 128 ⟶ 170:
-- Add tracking categories
if config.categories then
excerpt = Excerpt.addTrackingCategories( excerpt )
end
--
if params.inline then
return mw.text.trim( excerpt )
end
local tag = params.quote and 'blockquote' or 'div'
local block = mw.html.create( tag ):addClass( 'excerpt-block' ):addClass( params.class )
if config.styles then
local styles = frame:extensionTag( 'templatestyles', '', { src = config.styles } )
block:node( styles )
end
if params.hat then
local hat = Excerpt.getHat( page, section, params )
block:node( hat )
end
excerpt = mw.html.create( 'div' ):addClass( 'excerpt' ):wikitext( excerpt )
block:node( excerpt )
if
block:node( more )
end
return block
end
-- Filter the files in the given wikitext against the given filter
function Excerpt.filterFiles( wikitext, filter )
if not filter then return wikitext end
local filters, isBlacklist = Excerpt.parseFilter( filter )
local files = parser.getFiles( wikitext )
for index, file in pairs( files ) do
local name = parser.getFileName( file )
if isBlacklist and ( Excerpt.matchFilter( index, filters ) or Excerpt.matchFilter( name, filters ) )
or not isBlacklist and ( not Excerpt.matchFilter( index, filters ) and not Excerpt.matchFilter( name, filters ) ) then
wikitext = Excerpt.removeString( wikitext, file )
end
end
return wikitext
end
-- Filter the lists in the given wikitext against the given filter
function Excerpt.filterLists( wikitext, filter )
if not filter then return wikitext end
local filters, isBlacklist = Excerpt.parseFilter( filter )
local lists = parser.getLists( wikitext )
for index, list in pairs( lists ) do
if isBlacklist and Excerpt.matchFilter( index, filters )
or not isBlacklist and not Excerpt.matchFilter( index, filters ) then
wikitext = Excerpt.removeString( wikitext, list )
end
end
return wikitext
end
-- Filter the tables in the given wikitext against the given filter
function Excerpt.filterTables( wikitext, filter )
if not filter then return wikitext end
local filters, isBlacklist = Excerpt.parseFilter( filter )
local tables = parser.getTables( wikitext )
for index, t in pairs( tables ) do
local id = string.match( t, '{|[^\n]-id%s*=%s*["\']?([^"\'\n]+)["\']?[^\n]*\n' )
if isBlacklist and ( Excerpt.matchFilter( index, filters ) or Excerpt.matchFilter( id, filters ) )
or not isBlacklist and ( not Excerpt.matchFilter( index, filters ) and not Excerpt.matchFilter( id, filters ) ) then
wikitext = Excerpt.removeString( wikitext, t )
end
end
return wikitext
end
-- Filter the paragraphs in the given wikitext against the given filter
function Excerpt.filterParagraphs( wikitext, filter )
if not filter then return wikitext end
local filters, isBlacklist = Excerpt.parseFilter( filter )
local paragraphs = parser.getParagraphs( wikitext )
for index, paragraph in pairs( paragraphs ) do
if isBlacklist and Excerpt.matchFilter( index, filters )
or not isBlacklist and not Excerpt.matchFilter( index, filters ) then
wikitext = Excerpt.removeString( wikitext, paragraph )
end
end
return wikitext
end
-- Filter the templates in the given wikitext against the given filter
function Excerpt.filterTemplates( wikitext, filter )
if not filter then return wikitext end
local filters, isBlacklist = Excerpt.parseFilter( filter )
local templates = parser.getTemplates( wikitext )
for index, template in pairs( templates ) do
local name = parser.getTemplateName( template )
if isBlacklist and ( Excerpt.matchFilter( index, filters ) or Excerpt.matchFilter( name, filters ) )
or not isBlacklist and ( not Excerpt.matchFilter( index, filters ) and not Excerpt.matchFilter( name, filters ) ) then
wikitext = Excerpt.removeString( wikitext, template )
end
end
return wikitext
end
Line 177 ⟶ 285:
file = parameters[file]
if file and Excerpt.matchAny( file, '^.*%.', { '[Jj][Pp][Ee]?[Gg]', '[Pp][Nn][Gg]', '[Gg][Ii][Ff]', '[Ss][Vv][Gg]' }, '.*' ) then
file =
captions = pair[2]
for _, p in pairs( captions ) do
Line 207 ⟶ 315:
local fileName = 'File:' .. parser.getFileName( file )
local fileTitle = mw.title.new( fileName )
if fileTitle then
if not fileDescription or fileDescription == '' then
local frame = mw.getCurrentFrame()
fileDescription = frame:preprocess( '{{' .. fileName .. '}}' ) -- try Commons end
if fileDescription and string.match( fileDescription, '[Nn]on%-free' ) then
wikitext = Excerpt.removeString( wikitext, file )
end
end
end
Line 219 ⟶ 329:
end
function Excerpt.getHat( page, section,
local hat
-- Build the
if params.this then
hat = params.this
elseif params.quote then
hat = Excerpt.getMessage( 'this' )
elseif params.only then
hat = Excerpt.getMessage( params.only )
else
hat = Excerpt.getMessage( 'section' )
Line 234 ⟶ 344:
hat = hat .. ' ' .. Excerpt.getMessage( 'excerpt' )
-- Build the
if section then
hat = hat .. ' [[:' .. page .. '#' .. mw.uri.anchorEncode( section ) .. '|' .. params.displayTitle
.. ' § ' ..
else
hat = hat .. ' [[:' .. page .. '|' .. params.displayTitle .. ']].'
end
-- Build the edit link
local title = mw.title.new( page )
hat = hat .. '<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span>['
hat = hat .. editUrl .. ' ' .. mw.message.new( 'editsection' ):plain()
hat = hat .. ']<span class="mw-editsection-bracket">]</span></span>'
if config.hat then
Line 265 ⟶ 370:
end
function Excerpt.getReadMore( page, section
local link = "'''[[" .. page
if section then
link = link .. '#' .. section
end
link = link .. '|' .. text .. "]]'''"
link = mw.html.create( 'div' ):addClass( 'noprint excerpt-more' ):wikitext( link )
return link
Line 278 ⟶ 384:
-- @todo Use parser.getParagraphs() to get the first paragraph
function Excerpt.fixDates( excerpt )
local
local s
local e = 0
repeat
s, e = mw.ustring.find( excerpt,
until not s or s >
s, e = mw.ustring.find( excerpt,
if s and s <
local
if year1 and year2 and ( string.match( conjunction, '[%-–—]' ) or string.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(
excerpt = mw.ustring.sub( excerpt, 1, s ) .. year1 ..
end
end
Line 299 ⟶ 406:
end
-- Replace the first call to each reference defined outside of the
-- Then prefix the page title to the reference names to prevent conflicts
-- that is, replace <ref name="Foo"> for <ref name="Title of the article Foo">
Line 307 ⟶ 414:
-- @todo The current regex may fail in cases with both kinds of quotes, like <ref name="Darwin's book">
function Excerpt.fixReferences( excerpt, page, wikitext )
local references = parser.getReferences( excerpt )
local
for _, reference in pairs( references ) do
local name = parser.getTagAttribute( reference, 'name' )
if not fixed[ name ] then -- fix each reference only once
local content = parser.getTagContent( reference )
if not content then -- reference is self-closing
if not
if full then
excerpt = excerpt:gsub( Excerpt.escapeString( reference ), Excerpt.escapeString( full ), 1 )
end
table.insert( fixed, name )
end
end
end
end
-- Prepend the page title to the reference names to prevent conflicts with other references in the transcluding page
excerpt = excerpt:gsub( '< *[Rr][Ee][Ff][^>]*name *= *["\']?([^"\'>/]+)["\']?[^>/]*(/?) *>', '<ref name="' .. page:gsub( '"', '' ) .. ' %1"%2>' )
-- Remove reference groups because they don't apply to the transcluding page
excerpt = excerpt:gsub( '< *[Rr][Ee][Ff] *group *= *["\']?[^"\'>/]+["\'] *>', '<ref>' )
return excerpt
end
Line 363 ⟶ 453:
end
return excerpt
end
function Excerpt.removeBehaviorSwitches( excerpt )
return
end
function Excerpt.removeComments( excerpt )
return
end
function Excerpt.removeBold( excerpt )
return
end
Line 418 ⟶ 504:
return nil -- instruct gsub to make no change
end
end, 1 ) --
end
return excerpt
end
function Excerpt.addTrackingCategories( excerpt )
local currentTitle = mw.title.getCurrentTitle()
local contentCategory = config.categories.content
if contentCategory and currentTitle.isContentPage then
excerpt = excerpt .. '[[Category:' .. contentCategory .. ']]'
end
local namespaceCategory = config.categories[ currentTitle.namespace ]
if namespaceCategory then
excerpt = excerpt .. '[[Category:' .. namespaceCategory .. ']]'
end
return excerpt
end
Line 489 ⟶ 578:
-- Helper method to remove a string from a text
-- @param text Text from where to
-- @param str String to remove
-- @return The given text with the string removed
Line 495 ⟶ 584:
local pattern = Excerpt.escapeString( str )
if #pattern > 9999 then -- strings longer than 10000 bytes can't be put into regexes
pattern = Excerpt.escapeString( mw.ustring.sub( str, 1, 999 ) ) .. '.-' .. Excerpt.escapeString( mw.ustring.sub( str, -999 ) )
end
return
end
-- Helper method to convert a comma-separated list of numbers or min-max ranges into a list of booleans
-- @param filter Required. Comma-separated list of numbers or min-max ranges, for example '1,3-5'
-- @return Map from integers to booleans, for example {1=true,2=false,3=true,4=true,5=true}
-- @return Boolean indicating whether the
-- @
function Excerpt.parseFilter(
local
local
if string.sub( filter, 1, 1 ) == '-' then
isBlacklist = true
filter = string.sub( filter, 2 )
end
local
for _,
local min, max = mw.ustring.match(
if not max then min, max = string.match(
if max then
for i = min, max do
else
end
end
local filter = {cache = {}, terms = filters}
return filter, isBlacklist
end
-- Helper function to see if a value matches any of the given filters
function Excerpt.matchFilter( value,
if
return filter.terms[value]
else
if cached ~= nil then
return cached
end
local lang = mw.language.getContentLanguage()
local lcvalue = lang:lcfirst(value)
local ucvalue = lang:ucfirst(value)
for term in pairs( filter.terms ) do
if value == tostring(term)
or type(term) == "string" and (
lcvalue == term
or ucvalue == term
or mw.ustring.match( value, term )
) then
filter.cache[value] = true
return true
end
end
filter.cache[value] = false
end
end
return Excerpt
|