Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


PAS 2 HTML converterFormat this article printer-friendly!Bookmark function is only available for registered users!
actually... source-code2HTML converter...
Product:
Delphi all versions
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
05/08/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet pas2htm pas2html converter pas htm html source2htm source2html sourcecode sourcecode2htm sourcecode2html
Times Scored:
3
Visits:
3346
Uploader: Eber Irigoyen
Company: BTXSys
Reference: N/A
 
Question/Problem/Abstract:
have you seen some articles with hightlighted sintax?
well, here's my approach to make your source code look just like that and so now you can upload your articles and make'em look pretty =o)
Answer:



I created this unit that has only one procedure:
Procedure Source2Htm(Strs:TStrings);
why did I call it "source2htm" and not "PAS2HTM"?
well, because I think you can use this unit not only to convert Pascal-Delphi source code to html, if you modify the "keywords" you can use the same source code to hightlight pretty much any source code... so... given that...
oh... there's also some constants that you might want to look at:
- ModifB (Modificator begin "<B>" default)
- ModifE (Modificator end "</B>"default)
this modificators get inserted on  your source code to highlight the keywords that you want, but, you could change those to make the sourcecode look hightlighted and italic style, or whatever you want to
another constan is
- s2hFontModif (default "verdana size=2")... I put that so I could put source code that looks "aligned" or whatever special font you want to use fo it
- Comments (default to "//"): I just fixed this to not-highlight whatever is after that
- AddBR (Default to True) defines if BR is added at the end of each line

...here's the unit (I processed this unit with my own unit!)
------------------------------------Unit Src2htm.pas----------------
Unit src2htm;
//- Programmed by: EberSys
// - 03-21-2002 - v1.1
// - Added support for Delphi comments
// - Added - User may specify s2hFontModif='' so no font will be specified for the html
// - use it freely and if you make any improvements I'd appreciate if you would let me know
//

Interface

Uses classes;

Var KeyWords:TStrings;

Const
  ConvertExact:Boolean=True;
  AddBR:Boolean=True;
  ModifB:String='<b>';
  ModifE:String='</b>';
  s2hFontModif:String=''; //'face="verdana" size="2"';
  Comments:String='//';

Procedure Source2Htm(Strs:TStrings);

Implementation

Uses SysUtils;

Procedure Source2Htm(Strs:TStrings);
Var X, Y:Integer; ALine:String;

  Function Line2Html(ALine:String):String;
  Var OriLine, KW, RealKW:String; X, Posi, Len, SmallestPosi, CommentPosi:Integer; Found:Boolean;
  Begin
    Result:='';
    If Not (ALine='') Then
    Begin
      OriLine:=ALine;
      ALine:=UpperCase(ALine);
      Found:=False;
      SmallestPosi:=Length(ALine);
      //we have to lookup all the keywords
      //to validate code like:
      //For X:=0 To 40 Do
      //if you find "Do" first, it wouldn't
      //fix the words "For" and "To"
      //this way, we keep looking 'til we find out
      //that "For" is in the Smallest Position
      RealKW:='';
      For X:=0 To KeyWords.Count-1 Do
      Begin
        Posi:=Pos(UpperCase(KeyWords[X]), ALine);
        If (Posi>0) And (Posi<SmallestPosi) Then
        Begin
          KW:=KeyWords[X];
          Len:=Length(KeyWords[X]);
          If ( (Posi=1) Or (ALine[Posi-1] In [' ', '=', '(']) )
          And Not (ALine[Posi+Len] In ['a'..'z', 'A'..'Z', '0'..'9', '_']) Then
          Begin
            Found:=True;
            SmallestPosi:=Posi;
            RealKW:=KW;
            If (Posi=1) Then //there's no smaller than 1
              Break
          End    
        End
      End;
      CommentPosi:=Pos(Comments, ALine);
      If (CommentPosi=0) Then
        CommentPosi:=SmallestPosi;

      If (Found) And (CommentPosi>=SmallestPosi) Then
      Begin
        If (SmallestPosi=1)
        Or (ALine[SmallestPosi-1]=' ')
        Or (ALine[SmallestPosi-1]='(')
        Or (ALine[SmallestPosi-1]='=') Then
        Begin
          Len:=Length(RealKW);
          Result:=Result+Copy(OriLine, 1, SmallestPosi-1)+ModifB+RealKW+ModifE;
          Delete(OriLine, 1, SmallestPosi+Len-1)
        End;
        Result:=Result+Line2Html(OriLine)
      End
      Else
        Result:=Result+OriLine
    End
  End;

  Function ReplaceAll(FindStr, ReplaceWith, ReplaceIn:String):String;
  Var Posi:Integer;
  Begin
    Y:=1;
    Result:='';
    If Not (ReplaceIn='') Then
    Begin
      Posi:=Pos(FindStr, ReplaceIn);
      If (Posi>0) Then
      Begin
        Result:=Result+Copy(ReplaceIn, 1, Posi-1)+ReplaceWith;
        Delete(ReplaceIn, 1, Posi);
        Result:=Result+ReplaceAll(FindStr, ReplaceWith, ReplaceIn)
      End  
      Else
        Result:=ReplaceIn
    End
  End;

Begin
  Strs.Add('');
  For X:=0 To Strs.Count-1 Do
  Begin
    ALine:=Strs[X];
    ALine:=ReplaceAll('&', '&amp', ALine);
    ALine:=ReplaceAll('<', '&lt', ALine);
    ALine:=Line2Html(ALine);
    Y:=1;
    If Not (ALine='') Then
      While (ALine[Y]=' ') Do
      Begin
        Delete(ALine, Y, 1);
        Insert('&nbsp;', ALine, 1);
        Inc(Y, 6)
      End;
    If (AddBR) Then
      Strs[X]:=ALine+'<br>'
    Else
      Strs[X]:=ALine
  End;
  If Not (s2hFontModif='') Then
  Begin
    Strs.Insert(0, '<font '+s2hFontModif+'>');
    strs.Add('</font>')
  End  
End;

Initialization
  KeyWords:=TStringList.Create;

  KeyWords.Add('And');
  KeyWords.Add('Array');
  KeyWords.Add('As');
  KeyWords.Add('Asm');
  KeyWords.Add('Automated');
  KeyWords.Add('Begin');
  KeyWords.Add('Case');
  KeyWords.Add('Class');
  KeyWords.Add('Const');
  KeyWords.Add('Constructor');
  KeyWords.Add('Destructor');
  KeyWords.Add('dispinterface');
  KeyWords.Add('Div');
  KeyWords.Add('For');    //fix it
  KeyWords.Add('To');     //fix it
  KeyWords.Add('While');  //fix it
  KeyWords.Add('On');     //fix it
  KeyWords.Add('DownTo'); //fix it
  KeyWords.Add('Do');
  KeyWords.Add('Else');
  KeyWords.Add('End');
  KeyWords.Add('Except');
  KeyWords.Add('Exports');
  KeyWords.Add('File');
  KeyWords.Add('Finalization');
  KeyWords.Add('Finally');
  KeyWords.Add('Function');
  KeyWords.Add('Goto');
  KeyWords.Add('If');
  KeyWords.Add('Implementation');
  KeyWords.Add('In');
  KeyWords.Add('Inherited');
  KeyWords.Add('Initialization');
  KeyWords.Add('Inline');
  KeyWords.Add('Interface');
  KeyWords.Add('Is');
  KeyWords.Add('Label');
  KeyWords.Add('Library');
  KeyWords.Add('Message');
  KeyWords.Add('Mod');
  KeyWords.Add('Nil');
  KeyWords.Add('Not');
  KeyWords.Add('Object');
  KeyWords.Add('Of');
  KeyWords.Add('Or');  //fixed
  KeyWords.Add('Out');
  KeyWords.Add('Packed');
  KeyWords.Add('Private');
  KeyWords.Add('Procedure');
  KeyWords.Add('Program');
  KeyWords.Add('Property');
  KeyWords.Add('Protected');
  KeyWords.Add('Public');
  KeyWords.Add('Published');
  KeyWords.Add('Raise');
  KeyWords.Add('Record');
  KeyWords.Add('Repeat');
  KeyWords.Add('ResourceString');
  KeyWords.Add('Set');
  KeyWords.Add('Shl');
  KeyWords.Add('Shr');
  KeyWords.Add('String');
  KeyWords.Add('Then');
  KeyWords.Add('Threadvar');
  KeyWords.Add('Try');
  KeyWords.Add('Type');
  KeyWords.Add('Unit');
  KeyWords.Add('Until');
  KeyWords.Add('Uses');
  KeyWords.Add('Var');
  KeyWords.Add('Whith');
  KeyWords.Add('Xor');

Finalization
  KeyWords.Free

End.
------------------------------------Unit Src2htm.pas----------------

...ok, now for those people who like examples... here it is...
just create a new project, drop a TMemo and a TButton on your form... add src2htm to the uses seccion... on the onclick event of your button put this code:
Procedure TForm1.Button1Click(Sender: TObject);
Begin
  AddBR:=False; //set this to false if you are going to upload an article to Delphi3000!!!
  Source2Htm(Memo1.Lines)
End;


and run the program!

now, while the program is running go back to your source code, copy it all and paste it on the Memo of your running program, click the button... and voila!...

I hope this is useful, there's still some improvements that can be made... I'll update it if you are interested

keep up coding

EberSys





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
two quick things...
    Eber Irigoyen (Mar 21 2002 7:05PM)

=o|... I missed many Delphi KeyWords, but you can just add'em to the source2html unit.. I'll update the article too...
and the other thing...
if you use this code to upload an article to Delphi3000, save your converted code first (you might need if you update your article later)

salu2
EberSys
Respond

v1.1
Eber Irigoyen (Mar 21 2002 8:27PM)

I had uploaded a different (non-working) version of the unit (hehe... had a brain failure), is fixed now, plus I updated to version 1.1, that allows you to not-highlight Delphi comments! (only // comments)

salu2
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)