fpdoc.pp 6.9 KB

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