htmltoform.lpr 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. {
  2. Copyright (c) 2020 by Michael Van Canneyt [email protected]
  3. This file is part of the pas2js toolset
  4. HTML to pascal code converter program
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. program htmltoform;
  14. uses sysutils, classes, fpjson, jsonparser, sax,sax_html, custapp, formgen, webcoreformgen;
  15. Type
  16. { THTML2FormApplication }
  17. THTML2FormApplication = Class(TCustomApplication)
  18. Private
  19. FConv: THTMLToFormELements ;
  20. FGen : TWebCoreFormCodeGen;
  21. procedure ReadConfigFile(const aFileName: String);
  22. procedure Usage(S: String);
  23. procedure WriteLog(Sender: TObject; const Msg: String);
  24. Protected
  25. Procedure DoRun; override;
  26. Public
  27. Constructor Create(aOwner: TComponent); override;
  28. Destructor Destroy; override;
  29. end;
  30. { TMyApp }
  31. procedure THTML2FormApplication.Usage(S : String);
  32. begin
  33. if S<>'' then
  34. Writeln('Error : ',S);
  35. Writeln('Usage : ',ExtractFileName(ExeName),' -i file -o file [options]');
  36. Writeln('Where options is one or more of: ');
  37. Writeln('-h --help this message');
  38. Writeln('-b --below-id=ID Only create elements for child elements of element ID');
  39. Writeln('-c --config=FILE Read a JSON configuration file with options.');
  40. Writeln('-f --formclass=NAME name of pascal form class');
  41. Writeln('-F --form-file Generate a form file.');
  42. Writeln('-g --getelementfunction=NAME Name of getelement function');
  43. Writeln('-e --events emit code to bind events');
  44. Writeln('-i --input=file html file to read');
  45. writeln('-m --map=file read tag to pascal class map from file');
  46. writeln('-n --no-bind Do not call bindelements in constructor');
  47. writeln('-o --output=file pascal file to write');
  48. Writeln('-p --parentclass=NAME name of pascal form parent class');
  49. Writeln('-x --exclude=List Comma-separated list of IDs to exclude. if starts with @, then load from file');
  50. Halt(Ord(S<>''));
  51. end;
  52. procedure THTML2FormApplication.WriteLog(Sender: TObject; const Msg: String);
  53. begin
  54. Writeln(Msg);
  55. end;
  56. procedure THTML2FormApplication.ReadConfigFile(const aFileName : String);
  57. Var
  58. D : TJSONData;
  59. J : TJSONObject absolute D;
  60. F : TFileStream;
  61. H : THTML2ClassOptions;
  62. begin
  63. D:=Nil;
  64. H:=nil;
  65. F:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
  66. try
  67. D:=GetJSON(F);
  68. if D is TJSONObject then
  69. begin
  70. H:=THTML2ClassOptions.Create;
  71. H.FromJSON(J);
  72. FConv.LoadOptions(H);
  73. FGen.LoadOptions(H);
  74. end;
  75. finally
  76. H.Free;
  77. F.Free;
  78. D.Free;
  79. end;
  80. end;
  81. procedure THTML2FormApplication.DoRun;
  82. var
  83. S,IFN,OFN : String;
  84. begin
  85. StopOnException:=True;
  86. Terminate;
  87. S:=CheckOptions('c:hi:o:f:ep:b:g:x:m:Fndqa',
  88. ['config:','help','input:','output:','formclass:','event',
  89. 'parentclass:','below-id:','getelementfunction:','exclude:',
  90. 'map:','form-file','no-bind','defaultelements','quiet','actionlist']);
  91. if (S<>'') or HasOption('h','help') then
  92. Usage(S);
  93. IFN:=GetOptionValue('i','input');
  94. OFN:=GetOptionValue('o','output');
  95. FConv.DefaultElements:=HasOption('d','defaultelements');
  96. if HasOption('c','config') then
  97. ReadConfigFile(GetOptionValue('c','config'));
  98. if HasOption('f','formclass') then
  99. FGen.FormClassName:=GetOptionValue('f','formclass');
  100. if HasOption('p','parentclass') then
  101. FGen.ParentClassName:=GetOPtionValue('p','parentclass');
  102. if HasOption('g','getelementfunction') then
  103. FGen.GetElementFunction:=GetOptionValue('g','getelementfunction') ;
  104. if HasOption('F','form-file') or hasOption('a','actionlist') then
  105. begin
  106. FGen.Options:=FGen.Options+[foFormFile];
  107. FGen.EventSignature:='Sender : TObject';
  108. FGen.EventModifiers:='';
  109. FGen.AddMethods:=[];
  110. if hasOption('a','actionlist') then
  111. FGen.WebCoreOptions:=FGen.WebCoreOptions+[wcoUseActionList];
  112. end;
  113. if hasOption('e','event') then
  114. FGen.Options:=FGen.Options+[foEvents];
  115. if hasOption('n','no-bind') then
  116. FGen.Options:=FGen.Options-[foBindInConstructor];
  117. if HasOption('m','map') then
  118. begin
  119. FConv.Map.LoadFromFile(GetOptionValue('m','map'));
  120. FConv.DefaultElements:=HasOption('d','defaultelements');
  121. end;
  122. if Not HasOption('q','quiet') then
  123. FCOnv.OnLog:=@WriteLog;
  124. if HasOption('x','exclude') then
  125. begin
  126. S:=GetOPtionValue('x','exclude');
  127. if (S<>'') and (S[1]='@') then
  128. FConv.ExcludeIDS.LoadFromFile(Copy(S,2,Length(S)-1))
  129. else
  130. FConv.ExcludeIDS.CommaText:=S;
  131. end;
  132. if HasOption('b','below-id') then
  133. FConv.BelowID:=GetOptionValue('b','below-id');
  134. if IFN='' then
  135. Usage('Need input file');
  136. if OFN='' then
  137. Usage('Need output file');
  138. FConv.LoadFromFile(IFN);
  139. if FConv.FormElements.Count=0 then
  140. Writeln('No elements found')
  141. else
  142. begin
  143. FGen.FormElements:=FConv.FormElements;
  144. FGen.OutputUnitName:=ChangeFileExt(ExtractFIleName(ofn),'');
  145. FGen.Execute;
  146. FGen.SaveToFile(OFN);
  147. if foFormFile in FGen.Options then
  148. FGen.FormSource.SaveToFile(ChangeFileExt(OFN,'.dfm'));
  149. end;
  150. end;
  151. constructor THTML2FormApplication.Create(aOwner: TComponent);
  152. begin
  153. inherited Create(aOwner);
  154. FConv:=THTMLToFormELements.Create(Self);
  155. FGen:=TWebCoreFormCodeGen.Create(Self);
  156. end;
  157. destructor THTML2FormApplication.Destroy;
  158. begin
  159. FreeAndNil(FGen);
  160. FreeAndNil(FConv);
  161. inherited Destroy;
  162. end;
  163. begin
  164. With THTML2FormApplication.Create(Nil) do
  165. try
  166. Initialize;
  167. Run;
  168. finally
  169. Free;
  170. end;
  171. end.