From 31f74f65b082f33402f3e59ca04ed36461c0f1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Van=20Canneyt?= Date: Sat, 2 Apr 2022 10:17:51 +0200 Subject: [PATCH] * CGI demo for css minimizer/classname extractor --- packages/fcl-css/examples/fpcss.lpi | 45 ++++++++++++++ packages/fcl-css/examples/fpcss.pp | 94 +++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 packages/fcl-css/examples/fpcss.lpi create mode 100644 packages/fcl-css/examples/fpcss.pp diff --git a/packages/fcl-css/examples/fpcss.lpi b/packages/fcl-css/examples/fpcss.lpi new file mode 100644 index 0000000000..5f598d1234 --- /dev/null +++ b/packages/fcl-css/examples/fpcss.lpi @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + <UseAppBundle Value="False"/> + <ResourceType Value="res"/> + </General> + <BuildModes> + <Item Name="Default" Default="True"/> + </BuildModes> + <PublishOptions> + <Version Value="2"/> + <UseFileFilters Value="True"/> + </PublishOptions> + <RunParams> + <FormatVersion Value="2"/> + </RunParams> + <Units> + <Unit> + <Filename Value="fpcss.pp"/> + <IsPartOfProject Value="True"/> + </Unit> + </Units> + </ProjectOptions> + <CompilerOptions> + <Version Value="11"/> + <Target> + <Filename Value="fpcss.cgi" ApplyConventions="False"/> + </Target> + <SearchPaths> + <IncludeFiles Value="$(ProjOutDir)"/> + <OtherUnitFiles Value="../src"/> + <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/> + </SearchPaths> + </CompilerOptions> +</CONFIG> diff --git a/packages/fcl-css/examples/fpcss.pp b/packages/fcl-css/examples/fpcss.pp new file mode 100644 index 0000000000..c1d12720d5 --- /dev/null +++ b/packages/fcl-css/examples/fpcss.pp @@ -0,0 +1,94 @@ +{ Demo for CSS engine : CGI to minimize a CSS file or extract class names + + Copyright (C) 2022- michael Van Canneyt michael@freepascal.org + + This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. + + This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + 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 + also obtain it by writing to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. +} + +program fpcss; + +{ $DEFINE USEHTTPAPP} + +uses + SysUtils, classes, fpcssutils, + {$IFDEF USEHTTPAPP} fphttpapp{$ELSE} fpcgi {$ENDIF}, + httpdefs, httproute; + +Function GetCSS(aRequest : TRequest) : TStream; + +begin + Result:=TStringStream.Create(aRequest.Content); +end; + +procedure DoExtract(ARequest: TRequest; AResponse: TResponse); + +Var + S : TStream; + aList : TStrings; + Utils : TCSSUtils; + +begin + S:=Nil; + aList:=Nil; + Utils:=TCSSUtils.Create(Nil); + try + S:=GetCSS(aRequest); + aList:=TstringList.Create; + Utils.ExtractClassNames(S,aList); + aResponse.ContentLength:=Length(aResponse.Content); + aResponse.ContentType:='text/text'; + aResponse.Content:=aList.Text; + aResponse.SendResponse; + finally + aList.Free; + Utils.Free; + S.Free; + end; +end; + + +procedure DoMinimize(ARequest: TRequest; AResponse: TResponse); + +Var + Sin,SOut : TStream; + Utils : TCSSUtils; + +begin + Sin:=Nil; + Sout:=Nil; + Utils:=TCSSUtils.Create(Nil); + try + Sin:=GetCSS(aRequest); + SOut:=TStringStream.Create; + Utils.Minimize(Sin,Sout); + aResponse.ContentLength:=Length(aResponse.Content); + aResponse.ContentType:='text/text'; + aResponse.ContentStream:=SOut; + aResponse.ContentLength:=Sout.Size; + aResponse.SendResponse; + finally + Sout.Free; + Utils.Free; + Sin.Free; + end; +end; + + +begin + HTTPRouter.RegisterRoute('minimize',rmPost,@DoMinimize); + HTTPRouter.RegisterRoute('classnames',rmPost,@DoExtract); + {$IFDEF USEHTTPAPP} + Application.Port:=8080; + {$ENDIF} + Application.Title:='CSS utils CGI'; + Application.Initialize; + Application.Run; +end. +