fpdoc.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. $Id$
  3. FPDoc - Free Pascal Documentation Tool
  4. Copyright (C) 2000 - 2003 by
  5. Areca Systems GmbH / Sebastian Guenther, [email protected]
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. program FPDoc;
  13. uses
  14. SysUtils, Classes, Gettext, DOM, XMLWrite, PasTree, PParser,
  15. dGlobals, // GLobal definitions, constants.
  16. dwriter, // TFPDocWriter definition.
  17. dwlinear, // Linear (abstract) writer
  18. dw_LaTeX, // TLaTex writer
  19. dw_XML, // XML writer
  20. dw_HTML, // HTML writer
  21. dw_ipf, // IPF writer
  22. dw_man, // Man page writer
  23. dw_txt; // TXT writer
  24. const
  25. OSTarget: String = {$I %FPCTARGETOS%};
  26. CPUTarget: String = {$I %FPCTARGETCPU%};
  27. var
  28. Backend : String;
  29. BackendOptions : TStrings;
  30. InputFiles, DescrFiles: TStringList;
  31. PackageName, DocLang, ContentFile : String;
  32. Engine: TFPDocEngine;
  33. Procedure Usage(AnExitCode : Byte);
  34. Var
  35. I,P : Integer;
  36. S : String;
  37. L : TStringList;
  38. C : TFPDocWriterClass;
  39. begin
  40. Writeln(Format(SCmdLineHelp,[ExtractFileName(Paramstr(0))]));
  41. Writeln(SUsageOption010);
  42. Writeln(SUsageOption020);
  43. Writeln(SUsageOption030);
  44. Writeln(SUsageOption040);
  45. Writeln(SUsageOption050);
  46. Writeln(SUsageOption060);
  47. Writeln(SUsageOption070);
  48. Writeln(SUsageOption080);
  49. Writeln(SUsageOption090);
  50. Writeln(SUsageOption100);
  51. Writeln(SUsageOption110);
  52. Writeln(SUsageOption120);
  53. Writeln(SUsageOption130);
  54. Writeln(SUsageOption140);
  55. Writeln(SUsageOption150);
  56. Writeln(SUsageOption160);
  57. Writeln(SUsageOption170);
  58. L:=TStringList.Create;
  59. Try
  60. If (Backend='') then
  61. begin
  62. Writeln;
  63. Writeln(SUsageFormats);
  64. EnumWriters(L);
  65. For I:=0 to L.Count-1 do
  66. begin
  67. S:=L[i];
  68. P:=Pos('=',S);
  69. Writeln(Format(' %s - %s',[Copy(S,1,P-1)+Space(10-p),Copy(S,P+1,Length(S))]));
  70. end;
  71. Writeln(SUsageBackendHelp);
  72. end
  73. else
  74. begin
  75. Writeln;
  76. Writeln(Format(SUsageFormatSpecific,[Lowercase(Backend)]));
  77. C:=GetWriterClass(backend);
  78. C.Usage(L);
  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.
  255. {
  256. $Log$
  257. Revision 1.8 2005-01-14 17:55:07 michael
  258. + Added unix man page output; Implemented usage
  259. Revision 1.7 2005/01/12 21:11:41 michael
  260. + New structure for writers. Implemented TXT writer
  261. Revision 1.6 2005/01/09 15:59:50 michael
  262. + Split out latex writer to linear and latex writer
  263. Revision 1.5 2004/08/28 18:03:23 michael
  264. + Added warning if docnode not found (option --warn-no-node
  265. Revision 1.4 2003/10/08 11:41:54 yuri
  266. + Initial OS/2 IPF support added
  267. Revision 1.3 2003/03/27 17:14:13 sg
  268. * Added --ostarget and --cputarget
  269. Revision 1.2 2003/03/18 19:28:44 michael
  270. + Some changes to output handling, more suitable for tex output
  271. Revision 1.1 2003/03/17 23:03:20 michael
  272. + Initial import in CVS
  273. Revision 1.13 2003/03/13 22:02:13 sg
  274. * New version with many bugfixes and our own parser (now independent of the
  275. compiler source)
  276. Revision 1.12 2002/10/12 17:09:45 michael
  277. + Added check for package name
  278. Revision 1.11 2002/05/24 00:13:22 sg
  279. * much improved new version, including many linking and output fixes
  280. Revision 1.10 2002/03/12 10:58:36 sg
  281. * reworked linking engine and internal structure
  282. Revision 1.9 2002/01/08 13:00:06 michael
  283. + Added correct array handling and syntax highlighting is now optional
  284. Revision 1.8 2001/12/17 23:24:11 sg
  285. * Added "--package" switch
  286. * Now uses translation files written in lower-case
  287. Revision 1.7 2001/07/27 12:17:20 sg
  288. * Added "--html-search" command line argument
  289. Revision 1.6 2001/07/27 10:21:42 sg
  290. * Just a new, improved version ;)
  291. (detailed changelogs will be provided again with the next commits)
  292. }