Migrando adobe flex 3 para HTML 5 – mx:RichTextEditor

Olá.

Estou migrando a minha aplicação feita em adobe flex 3 para HTML 5 e no meu banco de dados há vários registros que armazenam informações geradas pelo componente mx:RichTextEditor.

Já não é novidade que os componentes richtext feitos em javascript não são compatíveis com o mx:RichTextEditor, então um meio é converter estes registros de um formato para outro.

Fiz um pequeno script em PHP que faz este processo.

<?php
$conteudo = utf8_decode('<TEXTFORMAT LEADING=\"2\"><P ALIGN=\"CENTER\"><FONT FACE=\"Verdana\" SIZE=\"48\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"CENTER\"><FONT FACE=\"Verdana\" SIZE=\"48\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\">Napoleon Hill</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"48\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\">nasceu 26 de outubro <B><I>1883</I></B> no Estado da Virgínia, nos Estados Unidos em uma família pobre e morreu no ano 1970. Quando <FONT COLOR=\"#FF0000\">tinha 10 anos</FONT> de idade sua mãe morreu e, em seguida, se tornou um filho rebelde.</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\">Dois anos depois seu pai se casou. Ao estar pela primeira vez frente a sua madrasta comentou: ”Napoleão é o pior homem que você pode encontrar”. Ela colocou as mãos em seus ombros e disse: “Menino, isso não é uma falta grave. Talvez você seja o cara mais esperto do mundo e, simplesmente, não estão sabendo o que fazer com a sua inteligência".[2]</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\">Aquelas palavras tiveram um impacto importante sobre a vida de Hill. Aos treze anos escreveu um pequeno jornal chamado “Mountain Reporter”. Ele começou a estudar Direito, mas teve que sair devido a problemas financeiros.</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\">E 1908, por causa de suas reportagens para o jornal, Napoleon Hill teve a oportunidade de entrevistar o industrial Andrew Carnegie, que não só era o homem mais rico do mundo na época, mas o segundo homem mais rico que a humanidade já conheceu, depois de John D. Rockefeller.</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"LEFT\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#66FF00\" LETTERSPACING=\"0\" KERNING=\"0\"><B><I>Andrew Carnegie</I></B><FONT COLOR=\"#124332\"> viu um brilho diferente nos olhos de Napoleon Hill e decidiu revelar ao jovem repórter a sua crença de que seria possível, por meio de extenso trabalho de pesquisa, identificar em homens de triunfo características que poderiam ser desenvolvidas nas pessoas. Era a ciência pela qual tinha prosperado e se tornado um homem tão importante, uma espécie de fórmula para o sucesso. Uma seleção de virtudes, que se combinadas em uma personalidade garantiriam o completo êxito de tal indivíduo. Disse que era necessário identificar as características dos homens e mulheres de sucesso e que poderiam ser implementadas pelo homem comum, contanto que houvesse um método.</FONT></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"CENTER\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING=\"2\"><P ALIGN=\"CENTER\"><FONT FACE=\"Verdana\" SIZE=\"16\" COLOR=\"#124332\" LETTERSPACING=\"0\" KERNING=\"0\"><B><I>O milionário, então, propôs a Hill que iniciasse um grandioso projeto para investigar minuciosamente essas virtudes de pessoas triunfadoras e que desenvolvesse um curso que permitisse aos interessados desenvolver estas características.</I></B></FONT></P></TEXTFORMAT>');

function cleanHTML($str){
	$str = str_replace('\"', '"', $str);

	$str = str_replace('<TEXTFORMAT LEADING="2">', '', $str);
	$str = str_replace('</TEXTFORMAT>', '', $str);

	$str = str_replace('<FONT', '<span', $str);
	$str = str_replace('</FONT>', '</span>', $str);

	$str = str_replace('ALIGN="', 'style="text-align: ', $str);
	$str = str_replace('"><FONT FACE="', '; font-family: ', $str);
	$str = str_replace('" SIZE="', '; font-size: ', $str);
	$str = str_replace('" COLOR="', 'px; color: ', $str);

	$str = str_replace('FACE="', 'style="font-family: ', $str);
	$str = str_replace('COLOR="', 'style="color: ', $str);

	$str = preg_replace('/<span SIZE="(\d+)">/', '<span style="font-size:${1}px">', $str);
	$str = str_replace(' SIZE="', ' style="font-size: ', $str);

	$str = preg_replace('/"\sLETTERSPACING="\d+" KERNING="\d+"/', ';"', $str);
	$str = preg_replace('/<LI><span style=".*?\"><\/span><\/LI>/', '', $str);
	$str = str_replace('<LI>', '<li>', $str);
	$str = str_replace('</LI>', '</li>', $str);
	$str = str_replace('<A HREF', '<a href', $str);
	$str = str_replace('</A>', '</a>', $str);
	$str = str_replace('<P', '<p', $str);
	$str = str_replace('</P>', '</p>', $str);
	$str = str_replace('<I>', '<em>', $str);
	$str = str_replace('</I>', '</em>', $str);
	$str = str_replace('<B>', '<strong>', $str);
	$str = str_replace('</B>', '</strong>', $str);
	$str = str_replace('<b>', '<strong>', $str);
	$str = str_replace('</B>', '</strong>', $str);
	$str = str_replace('LEFT', 'left', $str);
	$str = str_replace('RIGHT', 'right', $str);
	$str = str_replace('JUSTIFY', 'justify', $str);
	$str = str_replace('LEFT', 'left', $str);
	$str = str_replace('TARGET', 'target', $str);

	return $str;
}

echo cleanHTML($conteudo);

Fica a dica para quem estiver migrando de tecnologia.

Abraços