Modulo:Font to span

Versione del 2 set 2017 alle 12:22 di Daimona Eaytoy (discussione | contributi) (ulteriore fix punto e virgola)

La funzione principale di questo modulo è la conversione dei tag font, deprecati in HTML 5, in tag span con opportuno stile. Per utilizzarlo (sempre e comunque in subst) è necessario rintracciare un tag font di apertura e il corrispettivo tag di chiusura con un'espressione regolare e inserire tutto il testo trovato come primo parametro. Ad esempio, è possibile procedere così:

  • Utilizzare l'espressione regolare <font[^>]+>[^<]+<\/font> per trovare il testo da sostituire.
  • Sostituirlo con {{subst:#invoke:Font to span|main|1=$&}}, dove $& è l'intero testo trovato; occorre ricordarsi di inserire 1= altrimenti i simboli di uguale contenuti nel tag generano errore.

Il modulo provvede da solo alle operazioni necessarie, convertendo i parametri di dimensione, colore e face del testo. Inoltre, se all'interno dei tag si trova solo un wikilink o un link esterno con titolo, il tag viene spostato dentro al link per mantenere il colore voluto. L'unico caso non supportato è quello in cui i tag contengano soltano un link esterno senza titolo, per i quali sembra non esserci una soluzione semplice. Occorre inoltre fare particolare attenzione al subst, evitando di effettuare la sostituzione all'interno di tag <ref>...</ref> o <includeonly>...</includeonly>.

È inoltre possibile utilizzare il modulo per incorporare due tag, siano essi font o span (anche misti), situati uno dentro l'altro con lo stesso testo in mezzo. In questo caso, il modulo provvede ad estrarre dai due tag lo stile effettivo da utilizzare, cercandolo rispettivamente in:

Style del tag internoParametri sparsi nel tag internoStyle del tag esternoParametri sparsi nel tag esterno

Trovati i parametri finali li converte e semplifica come nella modalità principale e restituisce un unico tag con i parametri corretti. Per utilizzare questa funzionalità, è necessaria un'espressione regolare che trovi tutto il testo dall'apertura del tag esterno alla sua chiusura.

Ad esempio, sul testo <span style="color:green;font-family:Verdana"><font face=Monospace size=2>Testo di prova senza alcun significato messo qui come esempio</font></span> è necessaria una regex come (<(font|span)[^>]+>)(<(font|span)[^>]+>)[^<]+<\/\4><\/\2>, con un gruppo catturante sul tag esterno e uno sul tag interno. Come replace occorre chiamare il modulo nel seguente modo: {{subst:#invoke:Font to span|sempl|1=$1|2=$3|3=$&}}. Il risultato sarà <span style="color:green;font-family:Monospace;font-size:small">Testo di prova senza alcun significato messo qui come esempio</span>

Nota
Le dimensioni troppo grandi (+4, 7 e maggiori) vengono convertite in un formato fisso espresso in pixel, anziché variabile come per le altre.

--Input: <font....>Roba</font>
--Outpu: Roba1?<span style...>Roba2</span>Roba3?

local p={};
function p.main(frame)
	str=(mw.ustring.gsub(frame.args[1], "{{=}}", "="))
	local substype
--substype per eventuale spostamento testo
	if string.match(str, "<font[^>]+>[^%[]+<%/font>") then  
		substype=0;  --testo semplice
	elseif string.match(str, "<font[^>]+>%[%[[^%]]+]]<%/font>") then
		substype=1;  --testo +wl
	else 
		return str; 
	end
	
--riduzione stringa al solo font di apertura
n=string.find(str, ">")
s=string.sub(str,1,n)

--ricava face
face=string.match(s, "face=%a+")
if face==nil then face=string.match(s, 'face="[%a ]+"') end
if face~=nil then face=string.gsub(string.sub(face, 6), '"', "") end

--ricava size
size=string.match(s, "size=%d")
if size==nil then size=string.match(s, 'size="?%[-+]?%d+[ px]*"?') end
if size~=nil then size=string.gsub(string.sub(size, 6), '"', "") end

--ricava color
color=string.match(s, 'color="?#?[a-f0-9]+"?')
if color==nil then color=string.match(s, 'color="?%a+"?') end
if color~=nil then color=string.gsub(string.sub (color, 7), '"', "") end

--ricava style
style=string.match(s, 'style="[^"]+"')
if style~=nil then style=string.gsub(string.sub (style, 7), '"', "") end

--tabella conversione size
stab = {
	['1'] = '10px',
	['2'] = '13px',
	['3'] = '16px',
	['4'] = '18px',
	['5'] = '24px',
	['6'] = '32px',
	['7'] = '48px'
}

--conversione size secondo tabella
if size~=nil and string.match(size, "^[1-7]$") then size=stab[size] end

--aggiunge cancelletto a colore se in formato hex e sottinteso
if color~=nil and string.match(color, "^[a-f0-9]+$") then color = "#" .. color end

-- sistema parametri duplicati
if style~=nil then
	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%-face:") then face=nil end
end

--concatena tutto in span + style
if style==nil then style="" end
if not string.match(style, ";$") 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-face:" .. face .. ";" end
span='<span style="' .. style .. '">'
text=string.gsub(str, "<%/?font[^>]*>", "")
result= span .. text .. "</span>"

--spostamento substype
if substype==1 then
	wl=string.sub(str,string.find(str, "%[%["),string.find(str, "]]"))
	if string.match(wl, "|") then
		wl1=string.sub(wl,1,string.find(wl, "|"))
		wl2=string.gsub(string.sub(wl,string.find(wl, "|"),string.find(wl, "]")), "[|%]]", "")
		result=wl1 .. span .. wl2 .. "</span>]]"
	else
		wl1=string.gsub(wl, "[%]]", "")
		result=wl1 .. "|" .. span .. wl1 .. "</span>]]"
	end
end

return result

-- +pagina di appoggio con sintassi nominale per evitare beghe di conversione dell'uguale

end
return p