(*:Converts pascal source into HTML format with syntax highlighting.
   Highlighting parameters are taked from the registry (from the newest Delphi
   installed).
   This program can be used in two ways:

   pas2html <pas file> <html file> [Delphi version (numeric)]

   - Converts pascal file into html fragment.

   pas2html [Delphi version (numeric)]

   - Takes pascal source from the clipboard, converts it into html fragment and
     puts it back to the clipboard.

   @author Primoz Gabrijelcic
   @desc <pre>

This software is distributed under the BSD license.

Copyright (c) 2003, Primoz Gabrijelcic
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
- The name of the Primoz Gabrijelcic may not be used to endorse or promote
  products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   Author            : Primoz Gabrijelcic
   Creation date     : 2000
   Last modification : 2003-06-19
   Version           : 1.0
   Requires          : SynEdit, http://synedit.sourceforge.net/
</pre>*)(*
   History:
     1.2: 2003-06-19
       - Added support for command line parameters.
*)

{$APPTYPE CONSOLE}

program pas2html;

uses
  Windows,
  SysUtils,
  Classes,
  Clipbrd,
  SynHighlighterPas,
  SynExportHTML;

var
  ALines    : TStringList;
  dVersion  : integer;
  Exp       : TSynExporterHTML;
  idxVersion: integer;
  sl        : TStringList;
  Syn       : TSynPasSyn;

begin
  if ParamCount >= 2 then begin
    sl := TStringList.Create;
    sl.LoadFromFile(ParamStr(1));
    dVersion := StrToIntDef(ParamStr(3), 0);
  end
  else begin
    sl := nil;
    dVersion := StrToIntDef(ParamStr(1), 0);
  end;
  if assigned(sl) or Clipboard.HasFormat(CF_TEXT) then begin
    ALines := TStringList.Create;
    try
      Syn := TSynPasSyn.Create(nil);
      try
        // get syntax highlighter settings
        Syn.EnumUserSettings(ALines);
        if ALines.Count > 0 then begin
          if dVersion > 0 then begin
            idxVersion := ALines.IndexOf(IntToStr(dVersion)+'.0');
            if idxVersion < 0 then
              idxVersion := ALines.Count-1;
          end
          else
            idxVersion := ALines.Count-1;
          Syn.UseUserSettings(idxVersion);
        end;
        // load text from clipboard or file
        if assigned(sl) then
          ALines.Text := sl.Text
        else
          ALines.Text := Clipboard.AsText;
        // export ALines to HTML, as HTML fragment in text format
        Exp := TSynExporterHTML.Create(nil);
        try
          Exp.Highlighter := Syn;
          Exp.ExportAsText := TRUE;
          Exp.CreateHTMLFragment := TRUE;
          Exp.ExportAll(ALines);
          if assigned(sl) then
            Exp.SaveToFile(ParamStr(2))
          else
            Exp.CopyToClipboard;
        finally
          Exp.Free;
        end;
      finally
        Syn.Free;
      end;
    finally
      ALines.Free;
    end;
  end;
  sl.Free;
end.