Module:Consecutive links: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(add support for text prefix outside of link)
mNo edit summary
 
Line 27: Line 27:


if type == 'links' then
if type == 'links' then
   returnstrformat = function(linkprefix, word, linkpostfix)
   returnstrformat = function(linkprefix, word, linkpostfix, indicator, textprefix)
   fronttext = ''
   fronttext = ''
   if textprefixindicator ~= '' and string.sub(word, 1, string.len(textprefixindicator)) == textprefixindicator then
   if indicator ~= '' and string.sub(word, 1, string.len(indicator)) == indicator then
   word = string.sub(word, string.len(textprefixindicator) + 1)
   word = string.sub(word, string.len(indicator) + 1)
   fronttext = textprefix
   fronttext = textprefix
   end
   end
Line 36: Line 36:
     end
     end
elseif type == 'templates' then
elseif type == 'templates' then
   returnstrformat = function(linkprefix, word, linkpostfix)
   returnstrformat = function(linkprefix, word, linkpostfix, indicator, textprefix)
     return frame:expandTemplate{title = linkprefix, args={word, linkpostfix}}
     return frame:expandTemplate{title = linkprefix, args={word, linkpostfix}}
     end
     end
Line 46: Line 46:
     returnstr = returnstr .. replacer  
     returnstr = returnstr .. replacer  
   end
   end
   returnstr = returnstr .. returnstrformat(linkprefix, word, linkpostfix)
   returnstr = returnstr .. returnstrformat(linkprefix, word, linkpostfix, textprefixindicator, textprefix)
end
end
return prefix .. returnstr .. postfix
return prefix .. returnstr .. postfix

Latest revision as of 19:34, 19 December 2020


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 textprefixindicator = args[8] or ''
local textprefix = args[9] or ''

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

if type == 'links' then
  returnstrformat = function(linkprefix, word, linkpostfix, indicator, textprefix)
  	fronttext = ''
  	if indicator ~= '' and string.sub(word, 1, string.len(indicator)) == indicator then
  		word = string.sub(word, string.len(indicator) + 1)
  		fronttext = textprefix
  	end
    return fronttext .. '[[' .. linkprefix .. word  .. linkpostfix .. ']]'
    end
elseif type == 'templates' then
  returnstrformat = function(linkprefix, word, linkpostfix, indicator, textprefix)
    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, textprefixindicator, textprefix)
end
return prefix .. returnstr .. postfix
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)
  str = '{{SkillID to name|Axe}} </br>{{SkillID to name|Axe_Expertise}}'
  separator = '</br>'
  replacer = '</div><div class{{=}}"qud-skill-entry">'
local prefix ='<div class{{=}}"qud-skill-entry">'
local postfix = '</div>'
  args = {str, separator, replacer, prefix, postfix}
  return p.parse(args, 'links')
end
return p