cssmin.lpr 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. { Demo for CSS engine : minimize a CSS file
  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 cssmin;
  11. {$mode objfpc}{$H+}
  12. uses
  13. Classes, SysUtils, CustApp, fpcssutils;
  14. type
  15. { TCSSMinimizerApplication }
  16. TCSSMinimizerApplication = class(TCustomApplication)
  17. protected
  18. FInputFile,
  19. FOutputFile : String;
  20. procedure DoRun; override;
  21. public
  22. Function ParseOptions : String;
  23. procedure Minimize(const aInputFile,aOutputFile : String);
  24. constructor Create(TheOwner: TComponent); override;
  25. destructor Destroy; override;
  26. procedure Usage(Msg : string); virtual;
  27. end;
  28. { TCSSMinimizerApplication }
  29. procedure TCSSMinimizerApplication.DoRun;
  30. var
  31. ErrorMsg: String;
  32. begin
  33. Terminate;
  34. ErrorMsg:=CheckOptions('hi:o:', ['help','input:','output:']);
  35. if (ErrorMsg='') and not HasOption('h','help') then
  36. ErrorMsg:=ParseOptions;
  37. if HasOption('h','help') or (ErrorMsg<>'') then
  38. begin
  39. Usage(ErrorMsg);
  40. Exit;
  41. end;
  42. Minimize(FInputFile,FOutputFile);
  43. end;
  44. function TCSSMinimizerApplication.ParseOptions: String;
  45. begin
  46. Result:='';
  47. FInputFile:=GetOptionValue('i','input');
  48. FOutputFile:=GetOptionValue('o','output');
  49. if FInputFile='' then
  50. Exit('Need input file');
  51. if FOutputFile='' then
  52. FOutputFile:=ChangeFileExt(FInputFile,'.min.css');
  53. end;
  54. procedure TCSSMinimizerApplication.Minimize(const aInputFile, aOutputFile: String);
  55. Var
  56. SIn,SOut : TMemoryStream;
  57. Util : TCSSUtils;
  58. begin
  59. SOut:=Nil;
  60. Util:=Nil;
  61. SIn:=TMemoryStream.Create;
  62. try
  63. Sin.LoadFromFile(aInputFile);
  64. Sout:=TmemoryStream.Create;
  65. Util:=TCSSUtils.Create(Self);
  66. Util.Minimize(Sin,Sout);
  67. Sout.SaveToFile(aOutputFile);
  68. finally
  69. Sin.Free;
  70. SOut.Free;
  71. Util.Free;
  72. end;
  73. end;
  74. constructor TCSSMinimizerApplication.Create(TheOwner: TComponent);
  75. begin
  76. inherited Create(TheOwner);
  77. StopOnException:=True;
  78. end;
  79. destructor TCSSMinimizerApplication.Destroy;
  80. begin
  81. inherited Destroy;
  82. end;
  83. procedure TCSSMinimizerApplication.Usage(Msg: string);
  84. begin
  85. writeln('Usage: ', ExeName, ' -h');
  86. exitcode:=Ord(Msg<>'')
  87. end;
  88. var
  89. Application: TCSSMinimizerApplication;
  90. begin
  91. Application:=TCSSMinimizerApplication.Create(nil);
  92. Application.Title:='CSS Minimizer Application';
  93. Application.Run;
  94. Application.Free;
  95. end.