Modulo:Font to span: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m ops
Sbozzo nuova funzione per compito analogo, scorporo alcune operazioni dalla main in funzioni secondarie separate per essere usate dalla nuova funzione principale
Riga 1:
local p={};
 
--funzione di estrazione per utilizzo multiplo
local function extract(x)
face=string.match(x, "face%s*=%s*(%a+)") or string.match(x, 'face%s*=%s*"([%a, ]+)"')
size=string.match(x, 'size%s*=%s*"?([-+]?%d+[pxem%%]*)"?')
color=string.match(x, 'color%s*=%s*"?(rgb%([^)]+%))"?') or string.match(x, 'color%s*=%s*"?(#?%w+)"?')
style=string.match(x, 'style%s*=%s*"([^"]+)"')
return {face, size, color, style}
end
 
--funzione per ricostruire lo style
local function build(a,b,c,d)
--Mette insieme lo style
if d==nil then d="" end
if string.match(d, "[%a ]+[^;]$") then d = d .. ";" end
 
if a~=nil then d=d .. "color:" .. a .. ";" end
if b~=nil then d=d .. "font-size:" .. b .. ";" end
if c~=nil then d=d .. "font-family:" .. c .. ";" end
 
--Via le virgolette conclusive e restituisce style impacchettato
return string.gsub(d, ";+$", "")
end
 
local function convert(a,type)
 
if type=="size" then
--riporto valori fantasiosi di size a quelli reali
z=tonumber(a)
if z~=nil then
if z>7 then a="7"
elseif z<-2 then a="-2"
elseif z>4 and string.match(a, "^%+%d$") then a="+4" end
end
--tabella conversione size
stab = {
['1'] = 'x-small',
['2'] = 'small',
['3'] = 'medium',
['4'] = 'large',
['5'] = 'x-large',
['6'] = 'xx-large',
--['7'] = '48px',
['-2'] = 'x-small',
['-1'] = 'small',
['+1'] = 'large',
['+2'] = 'x-large',
['+3'] = 'xx-large'
--['+4'] = '48px'
}
a = stab[a] or a
end
 
if type=="color" then
--aggiunge cancelletto a colore se in formato hex e sottinteso. Il pattern è molto brutale ma funziona perché non esistono nomi di colore che lo rispettano
if string.match(a, "^[a-f0-9]+$") then a = "#" .. a end
 
--tabella semplificazione colore per i casi più semplici
ctab = {
['#FF0000'] = 'red',
['#FFC0CB'] = 'pink',
['#FFA500'] = 'orange',
['#FFFF00'] = 'yellow',
['#800080'] = 'purple',
['#008000'] = 'green',
['#0000FF'] = 'blue',
['#A52A2A'] = 'brown',
['#FFFFFF'] = 'white',
['#808080'] = 'gray',
['#000000'] = 'black'
}
a = ctab[a] or a
end
return a
end
 
function p.main(frame)
str=frame.args[1]
Line 13 ⟶ 89:
--riduzione stringa al solo font di apertura
ns=string.sub(str,1,string.find(str, ">"))
s=string.sub(str,1,n)
 
--estrazione attributi
--ricava face
attr=extract(s)
face=string.match(s, "face%s*=%s*(%a+)") or string.match(s, 'face%s*=%s*"([%a, ]+)"')
 
--torno ad attributi con nome per semplicità
--ricava size
face=attr[1]
size=string.match(s, 'size%s*=%s*"?([-+]?%d+[pxem%%]*)"?')
size=attr[2]
color=attr[3]
style=attr[4]
 
--conversione size e aggiustamento numeri
--ricava color
if size~=nil then size=convert(size, "size") end
color=string.match(s, 'color%s*=%s*"?(rgb%([^)]+%))"?') or string.match(s, 'color%s*=%s*"?(#?%w+)"?')
--uscita di sicurezza
 
--ricava style
style=string.match(s, 'style%s*=%s*"([^"]+)"')
 
--tabella conversione size
stab = {
['1'] = 'x-small',
['2'] = 'small',
['3'] = 'medium',
['4'] = 'large',
['5'] = 'x-large',
['6'] = 'xx-large',
--['7'] = '48px',
['-2'] = 'x-small',
['-1'] = 'small',
['+1'] = 'large',
['+2'] = 'x-large',
['+3'] = 'xx-large'
--['+4'] = '48px'
}
 
--tabella semplificazione colore per i casi più semplici
ctab = {
['#FF0000'] = 'red',
['#FFC0CB'] = 'pink',
['#FFA500'] = 'orange',
['#FFFF00'] = 'yellow',
['#800080'] = 'purple',
['#008000'] = 'green',
['#0000FF'] = 'blue',
['#A52A2A'] = 'brown',
['#FFFFFF'] = 'white',
['#808080'] = 'gray',
['#000000'] = 'black'
}
 
--riporto valori fantasiosi di size a quelli reali
if size~=nil then
z=tonumber(size)
if z~=nil then
if z>7 then size="7"
elseif z<-2 then size="-2"
elseif z>4 and string.match(size, "^%+%d$") then size="+4" end
end
end
 
--conversione size secondo tabella
if size~=nil then size = stab[size] or size end
if size == "+4" or size =="7" then return str end --font troppo grande, non sono riuscito a convertirlo
 
--conversione color ed eventuale aggiustamento cancelletto
--aggiunge cancelletto a colore se in formato hex e sottinteso. Il pattern è molto brutale ma funziona perché non esistono nomi di colore che lo rispettano
if color~=nil andthen string.matchcolor = convert(color, "^[a-f0-9]+$color") then color = "#" .. color end
if color~=nil then color = ctab[string.upper(color)] or color end
 
-- elimina parametri duplicati, lascia quello in style
Line 85 ⟶ 115:
end
 
--Mette insiemeCostruisce lo style
if style==nilbuild(color, thensize, face, style="" end)
if string.match(style, "[%a ]+[^;]$") then style = style .. ";" end
 
if color~=nil then style=style .. "color:" .. color .. ";" end
if size~=nil then style=style .. "font-size:" .. size .. ";" end
if face~=nil then style=style .. "font-family:" .. face .. ";" end
 
--Via le virgolette conclusive
style = string.gsub(style, ";+$", "")
 
--Controllo "di sicurezza": se lo style è (quasi) vuoto restituisce il testo di partenza e buonanotte
Line 128 ⟶ 150:
 
end
 
--funzione per accorpare due font/span con lo stesso testo in mezzo
function p.sempl(frame)
str=frame.args[3]
--estrazione parametri
x=extract(frame.args[1])
y=extract(frame.args[2])
--eliminazione duplicati dal primo
for i=1,3 do
if x[i] and y[i] then x[i]=nil end
end
--estrazione parametri dal primo style
if x[4]~=nil then
c1=string.match(x[4], 'color:([^;"]+)')
f1=string.match(x[4], 'font%-size:([^;"]+)')
s1=string.match(x[4], 'font%-face:([^;"]+)')
end
--estrazione parametri dal secondo style
if y[4]~=nil then
c2=string.match(y[4], 'color:([^;"]+)')
f2=string.match(y[4], 'font%-size:([^;"]+)')
s2=string.match(y[4], 'font%-face:([^;"]+)')
end
--rimozione dei duplicati da style1
if c1 and c2 then c1=nil end
if f1 and f2 then f1=nil end
if s1 and s2 then s1=nil end
--Parametro univoco, ordine di priorità: style dentro, tag dentro, style fuori, tag fuori
f=f2 or y[1] or f1 or x[1]
s=s2 or y[2] or s1 or x[2]
f=c2 or y[3] or c1 or x[3]
--conversione size e aggiustamento numeri
if s~=nil then s=convert(s, "size") end
--uscita di sicurezza
if s == "+4" or s =="7" then return str end --font troppo grande, non sono riuscito a convertirlo
 
--conversione color ed eventuale aggiustamento cancelletto
if c~=nil then c = convert(c, "color") end
--Ottiene uno style singolo con tutti e soli i parametri da mantenere
sty=build(f,s,c)
--Seconda uscita di sicurezza
if string.match(sty, "^%s*$") then return str end
--Costruisce lo span unico, ci rimette dentro il testo e lo restituisce
text=string.gsub(string.gsub(str, "<%/?font[^>]*>", ""), "<%/?span[^>]*>", "")
return '<span style="' .. sty .. '">' .. text .. "</span>"
end
 
 
 
return p