fpdoc.pp 6.9 KB

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