fpdoc.pp 7.0 KB

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