fpcss.pp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. { Demo for CSS engine : CGI to minimize a CSS file or extract class names
  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 fpcss;
  11. { $DEFINE USEHTTPAPP}
  12. uses
  13. SysUtils, classes, fpcssutils,
  14. {$IFDEF USEHTTPAPP} fphttpapp{$ELSE} fpcgi {$ENDIF},
  15. httpdefs, httproute;
  16. Function GetCSS(aRequest : TRequest) : TStream;
  17. begin
  18. Result:=TStringStream.Create(aRequest.Content);
  19. end;
  20. procedure DoExtract(ARequest: TRequest; AResponse: TResponse);
  21. Var
  22. S : TStream;
  23. aList : TStrings;
  24. Utils : TCSSUtils;
  25. begin
  26. S:=Nil;
  27. aList:=Nil;
  28. Utils:=TCSSUtils.Create(Nil);
  29. try
  30. S:=GetCSS(aRequest);
  31. aList:=TstringList.Create;
  32. Utils.ExtractClassNames(S,aList);
  33. aResponse.ContentLength:=Length(aResponse.Content);
  34. aResponse.ContentType:='text/text';
  35. aResponse.Content:=aList.Text;
  36. aResponse.SendResponse;
  37. finally
  38. aList.Free;
  39. Utils.Free;
  40. S.Free;
  41. end;
  42. end;
  43. procedure DoMinimize(ARequest: TRequest; AResponse: TResponse);
  44. Var
  45. Sin,SOut : TStream;
  46. Utils : TCSSUtils;
  47. begin
  48. Sin:=Nil;
  49. Sout:=Nil;
  50. Utils:=TCSSUtils.Create(Nil);
  51. try
  52. Sin:=GetCSS(aRequest);
  53. SOut:=TStringStream.Create;
  54. Utils.Minimize(Sin,Sout);
  55. aResponse.ContentLength:=Length(aResponse.Content);
  56. aResponse.ContentType:='text/text';
  57. aResponse.ContentStream:=SOut;
  58. aResponse.ContentLength:=Sout.Size;
  59. aResponse.SendResponse;
  60. finally
  61. Sout.Free;
  62. Utils.Free;
  63. Sin.Free;
  64. end;
  65. end;
  66. begin
  67. HTTPRouter.RegisterRoute('minimize',rmPost,@DoMinimize);
  68. HTTPRouter.RegisterRoute('classnames',rmPost,@DoExtract);
  69. {$IFDEF USEHTTPAPP}
  70. Application.Port:=8080;
  71. {$ENDIF}
  72. Application.Title:='CSS utils CGI';
  73. Application.Initialize;
  74. Application.Run;
  75. end.