fpdoc.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. {
  2. FPDoc - Free Pascal Documentation Tool
  3. Copyright (C) 2000 - 2003 by
  4. Areca Systems GmbH / Sebastian Guenther, [email protected]
  5. See the file COPYING, 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 FPDoc;
  12. uses
  13. SysUtils, Classes, Gettext, DOM, XMLWrite, PasTree, PParser,
  14. dGlobals, // GLobal definitions, constants.
  15. dwriter, // TFPDocWriter definition.
  16. dwlinear, // Linear (abstract) writer
  17. dw_LaTeX, // TLaTex writer
  18. dw_XML, // XML writer
  19. dw_HTML, // HTML writer
  20. dw_ipf, // IPF writer
  21. dw_man, // Man page writer
  22. dw_txt; // TXT writer
  23. const
  24. OSTarget: String = {$I %FPCTARGETOS%};
  25. CPUTarget: String = {$I %FPCTARGETCPU%};
  26. var
  27. Backend : String;
  28. BackendOptions : TStrings;
  29. InputFiles, DescrFiles: TStringList;
  30. PackageName, DocLang, ContentFile : String;
  31. Engine: TFPDocEngine;
  32. Procedure Usage(AnExitCode : Byte);
  33. Var
  34. I,P : Integer;
  35. S : String;
  36. L : TStringList;
  37. C : TFPDocWriterClass;
  38. begin
  39. Writeln(Format(SCmdLineHelp,[ExtractFileName(Paramstr(0))]));
  40. Writeln(SUsageOption010);
  41. Writeln(SUsageOption020);
  42. Writeln(SUsageOption030);
  43. Writeln(SUsageOption040);
  44. Writeln(SUsageOption050);
  45. Writeln(SUsageOption060);
  46. Writeln(SUsageOption070);
  47. Writeln(SUsageOption080);
  48. Writeln(SUsageOption090);
  49. Writeln(SUsageOption100);
  50. Writeln(SUsageOption110);
  51. Writeln(SUsageOption120);
  52. Writeln(SUsageOption130);
  53. Writeln(SUsageOption140);
  54. Writeln(SUsageOption150);
  55. Writeln(SUsageOption160);
  56. Writeln(SUsageOption170);
  57. L:=TStringList.Create;
  58. Try
  59. If (Backend='') then
  60. begin
  61. Writeln;
  62. Writeln(SUsageFormats);
  63. EnumWriters(L);
  64. For I:=0 to L.Count-1 do
  65. begin
  66. S:=L[i];
  67. P:=Pos('=',S);
  68. Writeln(Format(' %s - %s',[Copy(S,1,P-1)+Space(10-p),Copy(S,P+1,Length(S))]));
  69. end;
  70. Writeln(SUsageBackendHelp);
  71. end
  72. else
  73. begin
  74. Writeln;
  75. Writeln(Format(SUsageFormatSpecific,[Lowercase(Backend)]));
  76. C:=GetWriterClass(backend);
  77. C.Usage(L);
  78. If L.Count>0 then
  79. For I:=0 to (L.Count-1) div 2 do
  80. begin
  81. S:=L[i*2];
  82. Writeln(Format('%s %s',[S+Space(30-Length(S)),L[(i*2)+1]]));
  83. end;
  84. end;
  85. Finally
  86. L.Free;
  87. end;
  88. Halt(AnExitCode);
  89. end;
  90. procedure InitOptions;
  91. begin
  92. InputFiles := TStringList.Create;
  93. DescrFiles := TStringList.Create;
  94. BackendOptions := TStringList.Create;
  95. Engine := TFPDocEngine.Create;
  96. end;
  97. procedure FreeOptions;
  98. begin
  99. Engine.Free;
  100. BackendOptions.Free;
  101. DescrFiles.Free;
  102. InputFiles.Free;
  103. end;
  104. procedure ReadContentFile(const AParams: String);
  105. var
  106. i: Integer;
  107. begin
  108. i := Pos(',', AParams);
  109. Engine.ReadContentFile(Copy(AParams, 1, i - 1),
  110. Copy(AParams, i + 1, Length(AParams)));
  111. end;
  112. procedure ParseOption(const s: String);
  113. procedure AddToFileList(List: TStringList; const FileName: String);
  114. var
  115. f: Text;
  116. s: String;
  117. begin
  118. if Copy(FileName, 1, 1) = '@' then
  119. begin
  120. Assign(f, Copy(FileName, 2, Length(FileName)));
  121. Reset(f);
  122. while not EOF(f) do
  123. begin
  124. ReadLn(f, s);
  125. List.Add(s);
  126. end;
  127. Close(f);
  128. end else
  129. List.Add(FileName);
  130. end;
  131. var
  132. i: Integer;
  133. Cmd, Arg: String;
  134. begin
  135. if (s = '-h') or (s = '--help') then
  136. Usage(0)
  137. else if s = '--hide-protected' then
  138. Engine.HideProtected := True
  139. else if s = '--warn-no-node' then
  140. Engine.WarnNoNode := True
  141. else if s = '--show-private' then
  142. Engine.HidePrivate := False
  143. else
  144. begin
  145. i := Pos('=', s);
  146. if i > 0 then
  147. begin
  148. Cmd := Copy(s, 1, i - 1);
  149. Arg := Copy(s, i + 1, Length(s));
  150. end
  151. else
  152. begin
  153. Cmd := s;
  154. SetLength(Arg, 0);
  155. end;
  156. if Cmd = '--descr' then
  157. AddToFileList(DescrFiles, Arg)
  158. else if (Cmd = '-f') or (Cmd = '--format') then
  159. begin
  160. Arg:=UpperCase(Arg);
  161. If FindWriterClass(Arg)=-1 then
  162. WriteLn(StdErr, Format(SCmdLineInvalidFormat, [Arg]))
  163. else
  164. BackEnd:=Arg;
  165. end
  166. else if (Cmd = '-l') or (Cmd = '--lang') then
  167. DocLang := Arg
  168. else if (Cmd = '-i') or (Cmd = '--input') then
  169. AddToFileList(InputFiles, Arg)
  170. else if (Cmd = '-o') or (Cmd = '--output') then
  171. Engine.Output := Arg
  172. else if Cmd = '--content' then
  173. ContentFile := Arg
  174. else if Cmd = '--import' then
  175. ReadContentFile(Arg)
  176. else if Cmd = '--package' then
  177. PackageName := Arg
  178. else if Cmd = '--ostarget' then
  179. OSTarget := Arg
  180. else if Cmd = '--cputarget' then
  181. CPUTarget := Arg
  182. else
  183. begin
  184. BackendOptions.Add(Cmd);
  185. BackendOptions.Add(Arg);
  186. end;
  187. end;
  188. end;
  189. procedure ParseCommandLine;
  190. var
  191. i: Integer;
  192. begin
  193. for i := 1 to ParamCount do
  194. ParseOption(ParamStr(i));
  195. If (BackEnd='') then
  196. BackEnd:='html';
  197. if (PackageName='') then
  198. begin
  199. Writeln(SNeedPackageName);
  200. Usage(1);
  201. end;
  202. end;
  203. procedure CreateDocumentation;
  204. var
  205. i: Integer;
  206. WriterClass : TFPDocWriterClass;
  207. Writer : TFPDocWriter;
  208. begin
  209. for i := 0 to DescrFiles.Count - 1 do
  210. Engine.AddDocFile(DescrFiles[i]);
  211. Engine.SetPackageName(PackageName);
  212. if Length(DocLang) > 0 then
  213. TranslateDocStrings(DocLang);
  214. for i := 0 to InputFiles.Count - 1 do
  215. try
  216. ParseSource(Engine, InputFiles[i], OSTarget, CPUTarget);
  217. except
  218. on e: EParserError do
  219. WriteLn(StdErr, Format('%s(%d,%d): %s',
  220. [e.Filename, e.Row, e.Column, e.Message]));
  221. end;
  222. WriterClass:=GetWriterClass(Backend);
  223. Writer:=WriterClass.Create(Engine.Package,Engine);
  224. With Writer do
  225. Try
  226. If BackendOptions.Count>0 then
  227. for I:=0 to ((BackendOptions.Count-1) div 2) do
  228. If not InterPretOption(BackendOptions[I*2],BackendOptions[I*2+1]) then
  229. WriteLn(StdErr, Format(SCmdLineInvalidOption,[BackendOptions[I*2]+' '+BackendOptions[I*2+1]]));
  230. WriteDoc;
  231. Finally
  232. Free;
  233. end;
  234. if Length(ContentFile) > 0 then
  235. Engine.WriteContentFile(ContentFile);
  236. end;
  237. begin
  238. {$IFDEF Unix}
  239. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  240. {$ELSE}
  241. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  242. {$ENDIF}
  243. WriteLn(STitle);
  244. WriteLn(SCopyright);
  245. WriteLn;
  246. InitOptions;
  247. Try
  248. ParseCommandLine;
  249. CreateDocumentation;
  250. WriteLn(SDone);
  251. Finally
  252. FreeOptions;
  253. end;
  254. end.