htmltoform.lpr 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, 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. begin
  55. end;
  56. procedure THTML2FormApplication.DoRun;
  57. var
  58. S,IFN,OFN : String;
  59. begin
  60. StopOnException:=True;
  61. Terminate;
  62. S:=CheckOptions('c:hi:o:f:ep:b:g:x:m:Fndqa',
  63. ['config:','help','input:','output:','formclass:','event',
  64. 'parentclass:','below-id:','getelementfunction:','exclude:',
  65. 'map:','form-file','no-bind','defaultelements','quiet','actionlist']);
  66. if (S<>'') or HasOption('h','help') then
  67. Usage(S);
  68. IFN:=GetOptionValue('i','input');
  69. OFN:=GetOptionValue('o','output');
  70. FConv.DefaultElements:=HasOption('d','defaultelements');
  71. if HasOption('c','config') then
  72. ReadConfigFile(GetOptionValue('c','config'));
  73. if HasOption('f','formclass') then
  74. FGen.FormClassName:=GetOptionValue('f','formclass');
  75. if HasOption('p','parentclass') then
  76. FGen.ParentClassName:=GetOPtionValue('p','parentclass');
  77. if HasOption('g','getelementfunction') then
  78. FGen.GetElementFunction:=GetOptionValue('g','getelementfunction') ;
  79. if HasOption('F','form-file') or hasOption('a','actionlist') then
  80. begin
  81. FGen.Options:=FGen.Options+[foFormFile];
  82. FGen.EventSignature:='Sender : TObject';
  83. FGen.EventModifiers:='';
  84. FGen.AddMethods:=[];
  85. if hasOption('a','actionlist') then
  86. FGen.WebCoreOptions:=FGen.WebCoreOptions+[wcoUseActionList];
  87. end;
  88. if hasOption('e','event') then
  89. FGen.Options:=FGen.Options+[foEvents];
  90. if hasOption('n','no-bind') then
  91. FGen.Options:=FGen.Options-[foBindInConstructor];
  92. if HasOption('m','map') then
  93. begin
  94. FConv.Map.LoadFromFile(GetOptionValue('m','map'));
  95. FConv.DefaultElements:=HasOption('d','defaultelements');
  96. end;
  97. if Not HasOption('q','quiet') then
  98. FCOnv.OnLog:=@WriteLog;
  99. if HasOption('x','exclude') then
  100. begin
  101. S:=GetOPtionValue('x','exclude');
  102. if (S<>'') and (S[1]='@') then
  103. FConv.ExcludeIDS.LoadFromFile(Copy(S,2,Length(S)-1))
  104. else
  105. FConv.ExcludeIDS.CommaText:=S;
  106. end;
  107. if HasOption('b','below-id') then
  108. FConv.BelowID:=GetOptionValue('b','below-id');
  109. if IFN='' then
  110. Usage('Need input file');
  111. if OFN='' then
  112. Usage('Need output file');
  113. FConv.LoadFromFile(IFN);
  114. if FConv.FormElements.Count=0 then
  115. Writeln('No elements found')
  116. else
  117. begin
  118. FGen.FormElements:=FConv.FormElements;
  119. FGen.OutputUnitName:=ChangeFileExt(ExtractFIleName(ofn),'');
  120. FGen.Execute;
  121. FGen.SaveToFile(OFN);
  122. if foFormFile in FGen.Options then
  123. FGen.FormSource.SaveToFile(ChangeFileExt(OFN,'.dfm'));
  124. end;
  125. end;
  126. constructor THTML2FormApplication.Create(aOwner: TComponent);
  127. begin
  128. inherited Create(aOwner);
  129. FConv:=THTMLToFormELements.Create(Self);
  130. FGen:=TWebCoreFormCodeGen.Create(Self);
  131. end;
  132. destructor THTML2FormApplication.Destroy;
  133. begin
  134. FreeAndNil(FGen);
  135. FreeAndNil(FConv);
  136. inherited Destroy;
  137. end;
  138. begin
  139. With THTML2FormApplication.Create(Nil) do
  140. try
  141. Initialize;
  142. Run;
  143. finally
  144. Free;
  145. end;
  146. end.