Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

PAS 2 HTML converter

Title: PAS 2 HTML converter Question: 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 "&ltB" default) - ModifE (Modificator end "&lt/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='&ltb'; ModifE:String='&lt/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 (Posi0) And (Posi&ltSmallestPosi) 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 (Posi0) 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', '&ampamp', ALine); ALine:=ReplaceAll('&lt', '&amplt', ALine); ALine:=Line2Html(ALine); Y:=1; If Not (ALine='') Then While (ALine[Y]=' ') Do Begin Delete(ALine, Y, 1); Insert('&ampnbsp;', ALine, 1); Inc(Y, 6) End; If (AddBR) Then Strs[X]:=ALine+'&ltbr' Else Strs[X]:=ALine End; If Not (s2hFontModif='') Then Begin Strs.Insert(0, '&ltfont '+s2hFontModif+''); strs.Add('&lt/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