Skip to content

Commit

Permalink
Update demo and fix pretty print template bug
Browse files Browse the repository at this point in the history
Fix presentation issue in PrettyPrint when printing local template
Update README with md5 of latest update
Added an initial template in demo to provide some context
  • Loading branch information
darnocian committed Dec 8, 2020
1 parent 6428930 commit 01b7eae
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 27 deletions.
4 changes: 2 additions & 2 deletions demo/VelocityDemo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ A precompiled binary is in the repository to make it easier to demo and test: [S

```
$ md5sum Sempare.Template.Demo.exe
27bdc33aca231225f5a7c2269f84a801 *Sempare.Template.Demo.exe
0a5a5a9d9f0e72b51e7d87bfa53ce37f *Sempare.Template.Demo.exe
$ ls -al Sempare.Template.Demo.exe
-rwxr-xr-x 1 conrad 197121 4061184 Nov 3 14:32 Sempare.Template.Demo.exe*
-rwxr-xr-x 1 conrad 197121 4066304 Dec 8 19:06 Sempare.Template.Demo.exe*
```

Expand Down
Binary file modified demo/VelocityDemo/Sempare.Template.Demo.exe
Binary file not shown.
28 changes: 19 additions & 9 deletions demo/VelocityDemo/Sempare.Template.DemoForm.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,11 @@ object FormRealTime: TFormRealTime
Top = 1
Width = 464
Height = 253
ActivePage = tsPrettyPrint
ActivePage = tsTemplate
Align = alLeft
TabOrder = 0
object tsTemplate: TTabSheet
Caption = 'Template'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
object memoTemplate: TMemo
Left = 0
Top = 0
Expand Down Expand Up @@ -369,10 +365,6 @@ object FormRealTime: TFormRealTime
TabOrder = 1
object tsOutput: TTabSheet
Caption = 'Output'
ExplicitLeft = 0
ExplicitTop = 0
ExplicitWidth = 0
ExplicitHeight = 0
object memoOutput: TMemo
Left = 0
Top = 0
Expand Down Expand Up @@ -521,6 +513,15 @@ object FormRealTime: TFormRealTime
'UTF-8 (with BOM)'
'UTF-8 (without BOM)')
end
object cbAutoEvaluate: TCheckBox
Left = 208
Top = 114
Width = 153
Height = 17
Caption = 'Auto Evaluate'
TabOrder = 11
OnClick = cbConvertTabsToSpacesClick
end
end
object GroupBox1: TGroupBox
Left = 18
Expand All @@ -545,6 +546,15 @@ object FormRealTime: TFormRealTime
OnSetEditText = propertiesSetEditText
end
end
object butEval: TButton
Left = 389
Top = 194
Width = 75
Height = 25
Caption = '&Evaluate'
TabOrder = 7
OnClick = butEvalClick
end
object OpenDialog1: TOpenDialog
Left = 744
Top = 160
Expand Down
88 changes: 75 additions & 13 deletions demo/VelocityDemo/Sempare.Template.DemoForm.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ interface
Sempare.Template.Common,
Vcl.StdCtrls,
Vcl.OleCtrls,
SHDocVw, Vcl.Grids,
SHDocVw,
Vcl.Grids,
Vcl.ComCtrls,
Vcl.ExtCtrls,
Vcl.Imaging.pngimage;
Expand Down Expand Up @@ -92,6 +93,8 @@ TFormRealTime = class(TForm)
lblTitle: TLabel;
properties: TStringGrid;
GroupBox1: TGroupBox;
butEval: TButton;
cbAutoEvaluate: TCheckBox;
procedure cbConvertTabsToSpacesClick(Sender: TObject);
procedure cbStripRecurringSpacesClick(Sender: TObject);
procedure cbTrimLinesClick(Sender: TObject);
Expand All @@ -111,6 +114,7 @@ TFormRealTime = class(TForm)
procedure propertiesSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
procedure butSaveAsClick(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure butEvalClick(Sender: TObject);
private
{ Private declarations }
FEncoding: TEncoding;
Expand Down Expand Up @@ -152,6 +156,20 @@ procedure TFormRealTime.butClearClick(Sender: TObject);
Process;
end;

procedure TFormRealTime.butEvalClick(Sender: TObject);
begin
try
if FFilename <> '' then
butSave.Enabled := true;
FTemplate := Template.Parse(memoTemplate.Lines.Text);
// Template.TemplateText := memoTemplate.Lines.Text;
Process;
except
on e: exception do
memoOutput.Lines.Text := e.Message;
end;
end;

procedure TFormRealTime.butOpenClick(Sender: TObject);
begin
OpenDialog1.DefaultExt := '.template';
Expand Down Expand Up @@ -270,6 +288,9 @@ procedure TFormRealTime.cbSetEncodingClick(Sender: TObject);
procedure TFormRealTime.FormCreate(Sender: TObject);
begin
FContext := Template.Context();
FContext.Variable['name'] := 'world';
properties.Cells[0, 1] := 'name';
properties.Cells[1, 1] := 'world';
FEncoding := TEncoding.UTF8WithoutBOM;
FTemplate := Template.Parse('');
properties.Cells[0, 0] := 'Variable';
Expand All @@ -286,6 +307,52 @@ procedure TFormRealTime.FormCreate(Sender: TObject);
WebBrowser1.Enabled := true;
pcTemplate.ActivePageIndex := 0;
pcOutput.ActivePageIndex := 0;

memoTemplate.Text := '<% template("local_template") %> Hello <% name %><br> <% end %> ' + #13#10 + //
' ' + #13#10 + //
' Welcome to the Sempare Template Engine demo project. ' + #13#10 + //
' ' + #13#10 + //
' You can prototype and test templates here.<p> ' + #13#10 + //
' ' + #13#10 + //
' ' + #13#10 + //
' For HTML output, preview using the brower tab to the right.<p> ' + #13#10 + //
' ' + #13#10 + //
' ' + #13#10 + //
' Press the "Evaluate" button to process this template or enable the "auto evaluate" option to process on every keypress.<p> ' + #13#10 + //
' ' + #13#10 + //
' ' + #13#10 + //
' <% include("local_template") %> ' + #13#10 + //
' ' + #13#10 + //
' This project is available on <a href="https://github.com/sempare/sempare-delphi-template-engine">https://github.com/sempare/sempare-delphi-template-engine</a><p> ' + #13#10 + //
' ' + #13#10 + //
' <% include("local_template") %> ' + #13#10 + //
' ' + #13#10 + //
' ' + #13#10 + //
' Templates can work nicely on almost any Delphi construct.<p> ' + #13#10 + //
' ' + #13#10 + //
' You can have loops:<br> ' + #13#10 + //
' ' + #13#10 + //

' <% for i := 1 to 10 %> ' + #13#10 + //
' <% i %><br> ' + #13#10 + //
' <% end %> ' + #13#10 + //
' <p>' + #13#10 + //
' You can define local variables and have conditional blocks:<br> ' + #13#10 + //
' ' + #13#10 + //

' <% val := 42 %> <-- try change this ' + #13#10 + //
' <% if val = 42 %> ' + #13#10 + //
' the value is 42 ' + #13#10 + //
' <% else %> ' + #13#10 + //
' the value is <b><% val %></b>! ' + #13#10 + //
' <% end %> ' + #13#10 + //
' <p>' + #13#10 + //
' Review the documentation and tests to explore all the features. ' + #13#10 + //
' <p> ' + #13#10 + //
' Otherwise, please raise an issue on github or email <a href="mailto:support@sempare.ltd">support@sempare.ltd</a> for support. ' + #13#10 + //
' ' + #13#10 + //
' If you like this project, please consider supporting enhancements via a commercial license which also entitles you to priority support.<p> ' + #13#10;

Finit := true;
end;

Expand Down Expand Up @@ -313,16 +380,9 @@ procedure TFormRealTime.GridPropsToContext;

procedure TFormRealTime.memoTemplateChange(Sender: TObject);
begin
try
if FFilename <> '' then
butSave.Enabled := true;
FTemplate := Template.Parse(memoTemplate.Lines.Text);
// Template.TemplateText := memoTemplate.Lines.Text;
Process;
except
on e: exception do
memoOutput.Lines.Text := e.Message;
end;
if not cbAutoEvaluate.Checked then
exit;
butEvalClick(Sender);
end;

procedure TFormRealTime.Process;
Expand All @@ -346,12 +406,14 @@ procedure TFormRealTime.Process;

procedure TFormRealTime.propertiesGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
begin
Process;
if cbAutoEvaluate.Checked then
Process;
end;

procedure TFormRealTime.propertiesSetEditText(Sender: TObject; ACol, ARow: Integer; const Value: string);
begin
Process;
if cbAutoEvaluate.Checked then
Process;
end;

procedure TFormRealTime.SetOption(const AEnable: boolean; const AOption: TTemplateEvaluationOption);
Expand Down
6 changes: 3 additions & 3 deletions src/Sempare.Template.PrettyPrint.pas
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ procedure TPrettyPrintTemplateVisitor.Visit(AStmt: IProcessTemplateStmt);
procedure TPrettyPrintTemplateVisitor.Visit(AStmt: IDefineTemplateStmt);
begin
tab();
write('<% define ');
write('<%% define (');
AcceptVisitor(AStmt.Name, self);
writeln(' %>');
writeln(') %%>');
delta(4);
AcceptVisitor(AStmt.Container, self);
delta(-4);
tab();
writeln('<% end %>');
writeln('<%% end %%>');
end;

procedure TPrettyPrintTemplateVisitor.Visit(AStmt: IWithStmt);
Expand Down

0 comments on commit 01b7eae

Please sign in to comment.