65,679
editsMore actions
Added no margin version of htmlListNoBullets
No edit summary |
HiddenDragon (talk | contribs) (Added no margin version of htmlListNoBullets) |
||
(12 intermediate revisions by 2 users not shown) | |||
Line 9: | Line 9: | ||
local makeElementsLowercase | local makeElementsLowercase | ||
local useTemplateOnElements | local useTemplateOnElements | ||
local | local useTwoArgTemplateOnElements | ||
local textDelim | |||
local textLastDelim | |||
local function splitListString(listString) | local function splitListString(listString) | ||
local strings = {} | |||
for str in listString:gmatch("[^" .. listDelimiters .. "]+") do | |||
table.insert(strings, str:match("^%s*(.-)%s*$")) | |||
end | |||
return strings | |||
end | end | ||
Line 137: | Line 139: | ||
end | end | ||
-- Applies the | -- Applies the various per-element transforms. Frame is needed for template | ||
local function processElement(str) | -- expansion; it may be nil if template transforms won't be applied. | ||
local function processElement(str, frame) | |||
local original = str | local original = str | ||
if makeElementsPlural then | if makeElementsPlural then | ||
Line 150: | Line 153: | ||
if makeElementsLinks then | if makeElementsLinks then | ||
return "[[" .. original .. "|" .. str .. "]]" | return "[[" .. original .. "|" .. str .. "]]" | ||
elseif useTemplateOnElements then | |||
return frame:expandTemplate{ | |||
return | title = useTemplateOnElements, | ||
args = { str } | |||
} | |||
elseif useTwoArgTemplateOnElements then | |||
return frame:expandTemplate{ | |||
title = useTwoArgTemplateOnElements, | |||
args = { original, str } | |||
} | |||
end | end | ||
return str | return str | ||
end | end | ||
-- These functions implement | -- These functions implement different output styles. The elements will have | ||
-- already gone through processElement() at this point, so they only need to be | -- already gone through processElement() at this point, so they only need to be | ||
-- glued together to produce the desired | -- glued together to produce the desired style of listing format. | ||
local converters = { | local converters = { | ||
text = function (elements) | |||
local result = "" | |||
local count = #elements | |||
for i, str in ipairs(elements) do | for i, str in ipairs(elements) do | ||
if i < count then | if i == 1 then | ||
result = result .. | result = str | ||
elseif i < count then | |||
result = result .. textDelim .. str | |||
else | else | ||
result = result .. | result = result .. textLastDelim .. str | ||
end | end | ||
end | end | ||
return result | return result | ||
Line 197: | Line 195: | ||
result = result .. ", " .. str | result = result .. ", " .. str | ||
end | end | ||
end | |||
return result | |||
end, | |||
htmlList = function (elements) | |||
local result = "<ul>\n" | |||
for i, str in ipairs(elements) do | |||
result = result .. "<li>" .. str .. "</li>\n" | |||
end | |||
return result .. "</ul>" | |||
end, | |||
htmlListNoBullets = function (elements) | |||
local result = "<ul style='list-style: none;'>\n" | |||
for i, str in ipairs(elements) do | |||
result = result .. "<li>" .. str .. "</li>\n" | |||
end | |||
return result .. "</ul>" | |||
end, | |||
htmlListNoBulletsOrMargin = function (elements) | |||
local result = "<ul style='list-style: none; margin: 0'>\n" | |||
for i, str in ipairs(elements) do | |||
result = result .. "<li>" .. str .. "</li>\n" | |||
end | |||
return result .. "</ul>" | |||
end, | |||
tableList = function (elements) | |||
local result = "<div class=\"bg3wiki-tablelist\">" | |||
for i, str in ipairs(elements) do | |||
result = result .. str .. "\n" | |||
end | |||
return result .. "</div>" | |||
end, | |||
none = function (elements) | |||
local result = "" | |||
for i, str in ipairs(elements) do | |||
result = result .. str | |||
end | end | ||
return result | return result | ||
Line 204: | Line 237: | ||
function p.main(frame) | function p.main(frame) | ||
local args = getArgs(frame, { frameOnly = true }) | local args = getArgs(frame, { frameOnly = true }) | ||
return p._main(args) | return p._main(args, frame) | ||
end | end | ||
function p._main(args) | -- Frame is needed for template expansion; may be nil if the useTemplate and | ||
-- useTemplate2 args are nil. | |||
function p._main(args, frame) | |||
listDelimiters = args['delimiter'] or "," | listDelimiters = args['delimiter'] or "," | ||
makeElementsLinks = args['makeLinks'] | makeElementsLinks = args['makeLinks'] | ||
Line 214: | Line 249: | ||
makeElementsLowercase = args['makeLowercase'] | makeElementsLowercase = args['makeLowercase'] | ||
useTemplateOnElements = args['useTemplate'] | useTemplateOnElements = args['useTemplate'] | ||
useTwoArgTemplateOnElements = args['useTemplate2'] | |||
textDelim = args['textDelim'] | |||
if textDelim then | |||
textLastDelim = args['textLastDelim'] or textDelim | |||
else | |||
textDelim = ', ' | |||
textLastDelim = args['textLastDelim'] or ', and ' | |||
end | |||
local | local style = args['style'] or args['type'] or 'text' | ||
local converter = converters[ | local converter = converters[style] | ||
local elements = {} | local elements = {} | ||
local listString = args[1] | local listString = args[1] | ||
local strings = splitListString(listString) | local strings = splitListString(listString) | ||
for i, str in ipairs(strings) do | for i, str in ipairs(strings) do | ||
table.insert(elements, processElement(str)) | table.insert(elements, processElement(str, frame)) | ||
end | end | ||
return converter(elements) | return converter(elements) |