2
0

fpdoc.pp 6.8 KB

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