Module:List: Difference between revisions

From The Wiki Camp 2 Jr.
Jump to navigation Jump to search
(Created page with "-- This module requires the use of Module:List. local list = require('Module:List') local p = {} -- Local function which is used to get a correctly formatted entry. -- Function checks if the array had a value added by checking the counter, -- and returns the relevant result. local function getFormattedEntry(args, counter) if (counter == 1) then -- Check if the counter stayed the same. return "" -- Nothing was added to array; R...")
 
No edit summary
 
Line 1: Line 1:
-- This module requires the use of Module:List.
local libUtil = require('libraryUtil')
local list = require('Module:List')
local checkType = libUtil.checkType
local mTableTools = require('Module:TableTools')


local p = {}
local p = {}


-- Local function which is used to get a correctly formatted entry.
local listTypes = {
-- Function checks if the array had a value added by checking the counter,
['bulleted'] = true,
-- and returns the relevant result.
['unbulleted'] = true,
local function getFormattedEntry(args, counter)
['horizontal'] = true,
if (counter == 1) then -- Check if the counter stayed the same.
['ordered'] = true,
return "" -- Nothing was added to array; Return empty string.
['horizontal_ordered'] = true
elseif (counter == 2) then -- Check if only one value was added to the array.
}
return args[1] -- Only one value was added to array; Return that value.
 
else -- The array had more than one value added.
function p.makeListData(listType, args)
return list.makeList("unbulleted", args) -- Call list.makeList() to retrieve the formatted plainlist.
-- Constructs a data table to be passed to p.renderList.
local data = {}
 
-- Classes and TemplateStyles
data.classes = {}
data.templatestyles = ''
if listType == 'horizontal' or listType == 'horizontal_ordered' then
table.insert(data.classes, 'hlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Hlist/styles.css' }
}
elseif listType == 'unbulleted' then
table.insert(data.classes, 'plainlist')
data.templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Plainlist/styles.css' }
}
end
end
end
table.insert(data.classes, args.class)


--[[
-- Main div style
Local function which is used to format an appearance for a comic book,
data.style = args.style
in the style of:
Line 1: <comic book title> #<issue number> (with comic book title in italics)
Line 2: <release date>


For other usages, see createGenericEntry().
-- Indent for horizontal lists
 
if listType == 'horizontal' or listType == 'horizontal_ordered' then
The function works with the following combinations:
local indent = tonumber(args.indent)
-- Only comic book title (example: "The Incredible Hulk").
indent = indent and indent * 1.6 or 0
-- Title and issue number (example: "The Incredible Hulk" and "181").
if indent > 0 then
-- Title and release date (example: "The Incredible Hulk and "November 1974").
data.marginLeft = indent .. 'em'
-- Title, issue number and release date (example: "The Incredible Hulk", "181" and "November 1974").
end
end
-- Only release date (example: "November 1974").
-- List style types for ordered lists
--]]
-- This could be "1, 2, 3", "a, b, c", or a number of others. The list style
local function createComicEntry(appearanceMajor, appearanceMinor, appearanceDate)
-- type is either set by the "type" attribute or the "list-style-type" CSS
local fullString = {} -- Variable to save the array.
-- property.
local counter = 1 -- Variable to save the array counter.
if listType == 'ordered' or listType == 'horizontal_ordered' then  
data.listStyleType = args.list_style_type or args['list-style-type']
if (appearanceMajor ~= nil) then -- Check if a comic book title was entered.
data.type = args['type']


if (appearanceMinor == nil) then -- A comic book title was entered; Check if a issue number was entered.
-- Detect invalid type attributes and attempt to convert them to
fullString[counter] = appearanceMajor -- A issue was not entered; Add only the comic book title to the array.
-- list-style-type CSS properties.
counter = counter + 1 -- Increment counter by one.
if data.type
else
and not data.listStyleType
fullString[counter] = appearanceMajor .. " " .. appearanceMinor -- A issue was entered; Add both to the array.
and not tostring(data.type):find('^%s*[1AaIi]%s*$')
counter = counter + 1 -- Increment counter by one.
then
data.listStyleType = data.type
data.type = nil
end
end
end
end
if (appearanceDate ~= nil) then -- Check if a release date was entered.
-- List tag type
fullString[counter] = appearanceDate -- A release date was entered; Add it to the array.
if listType == 'ordered' or listType == 'horizontal_ordered' then
counter = counter + 1 -- Increment counter by one.
data.listTag = 'ol'
else
data.listTag = 'ul'
end
end


return getFormattedEntry(fullString, counter) -- Call getFormattedEntry() to get a correctly formatted entry.
-- Start number for ordered lists
end
data.start = args.start
if listType == 'horizontal_ordered' then
-- Apply fix to get start numbers working with horizontal ordered lists.
local startNum = tonumber(data.start)
if startNum then
data.counterReset = 'listitem ' .. tostring(startNum - 1)
end
end


--[[
-- List style
Local function which is used to format an appearance for most usages,
-- ul_style and ol_style are included for backwards compatibility. No
including television, film, books, songs and games, in the style of:
-- distinction is made for ordered or unordered lists.
Line 1: <minor work title> (in quotes) (Minor works include: TV episodes, chapters, songs and game missions)
data.listStyle = args.list_style
Line 2: <major work title> (in italics) (Major works include: TV series, films, books, albums and games)
Line 3: <release date>


For comic book usages, see createComicEntry().
-- List items
-- li_style is included for backwards compatibility. item_style was included
-- to be easier to understand for non-coders.
data.itemStyle = args.item_style or args.li_style
data.items = {}
for _, num in ipairs(mTableTools.numKeys(args)) do
local item = {}
item.content = args[num]
item.style = args['item' .. tostring(num) .. '_style']
or args['item_style' .. tostring(num)]
item.value = args['item' .. tostring(num) .. '_value']
or args['item_value' .. tostring(num)]
table.insert(data.items, item)
end
return data
end


The function works with the following combinations:
function p.renderList(data)
-- Only minor work title (example: "Live Together, Die Alone").
-- Renders the list HTML.
-- Minor work title and major work title (example: "Live Together, Die Alone" and "Lost").
-- Minor work title and release date (example: "Live Together, Die Alone" and "May 24, 2006").
-- Minor work title, major work title and release date (example: "Live Together, Die Alone", "Lost" and "May 24, 2006").
-- Only major work title (example: "Lost").
-- Return the blank string if there are no list items.
-- major work title and release date (example: "Lost" and "May 24, 2006").
if type(data.items) ~= 'table' or #data.items < 1 then
return ''
-- Only release date (example: "May 24, 2006").
--]]
local function createGenericEntry(appearanceMajor, appearanceMinor, appearanceDate)
local fullString = {} -- Variable to save the array.
local counter = 1 -- Variable to save the array counter.
if (appearanceMinor ~= nil) then -- Check if a minor appearance was entered.
fullString[counter] = appearanceMinor -- A minor appearance was entered; Add it to the array.
counter = counter + 1 -- Increment counter by one.
end
end
if (appearanceMajor ~= nil) then -- Check if a major appearance was entered.
-- Render the main div tag.
fullString[counter] = appearanceMajor -- A major appearance was entered; Add it to the array.
local root = mw.html.create('div')
counter = counter + 1 -- Increment counter by one.
for _, class in ipairs(data.classes or {}) do
root:addClass(class)
end
end
root:css{['margin-left'] = data.marginLeft}
if (appearanceDate ~= nil) then -- Check if a release date was entered.
if data.style then
fullString[counter] = appearanceDate -- A release date was entered; Add it to the array.
root:cssText(data.style)
counter = counter + 1 -- Increment counter by one.
end
end


return getFormattedEntry(fullString, counter) -- Call getFormattedEntry() to get a correctly formatted entry.
-- Render the list tag.
end
local list = root:tag(data.listTag or 'ul')
list
:attr{start = data.start, type = data.type}
:css{
['counter-reset'] = data.counterReset,
['list-style-type'] = data.listStyleType
}
if data.listStyle then
list:cssText(data.listStyle)
end


-- Local function which is used to format with a hash symbol comic book issues.
-- Render the list items
-- For other minor works, see getFormattedGenericMinorWork().
for _, t in ipairs(data.items or {}) do
local function getFormattedComicMinorWorkTitle(issue)
local item = list:tag('li')
if (issue ~= nil) then -- Check if the issue is not nil.
if data.itemStyle then
if (string.find(issue, "#")) then -- Check if the issue already has a hash symbol.
item:cssText(data.itemStyle)
return issue -- Hash symbol already present; Return issue.
end
else
if t.style then
local formattedString = string.gsub(issue, "%d+", "#%1") -- Hash symbol not found; Add the symbol before the issue number.
item:cssText(t.style)
return formattedString -- Return issue.
end
end
else
item
return nil -- issue is nil; Return nil.
:attr{value = t.value}
:wikitext(t.content)
end
end
return data.templatestyles .. tostring(root)
end
end


-- Local function which is used to format with quotes a minor work title of most types.
function p.renderTrackingCategories(args)
-- For comic book issues, see getFormattedComicMinorWork() (see [MOS:MINORWORK]).
local isDeprecated = false -- Tracks deprecated parameters.
local function getFormattedGenericMinorWorkTitle(title)
for k, v in pairs(args) do
if (title ~= nil) then -- Check if the title is not nil.
k = tostring(k)
return "\"" .. title .. "\"" -- Title is not nil; Add quotes to the title.
if k:find('^item_style%d+$') or k:find('^item_value%d+$') then
else
isDeprecated = true
return nil -- Title is nil; Return nil.
break
end
end
local ret = ''
if isDeprecated then
ret = ret .. '[[Category:List templates with deprecated parameters]]'
end
end
return ret
end
end


-- Local function which is used to format with italics a major work title (see [MOS:MAJORWORK]).
function p.makeList(listType, args)
local function getFormattedMajorWorkTitle(title)
if not listType or not listTypes[listType] then
if (title ~= nil) then -- Check if the title is not nil.
error(string.format(
return "''" .. title .. "''" -- Title is not nil; Add italics to the title.
"bad argument #1 to 'makeList' ('%s' is not a valid list type)",
else
tostring(listType)
return nil -- Title is nil; Return nil.
), 2)
end
end
checkType('makeList', 2, args, 'table')
local data = p.makeListData(listType, args)
local list = p.renderList(data)
local trackingCategories = p.renderTrackingCategories(args)
return list .. trackingCategories
end
end


-- Local function which does the actual main process.
for listType in pairs(listTypes) do
local function _getFormattedAppearance(args)
p[listType] = function (frame)
local appearanceMajor = args['major_work'] -- Get the title of the major work.
local mArguments = require('Module:Arguments')
local appearanceMinor = args['minor_work'] -- Get the title of the minor work.
local origArgs = mArguments.getArgs(frame, {
valueFunc = function (key, value)
local isComic = false -- Variable to save the status of wether the appearence is from a comic book.
if not value or not mw.ustring.find(value, '%S') then return nil end
if (args['issue'] ~= nil) then -- Check if the comic specific issue is not nil.
if mw.ustring.find(value, '^%s*[%*#;:]') then
appearanceMinor = args['issue'] -- Issue is not nil; Get the issue number.
return value
isComic = true -- Set isComic to true.
else
return value:match('^%s*(.-)%s*$')
end
return nil
end
})
-- Copy all the arguments to a new table, for faster indexing.
local args = {}
for k, v in pairs(origArgs) do
args[k] = v
end
return p.makeList(listType, args)
end
end
local appearanceDate = args['date'] -- Get the release date of the minor work.
local formattedAppearanceMajor = getFormattedMajorWorkTitle(appearanceMajor) -- Call getFormattedMajorWorkTitle() to get a formatted major work title.
if (isComic == false) then -- Check if the appearance is a comic book appearance.
-- The appearance is not a comic book appearance;
local formattedAppearanceMinor = getFormattedGenericMinorWorkTitle(appearanceMinor) -- Call getFormattedGenericMinorWorkTitle() to get a formatted minor work title.
return createGenericEntry(formattedAppearanceMajor, formattedAppearanceMinor, appearanceDate) -- Call createGenericEntry() to create an appearance entry.
else
-- The appearance is a comic book appearance.
local formattedAppearanceMinor = getFormattedComicMinorWorkTitle(appearanceMinor) -- Call getFormattedComicMinorWorkTitle() to get a formatted minor work title.
return createComicEntry(formattedAppearanceMajor, formattedAppearanceMinor, appearanceDate) -- Call createComicEntry() to create a comic book appearance entry.
end
end
--[[
Public function which is used to format the |first_appeared= and |last_appeared= fields.
The usage of this module allows for correct title formatting (see [MOS:MAJORWORK] and [MOS:MINORWORK]),
and correct line breaks based on guidelines (see [WP:UBLIST]).
Parameters:
-- |major_work= — optional; The title of the major work the fictional element appeared in.
Major works include TV series, films, books, albums and games.
-- |minor_work= — optional; The title of the minor work the fictional element appeared in.
Minor works include TV episodes, chapters, songs and game missions.
-- |issue= — optional; The number of the comic book issue the fictional element appeared in.
-- |date= — optional; The date of the publication/release of the minor work where the fictional element appeared in.
--]]
function p.getFormattedAppearance(frame)
local getArgs = require('Module:Arguments').getArgs -- Use Module:Arguments to access module arguments.
local args = getArgs(frame) -- Get the arguments sent via the template.
return _getFormattedAppearance(args) -- Call _getFormattedAppearance() to perform the actual process.
end
end


return p
return p

Latest revision as of 22:26, 3 September 2024

Documentation for this module may be created at Module:List/doc

local libUtil = require('libraryUtil')
local checkType = libUtil.checkType
local mTableTools = require('Module:TableTools')

local p = {}

local listTypes = {
	['bulleted'] = true,
	['unbulleted'] = true,
	['horizontal'] = true,
	['ordered'] = true,
	['horizontal_ordered'] = true
}

function p.makeListData(listType, args)
	-- Constructs a data table to be passed to p.renderList.
	local data = {}

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

	-- Main div style
	data.style = args.style

	-- Indent for horizontal lists
	if listType == 'horizontal' or listType == 'horizontal_ordered' then
		local indent = tonumber(args.indent)
		indent = indent and indent * 1.6 or 0
		if indent > 0 then
			data.marginLeft = indent .. 'em'
		end
	end
	
	-- List style types for ordered lists
	-- This could be "1, 2, 3", "a, b, c", or a number of others. The list style
	-- type is either set by the "type" attribute or the "list-style-type" CSS
	-- property.
	if listType == 'ordered' or listType == 'horizontal_ordered' then 
		data.listStyleType = args.list_style_type or args['list-style-type']
		data.type = args['type']

		-- Detect invalid type attributes and attempt to convert them to
		-- list-style-type CSS properties.
		if data.type 
			and not data.listStyleType
			and not tostring(data.type):find('^%s*[1AaIi]%s*$')
		then
			data.listStyleType = data.type
			data.type = nil
		end
	end
	
	-- List tag type
	if listType == 'ordered' or listType == 'horizontal_ordered' then
		data.listTag = 'ol'
	else
		data.listTag = 'ul'
	end

	-- Start number for ordered lists
	data.start = args.start
	if listType == 'horizontal_ordered' then
		-- Apply fix to get start numbers working with horizontal ordered lists.
		local startNum = tonumber(data.start)
		if startNum then
			data.counterReset = 'listitem ' .. tostring(startNum - 1)
		end
	end

	-- List style
	 -- ul_style and ol_style are included for backwards compatibility. No
	 -- distinction is made for ordered or unordered lists.
	data.listStyle = args.list_style

	-- List items
	-- li_style is included for backwards compatibility. item_style was included
	-- to be easier to understand for non-coders.
	data.itemStyle = args.item_style or args.li_style
	data.items = {}
	for _, num in ipairs(mTableTools.numKeys(args)) do
		local item = {}
		item.content = args[num]
		item.style = args['item' .. tostring(num) .. '_style']
			or args['item_style' .. tostring(num)]
		item.value = args['item' .. tostring(num) .. '_value']
			or args['item_value' .. tostring(num)]
		table.insert(data.items, item)
	end
	
	return data
end

function p.renderList(data)
	-- Renders the list HTML.
	
	-- Return the blank string if there are no list items.
	if type(data.items) ~= 'table' or #data.items < 1 then
		return ''
	end
	
	-- 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.
	local list = root:tag(data.listTag or 'ul')
	list
		:attr{start = data.start, type = data.type}
		:css{
			['counter-reset'] = data.counterReset,
			['list-style-type'] = data.listStyleType
		}
	if data.listStyle then
		list:cssText(data.listStyle)
	end

	-- Render the list items
	for _, t in ipairs(data.items or {}) do
		local item = list:tag('li')
		if data.itemStyle then
			item:cssText(data.itemStyle)
		end
		if t.style then
			item:cssText(t.style)
		end
		item
			:attr{value = t.value}
			:wikitext(t.content)
	end

	return data.templatestyles .. tostring(root)
end

function p.renderTrackingCategories(args)
	local isDeprecated = false -- Tracks deprecated parameters.
	for k, v in pairs(args) do
		k = tostring(k)
		if k:find('^item_style%d+$') or k:find('^item_value%d+$') then
			isDeprecated = true
			break
		end
	end
	local ret = ''
	if isDeprecated then
		ret = ret .. '[[Category:List templates with deprecated parameters]]'
	end
	return ret
end

function p.makeList(listType, args)
	if not listType or not listTypes[listType] then
		error(string.format(
			"bad argument #1 to 'makeList' ('%s' is not a valid list type)",
			tostring(listType)
		), 2)
	end
	checkType('makeList', 2, args, 'table')
	local data = p.makeListData(listType, args)
	local list = p.renderList(data)
	local trackingCategories = p.renderTrackingCategories(args)
	return list .. trackingCategories
end

for listType in pairs(listTypes) do
	p[listType] = function (frame)
		local mArguments = require('Module:Arguments')
		local origArgs = mArguments.getArgs(frame, {
			valueFunc = function (key, value)
			if not value or not mw.ustring.find(value, '%S') then return nil end
			if mw.ustring.find(value, '^%s*[%*#;:]') then
				return value
			else
				return value:match('^%s*(.-)%s*$')
			end
			return nil
		end
		})
		-- Copy all the arguments to a new table, for faster indexing.
		local args = {}
		for k, v in pairs(origArgs) do
			args[k] = v
		end
		return p.makeList(listType, args)
	end
end

return p