 |
Club Builder Foro de C++Builder en español
|
| Ver tema anterior :: Ver tema siguiente |
| Autor |
Mensaje |
ifrit Master


Registrado: 12 Jun 2007Mensajes: 509Ubicación: La Habana, Cuba
|
Publicado: Mie 21 Ene, 2009 7:11 am Asunto: Convertir texto a html se puede? |
|
|
amigos por pura necesidad estoy pretendiendo convertir el texto de un RichEdit (RE) a formato html , bueno a lo mas esencial del html ahora mismo me trave por aca
| Código: | AnsiString texto;
RE->SelStart = 1;
bool etB = false, etI = false,
etU = false, etFName = false, etColor = false, etSize = false;
Variant x,y;
for( int i = 1; i <RE>Text.Length(); i ++)
{
//**********************************
//para las negritas
bool a = RE->SelAttributes->Style.Contains(fsBold);
if(a && !etB){
texto += "<b>";
etB = true;
}
else{
if(!a && etB){
texto += "</b>";
etB = false;
}
}
//para las italicas
bool c = RE->SelAttributes->Style.Contains(fsItalic);
if(c && !etI){
texto += "<i>";
etI = true;
}
else{
if(!c && etI){
texto += "</i>";
etI = false;
}
}
//para las subrayadas
bool s = RE->SelAttributes->Style.Contains(fsUnderline);
if(s && !etU){
texto += "<u>";
etU = true;
}
else{
if(!s && etU){
texto += "</u>";
etU = false;
}
}
//agregando letras al texto y cambiando el nombre de la letra
if(((char)RE->Text[i] != '\n') && ((char)RE->Text[i] != '\r'))
{
//nombre de letra 1
String nowfName = RE->SelAttributes->Name;
//pasando a saber el name del siguiente caracter
RE->SelStart ++;
String beforefName = RE->SelAttributes->Name;
RE->SelStart --;
ListBox1->Items->Add(IntToStr(RE->SelStart)+" :"+nowfName);
if(!etFName)
{
texto += "<font>";
etFName = true;
}
/*//tamano de las letras
short s1 = RE->SelAttributes->Size;
RE->SelStart ++;
short s2 = RE->SelAttributes->Size;
RE->SelStart --;
if(!etSize)
{
texto += "<font>";
etSize = true;
}*/
//colores de letras
//TColor c1 = RE->SelAttributes->Color;
x=IntToHex(GetRValue(RE->SelAttributes->Color), 2) +
IntToHex(GetGValue(RE->SelAttributes->Color), 2) +
IntToHex(GetBValue(RE->SelAttributes->Color), 2) ;
//AnsiString strc1 = c11;
RE->SelStart ++;
y=IntToHex(GetRValue(RE->SelAttributes->Color), 2) +
IntToHex(GetGValue(RE->SelAttributes->Color), 2) +
IntToHex(GetBValue(RE->SelAttributes->Color), 2) ;
RE->SelStart --;
if(!etColor)
{
texto += "<font>";
etColor = true;
}
texto += RE->Text[i];
//nombre de letra 2
if((beforefName != nowfName) && (etFName))
{
texto += "</font>";
etFName = false;
}
if((y != x) && (etColor))
{
texto += "</font>";
etColor = false;
}
}
//pasando al siguiente caracter
RE->SelStart ++;
if((char)RE->Text[i] == ' ')
texto += "";
if((char)RE->Text[i] == '\n'){
RE2->Lines->Add(texto);
texto = "";
RE->SelStart = i+1;
}
} |
lo que sucede es que me pone la marca de negritas un tanto despues de la letra o texto que seleccione me podrian ayudar
bueno mirandolo bien ya esta arreglado el caracter en su posicion solo poner SelStar =1; al cominenzo y no en cero
pero aun cabe la pregunta hay algo massencillo por ahi que lo que estoy haciendo, porque me parece cromañon mi trabajo _________________ Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Ultima edición por ifrit el Jue 22 Ene, 2009 7:26 pm, editado 1 vez |
|
| Volver arriba |
|
 |
ifrit Master


Registrado: 12 Jun 2007Mensajes: 509Ubicación: La Habana, Cuba
|
Publicado: Jue 22 Ene, 2009 2:47 pm Asunto: |
|
|
Alguien sabe como convertir a html desde codigo plano?
gracias
bueno a falta de pan .....
me he demorado 4 dias aun no termino pero los resultados son los siguintes
puedo detectar y convertir a HTML con este extenso codigo que hice
veo negritas/cursiva, subrayada/tachada/alineaciones izquierda/derecha/centrada/colores de letras, tamaños de letras/nombre de letra/detecto url en ormato www/http/mailto
aca les dejo mi codigo
estoy abierto a sugerencias y modificaciones en el codigo | Código: | #pragma hdrstop
#include "Unit1.h"
#include <iostream>
#include <ctype>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
short fSize = NULL, fSize1 = NULL;
bool a = true, cl = false, etN = false, etU = false, etC = false, etS = false,
fN = false, fS = false, etAD = false, etAI = false, etAC = false,
etWww = false, etHttp = false, etMailto = false;
String texto, fName, fName1, textoWww,textoHttp,textoMailto;
Variant x,y;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// para cuando se teclea una direccion url como ejemplo www.ccsg.com
unsigned mask = SendMessage(this->RE->Handle,EM_GETEVENTMASK, 0, 0);
SendMessage(this->RE->Handle, EM_SETEVENTMASK,0, mask | ENM_LINK);
SendMessage(this->RE->Handle, EM_AUTOURLDETECT,true, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//linea salva error de 60 caracteres vacios
RE->Lines->Add(" ");
RE->SelStart = 1;
for(int i = 1; i<RE>Text.Length(); i++){
/*
if(a){
//agergando la linea salvadora
RE->Lines->Add("extra linea by mico");
RE->SelStart = 1;
}
a = false; */
//detectar url
//si es www
if ((char)RE->Text[i] == 'w') {
//si 4 caracteres despues de donde estoy no son fin de archivo
if(i+4 != RE->Text.Length()){
if(i +1 != '\r' && i+2 != '\r' && i+3 != '\r' && i+4 != '\r'){
//si el 4 caracter no es espacio
if((char)RE->Text[i+4]!= ' '){
//validando www.
if((char)RE->Text[i+1] == 'w' && (char)RE->Text[i+2] =='w' &&
(char)RE->Text[i+3] == '.'){
textoWww = "";
texto += " <a>ProcessMessages();
for(int j = i; j <RE>Text.Length() ; j++){
if((char)RE->Text[j] != '\r' && (char)RE->Text[j]!= '\n'){
textoWww += RE->Text[j];
texto += RE->Text[j];
}
if(((char)RE->Text[j] == ' ')||(RE->Text[j] == '\r') && !etWww){
texto += "\">"+textoWww+"</a>";
i=j;
etWww = true;
}
if(etWww){
break;
}
}
}
}
}
}
}
//si es http
if ((char)RE->Text[i] == 'h') {
//si 5 caracteres despues de donde estoy no son fin de archivo
if(i+5 != RE->Text.Length()){
if(i +1 != '\r' && i+2 != '\r' && i+3 != '\r' && i+4 != '\r' && i+5 != '\r'){
//si el 5 caracter no es espacio
if((char)RE->Text[i+5]!= ' '){
//validando http:
if((char)RE->Text[i+1] == 't' && (char)RE->Text[i+2] == 't' &&
(char)RE->Text[i+3] == 'p' && (char)RE->Text[i+4] == ':'){
textoHttp = "";
texto += " <a>ProcessMessages();
for(int j = i; j <RE>Text.Length() ; j++){
if((char)RE->Text[j] != '\r' && (char)RE->Text[j]!= '\n'){
textoHttp += RE->Text[j];
texto += RE->Text[j];
}
if(((char)RE->Text[j] == ' ')||(RE->Text[j] == '\r') && !etHttp){
texto += "\">"+textoHttp+"</a>";
i=j;
etHttp = true;
}
if(etHttp){
break;
}
}
}
}
}
}
}
//si es mailto
if ((char)RE->Text[i] == 'm') {
//si 6 caracteres despues de donde estoy no son fin de archivo
if(i+7 != RE->Text.Length()){
if(i+1 != '\r' && i+2 != '\r' && i+3 != '\r' && i+4 != '\r' &&
i+5 != '\r' && i+6 != '\r' && i+7 != '\r'){
//si el 6 caracter no es espacio
if((char)RE->Text[i+7]!= ' '){
//validando mailto:
if((char)RE->Text[i+1] == 'a' && (char)RE->Text[i+2] == 'i' &&
(char)RE->Text[i+3] == 'l' && (char)RE->Text[i+4] == 't' &&
(char)RE->Text[i+5] == 'o' && (char)RE->Text[i+6] == ':'){
textoMailto = "";
texto += " <a>ProcessMessages();
for(int j = i; j <RE>Text.Length() ; j++){
if((char)RE->Text[j] != '\r' && (char)RE->Text[j]!= '\n'){
textoMailto += RE->Text[j];
texto += RE->Text[j];
}
if(((char)RE->Text[j] == ' ')||(RE->Text[j] == '\r') && !etMailto){
texto += "\">"+textoMailto+"</a> ";
i=j;
etMailto = true;
}
if(etMailto){
break;
}
}
}
}
}
}
}
//fin url
Label2->Caption = RE->Text.Length();
Label4->Caption = RE->SelStart;
Label6->Caption = RE->Text[i];
Label8->Caption = RE->Lines->Strings[i-1];
Label10->Caption = i;
if(isalnum(RE->Text[i])){
Label12->Caption = RE->Text[i];
}
//pregunatndo por los caracteresde escape que no son letras
if(isspace(RE->Text[i])){
if ((char)RE->Text[i] == ' ') {
Label12->Caption = "' ' Espacio";
texto += "";
}
else
if ((char)RE->Text[i] == '\n') {
Label12->Caption = "\\n Salto de linea";
texto += "<br>";
RE1->Lines->Add(""+texto);
texto = "";
RE->SelStart = i-1;
}
else
if ((char)RE->Text[i] == '\f') {
Label12->Caption = "\\f Alimentacion de formato";
}
else
if ((char)RE->Text[i] == '\r') {
Label12->Caption = "\\r Retorno de carro";
}
else
if ((char)RE->Text[i] == '\t') {
Label12->Caption = "\\t Tabulacion horizaontal";
}
else
if ((char)RE->Text[i] == '\v') {
Label12->Caption = "\\v Tabulacion vertical";
}
}
Label14->Caption = RE->Lines->Text[i];
Label16->Caption = RE->Lines->Count;
//obteniendo el color de la seleccion
x=IntToHex(GetRValue(RE->SelAttributes->Color), 2) +
IntToHex(GetGValue(RE->SelAttributes->Color), 2) +
IntToHex(GetBValue(RE->SelAttributes->Color), 2) ;
//guardando el color en texto
if(!cl){
texto += "<font>";
Label22->Caption = x;
cl = true;
}
//guardando el tipo de letras de la seleccion
fName = RE->SelAttributes->Name;
if(!fN){
texto += "<font>";
Label25->Caption = RE->SelAttributes->Name;
fN = true;
}
//guardando el tamano de la letra de la seleccion
//fSize = RE->SelAttributes->Size;
if(!fS){
fSize = RE->SelAttributes->Size;
//codigo de jesus del dream weaver
texto += "<span>";
//texto += "<font>";
Label27->Caption = RE->SelAttributes->Size;
fS = true;
}
//obteniendo estilos de letras como bold / underline/ italic / strikeout
//nuevos añadidos++: alineacion izquierda/ centrada/ derecha
//negrita / bold
bool negrita = RE->SelAttributes->Style.Contains(fsBold);
if (negrita && !etN) {//si es negrita y la etiqueta es falsa
texto += "<b>"; //agregando al texto el codigo HTML de negrita
etN = true;
}
else
if (!negrita && etN) {
texto += "</b>";
etN = false;
}
//subrayada / underline
bool subrayada = RE->SelAttributes->Style.Contains(fsUnderline);
if (subrayada && !etU) {
texto += "<u>";
etU = true;
}
else
if (!subrayada && etU) {
texto += "</u>";
etU = false;
}
//cursiva / italic
bool cursiva = RE->SelAttributes->Style.Contains(fsItalic);
if (cursiva && !etC) {
texto += "<i>";
etC = true;
}
else
if (!cursiva && etC) {
texto += "</i>";
etC = false;
}
//tachada / strikeout
bool tachada = RE->SelAttributes->Style.Contains(fsStrikeOut);
if (tachada && !etS) {
texto += "<strike>";
etS = true;
}
else
if (!tachada && etS) {
texto += "</strike>";
etS = false;
}
//alineaciones del parrafo
//alineacio derecha
if(RE->Paragraph->Alignment == taRightJustify && !etAD){
texto += "<div>";
etAD = true;
}
else
if (RE->Paragraph->Alignment != taRightJustify && etAD) {
texto += "</div>";
etAD = false;
}
//alineacion izquierda
if(RE->Paragraph->Alignment == taLeftJustify && !etAI){
texto += "<div>";
etAI = true;
}
else
if (RE->Paragraph->Alignment != taLeftJustify && etAI) {
texto += "</div>";
etAI = false;
}
//alineacion centrada
if(RE->Paragraph->Alignment == taCenter && !etAC){
texto += "<div>";
etAC = true;
}
else
if (RE->Paragraph->Alignment != taCenter && etAC) {
texto += "</div>";
etAC = false;
}
Label29->Caption = RE->Text[RE->SelStart];
//mientras sean letras y no el final de la linea...
if ((!isspace(RE->Text[i]))&&((char)RE->Text[i] != '\n') || ((char)RE->Text[i] != '\r')) {
texto += RE->Text[i];
Label20->Caption = texto;
RE->SelStart ++;
//obteniendo el color de la seleccion posterior
y=IntToHex(GetRValue(RE->SelAttributes->Color), 2) +
IntToHex(GetGValue(RE->SelAttributes->Color), 2) +
IntToHex(GetBValue(RE->SelAttributes->Color), 2) ;
//guardando el color en texto si es diferente del anterior
//siempre despues de haber saltado de linea con el selstart ++
if(cl && (x != y)){
texto += "</font>";
Label22->Caption = y;
cl = false;
}
//obteniendo el tipo de letra de la seleccion porterior
fName1 = RE->SelAttributes->Name;
//guardando el font name en texto si es diferente del anterior
//siempre despues de haber saltado de linea con el selstart ++
if(fN && fName != fName1){
texto += "</font>";
Label25->Caption = RE->SelAttributes->Name;
fN = false;
}
//obteniendo el tamano de letra de la seleccion
fSize1 = RE->SelAttributes->Size;
//guardando el size del texto si es diferente del anterior
//siempre despues de haber saltado de linea con el selstart ++
if (fS && fSize != fSize1) {
texto += "</span>";
Label27->Caption = RE->SelAttributes->Size;
fS = false;
}
}
Label25->Caption = RE->SelAttributes->Name;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ColorBox1Select(TObject *Sender)
{
RE->SetFocus();
RE->SelAttributes->Color = ColorBox1->Selected;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TVariant ruta = "c:\\mico.html";
RE1->Lines->SaveToFile("c:\\mico.html");
CppWebBrowser1->Navigate2(&ruta);
ClientHeight = 559;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
ClientHeight = 402;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
//llenando las letras
cbName->Items = Screen->Fonts;
cbName->Text = RE->DefAttributes->Name;
//llenando los numeros
for (int i = 1; i <73>Items->Add(i);
}
cbSize->Text = RE->DefAttributes->Size;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbNameChange(TObject *Sender)
{
RE->SetFocus();
RE->SelAttributes->Name = cbName->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbSizeChange(TObject *Sender)
{
RE->SetFocus();
RE->SelAttributes->Size = cbSize->Text.ToInt();
}
//------- |
gracias al foro _________________ Todos somos ignorantes, lo que no todos ignoramos lo mismo... |
|
| Volver arriba |
|
 |
|
|
No puedes publicar temas nuevos en este foro No puedes responder a temas en este foro No puedes editar tus mensajes en este foro No puedes borrar tus mensajes en este foro No puedes votar en las encuestas de este foro
|
© 2002–2026 Club Builder · Funciona con phpBB.
|