Syntax Highlighting

Program SyntaxHighlight splits up each line of the html file into words by finding out if each character is in the set WordDelimiters. This set is defined in the StrUtils unit as comprising characters other than letters and digits. Each group of letters/digits makes up a word. Each word is tested to see if it is in the array of keywords. If it is, it is put between HTML strong tags so that it will be emboldened when viewed in a browser. Each line, after processing, is added to a new file – Out.html.

Vital code in this program is if ansiMatchText(CurrentWord, KeyWords) then. The useful ansiMatchText function returns True only if the string CurrentWords is one of the strings in the array KeyWords. Having 'text' in the identifier, we know that it is not case sensitive. The variable Keywords must be declared as being of type ansiString to satisfy the Lazarus compiler.

For your convenience, we include a procedure that saves the original HTML file into your program folder.

program SyntaxHighlight;
  {$APPTYPE CONSOLE}
uses
  SysUtils, StrUtils;
const
  INPUTFILE = 'TestBold.html';
  OUTPUTFILE = 'Out.html';
var
  i : integer;
  IFile, OFile : textfile;
  Keywords : Array[1..12] of ansiString;
  CurrentLine, CurrentWord, OutputLine : string;
  IsWord : Boolean;

procedure SaveTestBold;
var
  HTMLFile : textFile;
begin
  assignFile(HTMLFile, 'TestBold.html');
  rewrite(HTMLFile);
  writeln(HTMLFile, '<html>');
  writeln(HTMLFile, '<body>');
  writeln(HTMLFile, 'program Test;</br>');
  writeln(HTMLFile, '{$APPTYPE CONSOLE}</br>');
  writeln(HTMLFile, 'uses</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;Sysutils;</br>');
  writeln(HTMLFile, 'var</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;i : integer;</br>');
  writeln(HTMLFile, 'begin</br>');
  writeln(HTMLFile, '&nbsp;&nbsp; i:= 2;</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;repeat</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;&nbsp;&nbsp;i := i * 2;</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;until i > 1000;</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;writeln(i);</br>');
  writeln(HTMLFile, '&nbsp;&nbsp;readln;</br>');
  writeln(HTMLFile, 'end.');
  writeln(HTMLFile, '</body>');
  writeln(HTMLFile, '</html>');
  closeFile(HTMLFile);
end;

begin
  SaveTestBold;
  //Initialise
  KeyWords[1] := 'program';
  KeyWords[2] := 'const';
  KeyWords[3] := 'var';
  KeyWords[4] := 'begin';
  KeyWords[5] := 'end';
  KeyWords[6] := 'procedure';
  KeyWords[7] := 'function';
  KeyWords[8] := 'uses';
  KeyWords[9] := 'while';
  KeyWords[10] := 'repeat';
  KeyWords[11] := 'until';
  KeyWords[12] := 'do';

  assignFile(IFile, INPUTFILE);
  reset(IFile);
  assignFile(OFile, OUTPUTFILE);
  rewrite(OFile);
  while not eof(IFile) do
    begin
      readln(IFile, CurrentLine);
      CurrentWord := '';
      OutputLine :='';
      IsWord := True;
      if CurrentLine[1] in WordDelimiters then
        IsWord := False;
      for i := 1 to length(CurrentLine) do
        begin
          if IsWord then
            begin
              if not (CurrentLine[i] in WordDelimiters) then
                CurrentWord := CurrentWord + CurrentLine[i]
              else //word is complete
                begin
                  IsWord := False;
                   if ansiMatchText(CurrentWord, KeyWords) then
                    begin
                      CurrentWord := '<strong>' + CurrentWord + '</strong>';
                    end;
                  OutputLine := OutputLine + CurrentWord + CurrentLine[i];
                  CurrentWord := '';
                end; //else
            end //if IsWord
          else //IsWord is False
            begin
              if not (CurrentLine[i] in WordDelimiters) then
                begin
                  CurrentWord := CurrentWord + CurrentLine[i];//first letter
                  IsWord := True;
                end
              else
               OutputLine := OutputLine + CurrentLine[i]
            end; //else IsWord
        end;  //for
        writeln(OFile, OutputLine);
    end; //while
  closeFile(IFile);
  closeFile(OFile);
end.        

Programming - a skill for life!

Introduction to the string manipulation of text files