Modul:Excerpt: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
←Membuat halaman berisi 'local Transcluder = require('Module:Transcluder') local yesno = require('Module:Yesno') local ok, config = pcall(require, 'Module:Extract/config') if not ok then con...'
 
Update from master using Synchronizer #synchronizer
 
(8 revisi perantara oleh 2 pengguna tidak ditampilkan)
Baris 1:
-- Module:Excerpt implements the Excerpt template
local Transcluder = require('Module:Transcluder')
-- Documentation and master version: https://en.wiki-indonesia.club/wiki/Module:Excerpt
-- Authors: User:Sophivorus, User:Certes, User:Aidan9382 & others
-- License: CC-BY-SA-3.0
 
local yesnoTranscluder = require( 'Module:YesnoTranscluder' )
 
local ok, configyesno = pcall(require,( 'Module:Extract/configYesno' )
 
local ok, config = pcall( require, 'Module:Excerpt/config' )
if not ok then config = {} end
 
Baris 10 ⟶ 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
return value
end
Baris 19 ⟶ 24:
 
-- Helper function to handle errors
local function getError( message, value )
if type( message ) == 'string' then
message = Transcluder.getError( message, value )
end
if config.categories and config.categories.errors and mw.title.getCurrentTitle().isContentPage then
message:node( '[[Category:' .. config.categories.errors .. ']]' )
end
return message
Baris 30 ⟶ 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
return TNT.format( 'I18n/Module:ExtractExcerpt.tab', key )
end
 
-- Main entry point for templates
function p.main(frame)
argsfunction = Transcluderp.parseArgsmain( frame )
args = Transcluder.parseArgs( frame )
 
-- 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
page = title.prefixedText
 
-- Set variables from the template parameters
local fragmentsection = getArg( 2, mw.ustring.match( getArg( 1 ), 'fragment[^#]+#(.+)' ) )
local sectionhat = fragment or getArgyesno(2, mw.ustring.match(getArg(1), '[^#]+#([^#]+)hat', true ) )
local hatedit = yesno( getArg( 'hatedit', true ) )
local this = getArg( 'this' )
local only = getArg( 'only' )
local files = getArg( 'files', getArg( 'file', ( only == 'file' and 1 ) ) )
local lists = getArg( 'lists', getArg( 'list', ( only == 'list' and 1 ) ) )
local tables = getArg( 'tables', getArg( 'table', ( only == 'table' and 1 ) ) )
local sectionstemplates = not yesnogetArg( 'templates', getArg( 'sectionstemplate', ( only == 'template' and 1 ) ) )
local paragraphs = getArg( 'paragraphs', getArg( 'paragraph', ( only == 'paragraph' and 1 ) ) )
local templates = table.concat((config.templates or {}), ',')
local paragraphsreferences = getArg( 'paragraphsreferences' )
local referencessubsections = not yesno( getArg( 'referencessubsections' ) )
local noBoldnoLinks = not yesno( getArg( 'boldlinks', true ) )
local inlinenoBold = not yesno( getArg( 'inlinebold' ) )
local quoteonlyFreeFiles = yesno( getArg( 'quoteonlyfreefiles', true ) )
local morebriefDates = yesno( getArg( 'morebriefdates', false ) )
local classinline = yesno( getArg( 'classinline' ) )
local quote = yesno( getArg( 'quote' ) )
local more = yesno( getArg( 'more' ) )
local class = getArg( 'class' )
local displaytitle = getArg( 'displaytitle' ) or page
 
-- Build the hatnote
Baris 72 ⟶ 82:
hat = this
elseif quote then
hat = getMessage( 'this' )
elseif only then
hat = getMessage( only )
else
hat = getMessage( 'section' )
end
hat = hat .. ' ' .. getMessage( 'extractexcerpt' ) .. ' '
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 .. '|' .. displaytitle .. ']].'
end
if edit then
hat = hat .. "''" .. '<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span>['
hat = hat .. '<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span>['
hat = hat .. title:fullUrl('action=edit') .. ' edit'
hat = hat .. title:fullUrl( 'action=edit' ) .. ' ' .. mw.message.new( 'editsection' ):plain()
hat = hat .. ']<span class="mw-editsection-bracket">]</span></span>' .. "''"
hat = hat .. ']<span class="mw-editsection-bracket">]</span></span>'
local ok, Hatnote = pcall(require, 'Module:Hatnote')
end
if ok then
if config.hat then
hat = Hatnote._hatnote( hat, { extraclasses = 'dablink extract-hat', selfref = true } )
hat = config.hat .. hat .. '}}'
hat = frame:preprocess( hat )
else
hat = mw.html.create( 'div' ):addClass( 'dablink extractexcerpt-hat' ):wikitext( hat )
end
else
Baris 100 ⟶ 112:
-- Build the "Read more" link
if more and not inline then
more = "'''[[" .. page .. '#' .. ( section or '' ) .. "|" .. getMessage( 'more' ) .. "]]'''"
more = mw.html.create( 'div' ):addClass( 'noprint extractexcerpt-more' ):wikitext( more )
else
more = nil
end
 
-- Build the options for Module:Transcluder out of the template argumentsparameters and the desired defaults
local options = {
files = files,
Baris 112 ⟶ 124:
tables = tables,
paragraphs = paragraphs,
sections = subsections,
templates = templates ~= '' and '-' .. templates,
sections = sections,
categories = 0,
references = references,
only = only and mw.text.trim( only, 's' ) .. 's',
noLinks = noLinks,
noBold = noBold,
noSelfLinks = true,
noNonFreeFiles = onlyFreeFiles,
noBehaviorSwitches = true,
fixReferences = true,
Baris 124 ⟶ 137:
}
 
-- Get the extractexcerpt itself
local title = page .. '#' .. ( section or '' )
local ok, extractexcerpt = pcall( Transcluder.get, title, options )
if not ok then return getError(extract excerpt ) end
if mw.text.trim(extract 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
-- Add a line break in case the extract starts with a table or list
if briefDates then
extract = '\n' .. extract
local startpos = 1 -- skip initial templates
local s
local e = 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
 
-- If no file was found, try to extractget one from the removed infoboxesinfobox
local fileNamespaces = Transcluder.getNamespaces( 'File' )
if (files ~( only == '0file' or notonly == 'files' ) andor ( not Transcluder.matchAnyonly and (extract, '%[%[',files fileNamespaces,~= ':0' or not files ) ) ) and config.captions-- thencaller asked for files
not Transcluder.matchAny( excerpt, '%[%[', fileNamespaces, ':' ) and -- and there are no files in Transcluder's output
local templates = Transcluder.get(title, { only = 'templates', templates = templates, fixReferences = true } )
config.captions -- and we have the config option required to try finding files in templates
local parameters = Transcluder.getParameters(templates)
then
-- We cannot distinguish the infobox from the other templates so we search them all
local infobox = Transcluder.getTemplates( excerpt );
infobox = table.concat( infobox )
local parameters = Transcluder.getParameters( infobox )
local file, captions, caption
for _, pair in pairs( config.captions ) do
file = pair[1]
file = parameters[file]
if file and Transcluder.matchAny( file, '^.*%.', { '[Jj][Pp][Ee]?[Gg]', '[Pp][Nn][Gg]', '[Gg][Ii][Ff]', '[Ss][Vv][Gg]' }, '.*' ) then
file = mw.ustring.match( file, '%[?%[?.-:([^{|]+)%]?%]?' ) or file -- [[File:Example.jpg{{!}}upright=1.5]] to Example.jpg
captions = pair[2]
for _, p in pairs( captions ) do
if parameters[ p ] then caption = parameters[ p ] break end
end
excerpt = '[[File:' .. file .. '|thumb|' .. ( caption or '' ) .. ']]' .. excerpt
if ( onlyFreeFiles ) then
excerpt = Transcluder.removeNonFreeFiles( excerpt )
end
extract = '[[File:' .. file .. '|thumb|' .. (caption or '') .. ']]' .. extract
break
end
end
end
 
-- Unlike other elements, templates are filtered here
-- because we had to search the infoboxes for files
local trash
if only and ( only == 'template' or only == 'templates' ) then
trash, excerpt = Transcluder.getTemplates( excerpt, templates );
else -- Remove blacklisted templates
local blacklist = config.blacklist and table.concat( config.blacklist, ',' ) or ''
if templates then
if string.sub( templates, 1, 1 ) == '-' then --Unwanted templates. Append to blacklist
blacklist = templates .. ',' .. blacklist
else --Wanted templates. Replaces blacklist and acts as whitelist
blacklist = templates
end
else
blacklist = '-' .. blacklist
end
trash, excerpt = Transcluder.getTemplates( excerpt, blacklist );
end
 
-- Remove extra line breaks but leave one before and after so the parser interprets lists, tables, etc. correctly
excerpt = mw.text.trim( excerpt )
excerpt = string.gsub( excerpt, '\n\n\n+', '\n\n' )
excerpt = '\n' .. excerpt .. '\n'
 
-- Remove nested categories
extractexcerpt = frame:preprocess(extract excerpt )
local categories, extractexcerpt = Transcluder.getCategories(extract excerpt, options.categories )
 
-- Add tracking categories
Baris 164 ⟶ 228:
local contentCategory = config.categories.content
if contentCategory and mw.title.getCurrentTitle().isContentPage then
message:node(excerpt = excerpt .. '[[Category:' .. contentCategory .. ']]')
end
local namespaceCategory = config.categories[ mw.title.getCurrentTitle().namespace ]
if namespaceCategory then
extractexcerpt = extractexcerpt .. '[[Category:' .. namespaceCategory .. ']]'
end
end
Baris 179 ⟶ 243:
 
-- Combine and return the elements
local tag1 = 'div'
local tag2 = 'div'
if inline then
return mw.text.trim( excerpt )
tag1 = 'span'
tag2 = 'span'
elseif quote then
tag2 = 'blockquote'
end
local tag = 'div'
extract = mw.html.create(tag1):addClass('extract'):wikitext(extract)
if quote then
local block = mw.html.create(tag2):addClass('extract-block'):addClass(class)
tag = 'blockquote'
return block:node(styles):node(hat):node(extract):node(more)
end
excerpt = mw.html.create( 'div' ):addClass( 'excerpt' ):wikitext( excerpt )
local block = mw.html.create( tag ):addClass( 'excerpt-block' ):addClass( class )
return block:node( styles ):node( hat ):node( excerpt ):node( more )
end
 
-- Entry points for backwards compatibility
function p.lead( frame ) return p.main( frame ) end
function p.excerpt( frame ) return p.main( frame ) end
 
return p