Module:ColorParse

From Caves of Qud Wiki
Revision as of 02:08, 21 July 2020 by Teamtoto (talk | contribs) (minor refactor)
Jump to navigation Jump to search
Documentation

This module parses the game's color codes in strings and formats them for the wiki. This shouldn't be directly called anymore. Use Template:Qud text instead. If the text in question has pronouns in syntax =pronouns.(pronountype)= or a similar style, use Template:Grammar.

Usage

{{#invoke: ColorParse | parse |(your text here)}}

Code:

{{#invoke: ColorParse | parse |&RS&rt&Ko&yp&Ys&Rv&ra&Kl&yi&Yn&Rn&y}}

Result:

Lua error at line 53: attempt to call field 'determinexml' (a nil value).


local p = {}

local luatable = require'Module:Color'
local textutil = require'Module:Text Utility'
local process_args = require'Module:ProcessArgs'
local shaders = require'Module:ColorParse/Templates'
local stringbyte, stringchar = string.byte, string.char

--[Utility stuff]--

function normalize(input, type)
  type = type or 'xml'
  local a = '&y' 
  if type == 'notxml' then
    a = '&y'
  end
  local b = input:gsub("(~J211)", "")
  if not b:match('^%&') then
    b = a .. b 
  end
  --b = b:gsub("(%*)", "*")
  if type == 'xml' then
    b = b:gsub("([{}])", "")
  end
  b = b:gsub("(\\n)","\n")
  return b
end

function p.determinetype(input)
   return (input:match('&') ~= nil)
end

function strtotbl(str)
    tbl = {stringbyte(str, 1, #str)}
    for i = 1, #tbl do
        tbl[i] = stringchar(tbl[i])
    end
    return tbl
end

function p.linebreaks(frame)
  local b = string.gsub(frame,"(~)","\n\n")
  return b
end

--[ The main function. Takes the first argument and snips out stuff we don't need
--[ depending on regextype.

function p.parse(frame, regextype)
  args = frame.args or {[1] = frame}
  regextype = regextype
  if regextype == nil then
    if p.determinexml(args[1]) then
       regextype = 'xml'
    else
       regextype = 'notxml'
    end
  end
  --[Determine regex type]
  local regex = "&(%w)([^&\n]*)"
  if regextype == 'notxml' then
  	regex = "&(%w)([^&\n]*)"
  end

  local b = normalize(args[1],regextype)
  local a = string.gsub(b, regex, function(color,text)
       if text ~= nil and text ~= "" then
          local hexcolor = luatable.parse(color)
          if hexcolor == nil then
            error ("There was no specified color for color code: " .. color)
          end
          text = text or ""
          return "<span style=\"color: " .. hexcolor .. ";\">"  .. text .. "</span>"
       else
          return ""
       end
  end)
return a
end

function p.main(frame)

	local parsetype = nil
  	local unbolded = false
  	local tildes = false
  	local text = ''
 	local args = process_args.merge(true)
 	for _, arg in ipairs(args) do
    	if arg == 'notxml' then
        	parsetype = 'notxml'
      	elseif arg == 'unbolded' then
        	unbolded = true
      	elseif arg == 'tildes are new lines' then
        	tildes = true
        elseif text == '' then
        	text = arg
      	end
  	end
    
  	local returntext = p.parse(text, parsetype)
  	if tildes  == true then 
    	returntext = p.linebreaks(returntext)
  	end
  	if unbolded == true then
    	return returntext
  	else
  		return '<b>' .. (returntext or '') .. '</b>'
  	end
end


-- Beta Parse --
function p.shader(text, colors, type, rasterize)
    -- split the colors into a list 
    local colorlist = textutil.split(colors, '-')
    -- check shader type:
    -- default     | if pattern is shorter than text, text will remain uncolored (y)
    -- sequence    | pattern will repeat until text is over
    -- alternation | pattern will stretch to fit word length (?)
    local type = type or 'default'
    local brasterize = false
    if rasterize == 'true' then
       brasterize = true
    end
    local strtbl = strtotbl(text)
    local finaltbl = {}
    local interval = 1
    local bordered = false
    local overflowbehavior = function(f) return 'y' end
    if type == 'alternation' then
        interval = math.floor(#strtbl/#colorlist)  --sets interval that color code switches.
        if interval <= 0 then interval = 1 end
        overflowbehavior = function(f) return colorlist[#colorlist] end
    elseif type == 'sequence' or type == 'solid' then
        overflowbehavior = function(f) 
         local e = f%#colorlist
         if e == 0 then e = #colorlist end
         return colorlist[e]
         end
    elseif type == 'bordered' then
       bordered = true
       overflowbehavior = function(f)
        if f + #strtbl == 3 then return colorlist[2] else return colorlist[1] end
        end
    elseif type == 'chaotic' then
       overflowbehavior = function(f) return 'y' end -- placeholder
    end
    local ci = 1
    if bordered == true then
       ci = #colorlist
    end
    for i=1, #strtbl do
       if ci > #colorlist or ci <= 0 then
           hexcolor = overflowbehavior(ci)
       else
           hexcolor = colorlist[ci]
       end
          if brasterize then
            finaltbl[i] = "<span style=\"color: " .. luatable.parse(hexcolor) .. ";\">"  .. strtbl[i].. "</span>"
          else 
            finaltbl[i] = "&amp;" .. hexcolor .. strtbl[i]
          end
       if i%interval == 0 then
          if bordered == true then
              ci = ci - 1
          else
              ci = ci + 1
          end
       end      
    end
    return table.concat(finaltbl)
end

function p.shadermain(frame)
    local args = process_args.merge(true)
    if args[1] and args[2] then -- shader template
       output = applytemplate(args[1], args[2], (args['rasterize'] or 'true'))
    else
       output = p.shader(args['text'], args['colors'], args['type'], (args['rasterize'] or 'true'))
    end

    if (args['unbolded'] == 'true') then
       return output
    else
       return '<b>' .. (output or '') .. '</b>'
    end
end

function applytemplate(shader, text, raster)
   return p.shader(text, shaders[shader][1], shaders[shader][2], raster)
end

function p.shaderlist()
   local tblheader = '<tr><th>Shader</th><th>Colors</th><th>Type</th></tr>'
   local tr = {}
   i = 0
   for a, v in pairs(shaders) do
       tr[i] = ' <tr><td><b>' .. applytemplate(a, a, 'true') .. '</b></td><td>' .. v[1] .. '</td><td>' .. v[2] .. '</td></tr>'
       i = i + 1
   end
   return '<table class="wikitable sortable">' .. tblheader  .. table.concat(tr) .. '</table>'
end

function p.test()
    --return p.shader('Stopsvalinn', 'R-r-K-y-Y', 'sequence')
    return applytemplate('soul', 'soulcurding')
end

return p