aboutsummarylogtreecommitdiffstats
path: root/ifelseprocessor.pas
blob: a5a87004c418b419bbb482ba6347e81a7f804965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
unit ifelseprocessor;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, RegExpr;

function ProcessIfElse(const Text: string): string;

implementation

function ProcessIfElse(const Text: string): string;
var
  re: TRegExpr;
begin
  re := TRegExpr.Create('@ifelse\[([^\]]+)\](==|!=)\[([^\]]+)\]\{([^\}]*)\}\{([^\}]*)\};');
  try
    Result := Text;
    if re.Exec(Text) then
    begin
      repeat
        // Extract matched groups
        if (re.Match[2] = '==') and (re.Match[1] = re.Match[3]) then
          // Condition true - use left output
          Result := StringReplace(Result, re.Match[0], re.Match[4], [])
        else if (re.Match[2] = '!=') and (re.Match[1] <> re.Match[3]) then
          // Condition true - use left output
          Result := StringReplace(Result, re.Match[0], re.Match[4], [])
        else
          // Condition false - use right output
          Result := StringReplace(Result, re.Match[0], re.Match[5], []);
      until not re.ExecNext;
    end;
  finally
    re.Free;
  end;
end;

end.