Module:List:修订间差异

zhwiki>Xiplus-abot
小 已更改“Module:List”的保护设置:​高風險模板:100025引用<!-- 機器人3 -->([编辑=仅允许管理员](无限期)[移动=仅允许管理员](无限期))
 
IdleTap-bot​(留言 | 贡献)
导入自求闻百科
 
第1行: 第1行:
local libUtil = require('libraryUtil')
-- This module outputs different kinds of lists. At the moment, bulleted,
local checkType = libUtil.checkType
-- unbulleted, horizontal, ordered, and horizontal ordered lists are supported.
local mTableTools = require('Module:TableTools')
 
-- local libUtil = require('libraryUtil')
-- local checkType = libUtil.checkType
local TableTools = require('Module:TableTools')


local p = {}
local p = {}
第10行: 第13行:
['horizontal'] = true,
['horizontal'] = true,
['ordered'] = true,
['ordered'] = true,
['horizontal_ordered'] = true
['horizontal_ordered'] = true,
concat = true,
list_to_text = true
}
}


第17行: 第22行:
local data = {}
local data = {}


-- Classes and TemplateStyles
-- Classes
data.classes = {}
data.classes = {}
data.templatestyles = ''
if listType == 'horizontal' or listType == 'horizontal_ordered' then
if listType == 'horizontal' or listType == 'horizontal_ordered' then
table.insert(data.classes, 'hlist')
table.insert(data.classes, 'hlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Hlist/styles.css' }
}
elseif listType == 'unbulleted' then
elseif listType == 'unbulleted' then
table.insert(data.classes, 'plainlist')
table.insert(data.classes, 'plainlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Plainlist/styles.css' }
}
end
end
table.insert(data.classes, args.class)
table.insert(data.classes, args.class)
-- Main div style
data.style = args.style


-- Indent for horizontal lists
-- Indent for horizontal lists
第50行: 第45行:
-- property.
-- property.
if listType == 'ordered' or listType == 'horizontal_ordered' then  
if listType == 'ordered' or listType == 'horizontal_ordered' then  
data.listStyleType = args.list_style_type or args['list-style-type']
data.listStyleType = args.liststyletype or args['list-style-type']
data.type = args['type']
data.type = args['type']


第67行: 第62行:
if listType == 'ordered' or listType == 'horizontal_ordered' then
if listType == 'ordered' or listType == 'horizontal_ordered' then
data.listTag = 'ol'
data.listTag = 'ol'
elseif listType == 'concat' or listType == 'list_to_text' then
data.listTag = false
else
else
data.listTag = 'ul'
data.listTag = 'ul'
第84行: 第81行:
-- ul_style and ol_style are included for backwards compatibility. No
-- ul_style and ol_style are included for backwards compatibility. No
-- distinction is made for ordered or unordered lists.
-- distinction is made for ordered or unordered lists.
data.listStyle = args.list_style
data.listStyle = args.liststyle or args.style


-- List items
-- List items
-- li_style is included for backwards compatibility. item_style was included
-- li_style is included for backwards compatibility. item_style was included
-- to be easier to understand for non-coders.
-- to be easier to understand for non-coders.
data.itemStyle = args.item_style or args.li_style
data.itemStyle = args.itemstyle
data.items = {}
data.items = {}
for _, num in ipairs(mTableTools.numKeys(args)) do
if #args == 0 then
table.insert(args, args.default)
end
for num, v in TableTools.sparseIpairs(args) do
local item = {}
local item = {}
item.content = args[num]
item.content = v
item.style = args['item' .. tostring(num) .. '_style']
item.style = args['item' .. tostring(num) .. 'style']
or args['item_style' .. tostring(num)]
item.value = args['item' .. tostring(num) .. 'value']
or args['li_style' .. tostring(num)]
item.value = args['item' .. tostring(num) .. '_value']
or args['item_value' .. tostring(num)]
or args['li_value' .. tostring(num)]
table.insert(data.items, item)
table.insert(data.items, item)
end
-- 列表修饰函数。修饰函数在渲染列表时才调用。
data.modify = false --初始化
if args.modification or args['function'] then
-- 指定调用一个模块对参数进行修改。
-- 如果没有指定函数名称,则将整个模块作为函数。
local module_name = args.modification
local function_name = args['function']
local success, target_module=pcall(require, module_name)
if success then
if function_name and type(target_module)=='table' then
data.modify = target_module[function_name]
elseif type(target_module)=='function'
or (getmetatable(target_module) or {}).__call then
data.modify = target_module
end
end
end
if args.call then
-- 指定一个模板,并且调用它。并且调用模板时,以列表项内容为参数。
-- 如果有第二个参数,取para作为第二个参数。
local template_name = args.call
local frame = frame or mw.getCurrentFrame()
local para = args.para
local call_args = {}
local content_key = args.content_as or 1
for k,v in pairs(args) do
-- 修复于 2023-6-15:数字键会导致此处出错
local key = type(k) == 'string' and k:match('^para_(.+)')
if key then call_args[key]=v end
end
function data.modify(content)
-- 修复于 2023-6-15:错误处理了content参数
if content_key then
call_args[content_key] = content
end
return frame:expandTemplate{
title = template_name,
args = call_args
}
end
end
--[[
if args.format then
-- 指定一个格式,并将其应用。
-- 注意:一个格式标记只能使用一次!
-- 由于使用多余一次会导致错误,故该函数暂时禁用。
function data.modify(content)
return string.format(args.format, content)
end
end
]]
if args.replacement then
-- 指定一串文本,将其%s替换为其内容。
-- 可以替换多次。
function data.modify(content)
return args.replacement:gsub('%%s',content)
end
end
if args.prefix or args.suffix then
-- 指定前缀和后缀,将其应用。
local prefix = args.prefix or ''
local suffix = args.suffix or ''
function data.modify(content)
return prefix .. content .. suffix
end
end
end
第115行: 第183行:
-- Render the main div tag.
-- Render the main div tag.
local root = mw.html.create('div')
-- 这里简化代码,不这样渲染
for _, class in ipairs(data.classes or {}) do
root:addClass(class)
end
root:css{['margin-left'] = data.marginLeft}
if data.style then
root:cssText(data.style)
end


-- Render the list tag.
-- Render the list tag.
local list = root:tag(data.listTag or 'ul')
local list = mw.html.create(data.listTag or 'ul')
for i, class in ipairs(data.classes or {}) do
list:addClass(class)
end
list
list
:attr{start = data.start, type = data.type}
:attr{start = data.start, type = data.type}
:css{
:css{
['counter-reset'] = data.counterReset,
['counter-reset'] = data.counterReset,
['list-style-type'] = data.listStyleType
['list-style-type'] = data.listStyleType,
['margin-left'] = data.marginLeft
}
}
if data.listStyle then
if data.listStyle then
list:cssText(data.listStyle)
list:cssText(data.listStyle)
end
end
 
local modify = type(data.modify) == 'function' and data.modify or nil
-- Render the list items
-- Render the list items
for _, t in ipairs(data.items or {}) do
for i, t in ipairs(data.items or {}) do
local item = list:tag('li')
local item = list:tag('li')
local content
if modify then
content = modify(t.content)
else
content = t.content
end
if data.itemStyle then
if data.itemStyle then
item:cssText(data.itemStyle)
item:cssText(data.itemStyle)
第147行: 第220行:
item
item
:attr{value = t.value}
:attr{value = t.value}
:wikitext(t.content)
:wikitext(content)
end
end
 
return data.templatestyles .. tostring(root)
return tostring(list)
end
end


function p.renderTrackingCategories(args)
function p.renderList2(data, listType, separation, conjunction)
local isDeprecated = false -- Tracks deprecated parameters.
local list = {}
for k, v in pairs(args) do
local modify = type(data.modify) == 'function' and data.modify or nil
k = tostring(k)
for i, t in ipairs(data.items or {}) do
if k:find('^item_style%d+$') or k:find('^item_value%d+$') then
local item
isDeprecated = true
local content
break
if modify then
content = modify(t.content)
else
content = t.content
end
if data.itemStyle or t.style then -- 这一项定义了样式
item = mw.html.create'span'
:wikitext(content)
:cssText(data.itemStyle)
:cssText(t.style)
table.insert(list, tostring(item))
else -- 这一项没有定义任何样式
table.insert(list, content)
end
end
end
end
local ret = ''
if listType=='concat' then
if isDeprecated then
if separation and conjunction then
ret = ret .. '[[Category:含有过期参数的列表模板]]'
return mw.text.listToText(list, separation, conjunction)
else
return table.concat(list, separation)
end
elseif listType=='list_to_text' then
return mw.text.listToText(list, separation, conjunction)
end
end
return ret
end
end


function p.makeList(listType, args)
function p.makeList(listType, args)
-- 如果未指定listType或listType指定不正确,取默认值bulleted
if not listType or not listTypes[listType] then
if not listType or not listTypes[listType] then
error(string.format(
listType = 'bulleted'
"bad argument #1 to 'makeList' ('%s'不是合适的列表类型)",
tostring(listType)
), 2)
end
end
checkType('makeList', 2, args, 'table')
-- checkType('makeList', 2, args, 'table')
mw.logObject(args, 'args')
local data = p.makeListData(listType, args)
local data = p.makeListData(listType, args)
local list = p.renderList(data)
mw.logObject(data, 'data')
local trackingCategories = p.renderTrackingCategories(args)
if listType == 'concat' or listType == 'list_to_text' then
return list .. trackingCategories
return p.renderList2(data,listType,args.separation or args.sep,args.conjunction or args.conj)
else
return p.renderList(data)
end
end
end


for listType in pairs(listTypes) do
for listType in pairs(listTypes) do
p[listType] = function (frame)
p[listType] = function (frame)
local mArguments = require('Module:Arguments')
-- local mArguments = require('Module:Arguments')
local origArgs = mArguments.getArgs(frame, {
local origArgs = frame.args
frameOnly = ((frame and frame.args and frame.args.frameonly or '') ~= ''),
local parentFrame = frame:getParent()
valueFunc = function (key, value)
local parentArgs = type(parentFrame)=='table'
if not value or not mw.ustring.find(value, '%S') then return nil end
and parentFrame.args or nil
if mw.ustring.find(value, '^%s*[%*#;:]') then
local arguments = origArgs.arguments or 'both'
return value
local trim = origArgs.trim ~= '0'
else
-- 关于此参数的说明:
return value:match('^%s*(.-)%s*$')
-- self(默认):只考虑通过#invoke调用的模块自身的参数
-- parent:使用调用了此模块的模板时传入的参数
-- template:数字参数为parent,其他参数为self
-- both:同时考虑二者,且模板参数优先,相当于parentFirst
-- Copy all the arguments to a new table, for faster indexing.
local args = {}
if arguments=='self' or arguments=='both' or arguments=='template' then
for k, v in pairs(origArgs) do
if arguments ~= 'template' or type(k)~='number' then
v = trim and mw.text.trim(v) or v
args[k] = (v ~= '' or not trim) and v or nil
end
end
end
return nil
end
end
})
if arguments=='parent' or arguments=='both' or arguments=='template' then
-- Copy all the arguments to a new table, for faster indexing.
for k,v in pairs(parentArgs) do
local args = {}
if arguments ~= 'template' or type(k)=='number' then
for k, v in pairs(origArgs) do
v = trim and mw.text.trim(v) or v
args[k] = v
args[k] = (v ~= '' or not trim) and v or nil
end
end
end
end
mw.logObject(listType, 'listType')
mw.logObject(args, 'args')
return p.makeList(listType, args)
return p.makeList(listType, args)
end
end