Module:Wnt/Templates/Svgedit2
Appearance
Documentation for this module may be created at Module:Wnt/Templates/Svgedit2/doc
local p={}
function findfirst(histo, value)
local lessthans=1
for i = 1, #histo do
if histo[i]<value then lessthans=lessthans+1 end
end
return lessthans
end
function extractprofile(profile, levels)
profile = string.match(profile,levels .. "==(.+)") or "no match"
profile = string.match(profile,"(.-)==") or profile
local x=0
local debug=""
local pattern={}
for w in string.gmatch(profile, "%x%x%x%x%x%x") do
x=x+1
pattern[x]=w
debug = debug .. w
end
return pattern
end
function readdata(datafile)
local values={}
local line=string.gmatch(datafile,"([^,;\n]*)")
repeat
local id=string.match(line,"(%a%w*)")
local strvalue, exponent=string.match(line,"(-?%d+)E?e?(%d+)")
value = strvalue * 10 ^ (exponent or 0)
if id then values[id] = value end
line = string.gmatch(datafile,"[\n,;]([^,;\n]*)")
until not(line)
return values
end
function createorders(datafile,profile,override)
local maxlevel = string.match(profile,"(%d+)%s*==") + 0
local values = readdata(datafile)
local count=0
local histo={}
for k,v in values do
count=count+1
histo[count]=v
end
histo=table.sort(histo)
local maxvalue=histo[count]
local threshold={}
local threspos={}
local threscount=0
local thresdone={}
for k,v in override do
if (v <= maxvalue) then
threshold[k] = v
threspos[k] = findfirst(histo,v)
else maxlevel = math.min(k-1, maxlevel)
end
threscount=threscount+1
thresdone[threscount]=k
end
thresdone=table.sort(thresdone)
thresdone[0] = 0
threspos[0] = 1
for x = 1,threscount do
local first=thresdone[x-1]
local firstpos=threspos[threshold[first]]
local last=thresdone[x]
local lastpos=threspos[threshold[last]]
for fillinlevel=first+1,last-1 do
threshold[fillinlevel]=findfirst(math.floor(firstpos + (lastpos-firstpos)*(fillinlevel-first)/(last-first)))
end
end
local first=thresdone[threscount]
local firstpos=threspos[threshold[first]]
local last=maxlevel+1
local lastpos=count+1
for fillinlevel=first+1,last-1 do
threshold[fillinlevel]=findfirst(math.floor(firstpos + (lastpos-firstpos)*(fillinlevel-first)/(last-first)))
end
local pattern = extractprofile(profile,maxlevel)
local orders={}
for k,v in values do
local x=1
while v < threshold[x] do x=x+1 end
orders[k] = pattern[x]
end
return orders
end
function replacetags(svginput,orders)
local ids={}
local fills={}
local index=1
local debug = "replacetags-debug:"
svgnew=svginput
for w in string.gmatch(svginput, "(<.->)") do
debug = debug .. w
local fill = string.match(w, "fill:(.-);") or ""
local id = string.match(w, "id=\"(.-)\"") or ""
if (id ~= "") then
if (fill ~= "") then
if orders[id] then
wnew = string.gsub (w, fill, orders[id])
svgnew = string.gsub (svgnew, w, wnew)
end
else
local style = string.match(w, "(style=\")") or ""
if (style ~= "") then
wnew = string.gsub (w, style, style .. "fill:" .. orders[id] .. ";")
svgnew = string.gsub (svgnew, w, wnew)
else -- no style at all!
local indent=""
local insert=""
insert, indent = string.match(w,"((%s*)id=\")")
wnew = string.gsub (w, insert, (indent or "") .. "style=\"fill:" .. orders [id] .. ";\"\n" .. (indent or ""))
end
end
end
end
return svgnew, debug
end
function p.makesvg(frame)
local debug = "debug: "
local repdebug = ""
local args=frame.args
local parent=frame:getParent()
local pargs=parent.args
local svginput=pargs.svginput or args.svginput or "ERROR"
local profile=pargs.profile or args.profile or "==1== 000000"
local presets=pargs.presets or args.presets or ""
local z=1
local preset=""
local preposn=1
local override={}
debug = debug .. presets
for preset in string.gmatch(presets,"([%d\?]+)") do
if preset ~= "?" then override[z]=preset; debug=debug .. "preset" .. z .. preset end
z=z+1
end
svgoutput,repdebug=replacetags(svginput,orders,override)
return (repdebug .. debug .. "\n" .. "<pre>" .. svgoutput .. "</pre>")
end
return p