Closed jarroddavis68 closed 3 years ago
Hi,
There is a context option that you can look at: eoStripRecurringNewlines eoTrimLines
Newlines can be annoying at times. e.g. You can see a scenario described in #34. Although ,I've experienced this in other template engines in other languages too, and the only way is by using options like I've included.
Anyways, if you have something like this: var tpl := 'something'#10#10#10'something';
with the eoStripRecurringNewlines, the result should be: 'something'#10'something'.
But with something like: var tpl := 'something'#10#10#10'something'#10;
the trailing newline should remain and the output would be something like: 'something'#10'something'#10;
So if the template is in a file, the trailing newline should just be omitted.
Hope that helps.
Hmm, I see. Yea I've tried those options before I posted and they do not give me the results I am looking for. Here is a sample template that I'm using:
program <% ProgramName %>;
uses
SysUtils,
GamePascal;
begin
// Add your code here
end.
I really need the file to be formatted as is. These become more important for the larger template file that has some classes and structures. It would seem to me that if it would just NOT add the extra newline all would be right with the world. Why does it have to do so? Can there be an option to turn it off? I have not looked yet, but can you point me where in the code its adding it and I will just comment it out.
ATM, I have to add this code, which works but it just feels hacky and it may break in other scenarios:
SkipLine := False;
for Line in TplCode do
begin
if Line <> '' then
begin
Code.Add(Line);
SkipLine := True;
end
else if Line = '' then
begin
if not SkipLine then
begin
Code.Add(Line);
end;
SkipLine := not SkipLine ;
end;
end;
So far, the template files that I have, it restores the original newlines, but like I said, maybe it breaks with a different or user supplied template.
Hi Jarrod,
Can you double check if the issue is with carriage returns or just with newlines? Last week I did actually fix an issue where I spotted a bug where newlines were sorted ok, but carriage returns were not. More detail below.
Anyways, there is a fix for that on the dev branch. I'll push to master and tag a release next week.
There is also a way to override the custom stream writer now.
These were covered by the #36 and #37. FYI. It was mentioned on a discussion on https://en.delphipraxis.net/topic/3821-ann-sempare-template-engine-for-delphi/
In the dev branch, you can use the property:
var ctx := Template.Context();
ctx.StreamWriterProvider := function(const AStream: TStream; AContext: ITemplateContext): TStreamWriter
begin
result := TStreamWriter.Create(AStream);
end;
This should mean that there should be no mutation re newlines / whitespace as per the options, so it should then just be up to the template logic alone.
Regards, Conrad
Ahh, yes, that was it. I changed to the dev branch and now it works as expected. I didn't need to override the custom stream writer. Nice to know this feature exists an can take advantage of it if ever there is a need. Many thanks. I had originally installed via GetIt inside Delphi.
Awesome!
I'll get the getit updated next week as well.
I've tagged a new release and pushed for an update on Getit. That normally takes a few days.
SWEET!
I just found this project, it's cool, thanks. Using the Parse and Eval, I got it working in my project to insert source code template into the editor for the selected project type. The only problem I'm having is that it will add an extra line. How to prevent this? The template comes in laid out the way I need it to be and don't want any extra lines after eval. What I am doing in the meantime is looping through the lines and remove every other blank line after a non-blank line. Sigh. What can I do to change this behavior?