Module:Hatnote list:修订间差异

删除的内容 添加的内容
enwiki>Nihiltres
Updated from sandbox: added support for punctuation collapse when text is italicized. The update's content includes changes by users Johnuniq, Dexxor, and Nihiltres.
 
IdleTap-bot​(留言 | 贡献)
导入自求闻百科
 
第9行:
 
local mArguments --initialize lazily
local mFormatLink = require('Module:Format link')
local mHatnote = require('Module:Hatnote')
local libraryUtil = require('libraryUtil')
第23行 ⟶ 第22行:
 
--default options table used across the list stringification functions
-- 本地化注意
local stringifyListDefaultOptions = {
conjunction = "and和",
separator = ",、",
altSeparator = ";;",
space = " ",
formatted = false,
boldfaced = false,
quotes = false
}
 
--Searches display text only▼
local function searchDisp(haystack, needle)▼
return string.find(
string.sub(haystack, (string.find(haystack, '|') or 0) + 1), needle▼
)▼
end▼
 
-- Stringifies a list generically; probably shouldn't be used directly
local function stringifyList(list, options)
-- Type-checks, defaults, and a shortcut
checkType("stringifyList", 1, list, "table")
第50行 ⟶ 第45行:
local s = options.space
-- Format the list if requested
if options.formatted then list = mHatnote.formatPages(unpack(list)) end
list = mFormatLink.formatPages(
{categorizeMissing = mHatnote.missingTargetCat}, list
)
end▼
-- Set the separator; if any item contains it, use the alternate separator
local separator = options.separator
▲ --Searchessearches display text only
▲local function searchDisp(haystackt, needlef)
▲ return string.find(string.sub(haystackt, (string.find(haystackt, '|') or 0) + 1), needlef)
▲ end
for k, v in pairs(list) do
-- 本地化注意
if options.boldfaced then
list[k] = '\'\'\'' .. list[k] .. '\'\'\''
▲ end
if options.quotes then
list[k] = '「' .. list[k] .. '」'
end▼
if searchDisp(v, separator) then
separator = options.altSeparator
第65行 ⟶ 第67行:
-- Set the conjunction, apply Oxford comma, and force a comma if #1 has "§"
local conjunction = s .. options.conjunction .. s
-- 本地化注意
if #list == 2 and searchDisp(list[1], "§") or #list > 2 then▼
conjunction = -- separator .. conjunction的结果是“、和”
▲ -- if #list == 2 and searchDisp(list[1], "§") or #list > 2 then
▲ end
-- conjunction = separator .. conjunction
-- end
-- Return the formatted string
return mw.text.listToText(list, separator .. s, conjunction)
第73行 ⟶ 第77行:
 
--DRY function
-- 本地化注意
function p.conjList (conj, list, fmt, bold, quo)
return stringifyList(list, {conjunction = conj, formatted = fmt, boldfaced = bold, quotes=quo})
end
 
-- Stringifies lists with "and" or "or"
-- 本地化注意
function p.andList (...) return p.conjList("and", ...) end▼
function p.orListandList (...) return p.conjList("or和", ...) end
▲function p.andListorList (...) return p.conjList("and或", ...) end
 
--------------------------------------------------------------------------------
第89行 ⟶ 第95行:
 
--default options table used across the forSee family of functions
-- 本地化注意
local forSeeDefaultOptions = {
andKeyword = 'and和',
title = mw.title.getCurrentTitle().text,
otherText = 'other uses其他用法',
forSeeForm = 'For 关于%s, see s,请见%s.。',
}
 
--Collapses duplicate punctuation at end of string, ignoring italics and links
-- 本地化注意
local function punctuationCollapse (text)
return text:match("[.?!]('?)%1(%]?)%2%.$") and text:sub(1, -2) or text
local replacements = {
["%.%.$"] = ".",
["%?%.$"] = "?",
["%!%.$"] = "!",
["%.%]%]%.$"] = ".]]",
["%?%]%]%.$"] = "?]]",
["%!%]%]%.$"] = "!]]",
["%。%。$"] = "。",
["%?%。$"] = "?",
["%!%。$"] = "!",
["%。%]%]%。$"] = "。]]",
["%?%]%]%。$"] = "?]]",
["%!%]%]%。$"] = "!]]"
▲ )}
for k, v in pairs(replacements) do text = string.gsub(text, k, v) end
return text
end
 
第141行 ⟶ 第164行:
-- until the "and"'s stop. Blanks are ignored: "1|and||and|3" → {1, 3}
while args[i + 2] == options.andKeyword do
if args[i + 3] then
table.insert(forRow.pages, args[i + 3])
end
第152行 ⟶ 第175行:
table.insert(forTable, forRow)
until terminated or i > maxArg
 
return forTable
end
第170行 ⟶ 第193行:
for k, v in pairs(forSeeTable) do
local useStr = v.use or options.otherText
-- 本地化注意
local pagesStr =
local pagesStr = p.andList(v.pages, true, true, true) or '「\'\'\'' .. mHatnote._formatLink(mHatnote.disambiguate(options.title)) .. '\'\'\'」'
mFormatLink._formatLink{
categorizeMissing = mHatnote.missingTargetCat,
link = mHatnote.disambiguate(options.title)
}
local forSeeStr = string.format(options.forSeeForm, useStr, pagesStr)
forSeeStr = punctuationCollapse(forSeeStr)
第181行 ⟶ 第200行:
end
end
if options.extratext then table.insert(strList, punctuationCollapse(options.extratext..'.。')) end
-- Return the concatenated list
return table.concat(strList, ' ')
end