Modulo:Font to span: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m errore
m -errore grave, chissà quante pagine ci sono scappate...
 
(29 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1:
--ToDo: Aggiungere riconoscimento degli apici oltre alle virgolette in tutte le stringhe
local p={};
 
--funzione di estrazione per utilizzo multiplo
local function extract(x)
face=string.match(x, "face%s*=%s*([%ad%aæ,]+)") or string.match(x, 'face%s*=%s*"%s*([%a, ^"]+)"')
size=string.match(x, 'size%s*=%s*"?%s*([-+]?%d+[pxempxtem%%]*)"?') or string.match(x, 'size%s*=%s*"?%s*([%a-]+)"?')
color=string.match(x, 'color%s*=%s*"?%s*(rgb%([^)]+%))"?') or string.match(x, 'color%s*=%s*"?%s*(#?%w+)"?')
style=string.match(x, 'style%s*=%s*"([^"]+)"')
return {face, size, color, style}
Line 14 ⟶ 15:
--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
Line 28 ⟶ 29:
if type=="size" then
--riporto valori fantasiosi di size a quelli reali
z=tonumber(string.match(a, "(%d+) *pt")) or tonumber(a)
if z~=nil then
if z>7 then a="7"
Line 42 ⟶ 43:
['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
Line 54 ⟶ 55:
 
if type=="color" then
--via gli spazi messi fantasiosamente e l'ancor più fantasioso "solid"
a = string.gsub(a, " ", "");
a = string.gsub(a, "solid", "");
--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
--toglie il cancelletto dai colori non hex (sì, a giro c'è anche questo)
if string.match(a, "^%#[a-f0-9]*[g-z]") then a = string.gsub(a, "%#", "") end
 
--tabella semplificazione colore per i casi più semplici
ctab = {
['#FF0000ff0000'] = 'red',
['#FFC0CBffc0cb'] = 'pink',
['#FFA500ffa500'] = 'orange',
['#FFFF00ffff00'] = 'yellow',
['#800080'] = 'purple',
['#008000'] = 'green',
['#0000FF0000ff'] = 'blue',
['#A52A2Aa52a2a'] = 'brown',
['#FFFFFFffffff'] = 'white',
['#808080'] = 'gray',
['#000000'] = 'black'
Line 82 ⟶ 88:
if string.match(str, "<font[^>]+>%[%[[^%]]+]]<%/font>") then
substype=1; --solo wikilink
elseif string.match(str, "<font[^>]+>%[http[^%]]+ [^%]]+]<%/font>") or string.match(str, "<font[^>]+>%[\{\{fullurl[^%]]+ [^%]]+]<%/font>") then
substype=2; --solo link esterno CON TITOLO
elseif string.match(str, "<font[^>]+>%[http[^%] ]+]<%/font>") or string.match(str, "<font[^>]+>%[\{\{fullurl[^%] ]+]<%/font>") then
return str; --solo link esterno SENZA TITOLO: il font colora il numero progressivo dall'esterno, lo span no; non ho trovato un modo per colorarlo con span interno.
end --negli altri casi i risultati sono equivalenti
--riduzione stringa al solo font di apertura e minuscolizza il tutto
s=mw.ustring.lower(string.sub(str,1,string.find(str, ">")));
 
--estrazione attributi
Line 110 ⟶ 116:
-- elimina parametri duplicati, lascia quello in style
if style~=nil then
style=string.gsub(style, "background%-color", "background%-clor") --workaround per evitare match sul bgcol
if color~=nil and string.match(style, "color:") then color=nil end
if size~=nil and string.match(style, "font%-size:") then size=nil end
if face~=nil and string.match(style, "font%-family:") then face=nil end
style=string.gsub(style, "background%-clor", "background%-color") --annullo il workaround
end
 
Line 123 ⟶ 131:
--Costruisce lo span e ci inserisce il testo (substype 0)
span='<span style="' .. style .. '">'
text=string.gsub(str, "<%/?font[Ff][Oo][Nn][Tt][^>]*>", "")
result= span .. text .. "</span>"
 
Line 154 ⟶ 162:
function p.sempl(frame)
str=frame.args[3]
--estrazione parametri sparsi
x=extract(mw.ustring.lower(frame.args[1]))
y=extract(mw.ustring.lower(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
x[4]=string.gsub(x[4], "background%-color", "background%-clor") --workaround per evitare match sul bgcol
c1=string.match(x[4], 'color:([^;"]+)')
f1s1=string.match(x[4], 'font%-size:([^;"]+)')
s1f1=string.match(x[4], 'font%-family:([^;"]+)')
x[4]=string.gsub(string.gsub(string.gsub(x[4], 'font%-family:([^;"]+)', ""), 'font%-size:([^;"]+)', ""), 'color:([^;"]+)', "")
x[4]=string.gsub(x[4], "background%-clor", "background%-color") --annullo il workaround
end
--estrazione parametri dal secondo style
if y[4]~=nil then
y[4]=string.gsub(y[4], "background%-color", "background%-clor") --workaround per evitare match sul bgcol
c2=string.match(y[4], 'color:([^;"]+)')
f2s2=string.match(y[4], 'font%-size:([^;"]+)')
s2f2=string.match(y[4], 'font%-family:([^;"]+)')
y[4]=string.gsub(string.gsub(string.gsub(y[4], 'font%-family:([^;"]+)', ""), 'font%-size:([^;"]+)', ""), 'color:([^;"]+)', "")
y[4]=string.gsub(y[4], "background%-clor", "background%-color") --annullo il workaround
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]
fc=c2 or y[3] or c1 or x[3]
--conversione size e aggiustamento numeri
Line 189 ⟶ 196:
--conversione color ed eventuale aggiustamento cancelletto
if c~=nil then c = convert(c, "color") end
--Scorre i due style per trovare parametri uguali
if x[i4]~=nil and y[i] then x[i4]~=nil endthen
--liste di attributi
it1= string.gmatch(x[4], "([%a-]+:)")
it2= string.gmatch(y[4], "([%a-]+:)")
corr={}; --table vuota di corrispondenze
for i=1,3 in it1 do
for j in it2 do
if i==j then table.insert(corr, i) end --aggiunge il match alle corrispondenze
end
it2= string.gmatch(y[4], "([%a-]+:)") --ripristina iteratore per altri giri
end
--elimina le corrispondenze e relativo valore dal tag esterno
for i,v in ipairs(corr) do
x[4]=string.gsub(x[4], v.."[^;]+;*", "");
end
--formatta i due style eliminando spazi e punti e virgola superflui
x[4]=string.gsub(string.gsub(x[4], ";[ ;]+", ";"), "^[ ;]+", "")
y[4]=string.gsub(string.gsub(y[4], ";[ ;]+", ";"), "^[ ;]+", "")
--unisce ciò che rimane dei due style
if string.match(x[4], "[^;]$") then x[4] = x[4] .. ";" end
stile=x[4]..y[4]
elseif x[4]~=nil then
stile=string.gsub(string.gsub(x[4], ";[ ;]+", ";"), "^[ ;]+", "")
elseif y[4]~=nil then
stile=string.gsub(string.gsub(y[4], ";[ ;]+", ";"), "^[ ;]+", "")
end
--Ottiene uno style singolo con tutti e soli i parametri da mantenere
sty=build(fc,s,cf,stile)
--Seconda uscita di sicurezza
Line 197 ⟶ 232:
--Costruisce lo span unico, ci rimette dentro il testo e lo restituisce
text=string.gsub(string.gsub(str, "<%/?font[Ff][Oo][Nn][Tt][^>]*>", ""), "<%/?span[Ss][Pp][Aa][Nn][^>]*>", "")
return '<span style="' .. sty .. '">' .. text .. "</span>"
end