git-svn-id: https://192.168.0.254/svn/Componentes.Terceros.jvcl@19 7f62d464-2af8-f54e-996c-e91b33f51cbe
338 lines
9.0 KiB
ObjectPascal
338 lines
9.0 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é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: JvStrToHtml.pas 12472 2009-08-24 16:08:33Z 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_39/run/JvStrToHtml.pas $';
|
||
Revision: '$Revision: 12472 $';
|
||
Date: '$Date: 2009-08-24 18:08:33 +0200 (lun., 24 août 2009) $';
|
||
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: '£'; Html: '£'),
|
||
(Ch: '§'; Html: '§'),
|
||
(Ch: '°'; Html: '°'),
|
||
(Ch: '²'; Html: '²'),
|
||
(Ch: '³'; Html: '³'),
|
||
(Ch: 'µ'; Html: 'µ'),
|
||
(Ch: '·'; Html: '·'),
|
||
(Ch: '¼'; Html: '¼'),
|
||
(Ch: '½'; Html: '½'),
|
||
(Ch: '¿'; Html: '¿'),
|
||
(Ch: 'À'; Html: 'À'),
|
||
(Ch: 'Á'; Html: 'Á'),
|
||
(Ch: 'Â'; Html: 'Â'),
|
||
(Ch: 'Ã'; Html: 'Ã'),
|
||
(Ch: 'Ä'; Html: 'Ä'),
|
||
(Ch: 'Å'; Html: 'Å'),
|
||
(Ch: 'Æ'; Html: 'Æ'),
|
||
(Ch: 'Ç'; Html: 'Ç'),
|
||
(Ch: 'È'; Html: 'È'),
|
||
(Ch: 'É'; Html: 'É'),
|
||
(Ch: 'Ê'; Html: 'Ê'),
|
||
(Ch: 'Ë'; Html: 'Ë'),
|
||
(Ch: 'Ì'; Html: 'Ì'),
|
||
(Ch: 'Í'; Html: 'Í'),
|
||
(Ch: 'Î'; Html: 'Î'),
|
||
(Ch: 'Ï'; Html: 'Ï'),
|
||
(Ch: 'Ñ'; Html: 'Ñ'),
|
||
(Ch: 'Ò'; Html: 'Ò'),
|
||
(Ch: 'Ó'; Html: 'Ó'),
|
||
(Ch: 'Ô'; Html: 'Ô'),
|
||
(Ch: 'Õ'; Html: 'Õ'),
|
||
(Ch: 'Ö'; Html: 'Ö'),
|
||
(Ch: 'Ù'; Html: 'Ù'),
|
||
(Ch: 'Ú'; Html: 'Ú'),
|
||
(Ch: 'Û'; Html: 'Û'),
|
||
(Ch: 'Ü'; Html: 'Ü'),
|
||
(Ch: 'Ý'; Html: 'Ý'),
|
||
(Ch: 'ß'; Html: 'ß'),
|
||
(Ch: 'á'; Html: 'á'),
|
||
(Ch: 'à'; Html: 'à'),
|
||
(Ch: 'â'; Html: 'â'),
|
||
(Ch: 'ã'; Html: 'ã'),
|
||
(Ch: 'ä'; Html: 'ä'),
|
||
(Ch: 'å'; Html: 'å'),
|
||
(Ch: 'æ'; Html: 'æ'),
|
||
(Ch: 'ç'; Html: 'ç'),
|
||
(Ch: 'é'; Html: 'é'),
|
||
(Ch: 'è'; Html: 'è'),
|
||
(Ch: 'ê'; Html: 'ê'),
|
||
(Ch: 'ë'; Html: 'ë'),
|
||
(Ch: 'ì'; Html: 'ì'),
|
||
(Ch: 'í'; Html: 'í'),
|
||
(Ch: 'î'; Html: 'î'),
|
||
(Ch: 'ï'; Html: 'ï'),
|
||
(Ch: 'ñ'; Html: 'ñ'),
|
||
(Ch: 'ò'; Html: 'ò'),
|
||
(Ch: 'ó'; Html: 'ó'),
|
||
(Ch: 'ô'; Html: 'ô'),
|
||
(Ch: 'õ'; Html: 'õ'),
|
||
(Ch: 'ö'; Html: 'ö'),
|
||
(Ch: '÷'; Html: '÷'),
|
||
(Ch: 'ù'; Html: 'ù'),
|
||
(Ch: 'ú'; Html: 'ú'),
|
||
(Ch: 'û'; Html: 'û'),
|
||
(Ch: 'ü'; Html: 'ü'),
|
||
(Ch: 'ý'; Html: 'ý'),
|
||
(Ch: 'ÿ'; Html: 'ÿ'),
|
||
(Ch: '&'; Html: '&'),
|
||
(Ch: '´'; 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;
|
||
const
|
||
Nbsp = ' ';
|
||
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
|
||
begin
|
||
Ch := Value[I];
|
||
if Ch = ' ' then
|
||
begin
|
||
if (I > 1) and (Value[I - 1] = ' ') then
|
||
Inc(AddLen, 6 {Length(Nbsp)});
|
||
end
|
||
else
|
||
for J := Low(Conversions) to High(Conversions) do
|
||
if Ch = Conversions[J].Ch then
|
||
begin
|
||
Inc(AddLen, StrLen(Conversions[J].Html) - 1);
|
||
Break;
|
||
end;
|
||
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];
|
||
if Ch = ' ' then
|
||
begin
|
||
if (I > 1) and (Value[I - 1] = ' ') then
|
||
begin
|
||
HtmlLen := 6 {Length(Nbsp)};
|
||
Move(Nbsp[1], P[0], HtmlLen * SizeOf(Char));
|
||
Inc(P, HtmlLen);
|
||
Ch := #0;
|
||
end;
|
||
end
|
||
else
|
||
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 * SizeOf(Char)); // 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));
|
||
if ReplStr = ' ' then // special treatment for  
|
||
Ch := ' '
|
||
else
|
||
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
|
||
Ch := Chr(StrToInt(ReplStr))
|
||
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.
|