Debut de parseur html (avec tparser)

Description

Petit parseur HTML à l'etat de projet (oui j'vient juste de le faire), mais je le poste maintenant car j'aurai aimé de l'aide à ce sujet puisque je n'arrive pas à savoir comment colorer le texte pendant la frappe.

Dans mon cas je me suis contenté uniquement de la classe TParser, sinon aller voir la source de Florenth (http://www.delphifr.com/code.aspx?ID=31741) si vous voulez aller un peu plus loin.

Source / Exemple :


{*******************************
 |  Parseur de fichier HTML    |
 |  Début de projet            |
 |  Par LEVEUGLE Damien        |

                                                              • }
unit UHtml; interface uses Windows, Forms, SysUtils, Classes, ComCtrls, Graphics; type TParse = class(TParser) private BALISES : TStringList; CHAMPS : TStringList; public constructor Create( Stream : TStream ); destructor Destroy; procedure Color( Editeur : TRichEdit ); end; const FICHIER_BALISES = 'BALISES.txt'; FICHIER_CHAMPS = 'CHAMPS.txt'; implementation { constructeur } constructor TParse.Create( Stream : TStream ); begin BALISES := TStringList.Create; CHAMPS := TStringList.Create; BALISES.LoadFromFile( ExtractFilePath(Application.ExeName) + FICHIER_BALISES ); CHAMPS.LoadFromFile ( ExtractFilePath(Application.ExeName) + FICHIER_CHAMPS ); inherited Create( Stream ); end; { Destructeur } destructor TParse.Destroy; begin BALISES.Free; CHAMPS.Free; inherited Destroy; end; { Procedure qui parse et colorise le texte } procedure TParse.Color( Editeur : TRichEdit ); var Memoire : Integer; begin Editeur.SelStart := 1; Editeur.SelLength := Length( Editeur.Text ); Editeur.SelAttributes.Color := clBlack; while ( Self.Token <> toEOF ) do begin Application.ProcessMessages; case ( Self.Token ) of toString, toWString, toInteger, toFloat : begin Editeur.SelStart := Self.SourcePos; Editeur.SelLength := Length(Self.TokenString); Editeur.SelAttributes.Color := clGray; Editeur.SelLength := 0; end; toSymbol : begin { BALISES D'EN TETE ( ex : BODY ) } if ( BALISES.IndexOf( UpperCase(Self.TokenString) ) > -1 ) then begin Editeur.SelStart := Self.SourcePos; Editeur.SelLength := Length(Self.TokenString); Editeur.SelAttributes.Color := clBlue; Editeur.SelLength := 0; end; { PROPRIETES DES BALISES ( ex : "href" ) } if ( CHAMPS.IndexOf( UpperCase(Self.TokenString) ) > -1 ) then begin Editeur.SelStart := Self.SourcePos; Editeur.SelLength := Length(Self.TokenString); Editeur.SelAttributes.Color := clGray; Editeur.SelLength := 0; end; end; else begin { *** Valeurs *** } if ( Self.TokenString = '"' ) then begin Memoire := Self.SourcePos; Self.NextToken; while ( Self.TokenString <> '"' ) do Self.NextToken; Editeur.SelStart := Memoire; Editeur.SelLength := ( Self.SourcePos + 1 ) - Memoire; Editeur.SelAttributes.Style := [fsItalic]; Editeur.SelAttributes.Color := clRed; Editeur.SelLength := 0; Memoire := 0; end; { *** Feuille Style *** } if ( Self.TokenString = '{' ) then begin Memoire := Self.SourcePos; while ( Self.TokenString <> '}' ) do Self.NextToken; Editeur.SelStart := Memoire; Editeur.SelLength := ( Self.SourcePos + 1 ) - Memoire; Editeur.SelAttributes.Style := [fsItalic]; Editeur.SelAttributes.Color := clGreen; Editeur.SelLength := 0; Memoire := 0; end; { *** Symboles & Balises *** } if ( Self.TokenString = '>' ) or ( Self.TokenString = '/' ) or ( Self.TokenString = '<' ) then begin Editeur.SelStart := Self.SourcePos; Editeur.SelLength := Length(Self.TokenString); Editeur.SelAttributes.Color := clNavy; Editeur.SelLength := 0; end; end; end; Self.NextToken; end; end; end.

Conclusion :


Si vous avez des conseils sur cette pauvre source ?

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.