jvcllaz: Add new package JvNetLazR/D (html etc). Add demo HTMLParser.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6658 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2018-09-26 22:57:28 +00:00
parent 1f4d743bc7
commit 92c33dc1ee
25 changed files with 2310 additions and 0 deletions

View File

@ -0,0 +1,259 @@
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvFormToHtml.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com]
Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
// $Id$
unit JvFormToHtml;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Graphics, Controls, Forms, StdCtrls;
// JvComponentBase;
type
TJvFormToHtml = class(TComponent) //TJvComponent)
public
procedure FormToHtml(const Form: TCustomForm; const Filename: string);
end;
implementation
function FontToCss(const Font: TFont): string;
begin
Result := Format(';font-Size:%d;color:#%d;font-weight:', [Font.Size, Font.Color]);
if fsBold in Font.Style then
Result := Result + 'bold;'
else
Result := Result + 'normal;';
Result := Result + 'font-family:' + Font.Name;
end;
procedure TJvFormToHtml.FormToHtml(const Form: TCustomForm; const Filename: string);
var
I, J: Integer;
C: TComponent;
S, S2, St: string;
HTML: TStringList;
begin
HTML := TStringList.Create;
try
HTML.Add('<HTML><BODY>');
for I := 0 to Form.ComponentCount - 1 do
begin
C := Form.Components[I];
St := '';
if C is TLabel then
begin
St := Format('<LABEL style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TLabel(C).Left, TLabel(C).Top, TLabel(C).Height, TLabel(C).Width]) +
FontToCss((C as TLabel).Font) + '"' +
' TITLE="' + (C as TLabel).Hint + '"' +
' NAME=' + (C as TLabel).Name +
'>' +
TLabel(C).Caption + '</LABEL>';
end
else
if C is TButton then
begin
if not TButton(C).Enabled then
S := ' DISABLED'
else
S := '';
St := Format('<BUTTON style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TButton(C).Left, TButton(C).Top, TButton(C).Height, TButton(C).Width]) +
FontToCss(TButton(C).Font) + '"' +
' TITLE="' + TButton(C).Hint + '"' +
' TABORDER=' + IntToStr(TButton(C).TabOrder) +
' NAME=' + TButton(C).Name +
S +
'>' +
TButton(C).Caption + '</BUTTON>';
end
else
if C is TMemo then
begin
S := '';
if TMemo(C).ReadOnly then
S := S + ' ReadOnly';
if not TMemo(C).Enabled then
S := S + ' DISABLED';
S2 := '';
if TMemo(C).WordWrap then
S2 := S2 + ' WRAP=PHYSICAL'
else
S2 := S2 + ' WRAP=OFF';
St := Format('<TEXTAREA style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TMemo(C).Left, TMemo(C).Top, TMemo(C).Height, TMemo(C).Width]) +
FontToCss(TMemo(C).Font) + '"' +
' TITLE="' + TMemo(C).Hint + '"' +
S +
' NAME=' + TMemo(C).Name +
' TABORDER=' + IntToStr(TMemo(C).TabOrder) +
S2 +
'>' +
TMemo(C).Text + '</TEXTAREA>';
end
else
if C is TCheckBox then
begin
S := '';
if not TCheckBox(C).Enabled then
S := S + ' DISABLED';
if TCheckBox(C).Checked then
S := S + ' CHECKED';
St := Format('<INPUT style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TCheckBox(C).Left, TCheckBox(C).Top, TCheckBox(C).Height, 10]) +
FontToCss(TCheckBox(C).Font) + '"' +
' TITLE="' + TCheckBox(C).Hint + '"' +
S +
' TABORDER=' + IntToStr(TCheckBox(C).TabOrder) +
' NAME=' + TCheckBox(C).Name +
' TYPE="CHECKBOX">';
HTML.Add(St);
St := Format('<LABEL style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TCheckBox(C).Left + 13, TCheckBox(C).Top, TCheckBox(C).Height, TCheckBox(C).Width]) +
FontToCss(TCheckBox(C).Font) + '"' +
' TITLE="' + TCheckBox(C).Hint + '"' +
'>' +
TCheckBox(C).Caption + '</LABEL>';
end
else
if C is TRadioButton then
begin
S := '';
if not TRadioButton(C).Enabled then
S := S + ' DISABLED';
if TRadioButton(C).Checked then
S := S + ' CHECKED';
St := Format('<INPUT style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TRadioButton(C).Left, TRadioButton(C).Top, TRadioButton(C).Height, 10]) +
FontToCss(TRadioButton(C).Font) + '"' +
' TITLE="' + TRadioButton(C).Hint + '"' +
S +
' NAME=' + TRadioButton(C).Parent.Name +
' TABORDER=' + IntToStr(TRadioButton(C).TabOrder) +
' TYPE="RADIO">';
HTML.Add(St);
St := Format('<LABEL style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TRadioButton(C).Left + 13, TRadioButton(C).Top,
TRadioButton(C).Height, TRadioButton(C).Width]) +
FontToCss(TRadioButton(C).Font) + '"' +
' TITLE="' + TRadioButton(C).Hint + '"' +
'>' +
TRadioButton(C).Caption + '</LABEL>';
end
else
if C is TEdit then
begin
S := '';
if TEdit(C).ReadOnly then
S := S + ' ReadOnly';
if TEdit(C).MaxLength <> 0 then
S := S + ' MAXLENGTH=' + IntToStr(TEdit(C).MaxLength);
if not TEdit(C).Enabled then
S := S + ' DISABLED';
St := Format('<INPUT TYPE="TEXT" style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TEdit(C).Left, TEdit(C).Top, TEdit(C).Height, TEdit(C).Width]) +
FontToCss(TEdit(C).Font) + '"' +
' TITLE="' + TEdit(C).Hint + '"' +
' TABORDER=' + IntToStr(TEdit(C).TabOrder) +
' NAME=' + TEdit(C).Name +
S +
' Value=' + TEdit(C).Text +
'>';
end
else
if C is TComboBox then
begin
if not TComboBox(C).Enabled then
S := ' DISABLED'
else
S := '';
St := Format('<SELECT style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TComboBox(C).Left, TComboBox(C).Top, TComboBox(C).Height, TComboBox(C).Width]) +
FontToCss(TComboBox(C).Font) + '"' +
' TITLE="' + TComboBox(C).Hint + '"' +
' TABORDER=' + IntToStr(TComboBox(C).TabOrder) +
' NAME=' + TComboBox(C).Name +
S +
'>';
HTML.Add(St);
for J := 0 to TComboBox(C).Items.Count - 1 do
begin
if TComboBox(C).ItemIndex = J then
HTML.Add('<OPTION SELECTED>' + TComboBox(C).Items[J])
else
HTML.Add('<OPTION>' + TComboBox(C).Items[J]);
end;
St := '</SELECT>';
end
else
if C is TListBox then
begin
if not TListBox(C).Enabled then
S := ' DISABLED'
else
S := '';
St := Format('<SELECT style="position:absolute;Left:%d;Top:%d;Height:%d;Width:%d',
[TListBox(C).Left, TListBox(C).Top, TListBox(C).Height, TListBox(C).Width]) +
FontToCss(TListBox(C).Font) + '"' +
' MULTIPLE TITLE="' + TListBox(C).Hint + '"' +
' TABORDER=' + IntToStr(TListBox(C).TabOrder) +
' NAME=' + TListBox(C).Name +
S +
'>';
HTML.Add(St);
for J := 0 to TListBox(C).Items.Count - 1 do
begin
if TListBox(C).ItemIndex = J then
HTML.Add('<OPTION SELECTED>' + TListBox(C).Items[J])
else
HTML.Add('<OPTION>' + TListBox(C).Items[J]);
end;
St := '</SELECT>';
end;
if St <> '' then
HTML.Add(St);
end;
HTML.Add('</BODY></HTML>');
HTML.SaveToFile(Filename);
finally
HTML.Free;
end;
end;
end.

View File

@ -0,0 +1,483 @@
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvHTMLParser.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com]
Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
Alexander Samusenko[sandx att chat dott ru].
CarlEfird.
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
// $Id$
unit JvHtmlParser;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes;
///JclStrings,
// JvComponentBase;
type
PTagInfo = ^TTagInfo;
TTagInfo = record
BeginPos: Integer;
EndPos: Integer;
BeginContext: Integer;
EndContext: Integer;
Key: Integer;
end;
// (rom) definitely needs improvement
TJvParserInfo = class(TObject)
public
StartTag: string;
EndTag: string;
MustBe: Integer;
TakeText: Integer;
end;
TTagInfoList = class(TList)
public
procedure AddValue(const Value: TTagInfo);
procedure Clear; override;
end;
TJvKeyFoundEvent = procedure(Sender: TObject; Key, Results, OriginalLine: string) of object;
TJvKeyFoundExEvent = procedure(Sender: TObject; Key, Results, OriginalLine: string;
TagInfo: TTagInfo; Attributes: TStrings) of object;
TJvHTMLParser = class(TComponent) //TJvComponent)
private
FParser: TStringList;
FKeys: TStringList;
FFileName: TFileName;
FTagList: TTagInfoList;
FContent: string;
FOnKeyFound: TJvKeyFoundEvent;
FOnKeyFoundEx: TJvKeyFoundExEvent;
function GetParser: TStrings;
procedure SetParser(Value: TStrings);
procedure SetFileName(Value: TFileName);
procedure SetTagList(const Value: TTagInfoList);
function GetConditionsCount: Integer;
protected
procedure Loaded; override;
property TagList: TTagInfoList read FTagList write SetTagList;
public
procedure AnalyseString(const Str: string);
procedure AnalyseFile;
procedure AddCondition(const Keyword: string; const StartTag: string = '<';
const EndTag: string = '>'; TextSelection: Integer = 0);
procedure RemoveCondition(Index: Integer);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property ConditionsCount: Integer read GetConditionsCount;
procedure ClearConditions;
procedure GetCondition(Index: Integer; var Keyword, StartTag, EndTag: string); overload;
procedure GetCondition(Index: Integer; var Keyword, StartTag, EndTag: string;
var TextSelection: Integer); overload;
property Content: string read FContent;
published
property FileName: TFileName read FFileName write SetFileName;
property Parser: TStrings read GetParser write SetParser;
property OnKeyFound: TJvKeyFoundEvent read FOnKeyFound write FOnKeyFound;
property OnKeyFoundEx: TJvKeyFoundExEvent read FOnKeyFoundEx write FOnKeyFoundEx;
end;
implementation
uses
StrUtils,
{$IFNDEF COMPILER12_UP}
JvJCLUtils,
{$ENDIF ~COMPILER12_UP}
JvConsts;
{Comparison function. Used internally for observance of the sequences tags}
function CompareTags(Item1, Item2: Pointer): Integer;
begin
Result := (PTagInfo(Item1)^.BeginPos - PTagInfo(Item2)^.BeginPos);
end;
{ Utilities similar to corresponding JCL routines }
function StrFind(const Substr, S: string; const Index: SizeInt = 1): SizeInt;
begin
Result := PosEx(SubStr, S, Index);
end;
//=== { TJvHTMLParser } ======================================================
constructor TJvHTMLParser.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FParser := TStringList.Create;
FKeys := TStringList.Create;
FTagList := TTagInfoList.Create;
end;
destructor TJvHTMLParser.Destroy;
var
Index: Integer;
begin
FParser.Free;
for Index := FKeys.Count - 1 downto 0 do
FKeys.Objects[Index].Free;
FKeys.Free;
FTagList.Free;
inherited Destroy;
end;
procedure TJvHTMLParser.Loaded;
begin
inherited Loaded;
SetParser(FParser);
end;
procedure TJvHTMLParser.SetFileName(Value: TFileName);
begin
if FFileName <> Value then
begin
FFileName := Value;
if not (csDesigning in ComponentState) then
AnalyseFile;
end;
end;
function TJvHTMLParser.GetParser: TStrings;
begin
Result := FParser;
end;
procedure TJvHTMLParser.SetParser(Value: TStrings);
var
I: Integer;
Obj: TJvParserInfo;
Cap: string;
begin
if FParser <> Value then // make sure we don't assign to ourselves (that will clear the list)
FParser.Assign(Value);
for I := FKeys.Count - 1 downto 0 do
FKeys.Objects[I].Free;
FKeys.Clear;
I := 0;
while I < FParser.Count do
begin
Obj := TJvParserInfo.Create;
try
Cap := FParser[I];
Inc(I);
Obj.StartTag := FParser[I];
Inc(I);
Obj.EndTag := FParser[I];
Inc(I);
Obj.MustBe := StrToInt(FParser[I]);
Inc(I);
Obj.TakeText := StrToInt(FParser[I]);
Inc(I);
finally
FKeys.AddObject(Cap, Obj);
end;
end;
end;
// (rom) reimplemented with a TStringList
procedure TJvHTMLParser.AnalyseFile;
var
List: TStringList;
begin
List := TStringList.Create;
try
if FileExists(FileName) then
begin
List.LoadFromFile(FileName);
AnalyseString(List.Text);
end;
finally
List.Free;
end;
end;
procedure TJvHTMLParser.AnalyseString(const Str: string);
var
Str2, Str3: string;
StartTag1, StartTag2: string;
I, J, K, Index: Integer;
TagInfo: TTagInfo;
AttributesList: TStringList;
procedure InnerParseAttributes(Content: PChar; Strings: TStrings);
var
Head, Tail: PChar;
EOS, InQuote, LeadQuote: Boolean;
QuoteChar: Char;
begin
if (Content = nil) or (Content^ = #0) then
Exit;
Tail := Content;
QuoteChar := #0;
repeat
while CharInSet(Tail^, [Cr, Lf, ' ']) do
Inc(Tail);
Head := Tail;
InQuote := False;
LeadQuote := False;
while True do
begin
while (InQuote and not CharInSet(Tail^, [#0, '"'])) or
not CharInSet(Tail^, [#0, Cr, Lf, ' ', '"']) do
Inc(Tail);
if Tail^ = '"' then
begin
if (QuoteChar <> #0) and (QuoteChar = Tail^) then
QuoteChar := #0
else
begin
LeadQuote := Head = Tail;
QuoteChar := Tail^;
if LeadQuote then
Inc(Head);
end;
InQuote := QuoteChar <> #0;
if InQuote then
Inc(Tail)
else
Break;
end
else
Break;
end;
if not LeadQuote and (Tail^ <> #0) and (Tail^ = '"') then
Inc(Tail);
EOS := Tail^ = #0;
Tail^ := #0;
if Head^ <> #0 then
Strings.Add(Head);
Inc(Tail);
until EOS;
end;
procedure ParseAttributes(Strings: TStrings; const Value: string);
var
P: PChar;
Tmp: string;
begin
Strings.Clear;
Tmp := Value;
UniqueString(Tmp);
P := PChar(Tmp);
// if P^ in [#0, '<', '>'] then
if CharInSet(P^, [#0, '>']) then
Exit;
// skip first word (the tag) and any whitespace
while (P^ <> #0) and (P <> nil) do
begin
if P^ = ' ' then
begin
Inc(P);
Break;
end;
Inc(P);
end;
InnerParseAttributes(P, Strings);
end;
begin
if (FKeys.Count = 0) and (FParser.Count <> 0) then
SetParser(FParser);
FContent := Str;
AttributesList := TStringList.Create;
try
if FKeys.Count > 0 then
begin
FTagList.Clear;
for I := 0 to FKeys.Count - 1 do
begin
StartTag1 := TJvParserInfo(FKeys.Objects[I]).StartTag;
Starttag2 := '';
if (Length(StartTag1) > 2) and (StartTag1[Length(StartTag1)] = '>') then
begin
// split the tag so tags with attributes can be found
Delete(StartTag1, Length(StartTag1), 1);
StartTag2 := '>';
end;
J := 1;
while J <> 0 do
begin
// changed from StrSearch(case sensitive) to StrFind (case insensitive)
J := StrFind(StartTag1, Str, J);
if J > 0 then
begin
// changed from StrSearch(case sensitive) to StrFind (case insensitive)
K := StrFind(TJvParserInfo(FKeys.Objects[I]).EndTag, Str, J);
TagInfo.BeginPos := J;
TagInfo.EndPos := K + Length(TJvParserInfo(FKeys.Objects[I]).EndTag);
TagInfo.Key := I;
case TJvParserInfo(FKeys.Objects[I]).TakeText of
0: //Between limits
begin
if StartTag2 = '' then
TagInfo.BeginContext := J + Length(TJvParserInfo(FKeys.Objects[I]).StartTag)
else
// changed from StrSearch(case sensitive) to StrFind (case insensitive)
TagInfo.BeginContext := StrFind(StartTag2, Str, j) + 1;
TagInfo.EndContext := K;
end;
1: //All before start tag
begin
TagInfo.BeginContext := 1;
TagInfo.EndContext := J;
end;
2: //All after start tag
begin
if StartTag2 = '' then
TagInfo.BeginContext := J + Length(TJvParserInfo(FKeys.Objects[I]).StartTag)
else
// changed from StrSearch(case sensitive) to StrFind (case insensitive)
TagInfo.BeginContext := StrFind(StartTag2, Str, j) + 1;
TagInfo.EndContext := Length(Str);
end;
3: //The whole line if containing start tag
begin
TagInfo.BeginContext := J;
// changed from StrSearch(case sensitive) to StrFind (case insensitive)
TagInfo.EndContext := StrFind(Lf, Str, J);
end;
end;
FTagList.AddValue(TagInfo);
J := J + Length(TJvParserInfo(FKeys.Objects[I]).StartTag);
end;
end;
end;
end;
FTagList.Sort(@CompareTags);
with FTagList do
begin
for Index := 0 to Count - 1 do
begin
// Str2 now contains eveything between the start and end tags
Str2 := Copy(Str, PTagInfo(Items[Index])^.BeginContext,
PTagInfo(Items[Index])^.EndContext - PTagInfo(Items[Index])^.BeginContext);
if StartTag2 = '' then
Str3 := ''
else
//Str3 contains the start tag as found, may include attributes or other tags
Str3 := Copy(Str, PTagInfo(Items[Index])^.BeginPos,
PTagInfo(Items[Index])^.BeginContext - PTagInfo(Items[Index])^.BeginPos - 1);
if Assigned(FOnKeyFound) then
FOnKeyFound(Self, FKeys[PTagInfo(Items[Index])^.Key], Str2, Str);
if Assigned(FOnKeyFoundEx) then
begin
if Str3 <> '' then
ParseAttributes(AttributesList, Str3)
else
ParseAttributes(AttributesList, Str2);
FOnKeyFoundEx(Self, FKeys[PTagInfo(Items[Index])^.Key], Str2, Str,
PTagInfo(Items[Index])^, AttributesList);
end;
end;
end;
finally
AttributesList.Free;
end;
end;
procedure TJvHTMLParser.AddCondition(const Keyword: string;
const StartTag: string; const EndTag: string; TextSelection: Integer);
var
Obj: TJvParserInfo;
begin
Obj := TJvParserInfo.Create;
Obj.StartTag := StartTag;
Obj.EndTag := EndTag;
Obj.TakeText := TextSelection;
FKeys.AddObject(Keyword, TObject(Obj));
end;
procedure TJvHTMLParser.RemoveCondition(Index: Integer);
begin
FKeys.Objects[Index].Free;
FKeys.Delete(Index);
end;
procedure TJvHTMLParser.ClearConditions;
var
Index: Integer;
begin
FParser.Clear;
for Index := FKeys.Count - 1 downto 0 do
FKeys.Objects[Index].Free;
FKeys.Clear;
end;
procedure TJvHTMLParser.GetCondition(Index: Integer; var Keyword, StartTag, EndTag: string);
begin
Keyword := FKeys[Index];
StartTag := TJvParserInfo(FKeys.Objects[Index]).StartTag;
EndTag := TJvParserInfo(FKeys.Objects[Index]).EndTag;
end;
procedure TJvHTMLParser.GetCondition(Index: Integer; var Keyword, StartTag, EndTag: string;
var TextSelection: Integer);
begin
GetCondition(Index, Keyword, StartTag, EndTag);
TextSelection := TJvParserInfo(FKeys.Objects[Index]).TakeText;
end;
function TJvHTMLParser.GetConditionsCount: Integer;
begin
Result := FKeys.Count;
end;
procedure TJvHTMLParser.SetTagList(const Value: TTagInfoList);
begin
FTagList := Value;
end;
//=== { TTagInfoList } =======================================================
procedure TTagInfoList.AddValue(const Value: TTagInfo);
var
P: PTagInfo;
begin
GetMem(P, SizeOf(TTagInfo));
if P <> nil then
begin
P^ := Value;
Add(P);
end;
end;
procedure TTagInfoList.Clear;
var
I: Integer;
begin
for I := 0 to Count - 1 do
FreeMem(Items[I], SizeOf(TTagInfo));
inherited Clear;
end;
end.

View File

@ -0,0 +1,160 @@
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvStringListToHtml.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com]
Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
// $Id$
unit JvStringListToHtml;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes;
// JvComponentBase;
type
TJvStringListToHtml = class(TComponent) // TJvComponent)
private
FStrings: TStringList;
FHTML: TStringList;
FHTMLTitle: string;
FHTMLLineBreak: string;
FIncludeHeader: Boolean;
function GetHTML: TStrings;
function GetStrings: TStrings;
procedure SetStrings(const Value: TStrings);
procedure DoStringsChange(Sender: TObject);
procedure SetHTML(const Value: TStrings);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ConvertToHTML(Source: TStrings; const FileName: string);
procedure ConvertToHTMLStrings(Source, Destination: TStrings);
published
property HTML: TStrings read GetHTML write SetHTML stored False;
property Strings: TStrings read GetStrings write SetStrings;
property HTMLLineBreak: string read FHTMLLineBreak write FHTMLLineBreak;
property HTMLTitle: string read FHTMLTitle write FHTMLTitle;
property IncludeHeader: Boolean read FIncludeHeader write FIncludeHeader default True;
end;
implementation
procedure ConvertStringsToHTML(Source, Destination: TStrings; const HTMLTitle, HTMLLineBreak: string; IncludeHeader: Boolean);
var
I: Integer;
begin
if (Source = nil) or (Destination = nil) then
Exit;
Destination.BeginUpdate;
Source.BeginUpdate;
try
if IncludeHeader then
begin
Destination.Add('<HTML><HEAD>');
Destination.Add('<TITLE>' + HTMLTitle + '</TITLE></HEAD>');
Destination.Add('<BODY>');
end;
for I := 0 to Source.Count - 1 do
Destination.Add(Source[I] + HTMLLineBreak);
if IncludeHeader then
begin
Destination.Add('</BODY>');
Destination.Add('</HTML>');
end;
finally
Source.EndUpdate;
Destination.EndUpdate;
end;
end;
procedure TJvStringListToHtml.ConvertToHTML(Source: TStrings; const FileName: string);
var
Dest: TStringList;
begin
if Source = nil then
Exit;
Dest := TStringList.Create;
try
ConvertStringsToHTML(Source, Dest, HTMLTitle, HTMLLineBreak, True);
Dest.SaveToFile(FileName);
finally
Dest.Free;
end;
end;
procedure TJvStringListToHtml.ConvertToHTMLStrings(Source, Destination: TStrings);
begin
ConvertStringsToHTML(Source, Destination, HTMLTitle, HTMLLineBreak, IncludeHeader);
end;
constructor TJvStringListToHtml.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FStrings := TStringList.Create;
FHTML := TStringList.Create;
FStrings.OnChange := @DoStringsChange;
FHTMLLineBreak := '<BR>';
FHTMLTitle := 'Converted by TJvStringListToHtml';
FIncludeHeader := True;
end;
destructor TJvStringListToHtml.Destroy;
begin
FreeAndNil(FStrings);
FreeAndNil(FHTML);
inherited Destroy;
end;
procedure TJvStringListToHtml.DoStringsChange(Sender: TObject);
begin
FreeAndNil(FHTML);
end;
function TJvStringListToHtml.GetHTML: TStrings;
begin
if ComponentState * [csLoading, csDestroying] <> [] then
if FHTML.Count = 0 then
ConvertToHTMLStrings(Strings, FHTML);
Result := FHTML;
end;
procedure TJvStringListToHtml.SetHTML(const Value: TStrings);
begin
// do nothing
end;
function TJvStringListToHtml.GetStrings: TStrings;
begin
Result := FStrings;
end;
procedure TJvStringListToHtml.SetStrings(const Value: TStrings);
begin
FStrings.Assign(Value);
FHTML.Clear;
end;
end.

View File

@ -0,0 +1,492 @@
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvStrToHtml.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com]
Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
Andreas Hausladen [Andreas dott Hausladen att gmx dott de]
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
// $Id$
unit JvStrToHtml;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes;
// JvComponentBase;
type
TJvStrToHtml = class(TComponent) // TJvComponent)
private
FHtml: string;
FValue: string;
procedure SetHtml(const Value: string);
procedure SetValue(const Value: string);
public
constructor Create(AOwner: TComponent); override;
function TextToHtml(const Text: string): string;
function HtmlToText(const Text: string): string;
published
property Text: string read FValue write SetValue;
property Html: string read FHtml write SetHtml;
end;
function StringToHtml(const Value: string): string;
function HtmlToString(const Value: string): string;
//function CharToHtml(Ch: Char): string;
implementation
uses
LazUtf8;
type
TJvHtmlCodeRec = record
Ch: Cardinal;
Html: string;
end;
const
{ References:
http://www.w3.org/TR/REC-html40/charset.html#h-5.3
http://www.w3.org/TR/REC-html40/sgml/entities.html#h-24.2.1
http://www.w3.org/TR/REC-html40/sgml/entities.html#h-24.4.1
}
Conversions: array [0..240] of TJvHtmlCodeRec = (
(Ch: 34; Html: '&quot;'),
(Ch: 38; Html: '&amp;'),
(Ch: 39; Html: '&apos;'),
(Ch: 60; Html: '&lt;'),
(Ch: 62; Html: '&gt;'),
(Ch: 160; Html: '&nbsp;'),
(Ch: 161; Html: '&iexcl;'),
(Ch: 162; Html: '&cent;'),
(Ch: 163; Html: '&pound;'),
(Ch: 164; Html: '&curren;'),
(Ch: 165; Html: '&yen;'),
(Ch: 166; Html: '&brvbar;'),
(Ch: 167; Html: '&sect;'),
(Ch: 168; Html: '&uml;'),
(Ch: 169; Html: '&copy;'),
(Ch: 170; Html: '&ordf;'),
(Ch: 171; Html: '&laquo;'),
(Ch: 172; Html: '&not;'),
(Ch: 173; Html: '&shy;'),
(Ch: 174; Html: '&reg;'),
(Ch: 175; Html: '&macr;'),
(Ch: 176; Html: '&deg;'),
(Ch: 177; Html: '&plusmn;'),
(Ch: 178; Html: '&sup2;'),
(Ch: 179; Html: '&sup3;'),
(Ch: 180; Html: '&acute;'),
(Ch: 181; Html: '&micro;'),
(Ch: 182; Html: '&para;'),
(Ch: 183; Html: '&middot;'),
(Ch: 184; Html: '&cedil;'),
(Ch: 185; Html: '&sup1;'),
(Ch: 186; Html: '&ordm;'),
(Ch: 187; Html: '&raquo;'),
(Ch: 188; Html: '&frac14;'),
(Ch: 189; Html: '&frac12;'),
(Ch: 190; Html: '&frac34;'),
(Ch: 191; Html: '&iquest;'),
(Ch: 192; Html: '&Agrave;'),
(Ch: 193; Html: '&Aacute;'),
(Ch: 194; Html: '&Acirc;'),
(Ch: 195; Html: '&Atilde;'),
(Ch: 196; Html: '&Auml;'),
(Ch: 197; Html: '&Aring;'),
(Ch: 198; Html: '&AElig;'),
(Ch: 199; Html: '&Ccedil;'),
(Ch: 200; Html: '&Egrave;'),
(Ch: 201; Html: '&Eacute;'),
(Ch: 202; Html: '&Ecirc;'),
(Ch: 203; Html: '&Euml;'),
(Ch: 204; Html: '&Igrave;'),
(Ch: 205; Html: '&Iacute;'),
(Ch: 206; Html: '&Icirc;'),
(Ch: 207; Html: '&Iuml;'),
(Ch: 208; Html: '&ETH;'),
(Ch: 209; Html: '&Ntilde;'),
(Ch: 210; Html: '&Ograve;'),
(Ch: 211; Html: '&Oacute;'),
(Ch: 212; Html: '&Ocirc;'),
(Ch: 213; Html: '&Otilde;'),
(Ch: 214; Html: '&Ouml;'),
(Ch: 215; Html: '&times;'),
(Ch: 216; Html: '&Oslash;'),
(Ch: 217; Html: '&Ugrave;'),
(Ch: 218; Html: '&Uacute;'),
(Ch: 219; Html: '&Ucirc;'),
(Ch: 220; Html: '&Uuml;'),
(Ch: 221; Html: '&Yacute;'),
(Ch: 222; Html: '&THORN;'),
(Ch: 223; Html: '&szlig;'),
(Ch: 224; Html: '&agrave;'),
(Ch: 225; Html: '&aacute;'),
(Ch: 226; Html: '&acirc;'),
(Ch: 227; Html: '&atilde;'),
(Ch: 228; Html: '&auml;'),
(Ch: 229; Html: '&aring;'),
(Ch: 230; Html: '&aelig;'),
(Ch: 231; Html: '&ccedil;'),
(Ch: 232; Html: '&egrave;'),
(Ch: 233; Html: '&eacute;'),
(Ch: 234; Html: '&ecirc;'),
(Ch: 235; Html: '&euml;'),
(Ch: 236; Html: '&igrave;'),
(Ch: 237; Html: '&iacute;'),
(Ch: 238; Html: '&icirc;'),
(Ch: 239; Html: '&iuml;'),
(Ch: 240; Html: '&eth;'),
(Ch: 241; Html: '&ntilde;'),
(Ch: 242; Html: '&ograve;'),
(Ch: 243; Html: '&oacute;'),
(Ch: 244; Html: '&ocirc;'),
(Ch: 245; Html: '&otilde;'),
(Ch: 246; Html: '&ouml;'),
(Ch: 247; Html: '&divide;'),
(Ch: 248; Html: '&oslash;'),
(Ch: 249; Html: '&ugrave;'),
(Ch: 250; Html: '&uacute;'),
(Ch: 251; Html: '&ucirc;'),
(Ch: 252; Html: '&uuml;'),
(Ch: 253; Html: '&yacute;'),
(Ch: 254; Html: '&thorn;'),
(Ch: 255; Html: '&yuml;'),
(Ch: 338; Html: '&OElig;'),
(Ch: 339; Html: '&oelig;'),
(Ch: 352; Html: '&Scaron;'),
(Ch: 353; Html: '&scaron;'),
(Ch: 376; Html: '&Yuml;'),
(Ch: 402; Html: '&fnof;'),
(Ch: 710; Html: '&circ;'),
(Ch: 732; Html: '&tilde;'),
(Ch: 913; Html: '&Alpha;'),
(Ch: 914; Html: '&Beta;'),
(Ch: 915; Html: '&Gamma;'),
(Ch: 916; Html: '&Delta;'),
(Ch: 917; Html: '&Epsilon;'),
(Ch: 918; Html: '&Zeta;'),
(Ch: 919; Html: '&Eta;'),
(Ch: 920; Html: '&Theta;'),
(Ch: 921; Html: '&Iota;'),
(Ch: 922; Html: '&Kappa;'),
(Ch: 923; Html: '&Lambda;'),
(Ch: 924; Html: '&Mu;'),
(Ch: 925; Html: '&Nu;'),
(Ch: 926; Html: '&Xi;'),
(Ch: 927; Html: '&Omicron;'),
(Ch: 928; Html: '&Pi;'),
(Ch: 929; Html: '&Rho;'),
(Ch: 931; Html: '&Sigma;'),
(Ch: 932; Html: '&Tau;'),
(Ch: 933; Html: '&Upsilon;'),
(Ch: 934; Html: '&Phi;'),
(Ch: 935; Html: '&Chi;'),
(Ch: 936; Html: '&Psi;'),
(Ch: 937; Html: '&Omega;'),
(Ch: 945; Html: '&alpha;'),
(Ch: 946; Html: '&beta;'),
(Ch: 947; Html: '&gamma;'),
(Ch: 948; Html: '&delta;'),
(Ch: 949; Html: '&epsilon;'),
(Ch: 950; Html: '&zeta;'),
(Ch: 951; Html: '&eta;'),
(Ch: 952; Html: '&theta;'),
(Ch: 953; Html: '&iota;'),
(Ch: 954; Html: '&kappa;'),
(Ch: 955; Html: '&lambda;'),
(Ch: 956; Html: '&mu;'),
(Ch: 957; Html: '&nu;'),
(Ch: 958; Html: '&xi;'),
(Ch: 959; Html: '&omicron;'),
(Ch: 960; Html: '&pi;'),
(Ch: 961; Html: '&rho;'),
(Ch: 962; Html: '&sigmaf;'),
(Ch: 963; Html: '&sigma;'),
(Ch: 964; Html: '&tau;'),
(Ch: 965; Html: '&upsilon;'),
(Ch: 966; Html: '&phi;'),
(Ch: 967; Html: '&chi;'),
(Ch: 968; Html: '&psi;'),
(Ch: 969; Html: '&omega;'),
(Ch: 977; Html: '&thetasym;'),
(Ch: 978; Html: '&upsih;'),
(Ch: 982; Html: '&piv;'),
(Ch: 8194; Html: '&ensp;'),
(Ch: 8195; Html: '&emsp;'),
(Ch: 8201; Html: '&thinsp;'),
(Ch: 8204; Html: '&zwnj;'),
(Ch: 8205; Html: '&zwj;'),
(Ch: 8206; Html: '&lrm;'),
(Ch: 8207; Html: '&rlm;'),
(Ch: 8211; Html: '&ndash;'),
(Ch: 8212; Html: '&mdash;'),
(Ch: 8216; Html: '&lsquo;'),
(Ch: 8217; Html: '&rsquo;'),
(Ch: 8218; Html: '&sbquo;'),
(Ch: 8220; Html: '&ldquo;'),
(Ch: 8221; Html: '&rdquo;'),
(Ch: 8222; Html: '&bdquo;'),
(Ch: 8224; Html: '&dagger;'),
(Ch: 8225; Html: '&Dagger;'),
(Ch: 8226; Html: '&bull;'),
(Ch: 8230; Html: '&hellip;'),
(Ch: 8240; Html: '&permil;'),
(Ch: 8242; Html: '&prime;'),
(Ch: 8243; Html: '&Prime;'),
(Ch: 8249; Html: '&lsaquo;'),
(Ch: 8250; Html: '&rsaquo;'),
(Ch: 8254; Html: '&oline;'),
(Ch: 8364; Html: '&euro;'),
(Ch: 8482; Html: '&trade;'),
(Ch: 8592; Html: '&larr;'),
(Ch: 8593; Html: '&uarr;'),
(Ch: 8594; Html: '&rarr;'),
(Ch: 8595; Html: '&darr;'),
(Ch: 8596; Html: '&harr;'),
(Ch: 8629; Html: '&crarr;'),
(Ch: 8704; Html: '&forall;'),
(Ch: 8706; Html: '&part;'),
(Ch: 8707; Html: '&exist;'),
(Ch: 8709; Html: '&empty;'),
(Ch: 8711; Html: '&nabla;'),
(Ch: 8712; Html: '&isin;'),
(Ch: 8713; Html: '&notin;'),
(Ch: 8715; Html: '&ni;'),
(Ch: 8719; Html: '&prod;'),
(Ch: 8721; Html: '&sum;'),
(Ch: 8722; Html: '&minus;'),
(Ch: 8727; Html: '&lowast;'),
(Ch: 8730; Html: '&radic;'),
(Ch: 8733; Html: '&prop;'),
(Ch: 8734; Html: '&infin;'),
(Ch: 8736; Html: '&ang;'),
(Ch: 8743; Html: '&and;'),
(Ch: 8744; Html: '&or;'),
(Ch: 8745; Html: '&cap;'),
(Ch: 8746; Html: '&cup;'),
(Ch: 8747; Html: '&int;'),
(Ch: 8756; Html: '&there4;'),
(Ch: 8764; Html: '&sim;'),
(Ch: 8773; Html: '&cong;'),
(Ch: 8776; Html: '&asymp;'),
(Ch: 8800; Html: '&ne;'),
(Ch: 8801; Html: '&equiv;'),
(Ch: 8804; Html: '&le;'),
(Ch: 8805; Html: '&ge;'),
(Ch: 8834; Html: '&sub;'),
(Ch: 8835; Html: '&sup;'),
(Ch: 8836; Html: '&nsub;'),
(Ch: 8838; Html: '&sube;'),
(Ch: 8839; Html: '&supe;'),
(Ch: 8853; Html: '&oplus;'),
(Ch: 8855; Html: '&otimes;'),
(Ch: 8869; Html: '&perp;'),
(Ch: 8901; Html: '&sdot;'),
(Ch: 8968; Html: '&lceil;'),
(Ch: 8969; Html: '&rceil;'),
(Ch: 8970; Html: '&lfloor;'),
(Ch: 8971; Html: '&rfloor;'),
(Ch: 9674; Html: '&loz;'),
(Ch: 9824; Html: '&spades;'),
(Ch: 9827; Html: '&clubs;'),
(Ch: 9829; Html: '&hearts;'),
(Ch: 9830; Html: '&diams;')
);
var
ConversionsHash: array of Word;
{$IFNDEF UNICODE}
const
MB_ERR_INVALID_CHARS = 8;
{$ENDIF ~UNICODE}
{ TJvStrToHtml }
constructor TJvStrToHtml.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FValue := '';
FHtml := '';
end;
function TJvStrToHtml.HtmlToText(const Text: string): string;
begin
Result := HtmlToString(Text);
end;
procedure TJvStrToHtml.SetHtml(const Value: string);
begin
FValue := HtmlToText(Value);
end;
procedure TJvStrToHtml.SetValue(const Value: string);
begin
FHtml := TextToHtml(Value);
end;
function TJvStrToHtml.TextToHtml(const Text: string): string;
begin
Result := StringToHtml(Text);
end;
function GetHtmlHash(const S: string): Word;
var
I: Integer;
begin
Result := Length(S);
for I := 1 to Length(S) do
Result := Word(Result + Ord(S[I]) shl (I mod 4));
end;
procedure InitConversionsHash;
var
I: Integer;
begin
SetLength(ConversionsHash, Length(Conversions));
for I := 0 to High(ConversionsHash) do
ConversionsHash[I] := GetHtmlHash(Conversions[I].Html);
end;
function StringToHtml(const Value: String): String;
var
ResultLen: Integer;
CurrPos: Integer;
P, PEnd: PChar;
procedure Append(s: String);
var
n: Integer;
begin
n := Length(s);
if CurrPos + n > ResultLen then begin
ResultLen := ResultLen + 100;
SetLength(Result, ResultLen);
end;
Move(s[1], Result[CurrPos], n);
inc(CurrPos, n);
end;
var
J, n: Integer;
ch: Cardinal;
found: Boolean;
begin
if Value = '' then begin
Result := '';
exit;
end;
ResultLen := Length(Value);
SetLength(Result, ResultLen);
P := @Value[1];
PEnd := @Value[ResultLen];
CurrPos := 1;
while P <= PEnd do begin
n := 1;
if P^ in ['a'..'z', 'A'..'Z', '0'..'9', '_', ' '] then
Append(P^)
else begin
ch := UTF8CodePointToUniCode(P, n);
found := false;
for J := Low(Conversions) to High(Conversions) do
if ch = Conversions[J].Ch then begin
Append(Conversions[J].Html);
found := true;
break;
end;
if not found then
Append(Format('&#%d;', [ch]));
end;
inc(P, n);
end;
SetLength(Result, CurrPos-1);
end;
function HtmlToString(const Value: String): String;
var
ResultLen: Integer;
P, PEnd: PChar;
CurrPos: Integer;
procedure Append(s: String);
var
n: Integer;
begin
n := Length(s);
Move(s[1], Result[CurrPos], n);
inc(CurrPos, n);
end;
var
html: String;
found: Boolean;
J: Integer;
begin
if Value = '' then begin
Result := '';
exit;
end;
ResultLen := Length(Value);
SetLength(Result, ResultLen);
P := @Value[1];
PEnd := @Value[ResultLen];
CurrPos := 1;
while P <= PEnd do begin
if P^ = '&' then begin
html := '&';
while P < PEnd do begin
inc(P);
html := html + P^;
if P^ = ';' then break;
end;
found := false;
for J := Low(Conversions) to High(Conversions) do
if html = Conversions[J].Html then begin
Append(UnicodeToUTF8(Conversions[j].Ch));
found := true;
break;
end;
if not found then begin
Delete(html, Length(html), 1);
Delete(html, 1, 1);
if html[1] = 'x' then html[1] := '$';
Append(UnicodeToUTF8(StrToInt(html)));
end;
end else
Append(P^);
inc(P);
end;
SetLength(Result, CurrPos-1);
end;
end.