I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

August Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This     
  From the October 2009 Issue of Prestwood eMag
 
Delphi Using Data:
HTML to Source (TLRDelphi2HTML)
 
Posted 22 years ago on 6/1/1998
Take Away:

A basic Delphi source to HTML component.

KB100089

By Larry Rutledge

The toughest part of creating the newsletter each month is getting the source code to appear correctly for the web version. Fortunately, the tag allows me to display source code with at least halfway decent formatting.

After a few months of doing everything manually to get the source code lined up, I decided I needed a little help. So I started writing a Delphi project to generate HTML pages based on Delphi source files. I made it a component for two reasons; first, I wanted to drop the component onto a larger project I am working on to make the whole newsletter creation task easier. Second, I wanted to add design-time features that would allow me to generate the HTML files right from the test projects I created for the newsletter. This is how TLRDelphi2HTML came to be.

The Major Parts of TLRDelphi2HTML
There are three major parts to TLRDelphi2HTML that I will talk about in this article, 1) the parser, 2) the property editors, and 3) the component editors.

The Parser
To begin this project I needed a parser. This would allow me to a source file and identify the major pieces that I might want to add syntax highlighting to (i.e. keywords, numbers, strings, etc.)

I first went to the Internet and searched high and low for parsing source code. I found many great parsers and even downloaded and played around with a few of them. But there were some items I specifically needed in a parser that the others didn't offer completely, so I resorted to building my own. I found, however, that it wasn't as difficult as I had first expected.

In order to parse the file, you pull information out of the file a character at a time until you reach a designated separator token. In our case, every token is separated by a space (a token is any one piece of the source file, one keyword, one complete number, etc).

As we pull out each token, we write it to our new HTML file and apply our custom syntax highlighting to the token as we write it. For example, if we parsed out the word BEGIN and we wanted to apply bold to it in the HTML file, we would write out the following:

begin


To perform this tokenization process, we read a single character from the text file, we then check the character to determine what type of token we want to extract. If the character is an alpha character, we extract a word token. If the character is a numeric character, we extract a number. If the character is a symbol, we extract a symbol.

The method getToken performs this exact task. There are a number of functions that begin with the prefix get and are called by getToken. These methods each know how to extract a specific kind of token (i.e. getWord knows how to extract a word token, getQuote knows how to extract a pascal string, getComment knows how to extract pascal comments, etc.)

If you study this handful of methods, you should be able to see how the parsing process occurs.

The Property Editors
The property editors allowed me to define how certain properties of the component behave when modifying their contents from the Object Inspector. There are four property editors and they handle the HTML, Source, Destination, and About properties.

The purpose of the property editor for the HTML property is simply to make the Object Inspector display the string (html). This string appears as the value for the HTML property if no source has been converted, otherwise this value displays blank. Reading the actual contents of the HTML property return the HTML version of the source file.

The purpose of the property editor for the Source property is to display an ellipsis (...) button. When clicked on, an open dialog appears allowing the user to select the source file to convert.

The purpose of the property editor for the Destination property is to also display an ellipsis (...) button. The difference is, this button displays a save dialog allowing the user to specify the path and filename to store the resulting HTML as.

The purpose of the property editor for the About property is also to display an ellipsis (...) button. However, this button displays an About dialog when clicked.

To implement property editors, the first thing to do is to subclass the TPropertyEditor class and override three methods. The methods that must be overridden and their purpose are:

  • GetValue - Delphi calls this method when it needs to display a value for the property in the Object Inspector. The return value is a string and can be whatever you want (i.e. the actual value of the property, or a string like the (html) stored in the HTML property.
  • GetAttributes - Delphi calls this method when it needs to know the attributes of the property. Here you can specify things like paReadOnly, which means that no one can change the value through the object Inspector. Or you can specify the value paDialog, this makes the ellipsis (...) button appear.
  • Edit - Delphi calls this method when the property is modified (i.e. the ellipsis (...) button is clicked, etc).

Here is the declaration of the HTML property editor as it appears in the Interface section:

THTMLProperty = class(TPropertyEditor)

public

  function  GetValue      : String;

            override;

  function  GetAttributes : 

            TPropertyAttributes; 

            override;



  procedure Edit; override;

end;

          

And here is the implementation of the HTML property editor as it appears in the Implementation section:

function THTMLProperty.GetValue : 

         String;

begin

  if TLRDelphi2HTML(GetComponent(0)).

     HTML = '' then

    Result := ''

  else

    Result := '(html)';

end;

function THTMLProperty.GetAttributes :  

         TPropertyAttributes;

begin

  if TLRDelphi2HTML(GetComponent(0)).HTML =

     '' then

    Result := [paReadOnly]

  else

    Result := [paReadOnly, paDialog];

end;



procedure THTMLProperty.Edit;

begin

  with TfrmShowHTML.Create(Application) do

  try

    sourceFile := TLRDelphi2HTML(

                  GetComponent(0)).Source;

    ShowModal;

  finally

    Free;

  end;

end;

         

There is a lot more that property editors can do, and the best source for finding out this information is Ray Lischner's book Hidden Paths of Delphi 3.

The Component Editor
The component editor allows me to add items to the pop-up menu that appears when you right click the component at design-time. You can also modify what occurs when the component is double-clicked.

To implement a component editor you have to subclass the TComponentEditor class and override four methods. The methods that have to be overridden and their purpose are as follows:

  • GetVerbCount - When Delphi wants to display the pop-up menu, it asks how many menu items you want to add.
  • GetVerb - For each of the menu items that you said you wanted to add, Delphi asks what should appear in the menu.
  • ExecuteVerb - When a menu item is clicked Delphi tells you which one was clicked and leaves it up to you to handle it.
  • Edit - When the component is double-clicked, Delphi tells you with this method call.

Again, there is a lot more that component editors can do, and the best source for finding out this information is Ray Lischner's book Hidden Paths of Delphi 3.

To get the source code for this component and start learning how to add these cool features to your own components, download the

source code.


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1135A1
Enter key:
Article Contributed By Larry J. Rutledge :
I worked for Prestwood Software as a Delphi developer from 1997 through 2002. During that time I enjoyed working with Mike Prestwood and the other developers at Prestwood.
Visit Profile

 KB Article #100089 Counter
4513
Since 4/2/2008

Follow PrestwoodBoards on: 


©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech