Forum > LCL
Basic HTML Generation...
commodianus:
I'm trying to come up with a way of shortening the time it takes me to convert (as I'm doing by hand now), certain texts to another format for use in a popular software program.
Basically what I aim to do is parse text from one memo and output the generated code in another memo so I can just copy and paste it into my document.
There's two basic types of parsing I need to do, and I've really no idea how to go about it.
The first thing I need generated, is the HTML <p> and </p> tags. Example:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Would become:
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
The second type of generation I need is thus...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris "nisi ut aliquip ex ea commodo consequat". Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
would become
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris <i>"nisi ut aliquip ex ea commodo consequat"</i>. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Can someone help me out here? It would be very greatly appreciated.
Just a note, I'm using lazarus IDE under Ubuntu. I have setup a two panel form with two memos and a button to make the parsing happen.
eny:
If the above is all you want: go through the lines of the input, insert a <p> as soon as you hit a non-empy line, copy all non-empty lines and insert a </p> at the first empty line (or at the end of the text).
Same for the '"' : go through the lines character by character. At the first '"', insert <i> and at the second insert </i>.
Understanding the concept of state machines can help.
GordonShumway:
If your original (base) text will always be formatted the way you've just provided it and you always want the <p> tag around the paragraph you could open the text from a plain text file into a buffer and start going through it looking for line feeds (linux is the most flexible in terms of new line control characters. So unless you know if your source text is using Carriage Returns or Carriage Returns + Line Feed you will need to compensate.) and the like. Then simply insert the code you need. A quick Example:
--- Code: ---uses
StrUtils;
procedure AddCode;
var
aFile: TextFile;
txtStr: String; // A simple holder.
txtCoded: String; // Final with code added.
sStart, sPos: Integer;
begin
// Of course replace with the real thing.
Assign(aFile, 'TheRealFile.txt');
Reset(aFile); // Open for reading.
// Now we go through line by line.
while not Eof(aFile) do
begin
ReadLn(aFile, txtStr); // We store each new line into the holder variable.
sStart := 0;
sPos := 0; // Beginning of string.
while sStart <> Length(txtStr) do
begin
// We find the first instance of the quotation mark in the string and store it.
sStart := PosEx('"', txtStr, sStart);
// Next we copy the text before the quotation mark, then include your tag.
txtCoded := Copy(txtStr, sPos, (sStart - 1)) + '<i>"';
// Update our current string position to the character immediately AFTER the quotation mark.
sPos := sStart + 1;
// Find the next instance of the quotation mark.
sStart := PosEx('"', txtStr, (sStart + 1));
// Copy everything from right AFTER the opening quotation mark to right BEFORE the closing quotation mark.
// then add our closing quotation mark with your tag.
txtCoded := Copy(txtStr, sPos, (sStart - 1)) + ' " </i>';
// Set the new start position to right AFTER the closing quotation mark.
Inc(sStart);
end;
// Add the newly formated text to your Memo control.
Memo1.Lines.Add(txtCoded);
// Clear our variable.
txtCoded := '';
end;
end;
--- End code ---
NOTICE In the above that we never do copy the quotation marks from the source text, we always added our own!!!
This example is just a simple one, and I haven't tested this iteration but it should give you an idea of how to do what you're looking for. If you need memo to memo support just read from the first Memo line by line and do something similar to the above, and it should appear in your second Memo. Obviously there are faster and better methods available but it sounds like something like this could work for what you're doing. If you need any more help let me know.
Hope it helps. :D
commodianus:
Ok I think I've got something here, though I could use a hand trying to figure out why it's not looping through. I've been pulling my hair out maybe some fresh eyes can help me figure this out. e.g, in a two paragraph setup, the first paragraph is treated properly but the second one gets none.
--- Code: ---procedure TForm1.Button4Click(Sender: TObject);
var
PastLine, LineCount, I: integer;
Holder, Sniff: String;
WasCls, WasOp, HadStart, Trig, FirstLine, LastLine, PrevLine, HadBreak: Boolean;
begin
HadBreak := False;
LineCount := 0;
for I := 0 to Memo1.Lines.Count do
begin
Sniff := Copy(Memo1.Lines[I], 1,1);
LineCount := LineCount +1;
if LineCount = 1 then //First line, insert <p> #13#10
begin
Holder := Memo1.Lines[I];
Memo1.Lines[I] := '<p>' + #13#10 + Holder;
HadBreak := False;
WasOp := True;
WasCls := False;
end else
begin
if LineCount = Memo1.Lines.Count then //Last Line, add </p> & Break
begin
Holder := Memo1.Lines[I];
Memo1.Lines[I] := '</p>' + #13#10#13#10 + Holder;
HadBreak := True;
WasCls := True;
WasOP := False;
end else; //last line
if Sniff = '' then //Line Break Found
begin
if WasOp = False then
begin
if HadBreak = False then //This is our first break. Start Paragraph.
begin
HadBreak := True;
Holder := Memo1.Lines[I];
Memo1.Lines[I] := '<p>' + Holder;
WasOP := True;
WasCls := False;
end else // This is the end of a paragraph.
begin
Holder := Memo1.Lines[I];
Memo1.Lines[I] := '</p>' + Holder;
WasOp := False;
WasCls := True;
end;
end; //</p>
end;
end;
end;
end;
--- End code ---
eny:
No exceptions? 8-)
Make sure to count to Count-1:
--- Code: ---for I := 0 to Memo1.Lines.Count - 1
--- End code ---
Navigation
[0] Message Index
[#] Next page