Module:Consecutive links: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
mNo edit summary
(big refactor, shouldn't break anything?)
Line 1: Line 1:
local p = {}
local p = {}
local text_util = require('Module:Text Utility')


function p.parse(args, brackets)
function p.split(splitstr, separator)
  local b = text_util.split(splitstr, separator)
  return b
end
 
function p.parse(args, type)
frame = mw.getCurrentFrame()
if args.args ~= nil then
if args.args ~= nil then
   args = args.args
   args = args.args
end
end
local separator = args[2]
local type = type or 'links'
local replacer = args[3]
local separator = '%s*' .. (args[2] or ',') .. '%s*'
local prefix = args[4]
local replacer = args[3] or ', '
local postfix = args[5]
local prefix = args[4] or ''
local linkprefix = args[6]
local postfix = args[5] or ''
local linkpostfix = args[7]
local linkprefix = args[6] or ''
local brackets = brackets or {'[[', ']]', ''}
local linkpostfix = args[7] or ''
 
local b = p.split(args[1], separator)
local returnstr = ''
local returnstrformat


local b = string.gsub(args[1], "%s*" .. separator .. "%s*", linkpostfix .. brackets[2] .. replacer .. brackets[1] .. linkprefix.. brackets[3])
if type == 'links' then
return prefix .. brackets[1] .. linkprefix.. brackets[3] .. b .. linkpostfix ..brackets[2] .. postfix
  returnstrformat = function(linkprefix, word, linkpostfix)
    return '[[' .. linkprefix .. word .. linkpostfix .. ']]'
    end
elseif type == 'templates' then
  returnstrformat = function(linkprefix, word, linkpostfix)
    return frame:expandTemplate{title = linkprefix, args={word, linkpostfix}}
    end
else
  error('Type must be either links or templates!')
end
for i, word in ipairs(b) do
  if returnstr ~= '' then
    returnstr = returnstr .. replacer
  end
  returnstr = returnstr .. returnstrformat(linkprefix, word, linkpostfix)
end
return returnstr
end
end


function p.consecutivelinks(frame)
function p.consecutivelinks(frame)
return p.parse(frame.args, {'[[', ']]', ''})
  return p.parse(frame.args, 'links')
end
end


function p.consecutivetemplates(frame)
function p.consecutivetemplates(frame)
return p.parse(frame.args, {'{{', '}}', '|'})
  return p.parse(frame.args, 'templates')
end
end


function p.test(frame)
  return p.parse({'Joppa,Grit Gate,Bey Lah', nil, nil, nil, nil, 'favilink', nil}, 'templates')
end
return p
return p

Revision as of 18:10, 16 November 2019


local p = {}
local text_util = require('Module:Text Utility')

function p.split(splitstr, separator)
  local b = text_util.split(splitstr, separator)
  return b
end

function p.parse(args, type)
frame = mw.getCurrentFrame()
if args.args ~= nil then
  args = args.args
end
local type = type or 'links'
local separator = '%s*' .. (args[2] or ',') .. '%s*'
local replacer = args[3] or ', '
local prefix = args[4] or ''
local postfix = args[5] or ''
local linkprefix = args[6] or ''
local linkpostfix = args[7] or ''

local b = p.split(args[1], separator)
local returnstr = ''
local returnstrformat

if type == 'links' then
  returnstrformat = function(linkprefix, word, linkpostfix)
    return '[[' .. linkprefix .. word .. linkpostfix .. ']]'
    end
elseif type == 'templates' then
  returnstrformat = function(linkprefix, word, linkpostfix)
    return frame:expandTemplate{title = linkprefix, args={word, linkpostfix}}
    end
else
  error('Type must be either links or templates!')
end
for i, word in ipairs(b) do
  if returnstr ~= '' then
    returnstr = returnstr .. replacer 
  end
  returnstr = returnstr .. returnstrformat(linkprefix, word, linkpostfix)
end
return returnstr
end

function p.consecutivelinks(frame)
  return p.parse(frame.args, 'links')
end

function p.consecutivetemplates(frame)
  return p.parse(frame.args, 'templates')
end


function p.test(frame)
  return p.parse({'Joppa,Grit Gate,Bey Lah', nil, nil, nil, nil, 'favilink', nil}, 'templates')
end
return p