Module:Excerpt/ja: Difference between revisions
Created page with "-- Module:Excerpt implements the Excerpt template -- Documentation https://www.mediawiki.org/wiki/Module:Excerpt -- By User:Sophivorus, User:Certes & others -- Version 1.5 -- License CC BY-SA-3.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 = {} -- Helper function to get arguments local args function getArg( key, default..." |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
-- Module:Excerpt implements the Excerpt template | -- Module:Excerpt implements the Excerpt template | ||
-- Documentation https:// | -- Documentation and master version: https://en.wikipedia.org/wiki/Module:Excerpt | ||
-- | -- Authors: User:Sophivorus, User:Certes, User:Aidan9382 & others | ||
-- License: CC-BY-SA-3.0 | |||
-- License CC BY-SA-3.0 | |||
local Transcluder = require( 'Module:Transcluder' ) | local Transcluder = require( 'Module:Transcluder' ) | ||
Line 16: | Line 15: | ||
-- Helper function to get arguments | -- Helper function to get arguments | ||
local args | local args | ||
function getArg( key, default ) | local function getArg( key, default ) | ||
local value = args[ key ] | local value = args[ key ] | ||
if value and mw.text.trim( value ) ~= '' then | if value and mw.text.trim( value ) ~= '' then | ||
Line 25: | Line 24: | ||
-- Helper function to handle errors | -- Helper function to handle errors | ||
function getError( message, value ) | local function getError( message, value ) | ||
if type( message ) == 'string' then | if type( message ) == 'string' then | ||
message = Transcluder.getError( message, value ) | message = Transcluder.getError( message, value ) | ||
Line 36: | Line 35: | ||
-- Helper function to get localized messages | -- Helper function to get localized messages | ||
function getMessage( key ) | local function getMessage( key ) | ||
local ok, TNT = pcall( require, 'Module:TNT' ) | local ok, TNT = pcall( require, 'Module:TNT' ) | ||
if not ok then return key end | if not ok then return key end | ||
Line 42: | Line 41: | ||
end | end | ||
-- Main entry point for templates | |||
function p.main( frame ) | function p.main( frame ) | ||
args = Transcluder.parseArgs( frame ) | args = Transcluder.parseArgs( frame ) | ||
Line 47: | Line 47: | ||
-- Make sure the requested page exists | -- Make sure the requested page exists | ||
local page = getArg( 1 ) | local page = getArg( 1 ) | ||
if not page then return getError( 'no-page' ) end | if not page or page == '{{{1}}}' then return getError( 'no-page' ) end | ||
local title = mw.title.new(page) | local title = mw.title.new(page) | ||
if not title then return getError( ' | if not title then return getError( 'invalid-title', page ) end | ||
if title.isRedirect then title = title.redirectTarget end | if title.isRedirect then title = title.redirectTarget end | ||
if not title.exists then return getError( 'page-not-found', page ) end | if not title.exists then return getError( 'page-not-found', page ) end | ||
Line 70: | Line 70: | ||
local noBold = not yesno( getArg( 'bold' ) ) | local noBold = not yesno( getArg( 'bold' ) ) | ||
local onlyFreeFiles = yesno( getArg( 'onlyfreefiles', true ) ) | local onlyFreeFiles = yesno( getArg( 'onlyfreefiles', true ) ) | ||
local briefDates = yesno( getArg( 'briefdates', false ) ) | |||
local inline = yesno( getArg( 'inline' ) ) | local inline = yesno( getArg( 'inline' ) ) | ||
local quote = yesno( getArg( 'quote' ) ) | local quote = yesno( getArg( 'quote' ) ) | ||
local more = yesno( getArg( 'more' ) ) | local more = yesno( getArg( 'more' ) ) | ||
local class = getArg( 'class' ) | local class = getArg( 'class' ) | ||
local displaytitle = getArg( 'displaytitle' ) or page | |||
-- Build the hatnote | -- Build the hatnote | ||
Line 87: | Line 89: | ||
end | end | ||
hat = hat .. ' ' .. getMessage( 'excerpt' ) .. ' ' | hat = hat .. ' ' .. getMessage( 'excerpt' ) .. ' ' | ||
if section | if section then | ||
hat = hat .. '[[:' .. page .. '#' .. mw.uri.anchorEncode( section ) .. '|' .. | hat = hat .. '[[:' .. page .. '#' .. mw.uri.anchorEncode( section ) .. '|' .. displaytitle | ||
.. ' § ' .. mw.ustring.gsub( section, '%[%[([^]|]+)|?[^]]*%]%]', '%1' ) .. ']].' -- remove nested links | .. ' § ' .. mw.ustring.gsub( section, '%[%[([^]|]+)|?[^]]*%]%]', '%1' ) .. ']].' -- remove nested links | ||
else | else | ||
hat = hat .. '[[:' .. page .. '|' .. | hat = hat .. '[[:' .. page .. '|' .. displaytitle .. ']].' | ||
end | end | ||
if edit then | if edit then | ||
Line 141: | Line 143: | ||
if mw.text.trim( excerpt ) == '' and not only then | if mw.text.trim( excerpt ) == '' and not only then | ||
if section then return getError( 'section-empty', section ) else return getError( 'lead-empty' ) end | 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 briefDates then | |||
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 | end | ||