{ HtmlCode unit Copyright (C) 2012 by Bart Broersma This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version with the following modification: As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules,and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } unit HtmlCode; {$mode objfpc}{$H+} interface uses {CopyLeft,} Classes, SysUtils, Graphics, fpimage{, LCL_Misc}; type THtmlDocType = (DocType_Html_401_Strict, DocType_Html_401_TR, DocType_Html_5, DocType_None); THtmlDocTypeStrings = Array[THtmlDocType] of String; const SDocType_Html401_Strict = ''; SDocType_Html401_TR = ''; SDocType_Html_5 = ''; SDocType_None = ''; HtmlDocTypeStrings: THtmlDocTypeStrings = (SDocType_Html401_Strict, SDocType_Html401_TR, SDocType_Html_5, SDocType_None); html_start = ''; html_end = ''; head_start = ''; head_end = ''; meta_content_charset_utf8 = ''; meta_content_style_type = ''; meta_content_script_type = ''; meta_name_author = ''; meta_name_generator = ''; title_start = ''; title_end = ''; anchor_end = ''; bold_start= ''; bold_end= ''; italic_start= ''; italic_end= ''; underline_start= ''; underline_end= ''; sub_start= ''; sub_end= ''; sup_start= ''; sup_end= ''; emphasis_start = ''; emphasis_end = ''; Strong_start = ''; Strong_end = ''; Code_start = ''; Code_end = ''; Quote_start = ''; Quote_end = ''; BlockQuote_start = '
'; BlockQuote_end = '
'; PRE_start = '
';
   PRE_end = '
'; Comment_start= ''; Script_start = ''; Style_start = ''; Font_end=''; CenterAlign_start= '
'; CenterAlign_end= '
'; LeftAlign_start= '
'; LeftAlign_end= '
'; RightAlign_start= '
'; RightAlign_end= '
'; JustifyAlign_start = '
'; JustifyAlign_end= '
'; Row_start = ''; Row_end = ''; Col_start = ''; Col_end = ''; ColHeading_start = ''; ColHeading_end = ''; Table_end = ''; UnnumberedList_start= ''; NumberedList_start= '
    '; NumberedList_end= '
'; ListItem_start= '
  • '; ListItem_end= '
  • '; WordList_start= '
    '; WordList_end= '
    '; WordTerm_start= '
    '; WordTerm_end= '
    '; WordDef_start= '
    '; WordDef_end= '
    '; Red_start = ''; Green_start = ''; Blue_start = ''; Purple_start = ''; Orange_start = ''; Yellow_start = ''; Black_start = ''; White_start = ''; Gray_start = ''; Maroon_start = ''; Olive_start = ''; Fuchsia_start = ''; Lime_start = ''; Navy_start = ''; Aqua_start = ''; Teal_start = ''; Silver_start = ''; Paragraph_start= '

    '; Paragraph_end= '

    '; Div_start = '
    '; Div_end = '
    '; Span_start = ''; Span_end = ''; LineBreak = '
    ' ; CopyRight = '©'; Registered = '®'; Trademark = '™'; NbSpace = ' '; Lesser = '<'; Greater = '>'; Ampersand = '&'; function Favicon(const ASource: String): String; function StyleSheet(const ASource: String): String; function StyleSheet(const ASource, AMedia: String): String; function Table_Start(const AID, ACLass, ASummary: String): String; function CreateTable(const AID, ACLass, ASummary: String; const ColCount, RowCount: Integer): String; function TabbedTextToHtmlTableContent(const AText: String): String; function Img(const ASource, AId, AClass, AFloatStyle, AWidth, AHeight, AAlt, ATitle: String): String; function CreateHtml(const ADocType: THtmlDocType; const AAuthor, ATitle, AFavicon, AStyleSheet, AInlineStyle: String; const AGeneratorName: String = ''): String; function HtmlToColor(const Value: String): TColor; function ColorToHtml(const AColor: TColor): String; Procedure DoHtmlDirSeparators (Var FileName : String); function UrlEscape(const Url: String): String; implementation const HtmlDirectorySeparator = '/'; function Favicon(const ASource: String): String; begin Result := Format('',[ASource]) + LineEnding + Format('',[ASource]); end; function StyleSheet(const ASource: String): String; begin Result := Format('',[ASource]); end; function StyleSheet(const ASource, AMedia: String): String; begin Result := Format('',[ASource, AMedia]); end; function Table_Start(const AID, ACLass, ASummary: String): String; begin Result := ' '' then Result := Result + ' id="' + AID + '"'; if AClass <> '' then Result := Result + ' class="' + AClass + '"'; if ASummary <> '' then Result := Result + ' summary="' + ASummary + '"'; Result := Result + '>'; end; function CreateTable(const AID, ACLass, ASummary: String; const ColCount, RowCount: Integer): String; const Ident = #32#32; var Row, Col: Integer; begin Result := Table_Start(AID, ACLass, ASummary) + LineEnding; for Row := 1 to RowCount do begin Result := Result + row_start + LineEnding; for Col := 1 to ColCount do begin if (Row = 1) then Result := Result + Ident + ColHeading_start + LineEnding else Result := Result + Ident + Col_start + LineEnding; if (Row = 1) then Result := Result + Ident + ColHeading_end + LineEnding else Result := Result + Ident + Col_end + LineEnding; end; Result := Result + row_end + LineEnding; end; Result := Result + table_end; end; function TabbedTextToHtmlTableContent(const AText: String): String; const Tab = #9; var SL: TStringList; i, p: Integer; Current, S: String; begin Result := ''; //workaround for bug in ClipBoard.GetAsText if (Length(AText) = 0) then exit; SL := TStringList.Create; try p := Pos(#0, AText); if p > 0 then begin SL.Text := Copy(AText,1,p-1); end else SL.Text := AText; for i := 0 to SL.Count - 1 do begin Current := SL.Strings[i]; //debugln('Current = "',Current,'" (',DbgS(Length(Current)),')'); if (Length({Trim}(Current)) > 0) then begin Result := Result + row_start + LineEnding; repeat p := Pos(Tab, Current); if p = 0 then S := Current else S := Copy(Current,1,p-1); if (p > 0) then System.Delete(Current,1,p); //debugln('S = "',S,'" p = ',DbgS(p)); Result := Result + ' ' + col_start + S + col_end + LineEnding; until (p = 0) or (Length(Current) = 0); Result := Result + row_end + LineEnding; end;//Length(Current) > 0 end;//for finally Sl.Free; end; end; function Img(const ASource, AId, AClass, AFloatStyle, AWidth, AHeight, AAlt, ATitle: String): String; var _Alt, _Title: String; begin //alt _Alt := AAlt; _Title := ATitle; if _Alt = '' then _Alt := _Title; if (_Title = '') and (_Alt <> '') then _Title := _Alt; Result := ' '' then Result := Result + ' id="' + AId + '"'; if AClass <> '' then Result := Result + ' class="' + AClass + '"'; if AFloatStyle <> '' then Result := Result + ' style="float:' + AFloatStyle + ';"'; if AWidth <> '' then Result := Result + ' width="' + AWidth + '"'; if AHeight <> '' then Result := Result + ' height="' + AHeight + '"'; if _Alt <> '' then Result := Result + ' alt="' + _Alt + '"'; if _Title <> '' then Result := Result + ' title ="' + _Title + '"'; Result := Result + '>'; end; function CreateHtml(const ADocType: THtmlDocType; const AAuthor, ATitle, AFavicon, AStyleSheet, AInlineStyle: String; const AGeneratorName: String = ''): String; begin Result := HtmlDocTypeStrings[ADocType]; if (Result <> '') then Result := Result + LineEnding; Result := Result + Html_Start + LineEnding; Result := Result + Head_Start + LineEnding + LineEnding; Result := Result + meta_content_charset_utf8 + LineEnding; Result := Result + meta_content_style_type + LineEnding; Result := Result + meta_content_script_type + LineEnding; if (AAuthor <> '') then Result := Result + Format(meta_name_author,[AAuthor]) + LineEnding; if (AGeneratorName <> '') then Result := Result + Format(meta_name_generator,[AGeneratorName]) + LineEnding; Result := Result + LineEnding; Result := Result + Title_Start + ATitle + Title_End + LineEnding + LineEnding; { } if (AFavicon <> '') then Result := Result + Favicon(AFavicon) + LineEnding; if (AStyleSheet <> '') then Result := Result + AStyleSheet + LineEnding; if (AInlineStyle <> '') then Result := Result + LineEnding + AInlineStyle + LineEnding + LineEnding; Result := Result + LineEnding + LineEnding + Head_End + LineEnding; Result := Result + LineEnding + Html_End; end; function HtmlToColor(const Value: String): TColor; begin //ToDo Result := 0; end; { } function ColorToHtml(const AColor: TColor): String; const //uses unthemed original values for this clBlack = TColor($000000); clMaroon = TColor($000080); clGreen = TColor($008000); clOlive = TColor($008080); clNavy = TColor($800000); clPurple = TColor($800080); clTeal = TColor($808000); clGray = TColor($808080); clSilver = TColor($C0C0C0); clRed = TColor($0000FF); clLime = TColor($00FF00); clYellow = TColor($00FFFF); clBlue = TColor($FF0000); clFuchsia = TColor($FF00FF); clAqua = TColor($FFFF00); clLtGray = TColor($C0C0C0); // clSilver alias clDkGray = TColor($808080); // clGray alias clWhite = TColor($FFFFFF); var RgbTriple: TFPColor; begin case AColor of clRed: Result := 'red'; clGreen: Result := 'green'; clBlue: Result := 'blue'; clPurple: Result := 'purple'; clYellow: Result := 'yellow'; clBlack: Result := 'black'; clWhite: Result := 'white'; clGray: Result := 'gray'; clMaroon: Result := 'maroon'; clFuchsia: Result := 'fuchsia'; clLime: Result := 'lime'; clNavy: Result := 'navy'; clAqua: Result := 'aqua'; clTeal: Result := 'teal'; clSilver: Result := 'silver'; else begin RgbTriple := TColorToFPColor(AColor); Result := '#'+ IntToHex(RgbTriple.Red div $FF,2) + IntToHex(RgbTriple.Green div $FF,2) + IntToHex(RgbTriple.Blue div $FF,2); end; end; end; Procedure DoHtmlDirSeparators (Var FileName : String); Var I : longint; begin For I:=1 to Length(FileName) do If FileName[I] in AllowDirectorySeparators then FileName[i] := HtmlDirectorySeparator; end; function UrlEscape(const Url: String): String; //see: http://en.wikipedia.org/wiki/Percent-encoding begin Result := StringReplace(Url, #32, '%20', [rfReplaceAll]); end; end.