Skip to content

Commit 53cff2e

Browse files
committed
Add default & alternate filters.
Add corresponding regression test and documentation.
1 parent 8c2ba5d commit 53cff2e

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

docs/tags.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ parser.
291291
Add a parameter into an URL. This routine adds the '?' and '&'
292292
character if needed. *VALUE* can be a tag variable name.
293293

294+
**ALTERNATE(VALUE)**
295+
.. index:: Filter, ALTERNATE
296+
297+
Returns VALUE if the current value is not the empty string otherwise the
298+
current value is returned (that is, the empty string is returned).
299+
294300
**BR_2_EOL(EOL)**
295301
.. index:: Filter, BR_2_EOL
296302

@@ -326,6 +332,12 @@ parser.
326332

327333
Converts any suite of spaces by a single space character.
328334

335+
**DEFAULT(VALUE)**
336+
.. index:: Filter, DEFAULT
337+
338+
Returns VALUE if the current value is the empty string otherwise the
339+
current value is returned.
340+
329341
**DEL_PARAM(NAME)**
330342
.. index:: Filter, DEL_PARAM
331343

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
DEF
3+
A
4+
5+
6+
ALT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from test_support import *
2+
3+
run('testme', ["testme141.tmplt"])

regtests/tests/0141_default_alternate/test.yaml

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
@_DEFAULT(DEF):VAR4_@
3+
@_DEFAULT(DEF):VAR6_@
4+
5+
@_ALTERNATE(ALT):VAR4_@
6+
@_ALTERNATE(ALT):VAR6_@

src/templates_parser-filter.adb

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------------
22
-- Templates Parser --
33
-- --
4-
-- Copyright (C) 2003-2018, AdaCore --
4+
-- Copyright (C) 2003-2022, AdaCore --
55
-- --
66
-- This library is free software; you can redistribute it and/or modify --
77
-- it under terms of the GNU General Public License as published by the --
@@ -103,6 +103,8 @@ package body Filter is
103103
Web_NBSP_Token : aliased constant String := "WEB_NBSP";
104104
Wrap_Token : aliased constant String := "WRAP";
105105
Yes_No_Token : aliased constant String := "YES_NO";
106+
Default_Token : aliased constant String := "DEFAULT";
107+
Alternate_Token : aliased constant String := "ALTERNATE";
106108

107109
-- Filters Table
108110

@@ -128,6 +130,9 @@ package body Filter is
128130
Add_Param =>
129131
(Add_Param_Token'Access, Add_Param'Access),
130132

133+
Alternate =>
134+
(Alternate_Token'Access, Alternate'Access),
135+
131136
BR_2_EOL =>
132137
(BR_2_EOL_Token'Access, BR_2_EOL'Access),
133138

@@ -146,6 +151,9 @@ package body Filter is
146151
Contract =>
147152
(Contract_Token'Access, Contract'Access),
148153

154+
Default =>
155+
(Default_Token'Access, Default'Access),
156+
149157
Del_Param =>
150158
(Del_Param_Token'Access, Del_Param'Access),
151159

@@ -343,6 +351,28 @@ package body Filter is
343351
end if;
344352
end Add_Param;
345353

354+
---------------
355+
-- Alternate --
356+
---------------
357+
358+
function Alternate
359+
(S : String;
360+
C : not null access Filter_Context;
361+
P : Parameter_Data := No_Parameter) return String
362+
is
363+
pragma Unreferenced (C);
364+
begin
365+
if P = No_Parameter then
366+
raise Template_Error with "missing parameter for ALTERNATE filter";
367+
end if;
368+
369+
if S = "" then
370+
return "";
371+
else
372+
return To_String (P.S);
373+
end if;
374+
end Alternate;
375+
346376
--------------
347377
-- BR_2_EOL --
348378
--------------
@@ -570,6 +600,28 @@ package body Filter is
570600
end if;
571601
end Contract;
572602

603+
-------------
604+
-- Default --
605+
-------------
606+
607+
function Default
608+
(S : String;
609+
C : not null access Filter_Context;
610+
P : Parameter_Data := No_Parameter) return String
611+
is
612+
pragma Unreferenced (C);
613+
begin
614+
if P = No_Parameter then
615+
raise Template_Error with "missing parameter for DEFAULT filter";
616+
end if;
617+
618+
if S = "" then
619+
return To_String (P.S);
620+
else
621+
return S;
622+
end if;
623+
end Default;
624+
573625
---------------
574626
-- Del_Param --
575627
---------------

src/templates_parser.adb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ package body Templates_Parser is
130130
-- Add an HTTP parameter to the string, add the '&' parameter
131131
-- separator if needed.
132132

133+
Alternate,
134+
-- Returns parameter if value is not null
135+
133136
BR_2_EOL,
134137
-- Replaces all <BR> HTML tags by a given end-of-line sequence
135138

@@ -148,6 +151,9 @@ package body Templates_Parser is
148151
Contract,
149152
-- Replaces a suite of spaces by a single space character
150153

154+
Default,
155+
-- Returns parameter if value is empty
156+
151157
Del_Param,
152158
-- Delete an HTTP parameter from the string, removes the '&'
153159
-- Parameter separator if needed.
@@ -394,6 +400,11 @@ package body Templates_Parser is
394400
C : not null access Filter_Context;
395401
P : Parameter_Data := No_Parameter) return String;
396402

403+
function Alternate
404+
(S : String;
405+
C : not null access Filter_Context;
406+
P : Parameter_Data := No_Parameter) return String;
407+
397408
function BR_2_EOL
398409
(S : String;
399410
C : not null access Filter_Context;
@@ -424,6 +435,11 @@ package body Templates_Parser is
424435
C : not null access Filter_Context;
425436
P : Parameter_Data := No_Parameter) return String;
426437

438+
function Default
439+
(S : String;
440+
C : not null access Filter_Context;
441+
P : Parameter_Data := No_Parameter) return String;
442+
427443
function Del_Param
428444
(S : String;
429445
C : not null access Filter_Context;

0 commit comments

Comments
 (0)