{ 功能:把文本文件Filename的文本转换成html代码,返回值是生成的html代码,格式如下 result:='<span> 生成的文本的html代码 </span>' } function TextToHtml(const Filename: string):string; var RawText: TStringList; Checker: TStringList; HtmlText: TStringList; I, J, K, L: Integer; S, Str, Tmp, TmpRes: string; const TAB = 9; SPACE = 32; procedure StrSlice(const Str: string; Source: TStrings); var I, J, K: Integer; S: string; Follow: Boolean; begin if (Length(Str) > 1) then begin I := 1; while (I <= Length(Str)) do begin // white if (Str[I] = Chr(SPACE)) or (Str[I] = Chr(TAB)) then begin J := I + 1; Follow := TRUE; while ((J <= Length(Str)) and Follow) do if ((Str[J] = Chr(SPACE)) or (Str[J] = Chr(TAB))) then Inc(J) else Follow := FALSE; S := ''; for K := I to J - 1 do S := S + Str[K]; Source.Add(S); I := J; end else begin J := I + 1; Follow := TRUE; while ((J <= Length(Str)) and Follow) do if ((Str[J] = Chr(SPACE)) or (Str[J] = Chr(TAB))) then Follow := FALSE else Inc(J); S := ''; for K := I to J - 1 do S := S + Str[K]; Source.Add(S); I := J;
end; end; end else Source.Add(Str); end; begin RawText := TStringList.Create; // oryginalny plik Checker := TStringList.Create; // string podzielony semantycznie HtmlText := TStringList.Create; // tekst sformatowany na html
for J := 0 to Checker.Count - 1 do begin if (Checker[J] <> '') then begin TmpRes := Checker[J]; if ((Checker[J][1] = Chr(SPACE)) or (Checker[J][1] = Chr(TAB))) then begin S := '';
if (Length(Checker[J]) = 1) and (J <> 0) then // pojedyncza spacja nie rozpoczynajaca linii Str := Str + ' ' else begin for K := 1 to Length(Checker[J]) do S := S + '&' + 'nbsp' + ';'; Str := Str + S; end; end else begin Tmp := ''; for L := 1 to Length(Checker[J]) do begin if Checker[J][L] = '<' then Tmp := Tmp + '&' + 'lt' + ';' else if Checker[J][L] = '>' then Tmp := Tmp + '&' + 'gt' + ';' else if Checker[J][L] = '&' then Tmp := Tmp + '&' + 'amp' + ';' else Tmp := Tmp + Checker[J][L]; end; Str := Str + Tmp; end;