Modul:ColorAlphabet
---- This module is supposed to apply Green-Armytage's standard for a "color alphabet" to letters in a string of input text.
---- {{#invoke:ColorAlphabet|blank|whatever}} for blank,
---- {{#invoke:ColorAlphabet|letter|whatever}} for colored letters
local p={}
function p.letter(frame,blank)
local debuglog=""
local args=frame.args
local parent=frame.getParent(frame)
local pargs={}
if parent then pargs=parent.args end
local text=args[1] or pargs[1] or ""
local lookup={}
lookup.file=args.lookup or pargs.lookup or "Module talk:ColorAlphabet/lookup"
page=mw.title.new(lookup.file)
if not(page) then return "error: failed to open lookup file" .. tostring(lookup.file) end
lookup.text=page.getContent(page)
local prowl=mw.ustring.gmatch(lookup.text,"(%a):(.-)|(.-)\n") -- | marks the position of the letter
repeat
local i,j,k=prowl()
if not(i) then break end
-- debuglog=debuglog..tostring(i)..tostring(j)..tostring(k)
lookup[i]=j .. (blank or i) .. k -- create the lookup table of what letters are supposed to be transliterated to. If blank == " " then spaces, not letters go in the middle
until false
lookup["["]="";lookup["]"]="" -- don't include these in output
local wikistart={};wikiend={}
local s=0;local e=0
repeat
s,e=mw.ustring.find(text,"%[%[([^%[%]]-)%]%]",e)
if (not(s) or not(e)) then break end
wikistart[s]=mw.ustring.sub(text,s,e)
wikiend[e]=true
until false
local prowl=mw.ustring.gmatch(text,"(.)")
local output=""
local wl=1
position=0
repeat
local letter=prowl()
if not(letter) then break end
position=position+1
if wikistart[position] then output=output..'<div style="position:relative;"><div style="position:absolute;top:0px;left:0px;clip:rect(0px,'..3*mw.ustring.len(wikistart[position])..'px,20px,0px);overflow:hidden;">[[File:Transparent600.gif|frameless|link=http://en.wikipedia.org/wiki'..wikistart[position].. '|alt=Example alt text]]</div></div>' end
if wikiend[position] then output=output.."" end
output=output..(lookup[letter] or letter) -- unicode letters will never all be looked up, handle punctuation the same
until false
return frame.preprocess(frame,'<div style="position:relative;">'..output..'</div>')
end
function p.blank(frame)
local blank=" "
return p.letter(frame,blank)
end
return p