Modulo:Font to span: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m copia-incolla pasticcioso
m -errore grave, chissà quante pagine ci sono scappate...
 
(23 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 125 ⟶ 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 157 ⟶ 163:
str=frame.args[3]
--estrazione parametri sparsi
x=extract(mw.ustring.lower(frame.args[1]))
y=extract(mw.ustring.lower(frame.args[2]))
 
--estrazione parametri dal primo style
Line 211 ⟶ 217:
y[4]=string.gsub(string.gsub(y[4], ";[ ;]+", ";"), "^[ ;]+", "")
--unisce ciò che rimane dei due style
if string.match(x[4], "[%a -]+[^;]$") then x[4] = x[4] .. ";" end
stile=x[4]..y[4]
elseif x[4]~=nil then
Line 226 ⟶ 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