DelphiFAQ Home Search:

Highlighting HTML-Tags in TRichEdit

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

You need a quick and dirty syntax highlighting to display HTML?

Use the procedure HTMLSyntax() - provided below - as shown in the example at the bottom. To optimize the display, suppress updates by calling the TRichEdit's BeginUpdate() and EndUpdate. This code is too slow to be used in an OnChange() event handler. Another problem is that there is not much of error recovery if you have to handle faulty HTML. But it is suitable to display existing HTML files only.

If you need a real syntax highlighting editor for your FrontPage clone project, I recommend the excellent open source tool mwEdit. This project is asleep since approximately 2001 when it was considered mature and complete. I still have the source code because I use it in some of my own projects (SourceCoder for example). mwEdit is lightning fast and supports many many grammars (languages, e.g. Pascal, C, perl, SQL.. maybe 20 languages). Write me (ptiemann_2000@yahoo.com) if you have a problem finding it.

procedure HTMLSyntax(RichEdit: TRichEdit; TextCol, TagCol, DopCol: TColor);
var
  i,
  iDop : integer;
  s    : string;
  Col  : TColor;
  isTag,
  isDop: boolean;
begin { HTMLSyntax }
  iDop := 0; 
  isDop := False; 
  isTag := False; 
  Col := TextCol; 
  RichEdit.SetFocus; 
  
  for i := 0 to Length(RichEdit.Text) do 
  begin 
    RichEdit.SelStart := i; 
    RichEdit.SelLength := 1; 
    s := RichEdit.SelText; 
    
    if (s='<') or (s=char(123)) then 
      isTag := True; 
    
    if isTag then 
      if (s=char(34)) then 
        if not isDop then 
        begin 
          iDop := 1; 
          isDop := True
        end { not isDop } 
        else 
          isDop := False; 
    
    if isTag then 
      if isDop then 
      begin 
        if iDop<>1 then 
          Col := DopCol
      end { isDop } 
      else 
        Col := TagCol 
    else 
      Col := TextCol; 
    
    RichEdit.SelAttributes.Color := Col; 
    
    iDop := 0; 
    
    if (s='>') or (s=char(125)) then 
      isTag := False
  end; { for i } 
  RichEdit.SelLength := 0
end; { HTMLSyntax } 


procedure TForm1.Button1Click(Sender: TObject); 
begin { TForm1.Button1Click } 
  RichEdit1.Lines.BeginUpdate; 
  HTMLSyntax(RichEdit1, clBlue, clRed, clGreen); 
  RichEdit1.Lines.EndUpdate
end; { TForm1.Button1Click }

Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.