内容协议 本文采取的内容协议为CC BY-SA 4.0
|
来自ZH Wikipedia
|
模板:Uses Lua 相对英文版做出了一些本地化调整,增加了link、noerror参数等。
This module implements Template:Excerpt.
- See Template:Excerpt for documentation on the template parameters
- See Module:Excerpt/config for configuration of this module
搬运自中文维基百科。
-- Please DO NOT directly sync with the internationalized version
-- Module:Excerpt implements the Excerpt template
-- Documentation https://www.mediawiki.org/wiki/Module:Excerpt
-- By User:Sophivorus, User:Certes & others
-- Version 100.0 Chinese Version
-- License CC-BY-SA-4.0
local Transcluder = require('Module:Transcluder')
local yesno = require('Module:Yesno')
local ok, config = pcall(require, 'Module:Excerpt/config')
if not ok then config = {} end
local p = {}
local args
function getArg(key, default)
local value = args[key]
if value and mw.text.trim(value) ~= '' then
return value
end
return default
end
function getError(message, value, noError)
if noError then return "" end
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
end
function getMessage(key)
local ok, TNT = pcall(require, 'Module:TNT')
if not ok then return key end
local ok2, msg = pcall(TNT.format, TNT, 'I18n/Module:Excerpt.tab', key)
if not ok2 then return key end
return msg
end
function p.main(frame)
args = Transcluder.parseArgs(frame)
local page = getArg(1, getArg('article') )
local noError = yesno( getArg('noerror', false) )
if not page then return getError('no-page', nil, noError) 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, noError) end
page = title.prefixedText
local link=getArg('link')
local fragment = getArg('fragment')
local section = fragment or getArg(2, getArg('section',mw.ustring.match( getArg(1, getArg('article') ), '[^#]+#(.+)') ) )
local dd = yesno( getArg('dd', false) )
local hat = yesno( getArg('hat', true) ) or dd
local edit = yesno( getArg('edit', true) )
local this = getArg('this')
local only = getArg('only') or (dd and 'paragraphs') or nil
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 templates = getArg('templates', getArg('template', ( only == 'template' and 1 ) ) )
local paragraphs = getArg('paragraphs', getArg('paragraph', ( only == 'paragraph' and 1 ) ) )
local references = getArg('references')
local subsections = not yesno( getArg('subsections') )
local noBold = not yesno( getArg('bold') )
local freefiles = yesno( getArg('freefiles') )
local inline = yesno( getArg('inline') )
local quote = yesno( getArg('quote') )
local more = yesno( getArg('more') )
local class = getArg('class')
if hat and not inline then
if this then hat = this
elseif quote then hat = getMessage('this')
elseif only then hat = getMessage(only)
else hat = getMessage('section') end
hat = hat .. getMessage('excerpt')
if link then hat = hat .. link
elseif section and not fragment then
hat = hat .. '[[:' .. page .. '#' .. mw.uri.anchorEncode(section) .. '|' .. page .. ' § ' .. mw.ustring.gsub(section, '%[%[([^]|]+)|?[^]]*%]%]', '%1') .. ']]'
else hat = hat .. '[[:' .. page .. '|' .. page .. ']]' end
if edit then
hat = hat .. '<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span>[' .. title:fullUrl('action=edit') .. ' ' .. mw.message.new('editsection'):plain() .. ']<span class="mw-editsection-bracket">]</span></span>'
end
if config.hat then hat = config.hat .. hat .. '}}'; hat = frame:preprocess(hat)
else hat = mw.html.create('div'):addClass('dablink excerpt-hat'):wikitext(hat) end
else hat = nil end
if more and not inline then
more = "'''[[" .. page .. '#' .. (section or '') .. "|" .. getMessage('more') .. "]]'''"
more = mw.html.create('div'):addClass('noprint excerpt-more'):wikitext(more)
else more = nil end
local options = {
files = files, lists = lists, tables = tables, paragraphs = paragraphs,
sections = subsections, categories = 0, references = references,
only = only and mw.text.trim(only, 's') .. 's', noBold = noBold,
noSelfLinks = true, noNonFreeFiles = freefiles,
noBehaviorSwitches = true, fixReferences = true, linkBold = true,
}
local title2 = page .. '#' .. (section or '')
local ok2, excerpt = pcall(Transcluder.get, title2, options)
if not ok2 then return getError(excerpt, nil, noError) end
if mw.text.trim(excerpt) == '' and not only then
if section then return getError('section-empty', section, noError) else return getError('lead-empty', nil, noError) end
end
local fileNamespaces = Transcluder.getNamespaces('File')
if ((only == 'file' or only == 'files') or (not only and (files ~= '0' or not files))) and not Transcluder.matchAny(excerpt, '%[%[', fileNamespaces, ':') and config.captions then
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
captions = pair[2]
for _, p2 in pairs(captions) do if parameters[p2] then caption = parameters[p2] break end end
excerpt = '[[File:' .. file .. '|thumb|' .. (caption or '') .. ']]' .. excerpt
if freefiles then excerpt = Transcluder.removeNonFreeFiles(excerpt) end
break
end
end
end
local trash
if only and (only == 'template' or only == 'templates') then trash, excerpt = Transcluder.getTemplates(excerpt, templates)
else
local blacklist = config.blacklist and table.concat(config.blacklist, ',') or ''
if templates then
if string.sub(templates, 1, 1) == '-' then blacklist = templates .. ',' .. blacklist
else blacklist = templates end
else blacklist = '-' .. blacklist end
trash, excerpt = Transcluder.getTemplates(excerpt, blacklist)
end
local oneparexcerpt=false
excerpt = mw.text.trim(excerpt)
if dd then
excerpt = string.gsub(excerpt, '\n\n+', '\n'); excerpt = string.gsub(excerpt, '\n', '\n:')
else
excerpt = string.gsub(excerpt, '\n\n\n+', '\n\n')
if string.find(excerpt, '\n\n')==nil then oneparexcerpt=true end
excerpt = '\n' .. excerpt .. '\n'
end
excerpt = frame:preprocess(excerpt)
local categories, excerpt2 = Transcluder.getCategories(excerpt, options.categories); excerpt = excerpt2
if config.categories then
local contentCategory = config.categories.content
if contentCategory and mw.title.getCurrentTitle().isContentPage then excerpt = excerpt .. '[[Category:' .. contentCategory .. ']]' end
local namespaceCategory = config.categories[ mw.title.getCurrentTitle().namespace ]
if namespaceCategory then excerpt = excerpt .. '[[Category:' .. namespaceCategory .. ']]' end
end
excerpt = excerpt .. '\n<span class="nothing"></span>'
if dd then return mw.html.create('dd'):wikitext(excerpt) end
local styles
if config.styles then styles = frame:extensionTag('templatestyles', '', { src = config.styles }) end
local createp=false
local tag1 = 'div'; local tag2 = 'div'
if inline then tag1 = 'span'; tag2 = 'span'
elseif quote then tag2 = 'blockquote'
elseif oneparexcerpt then createp=true end
if createp then excerpt = mw.html.create('p'):wikitext(excerpt); excerpt = mw.html.create(tag1):addClass('excerpt'):node(excerpt)
else excerpt = mw.html.create(tag1):addClass('excerpt'):wikitext(excerpt) end
local block = mw.html.create(tag2):addClass('excerpt-block'):addClass(class)
return block:node(styles):node(hat):node(excerpt):node(more)
end
function p.lead(frame) return p.main(frame) end
function p.excerpt(frame) return p.main(frame) end
return p
