Module:ParseList: Difference between revisions

From Baldur's Gate 3 Wiki
Jump to navigation Jump to search
(Undo revision 20568 by Taylan (talk))
Tag: Undo
No edit summary
Line 2: Line 2:
local p = {}
local p = {}


-- Config options, these may be overwritten in the main function
-- Config options, may be overwritten in the main function
local delimiter = ","
local listDelimiters = ","
local makeLinks = false
local makeElementsLinks = false
local pluralize = false
local pluralizeElements = false
local joinWord = "and"
local finalJoiningWord = "and"


local function splitString(str)
function p.main(frame)
     local values = {}
local args = getArgs(frame, { frameOnly = true })
     for value in str:gmatch("[^" .. delimiter .. "]+") do
return p._main(args)
         table.insert(values, value:match("^%s*(.-)%s*$"))
end
 
function p._main(args)
listDelimiters = args['delimiter'] or listDelimiters
makeElementsLinks = args['makeLinks'] or makeElementsLinks
pluralizeElements = args['pluralize'] or pluralizeElements
finalJoiningWord = args['joiningWord'] or finalJoiningWord
local type = args['type'] or 'sentence'
local converter = converters[type]
local elements = {}
for str in splitListString(args[1]) do
table.insert(elements, processElement(str))
end
return converter(elements)
end
 
local function splitListString(listString)
     local strings = {}
     for str in listString:gmatch("[^" .. listDelimiters .. "]+") do
         table.insert(strings, str:match("^%s*(.-)%s*$"))
     end
     end
     return values
     return strings
end
end


Line 23: Line 43:
}
}


local function processValue(str)
local function processElement(str)
if pluralize then
if pluralizeElements then
local plural = specialPlurals[str] or str .. "s"
local plural = specialPlurals[str] or str .. "s"
if makeLinks then
if makeElementsLinks then
return "[[" .. str .. "|" .. plural .. "]]"
return "[[" .. str .. "|" .. plural .. "]]"
else
else
Line 32: Line 52:
end
end
else
else
if makeLinks then
if makeElementsLinks then
return "[[" .. str .. "]]"
return "[[" .. str .. "]]"
else
else
Line 40: Line 60:
end
end


local function processList(listString)
local converters = {
local values = splitString(listString)
sentence = function (elements)
    local count = #values
    local count = #elements
    if count == 0 then
    if count == 0 then
    return ""
    return ""
    elseif count == 1 then
    elseif count == 1 then
    return processValue(values[1])
    return elements[1]
    elseif count == 2 then
    elseif count == 2 then
    local v1 = processValue(values[1])
return elements[1] .. " " .. finalJoiningWord .. " " .. elements[2]
    local v2 = processValue(values[2])
    end
return v1 .. " " .. joinWord .. " " .. v2
    local result = ""
    end
for i, str in ipairs(elements) do
    local result = ""
if i < count then
for i, value in ipairs(values) do
result = result .. str .. ", "
local v = processValue(value, linked)
else
if i < count then
result = result .. finalJoiningWord .. " " .. str
result = result .. v .. ", "
end
else
    end
result = result .. joinWord .. " " .. v
    return result
end,
htmlList = function (elements)
local result = ""
for str in elements do
result = result .. "* " .. str
end
end,
simpleList = function (elements)
local result = ""
local first = true
for str in elements do
if first then
result = str
first = false
else
result = result .. ", " .. str
end
end
end
    end
return result
    return result
end,
end
}
 
function p.toSentence(frame)
local args = getArgs(frame, { frameOnly = true })
delimiter = args['delimiter'] or delimiter
makeLinks = args['makeLinks'] or makeLinks
pluralize = args['pluralize'] or pluralize
joinWord = args['joinWord'] or joinWord
return processList(frame.args[1])
end


return p
return p

Revision as of 23:33, 23 July 2023

Doc page: Module:ParseList/doc

This module allows you to process a list of values (separated by a comma by default) and display them in a variety of useful formats.

{{#invoke:ParseList|main|Fighter, Cleric, Wizard}}

Becomes:

Lua error at line 22: attempt to index global 'converters' (a nil value).

Parameters

The first parameter is the list to be processed. The remaining parameters are optional:

parameter default meaning
style 'text' The desired output format, see below for possibilities.
type Deprecated synonym for style.
delimiter ',' The character that should serve as a delimiter of the input list. This can actually be a string of multiple characters which will all work as a delimiter, but that usage is discouraged as it may cause confusion.
makePlural (empty) If provided and not blank, means that each element will be turned into a plural word.
makeSingular (empty) If provided and not blank, means that each element will be turned into singular.
makeLowercase (empty) If provided and not blank, means that each element will be made all-lowercase.
makeLinks (empty)

If provided and not blank, means that each element will be made into a link.

If any transforms like plural, singular, or lowercase were specified, the original element will be the link destination, and the transformed version the link text. E.g., the list element "Apple" would become [[Apple|apples]] if makePlural, makeLowercase, and makeLinks were provided.

useTemplate (empty)

If provided, will use the provided template on each element, i.e., turn every X into {{Template|X}}.

This is done after any transforms like plural, singular and lowercase. E.g., the list element "Apple" would become {{Template|apples}} if makePlural, makeLowercase, and useTemplate were provided.

useTemplate2 (empty)

If provided, will use the provided template on each element and its transformed form as two template arguments.

E.g., the list element "Apple" would become {{Template|Apple|apples}} if makePlural, makeLowercase, and useTemplate were provided.

textDelim ', '

If style is text, this is used as the delimiter for the output list.

Setting this causes the default value for textLastDelim to be changed to the same value, since the default value of textLastDelim is unlikely to be useful with anything other than the default value of textDelim.

Note that the default value is not just a comma; it's a comma followed by a space character.

textLastDelim (conditional)

If style is text, this is used as the delimiter between the last two elements of the output list.

If textDelim is not set (i.e., left at its default value), then the default value for this is ', and ' such that an input list "X, Y, Z" would be turned into "X, Y, and Z". You could leave textDelim unset, but set textLastDelim = <nowiki>, or </nowiki> to get the output "X, Y, or Z" instead.

If textDelim is set, then this defaults to the same value as that. E.g., specifying textDelim = <nowiki> + </nowiki> would transform the input list "X, Y, Z" into "X + Y + Z" without needing to also change the value of textLastDelim.

Note: The use of <nowiki> here is to prevent leading and trailing space characters from being ignored when setting textDelim or textLastDelim to a string that should start and/or end with a space character.

The automatic plural/singular transforms are able to recognize certain irregular plurals and handle them correctly, such as "thieves" and "quarterstaves." This is handled partly through a table of explicit special conversions like staff -> staves, and partly through logical rules like ...f --> ...ves. If you find a word that produces a wrong result, it should be added to the table of explicit conversions.

The makeLink, useTemplate, and useTemplate2 arguments are mutually exclusive; when several are provided, only the first will take effect.

Output types

The value of the type parameter can be the following:

value example output
text Lua error at line 22: attempt to index global 'converters' (a nil value).
htmlList Lua error at line 22: attempt to index global 'converters' (a nil value).
htmlListNoBullets Lua error at line 22: attempt to index global 'converters' (a nil value).
simpleList Lua error at line 22: attempt to index global 'converters' (a nil value).

Examples

Make Links

{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes }}

Result:

Lua error at line 22: attempt to index global 'converters' (a nil value).

Make plural

{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makePlural = yes }}

Result:

Lua error at line 22: attempt to index global 'converters' (a nil value).

Make Plural Links

{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes }}

Result:

Lua error at line 22: attempt to index global 'converters' (a nil value).

Make Plural Links, end with "or"

{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | textLastDelim = <nowiki>, or </nowiki> }}

Result:

Lua error at line 22: attempt to index global 'converters' (a nil value).

Make Plural Links, turn into HTML list

{{#invoke: ParseList | main | Fighter, Cleric, Wizard | makeLinks = yes | makePlural = yes | type = htmlList }}

Result:

Lua error at line 22: attempt to index global 'converters' (a nil value).


local getArgs = require('Module:Arguments').getArgs
local p = {}

-- Config options, may be overwritten in the main function
local listDelimiters = ","
local makeElementsLinks = false
local pluralizeElements = false
local finalJoiningWord = "and"

function p.main(frame)
	local args = getArgs(frame, { frameOnly = true })
	return p._main(args)
end

function p._main(args)
	listDelimiters = args['delimiter'] or listDelimiters
	makeElementsLinks = args['makeLinks'] or makeElementsLinks
	pluralizeElements = args['pluralize'] or pluralizeElements
	finalJoiningWord = args['joiningWord'] or finalJoiningWord
	
	local type = args['type'] or 'sentence'
	local converter = converters[type]
	local elements = {}
	for str in splitListString(args[1]) do
		table.insert(elements, processElement(str))
	end
	return converter(elements)
end

local function splitListString(listString)
    local strings = {}
    for str in listString:gmatch("[^" .. listDelimiters .. "]+") do
        table.insert(strings, str:match("^%s*(.-)%s*$"))
    end
    return strings
end

local specialPlurals = {
	Thief = "Thieves",
	thief = "thieves",
	Quarterstaff = "Quarterstaves",
	quarterstaff = "quarterstaves",
}

local function processElement(str)
	if pluralizeElements then
		local plural = specialPlurals[str] or str .. "s"
		if makeElementsLinks then
			return "[[" .. str .. "|" .. plural .. "]]"
		else
			return plural
		end
	else
		if makeElementsLinks then
			return "[[" .. str .. "]]"
		else
			return str
		end
	end
end

local converters = {
	sentence = function (elements)
	    local count = #elements
	    if count == 0 then
	    	return ""
	    elseif count == 1 then
	    	return elements[1]
	    elseif count == 2 then
			return elements[1] .. " " .. finalJoiningWord .. " " .. elements[2]
	    end
	    local result = ""
		for i, str in ipairs(elements) do
			if i < count then
				result = result .. str .. ", "
			else
				result = result .. finalJoiningWord .. " " .. str
			end
	    end
	    return result
	end,
	htmlList = function (elements)
		local result = ""
		for str in elements do
			result = result .. "* " .. str
		end
	end,
	simpleList = function (elements)
		local result = ""
		local first = true
		for str in elements do
			if first then
				result = str
				first = false
			else
				result = result .. ", " .. str
			end
		end
		return result
	end,
}

return p