htmltoform.lpr 5.7 KB

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