2
0

extractcssclasses.lpr 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, fpcssScanner, 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. if HasOption('x','extended') then
  42. Util.ExtraScannerOptions:=[csoExtendedIdentifiers];
  43. Util.ExtractClassNames(aFileName,L);
  44. if aOutput='' then
  45. begin
  46. if L.Count>0 then
  47. Writeln(L.Text);
  48. end
  49. else
  50. L.SaveToFile(aOutput);
  51. finally
  52. Util.Free;
  53. L.Free;
  54. end;
  55. end;
  56. procedure TCSSClassNamesApplication.DoRun;
  57. Const
  58. Opts = 'ho::x';
  59. LongOpts : Array of string = ('help','output::','extended');
  60. var
  61. FN, FNOutput : String;
  62. ErrorMsg: String;
  63. files : TStringArray;
  64. begin
  65. Terminate;
  66. ErrorMsg:=CheckOptions(Opts,LongOpts);
  67. if HasOption('h','help') or (ErrorMsg<>'') then
  68. begin
  69. Usage(ErrorMsg);
  70. Exit;
  71. end;
  72. Files:=GetNonOptions(Opts,LongOpts);
  73. FNOutput:=GetOptionValue('o','output');
  74. if (FNOutput<>'') and FileExists(FNOutput) then
  75. DeleteFile(FNOutput);
  76. For FN in Files do
  77. OutputClassNames(FN,FNOUtput);
  78. end;
  79. constructor TCSSClassNamesApplication.Create(TheOwner: TComponent);
  80. begin
  81. inherited Create(TheOwner);
  82. StopOnException:=True;
  83. end;
  84. destructor TCSSClassNamesApplication.Destroy;
  85. begin
  86. inherited Destroy;
  87. end;
  88. procedure TCSSClassNamesApplication.Usage(aError: String);
  89. begin
  90. if aError<>'' then
  91. Writeln('Error: ',aError);
  92. Writeln('Usage: ', ExeName, ' [options] FILES');
  93. Writeln('where options is one or more of:');
  94. Writeln('-h --help This help message');
  95. Writeln('-o --output=FILE Write class names to this file.');
  96. Writeln('-x --extended Allow non-standard identifiers.');
  97. exitCode:=Ord(aError<>'');
  98. end;
  99. var
  100. Application: TCSSClassNamesApplication;
  101. begin
  102. Application:=TCSSClassNamesApplication.Create(nil);
  103. Application.Title:='Extract CSS Application';
  104. Application.Run;
  105. Application.Free;
  106. end.