extractcssclasses.lpr 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. { Demo for CSS engine : extract class names from a list of CSS files
  2. Copyright (C) 2022- michael Van Canneyt [email protected]
  3. This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
  4. published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  5. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  6. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. A copy of the GNU General Public License is available on the World Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can
  8. also obtain it by writing to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
  9. }
  10. program extractcssclasses;
  11. {$mode objfpc}{$H+}
  12. uses
  13. Classes, SysUtils, CustApp, fpcssutils, fpcsstree;
  14. type
  15. { TCSSClassNamesApplication }
  16. TCSSClassNamesApplication = class(TCustomApplication)
  17. private
  18. procedure OutputClassNames(const aFileName, aOutput: String);
  19. protected
  20. procedure DoRun; override;
  21. public
  22. constructor Create(TheOwner: TComponent); override;
  23. destructor Destroy; override;
  24. procedure Usage(aError : String); virtual;
  25. end;
  26. { TCSSClassNamesApplication }
  27. procedure TCSSClassNamesApplication.OutputClassNames(const aFileName,aOutput : String);
  28. Var
  29. L : TStringList;
  30. Util : TCSSUtils;
  31. begin
  32. Util:=Nil;
  33. L:=TStringList.Create;
  34. try
  35. Util:=TCSSUtils.Create(Self);
  36. L.Sorted:=True;
  37. L.Duplicates:=dupIgnore;
  38. L.Options:=L.Options-[soTrailingLineBreak];
  39. if (aOutput<>'') and FileExists(aOutput) then
  40. L.LoadFromFile(aOutput);
  41. Util.ExtractClassNames(aFileName,L);
  42. if aOutput='' then
  43. begin
  44. if L.Count>0 then
  45. Writeln(L.Text);
  46. end
  47. else
  48. L.SaveToFile(aOutput);
  49. finally
  50. Util.Free;
  51. L.Free;
  52. end;
  53. end;
  54. procedure TCSSClassNamesApplication.DoRun;
  55. Const
  56. Opts = 'ho::';
  57. LongOpts : Array of string = ('help','output::');
  58. var
  59. FN, FNOutput : String;
  60. ErrorMsg: String;
  61. files : TStringArray;
  62. begin
  63. Terminate;
  64. ErrorMsg:=CheckOptions(Opts,LongOpts);
  65. if HasOption('h','help') or (ErrorMsg<>'') then
  66. begin
  67. Usage(ErrorMsg);
  68. Exit;
  69. end;
  70. Files:=GetNonOptions(Opts,LongOpts);
  71. FNOutput:=GetOptionValue('o','output');
  72. if (FNOutput<>'') and FileExists(FNOutput) then
  73. DeleteFile(FNOutput);
  74. For FN in Files do
  75. OutputClassNames(FN,FNOUtput);
  76. end;
  77. constructor TCSSClassNamesApplication.Create(TheOwner: TComponent);
  78. begin
  79. inherited Create(TheOwner);
  80. StopOnException:=True;
  81. end;
  82. destructor TCSSClassNamesApplication.Destroy;
  83. begin
  84. inherited Destroy;
  85. end;
  86. procedure TCSSClassNamesApplication.Usage(aError: String);
  87. begin
  88. if aError<>'' then
  89. Writeln('Error: ',aError);
  90. Writeln('Usage: ', ExeName, ' [options] FILES');
  91. Writeln('where options is one or more of:');
  92. Writeln('-h --help This help message');
  93. Writeln('-o --output=FILE Write class names to this file.');
  94. exitCode:=Ord(aError<>'');
  95. end;
  96. var
  97. Application: TCSSClassNamesApplication;
  98. begin
  99. Application:=TCSSClassNamesApplication.Create(nil);
  100. Application.Title:='Extract CSS Application';
  101. Application.Run;
  102. Application.Free;
  103. end.