Удаление HTML элементов из текста

Материал из DRKB


Как-то раз пришлось решить задачу удаления из файла элементов HTML таких, как, например, ненужные ссылки, и в то же время преобразования возврата каретки в HTML параграфы, знаков табуляции в пробелы и т.д. В результате соответственно должен был получиться новый HTML документ.

Следующие две процедуры показывают, как это можно сделать:


procedure TMainForm.LoadFileIntoList(TextFileName: string; AWebPage: TStringList; WithFilter: Boolean);
var
  CurrentFile: TStringList;
begin
  CurrentFile := TStringList.Create;
  try
    CurrentFile.LoadFromFile(TextFileName);
    if WithFilter then
      FilterHTML(CurrentFile,AWebPage)
    else
      with AWebPage do AddStrings(CurrentFile);
  finally
    CurrentFile.Free;
  end;
end;

procedure TMainForm.FilterHTML(FilterInput, AWebPage: TStringList);
var
  i, j: LongInt;
  S: string;
begin
  FilterMemo.Lines.Clear;
  FilterMemo.Lines := FilterInput;

  with AWebPage do
  begin
    FilterMemo.SelectAll;
    j := FilterMemo.SelLength;

    if j > 0 then
    begin
      i := 0;
      repeat
        if FilterMemo.Lines.GetText[i] = Char(VK_RETURN) then      // ищем cr
          S := S + sLineBreak
        else if FilterMemo.Lines.GetText[i] = '<' then
          repeat
            Inc(i);
          until (FilterMemo.Lines.GetText[i] = '>')
        else if FilterMemo.Lines.GetText[i] = Char(VK_TAB) then // ищем tab
          S := S + '    '
        else
          S := S + FilterMemo.Lines.GetText[i];     // добавляем текст
        Inc(i);
      until i = j+1;
      Add(S);     // добавляем строку в WebPage
    end
    else
      Add('No data entered into field.');   // no data in text file
  end;
end;


Применение функции:

Всё, что нужно сделать - это вызвать :

LoadFileIntoList("filename.txt", Webpage, True);

Где filename - это имя файла, который вы хотите обработать.

"WebPage" - это TStringList

последний параметр в функции указывает, применять или нет HTML-фильтр.


PS: В этом примере объект TMemo (который вызывается из "FilterMemo") лежит на форме и поэтому не видим.

  WebPage := TStringList.Create;
  try
    Screen.Cursor := crHourGlass;
    AddHeader(WebPage);
    with WebPage do
    begin
      Add('Personal Details');
      LoadFileIntoList("filename.txt",Webpage, True);
    end;
    AddFooter(WebPage);
  finally
    WebPage.SaveToFile(HTMLFileName);
    WebPage.Free;
    Screen.Cursor := crDefault;
  end;


Source: Взято из http://forum.sources.ru
ID: 03531