Skip to content

Commit d32e6ca

Browse files
committed
Add support for generating and displaying deprecated warnings
1 parent dc145b9 commit d32e6ca

File tree

2 files changed

+107
-17
lines changed

2 files changed

+107
-17
lines changed

Diff for: Src/UMain.pas

+16
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ TMain = class(TObject)
3030
fConfig: TConfig; // Program configurations object
3131
fConsole: TConsole; // Object used to write to console
3232
fSignedOn: Boolean; // Flag shows if sign on message has been displayed
33+
fWarnings: TArray<string>; // Command line parser warnings
3334
procedure Configure;
3435
procedure SignOn;
3536
procedure ShowHelp;
37+
procedure ShowWarnings;
3638
function GetInputSourceCode: string;
3739
procedure WriteOutput(const S: string);
3840
public
@@ -69,6 +71,8 @@ procedure TMain.Configure;
6971
Params := TParams.Create(fConfig);
7072
try
7173
Params.Parse; // parse command line, updating configuration object
74+
if Params.HasWarnings then
75+
fWarnings := Params.Warnings;
7276
finally
7377
FreeAndNil(Params);
7478
end;
@@ -107,6 +111,8 @@ procedure TMain.Execute;
107111
begin
108112
// Sign on and initialise program
109113
SignOn;
114+
if Length(fWarnings) > 0 then
115+
ShowWarnings;
110116
SourceCode := GetInputSourceCode;
111117
Renderer := TRendererFactory.CreateRenderer(SourceCode, fConfig);
112118
XHTML := Renderer.Render;
@@ -165,6 +171,16 @@ procedure TMain.ShowHelp;
165171
end;
166172
end;
167173

174+
procedure TMain.ShowWarnings;
175+
var
176+
W: string;
177+
resourcestring
178+
sWarning = 'WARNING: %s';
179+
begin
180+
for W in fWarnings do
181+
fConsole.WriteLn(Format(sWarning, [W]));
182+
end;
183+
168184
procedure TMain.SignOn;
169185
resourcestring
170186
// Sign on message format string

Diff for: Src/UParams.pas

+91-17
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ TParams = class(TObject)
101101
var
102102
fParamQueue: TQueue<string>; // Queue of parameters to be processed
103103
fCmdLookup: TDictionary<string, TCommandId>;
104+
fV1CommandLookup: TList<string>;
104105
fSwitchCmdLookup: TList<string>;
105106
fConfigBlacklist: TList<TCommandId>;
106107
fEncodingLookup: TDictionary<string, TOutputEncodingId>;
@@ -110,6 +111,7 @@ TParams = class(TObject)
110111
fPaddingLookup: TDictionary<string, Char>;
111112
fViewportLookup: TDictionary<string, TViewport>;
112113
fConfig: TConfig; // Reference to program's configuration object
114+
fWarnings: TList<string>;
113115
procedure GetConfigParams;
114116
procedure GetCmdLineParams;
115117
procedure ParseCommand(const IsConfigCmd: Boolean);
@@ -123,10 +125,15 @@ TParams = class(TObject)
123125
UInt16;
124126
function GetPaddingParameter(const Cmd: TCommand): Char;
125127
function GetViewportParameter(const Cmd: TCommand): TViewport;
128+
function GetWarnings: TArray<string>;
129+
function AdjustCommandName(const Name: string; IsCfgCmd: Boolean): string;
130+
function IsV1Command(const Name: string): Boolean;
126131
public
127132
constructor Create(const Config: TConfig);
128133
destructor Destroy; override;
129134
procedure Parse;
135+
property Warnings: TArray<string> read GetWarnings;
136+
function HasWarnings: Boolean;
130137
end;
131138

132139
/// <summary>Type of exception raised for a command related error.</summary>
@@ -166,6 +173,20 @@ implementation
166173

167174
{ TParams }
168175

176+
function TParams.AdjustCommandName(const Name: string;
177+
IsCfgCmd: Boolean): string;
178+
begin
179+
if IsCfgCmd then
180+
begin
181+
if StartsStr('--', Name) then
182+
Result := RightStr(Name, Length(Name) - 2)
183+
else
184+
Result := Name;
185+
end
186+
else
187+
Result := Name;
188+
end;
189+
169190
constructor TParams.Create(const Config: TConfig);
170191
begin
171192
Assert(Assigned(Config), 'TParams.Create: Config is nil');
@@ -231,6 +252,16 @@ constructor TParams.Create(const Config: TConfig);
231252
Add('-rc', siInputClipboard);
232253
Add('-wc', siOutputClipboard);
233254
end;
255+
fV1CommandLookup := TList<string>.Create(
256+
TTextComparer.Create
257+
);
258+
with fV1CommandLookup do
259+
begin
260+
Add('-wc');
261+
Add('-rc');
262+
Add('-hidecss');
263+
Add('-frag');
264+
end;
234265
// list of short form commands that are switches
235266
fSwitchCmdLookup := TList<string>.Create;
236267
with fSwitchCmdLookup do
@@ -322,17 +353,20 @@ constructor TParams.Create(const Config: TConfig);
322353
Add('phone', vpPhone);
323354
Add('tablet', vpPhone);
324355
end;
356+
fWarnings := TList<string>.Create(TTextComparer.Create);
325357
end;
326358

327359
destructor TParams.Destroy;
328360
begin
361+
fWarnings.Free;
329362
fViewportLookup.Free;
330363
fPaddingLookup.Free;
331364
fVerbosityLookup.Free;
332365
fBooleanLookup.Free;
333366
fDocTypeLookup.Free;
334367
fEncodingLookup.Free;
335368
fSwitchCmdLookup.Free;
369+
fV1CommandLookup.Free;
336370
fCmdLookup.Free;
337371
fParamQueue.Free;
338372
inherited;
@@ -467,37 +501,47 @@ function TParams.GetViewportParameter(const Cmd: TCommand): TViewport;
467501
Result := fViewportLookup[Param];
468502
end;
469503

470-
procedure TParams.Parse;
504+
function TParams.GetWarnings: TArray<string>;
505+
var
506+
Idx: Integer;
507+
begin
508+
SetLength(Result, fWarnings.Count);
509+
for Idx := 0 to Pred(fWarnings.Count) do
510+
Result[Idx] := fWarnings[Idx];
511+
end;
471512

472-
procedure ParseQueue(const IsConfigCmd: Boolean; const ErrorFmtStr: string);
513+
function TParams.HasWarnings: Boolean;
514+
begin
515+
Result := fWarnings.Count > 0;
516+
end;
473517

474-
function AdjustCommandName(const Name: string): string;
475-
begin
476-
WriteLn('DBG: ', Name);
477-
if IsConfigCmd then
478-
begin
479-
if StartsStr('--', Name) then
480-
Result := RightStr(Name, Length(Name) - 2)
481-
else
482-
Result := Name;
483-
end
484-
else
485-
Result := Name;
486-
end;
518+
function TParams.IsV1Command(const Name: string): Boolean;
519+
begin
520+
Result := fV1CommandLookup.Contains(Name);
521+
end;
522+
523+
procedure TParams.Parse;
487524

525+
procedure ParseQueue(const IsConfigCmd: Boolean; const ErrorFmtStr: string);
526+
var
527+
CmdName: string;
488528
begin
489529
try
490530
while fParamQueue.Count > 0 do
491531
begin
492532
if AnsiStartsStr('-', fParamQueue.Peek) then
493-
ParseCommand(IsConfigCmd)
533+
begin
534+
CmdName := fParamQueue.Peek;
535+
ParseCommand(IsConfigCmd);
536+
end
494537
else
495538
ParseFileName;
496539
end;
497540
except
498541
on E: ECommandError do
499542
raise Exception.CreateFmt(
500-
ErrorFmtStr, [Format(E.FormatStr, [AdjustCommandName(E.CommandName)])]
543+
ErrorFmtStr,
544+
[Format(E.FormatStr, [AdjustCommandName(E.CommandName, IsConfigCmd)])]
501545
);
502546
on E: Exception do
503547
raise Exception.CreateFmt(ErrorFmtStr, [E.Message]);
@@ -522,6 +566,9 @@ procedure TParams.ParseCommand(const IsConfigCmd: Boolean);
522566
sMustBeSwitch = '%s must be a switch command: append "+" or "-"';
523567
sBlacklisted = 'The "%s" command is not permitted in the config file.'#13#10
524568
+ 'Please edit the file';
569+
// Warnings
570+
sDeprecatedCmd = '*** The "%s" command is deprecated';
571+
sDepDocType = 'The "html4" parameter to the "%s" command deprecated';
525572
var
526573
Command: TCommand;
527574
CommandId: TCommandId;
@@ -541,11 +588,19 @@ procedure TParams.ParseCommand(const IsConfigCmd: Boolean);
541588

542589
case CommandId of
543590
siInputClipboard:
591+
begin
592+
if IsV1Command(Command.Name) then
593+
fWarnings.Add(Format(sDeprecatedCmd, [Command.Name]));
544594
fConfig.InputSource := isClipboard;
595+
end;
545596
siInputStdIn:
546597
fConfig.InputSource := isStdIn;
547598
siOutputClipboard:
599+
begin
600+
if IsV1Command(Command.Name) then
601+
fWarnings.Add(Format(sDeprecatedCmd, [Command.Name]));
548602
fConfig.OutputSink := osClipboard;
603+
end;
549604
siOutputFile:
550605
begin
551606
fConfig.OutputFile := GetStringParameter(Command);
@@ -562,14 +617,30 @@ procedure TParams.ParseCommand(const IsConfigCmd: Boolean);
562617
siOutputDocType:
563618
begin
564619
fConfig.DocType := GetDocTypeParameter(Command);
620+
if fConfig.DocType = dtHTML4 then
621+
fWarnings.Add(
622+
Format(sDepDocType, [AdjustCommandName(Command.Name, IsConfigCmd)])
623+
);
565624
fParamQueue.Dequeue;
566625
end;
567626
siFragment:
627+
begin
628+
if IsV1Command(Command.Name) then
629+
fWarnings.Add(Format(sDeprecatedCmd, [Command.Name]));
568630
fConfig.DocType := dtFragment;
631+
end;
569632
siForceHideCSS:
633+
begin
634+
fWarnings.Add(
635+
Format(sDeprecatedCmd, [AdjustCommandName(Command.Name, IsConfigCmd)])
636+
);
570637
fConfig.HideCSS := True;
638+
end;
571639
siHideCSS:
572640
begin
641+
fWarnings.Add(
642+
Format(sDeprecatedCmd, [AdjustCommandName(Command.Name, IsConfigCmd)])
643+
);
573644
if Command.IsSwitch then
574645
fConfig.HideCSS := Command.SwitchValue
575646
else
@@ -597,6 +668,9 @@ procedure TParams.ParseCommand(const IsConfigCmd: Boolean);
597668
end;
598669
siLegacyCSS:
599670
begin
671+
fWarnings.Add(
672+
Format(sDeprecatedCmd, [AdjustCommandName(Command.Name, IsConfigCmd)])
673+
);
600674
fConfig.LegacyCSSNames := GetBooleanParameter(Command);
601675
fParamQueue.Dequeue;
602676
end;

0 commit comments

Comments
 (0)