316 lines
8.5 KiB
ObjectPascal
316 lines
8.5 KiB
ObjectPascal
|
|
{-----------------------------------------------------------------------------
|
|||
|
|
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<EFBFBD>bastien Buysse [sbuysse att buypin dott com]
|
|||
|
|
Portions created by S<EFBFBD>bastien Buysse are Copyright (C) 2001 S<EFBFBD>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.sourceforge.net
|
|||
|
|
|
|||
|
|
Known Issues:
|
|||
|
|
-----------------------------------------------------------------------------}
|
|||
|
|
// $Id: JvStrToHtml.pas 11191 2007-02-17 20:13:16Z ahuser $
|
|||
|
|
|
|||
|
|
unit JvStrToHtml;
|
|||
|
|
|
|||
|
|
{$I jvcl.inc}
|
|||
|
|
|
|||
|
|
interface
|
|||
|
|
|
|||
|
|
uses
|
|||
|
|
{$IFDEF UNITVERSIONING}
|
|||
|
|
JclUnitVersioning,
|
|||
|
|
{$ENDIF UNITVERSIONING}
|
|||
|
|
SysUtils, Classes,
|
|||
|
|
JvComponentBase;
|
|||
|
|
|
|||
|
|
type
|
|||
|
|
TJvStrToHtml = class(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;
|
|||
|
|
|
|||
|
|
{$IFDEF UNITVERSIONING}
|
|||
|
|
const
|
|||
|
|
UnitVersioning: TUnitVersionInfo = (
|
|||
|
|
RCSfile: '$URL: https://jvcl.svn.sourceforge.net/svnroot/jvcl/tags/JVCL3_32/run/JvStrToHtml.pas $';
|
|||
|
|
Revision: '$Revision: 11191 $';
|
|||
|
|
Date: '$Date: 2007-02-17 21:13:16 +0100 (sam., 17 févr. 2007) $';
|
|||
|
|
LogPath: 'JVCL\run'
|
|||
|
|
);
|
|||
|
|
{$ENDIF UNITVERSIONING}
|
|||
|
|
|
|||
|
|
implementation
|
|||
|
|
|
|||
|
|
|
|||
|
|
type
|
|||
|
|
TJvHtmlCodeRec = packed record
|
|||
|
|
Ch: Char;
|
|||
|
|
Html: PChar;
|
|||
|
|
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 [1..75] of TJvHtmlCodeRec = (
|
|||
|
|
(Ch: '"'; Html: '"'),
|
|||
|
|
(Ch: '<'; Html: '<'),
|
|||
|
|
(Ch: '>'; Html: '>'),
|
|||
|
|
(Ch: '^'; Html: 'ˆ'),
|
|||
|
|
(Ch: '~'; Html: '˜'),
|
|||
|
|
(Ch: '<27>'; Html: '£'),
|
|||
|
|
(Ch: '<27>'; Html: '§'),
|
|||
|
|
(Ch: '<27>'; Html: '°'),
|
|||
|
|
(Ch: '<27>'; Html: '²'),
|
|||
|
|
(Ch: '<27>'; Html: '³'),
|
|||
|
|
(Ch: '<27>'; Html: 'µ'),
|
|||
|
|
(Ch: '<27>'; Html: '·'),
|
|||
|
|
(Ch: '<27>'; Html: '¼'),
|
|||
|
|
(Ch: '<27>'; Html: '½'),
|
|||
|
|
(Ch: '<27>'; Html: '¿'),
|
|||
|
|
(Ch: '<27>'; Html: 'À'),
|
|||
|
|
(Ch: '<27>'; Html: 'Á'),
|
|||
|
|
(Ch: '<27>'; Html: 'Â'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ã'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ä'),
|
|||
|
|
(Ch: '<27>'; Html: 'Å'),
|
|||
|
|
(Ch: '<27>'; Html: 'Æ'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ç'),
|
|||
|
|
(Ch: '<27>'; Html: 'È'),
|
|||
|
|
(Ch: '<27>'; Html: 'É'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ê'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ë'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ì'),
|
|||
|
|
(Ch: '<27>'; Html: 'Í'),
|
|||
|
|
(Ch: '<27>'; Html: 'Î'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ï'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ñ'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ò'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ó'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ô'),
|
|||
|
|
(Ch: '<27>'; Html: 'Õ'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ö'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ù'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ú'),
|
|||
|
|
(Ch: '<27>'; Html: 'Û'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ü'),
|
|||
|
|
(Ch: '<27>'; Html: 'Ý'),
|
|||
|
|
(Ch: '<27>'; Html: 'ß'),
|
|||
|
|
(Ch: '<27>'; Html: 'á'),
|
|||
|
|
(Ch: '<27>'; Html: 'à'),
|
|||
|
|
(Ch: '<27>'; Html: 'â'),
|
|||
|
|
(Ch: '<27>'; Html: 'ã'),
|
|||
|
|
(Ch: '<27>'; Html: 'ä'),
|
|||
|
|
(Ch: '<27>'; Html: 'å'),
|
|||
|
|
(Ch: '<27>'; Html: 'æ'),
|
|||
|
|
(Ch: '<27>'; Html: 'ç'),
|
|||
|
|
(Ch: '<27>'; Html: 'é'),
|
|||
|
|
(Ch: '<27>'; Html: 'è'),
|
|||
|
|
(Ch: '<27>'; Html: 'ê'),
|
|||
|
|
(Ch: '<27>'; Html: 'ë'),
|
|||
|
|
(Ch: '<27>'; Html: 'ì'),
|
|||
|
|
(Ch: '<27>'; Html: 'í'),
|
|||
|
|
(Ch: '<27>'; Html: 'î'),
|
|||
|
|
(Ch: '<27>'; Html: 'ï'),
|
|||
|
|
(Ch: '<27>'; Html: 'ñ'),
|
|||
|
|
(Ch: '<27>'; Html: 'ò'),
|
|||
|
|
(Ch: '<27>'; Html: 'ó'),
|
|||
|
|
(Ch: '<27>'; Html: 'ô'),
|
|||
|
|
(Ch: '<27>'; Html: 'õ'),
|
|||
|
|
(Ch: '<27>'; Html: 'ö'),
|
|||
|
|
(Ch: '<27>'; Html: '÷'),
|
|||
|
|
(Ch: '<27>'; Html: 'ù'),
|
|||
|
|
(Ch: '<27>'; Html: 'ú'),
|
|||
|
|
(Ch: '<27>'; Html: 'û'),
|
|||
|
|
(Ch: '<27>'; Html: 'ü'),
|
|||
|
|
(Ch: '<27>'; Html: 'ý'),
|
|||
|
|
(Ch: '<27>'; Html: 'ÿ'),
|
|||
|
|
(Ch: '&'; Html: '&'),
|
|||
|
|
(Ch: '<27>'; Html: '´'),
|
|||
|
|
(Ch: '`'; Html: '`')
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
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 StringToHtml(const Value: string): string;
|
|||
|
|
var
|
|||
|
|
I, J: Integer;
|
|||
|
|
Len, AddLen, HtmlLen: Integer;
|
|||
|
|
P: PChar;
|
|||
|
|
Ch: Char;
|
|||
|
|
begin
|
|||
|
|
Len := Length(Value);
|
|||
|
|
// number of chars to add
|
|||
|
|
AddLen := 0;
|
|||
|
|
for I := 1 to Len do
|
|||
|
|
for J := Low(Conversions) to High(Conversions) do
|
|||
|
|
if Value[I] = Conversions[J].Ch then
|
|||
|
|
begin
|
|||
|
|
Inc(AddLen, StrLen(Conversions[J].Html) - 1);
|
|||
|
|
Break;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
if AddLen = 0 then
|
|||
|
|
Result := Value
|
|||
|
|
else
|
|||
|
|
begin
|
|||
|
|
SetLength(Result, Len + AddLen);
|
|||
|
|
P := Pointer(Result);
|
|||
|
|
for I := 1 to Len do
|
|||
|
|
begin
|
|||
|
|
Ch := Value[I];
|
|||
|
|
for J := Low(Conversions) to High(Conversions) do
|
|||
|
|
if Ch = Conversions[J].Ch then
|
|||
|
|
begin
|
|||
|
|
HtmlLen := StrLen(Conversions[J].Html);
|
|||
|
|
Move(Conversions[J].Html[0], P[0], HtmlLen); // Conversions[].Html is a PChar
|
|||
|
|
Inc(P, HtmlLen);
|
|||
|
|
Ch := #0;
|
|||
|
|
Break;
|
|||
|
|
end;
|
|||
|
|
if Ch <> #0 then
|
|||
|
|
begin
|
|||
|
|
P[0] := Ch;
|
|||
|
|
Inc(P);
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function HtmlToString(const Value: string): string;
|
|||
|
|
var
|
|||
|
|
I, Index, Len: Integer;
|
|||
|
|
Start, J: Integer;
|
|||
|
|
Ch: Char;
|
|||
|
|
ReplStr: string;
|
|||
|
|
begin
|
|||
|
|
Len := Length(Value);
|
|||
|
|
SetLength(Result, Len); // worst case
|
|||
|
|
Index := 0;
|
|||
|
|
I := 1;
|
|||
|
|
while I <= Len do
|
|||
|
|
begin
|
|||
|
|
Ch := Value[I];
|
|||
|
|
// html entitiy
|
|||
|
|
if Ch = '&' then
|
|||
|
|
begin
|
|||
|
|
Start := I;
|
|||
|
|
Inc(I);
|
|||
|
|
while (I <= Len) and (Value[I] <> ';') and (I < Start + 20) do
|
|||
|
|
Inc(I);
|
|||
|
|
if Value[I] <> ';' then
|
|||
|
|
I := Start
|
|||
|
|
else
|
|||
|
|
begin
|
|||
|
|
Ch := #0;
|
|||
|
|
ReplStr := LowerCase(Copy(Value, Start, I - Start + 1));
|
|||
|
|
for J := Low(Conversions) to High(Conversions) do
|
|||
|
|
if Conversions[J].Html = ReplStr then
|
|||
|
|
begin
|
|||
|
|
Ch := Conversions[J].Ch;
|
|||
|
|
Break;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
// if no conversion was found, it may actually be a number
|
|||
|
|
if Ch = #0 then
|
|||
|
|
begin
|
|||
|
|
if StrToIntDef(ReplStr, -1) <> -1 then
|
|||
|
|
begin
|
|||
|
|
Ch := Chr(StrToInt(ReplStr));
|
|||
|
|
end
|
|||
|
|
else
|
|||
|
|
begin
|
|||
|
|
I := Start;
|
|||
|
|
Ch := Value[I];
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
Inc(I);
|
|||
|
|
Inc(Index);
|
|||
|
|
Result[Index] := Ch;
|
|||
|
|
end;
|
|||
|
|
if Index <> Len then
|
|||
|
|
SetLength(Result, Index);
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
function CharToHtml(Ch: Char): string;
|
|||
|
|
var
|
|||
|
|
I: Integer;
|
|||
|
|
begin
|
|||
|
|
for I := Low(Conversions) to High(Conversions) do
|
|||
|
|
if Conversions[I].Ch = Ch then
|
|||
|
|
begin
|
|||
|
|
Result := Conversions[I].Html;
|
|||
|
|
Exit;
|
|||
|
|
end;
|
|||
|
|
Result := Ch;
|
|||
|
|
end;
|
|||
|
|
|
|||
|
|
{$IFDEF UNITVERSIONING}
|
|||
|
|
initialization
|
|||
|
|
RegisterUnitVersion(HInstance, UnitVersioning);
|
|||
|
|
|
|||
|
|
finalization
|
|||
|
|
UnregisterUnitVersion(HInstance);
|
|||
|
|
{$ENDIF UNITVERSIONING}
|
|||
|
|
|
|||
|
|
end.
|
|||
|
|
|