Module:Excerpt/sandbox: Difference between revisions

Content deleted Content added
Restore more functionalities
Restore more functionality
Line 41:
local links = yesno( Excerpt.getArg( 'links', true ) )
local bold = yesno( Excerpt.getArg( 'bold', false ) )
local onlyFreeFiles = yesno( Excerpt.getArg( 'onlyfreefiles', true ) )
local briefDates = yesno( Excerpt.getArg( 'briefdates', false ) )
local inline = yesno( Excerpt.getArg( 'inline' ) )
Line 74 ⟶ 73:
end
 
-- Remove unwanted elements
if onlyFreeFiles then
excerpt = Excerpt.removeNonFreeFilesremoveComments( excerpt )
end
 
-- Remove blacklisted templates
excerpt = Excerpt.removeBlacklist( excerpt )
excerpt = Excerpt.removeSelfLinks( excerpt )
excerpt = Excerpt.removeNonFreeFiles( excerpt )
excerpt = Excerpt.removeBehaviorSwitches( excerpt )
 
-- Remove referenceswikilinks
if not referenceslinks then
excerpt = Excerpt.removeReferencesremoveLinks( excerpt )
end
 
Line 92 ⟶ 91:
end
 
if onlyFreeFilesreferences then
-- Remove wikilinks
excerpt = Excerpt.removeSelfLinksfixReferences( excerpt, page, wikitext )
else
if not links then
excerpt = Excerpt.removeLinksremoveReferences( excerpt )
end
 
Line 151 ⟶ 150:
function Excerpt.addInfoboxFile( excerpt )
-- We cannot distinguish the infobox from the other templates, so we search them all
local templates = parser.getTemplates( excerpt );
for _, template in pairs( templates ) do
local parameters = parser.getTemplateParameters( template )
Line 227 ⟶ 226:
if edit then
local title = mw.title.getCurrentTitle()
local editUrl = title:fullUrl( 'action=edit' );
if editIntro then
editUrl = title:fullUrl( 'action=edit&editintro=' .. editIntro )
Line 278 ⟶ 277:
end
end
return excerpt
end
 
-- Replace the first call to each reference defined outside of the text for the full reference, to prevent undefined references
-- 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">
-- and also <ref name="Foo" /> for <ref name="Title of the article Foo" />
-- also remove reference groups: <ref name="Foo" group="Bar"> for <ref name="Title of the article Foo">
-- and <ref group="Bar"> for <ref>
-- @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 refNames = {}
local refName
local refBody
local position = 1
while position < mw.ustring.len( excerpt ) do
refName, position = mw.ustring.match( excerpt, '<%s*[Rr][Ee][Ff][^>]*name%s*=%s*["\']?([^"\'>]+)["\']?[^>]*/%s*>()', position )
if not linksrefName then
refName = mw.text.trim( refName )
if not refNames[ refName ] then -- make sure we process each ref name only once
table.insert( refNames, refName )
refName = Excerpt.escapeString( refName )
refBody = mw.ustring.match( excerpt, '<%s*[Rr][Ee][Ff][^>]*name%s*=%s*["\']?%s*' .. refName .. '%s*["\']?[^>/]*>.-<%s*/%s*[Rr][Ee][Ff]%s*>' )
if not refBody then -- the ref body is not in the excerpt
refBody = mw.ustring.match( wikitext, '<%s*[Rr][Ee][Ff][^>]*name%s*=%s*["\']?%s*' .. refName .. '%s*["\']?[^/>]*>.-<%s*/%s*[Rr][Ee][Ff]%s*>' )
if refBody then -- the ref body was found elsewhere
excerpt = mw.ustring.gsub( excerpt, '<%s*[Rr][Ee][Ff][^>]*name%s*=%s*["\']?%s*' .. refName .. '%s*["\']?[^>]*/?%s*>', mw.ustring.gsub( refBody, '%%', '%%%%' ), 1 )
end
end
end
else
position = mw.ustring.len( excerpt )
end
end
page = string.gsub( page, '"', '' ) -- remove any quotation marks from the page title
excerpt = mw.ustring.gsub( excerpt, '<%s*[Rr][Ee][Ff][^>]*name%s*=%s*["\']?([^"\'>/]+)["\']?[^>/]*(/?)%s*>', '<ref name="' .. page .. ' %1"%2>' )
excerpt = mw.ustring.gsub( excerpt, '<%s*[Rr][Ee][Ff]%s*group%s*=%s*["\']?[^"\'>/]+["\']%s*>', '<ref>' )
return excerpt
end
Line 308 ⟶ 344:
end
return excerpt
end
 
function Excerpt.removeBold( excerpt )
return string.gsub( excerpt, "'''", '' )
end
 
function Excerpt.removeBehaviorSwitches( excerpt )
return string.gsub( excerpt, '__[A-Z]+__', '' )
end
 
function Excerpt.removeComments( excerpt )
return string.gsub( excerpt, '<!%-%-.-%-%->', '' )
end
 
Line 380 ⟶ 428:
-- args from Lua calls have priority over parent args from template
function Excerpt.getArg( key, default )
local frame = mw.getCurrentFrame();
for k, value in pairs( frame:getParent().args ) do
if k == key and mw.text.trim( value ) ~= '' then