Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace string inside a HTML and TXT file with content of Memo

Posted on 2014-10-17
9
Medium Priority
?
290 Views
Last Modified: 2014-10-28
Hi,

I need to replace a string inside a HTML and a TXT file with the content of TMemo.
Here the formatting (soft and hard linebreaks) inside the TMemo must be preserved.
Inside the HTML or TXT file the replacement-string is formatted like %myreplacementcode%.

I've tried using StringReplace but all content inside the memo is than placed on a single line.

Any suggestions?
0
Comment
Question by:Stef Merlijn
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 40386528
Can you show some code, just a few lines how you tried with StringReplace() and it all ended up on a single line.
0
 
LVL 36

Assisted Solution

by:ste5an
ste5an earned 2000 total points
ID: 40386552
The problem with HTML should be obvious: A line break (lf, crlf) in TMemo is not interpreted in HTML as line break. You need to parse those. E.g. by using <br> or a <p>.
0
 

Author Comment

by:Stef Merlijn
ID: 40386823
I started simple:
StringReplace(vHTMLString,'%MyMemoContent%', Memo1.Lines.Text, [rfReplaceAll, rfIgnoreCase]);

Open in new window

Then I converted the Memo to String with following function:
function MemoToStr(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + chr(13);
    Result := Result + Copy((Sender AS TMemo).Lines.Strings[i], 1, Length((Sender AS TMemo).Lines.Strings[i]) -2);
  end;
end;

Open in new window

StringReplace(vHTMLString,'%MyMemoContent%', MemoToStr(Memo1), [rfReplaceAll, rfIgnoreCase]);

Open in new window


So I guess I need to start working on a different kind of parsing.
Any suggestions are more then welkom.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Stef Merlijn
ID: 40393722
This is the way to do it.
function MemoToStrHTML(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + (Sender AS TMemo).Lines.Strings[i];
  end;
end;

Open in new window

0
 

Author Comment

by:Stef Merlijn
ID: 40394336
I've requested that this question be closed as follows:

Accepted answer: 0 points for Delphiwizard's comment #a40393722

for the following reason:

Provided my own answer.
0
 
LVL 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40393839
Nope, a helper should be the first choice:

type
  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

implementation

function StringsHelper.ToHTML: string;
var
  I: Integer;
begin
  for i := 0 to Self.Count - 1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + Self.Strings[i];
  end;
end;

Open in new window


Then it's a nice Memo.Lines.ToHTML.
0
 

Author Comment

by:Stef Merlijn
ID: 40394340
ste5an:
Can you show me how to implement this in real life?
Is it sufficient to add your code to the main unit of the application?
0
 
LVL 36

Expert Comment

by:ste5an
ID: 40394368
Here is a single file sample. Just fill in your code:

unit Unit8;

interface

uses
  System.Classes,
  Vcl.Controls, Vcl.Dialogs, Vcl.Forms, Vcl.StdCtrls;

type
  TForm8 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

  MemoHelper = class helper for TMemo
  public
    function ToHTML: string;
  end;

  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.Button1Click(Sender: TObject);
begin
  ShowMessage(Memo1.ToHTML);
  ShowMessage(Memo1.Lines.ToHTML);
end;

{ MemoHelper }

function MemoHelper.ToHTML: string;
begin
  Result := 'HTML from TMemo';
end;

{ StringsHelper }

function StringsHelper.ToHTML: string;
begin
  Result := 'HTML from TStrings';
end;

end.

Open in new window


The helper classes should be placed in separate files.

Either you a fan of you own big helper file, then you only need one to place in the uses. Or one file per helper base class. So I would place them into Vcl.StdCtrls.Helper.pas and System.Classes.Helper.pas. Or one file per helper class Helper.Memo.pas and Helper.Strings.pas.

The number and naming depends on how you want to organize your namespaces.
0
 

Author Comment

by:Stef Merlijn
ID: 40408136
Thank you ste5an
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question