fpdoc.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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,
  15. dGlobals, PasTree, PParser, dw_XML, dw_HTML, dw_LaTeX, dw_ipf;
  16. resourcestring
  17. STitle = 'FPDoc - Free Pascal Documentation Tool';
  18. SCopyright = '(c) 2000 - 2003 Areca Systems GmbH / Sebastian Guenther, [email protected]';
  19. SCmdLineHelp = 'See documentation for usage.';
  20. SCmdLineInvalidOption = 'Ignoring unknown option "%s"';
  21. SCmdLineInvalidFormat = 'Invalid format "%s" specified';
  22. SCmdLineOutputOptionMissing = 'Need an output filename, please specify one with --output=<filename>';
  23. SWritingPages = 'Writing %d pages...';
  24. SNeedPackageName = 'No package name specified. Please specify one using the --package option.';
  25. SDone = 'Done.';
  26. type
  27. TCmdLineAction = (actionHelp, actionConvert);
  28. TOutputFormat = (fmtHTM, fmtHTML, fmtXHTML, fmtLaTeX, fmtXMLStruct, fmtIPF);
  29. const
  30. CmdLineAction: TCmdLineAction = actionConvert;
  31. OutputFormat: TOutputFormat = fmtHTML;
  32. OSTarget: String = {$I %FPCTARGETOS%};
  33. CPUTarget: String = {$I %FPCTARGETCPU%};
  34. var
  35. InputFiles, DescrFiles: TStringList;
  36. PackageName, DocLang, ContentFile, SearchPage: String;
  37. Engine: TFPDocEngine;
  38. procedure InitOptions;
  39. begin
  40. InputFiles := TStringList.Create;
  41. DescrFiles := TStringList.Create;
  42. Engine := TFPDocEngine.Create;
  43. end;
  44. procedure FreeOptions;
  45. begin
  46. Engine.Free;
  47. DescrFiles.Free;
  48. InputFiles.Free;
  49. end;
  50. procedure ReadContentFile(const AParams: String);
  51. var
  52. i: Integer;
  53. begin
  54. i := Pos(',', AParams);
  55. Engine.ReadContentFile(Copy(AParams, 1, i - 1),
  56. Copy(AParams, i + 1, Length(AParams)));
  57. end;
  58. procedure ParseOption(const s: String);
  59. procedure AddToFileList(List: TStringList; const FileName: String);
  60. var
  61. f: Text;
  62. s: String;
  63. begin
  64. if Copy(FileName, 1, 1) = '@' then
  65. begin
  66. Assign(f, Copy(FileName, 2, Length(FileName)));
  67. Reset(f);
  68. while not EOF(f) do
  69. begin
  70. ReadLn(f, s);
  71. List.Add(s);
  72. end;
  73. Close(f);
  74. end else
  75. List.Add(FileName);
  76. end;
  77. var
  78. i: Integer;
  79. Cmd, Arg: String;
  80. begin
  81. if (s = '-h') or (s = '--help') then
  82. CmdLineAction := actionHelp
  83. else if s = '--hide-protected' then
  84. Engine.HideProtected := True
  85. else if s = '--warn-no-node' then
  86. Engine.WarnNoNode := True
  87. else if s = '--show-private' then
  88. Engine.HidePrivate := False
  89. else
  90. begin
  91. i := Pos('=', s);
  92. if i > 0 then
  93. begin
  94. Cmd := Copy(s, 1, i - 1);
  95. Arg := Copy(s, i + 1, Length(s));
  96. end else
  97. begin
  98. Cmd := s;
  99. SetLength(Arg, 0);
  100. end;
  101. if Cmd = '--descr' then
  102. AddToFileList(DescrFiles, Arg)
  103. else if (Cmd = '-f') or (Cmd = '--format') then
  104. begin
  105. Arg := UpperCase(Arg);
  106. if Arg = 'HTM' then
  107. OutputFormat := fmtHTM
  108. else if Arg = 'HTML' then
  109. OutputFormat := fmtHTML
  110. else if Arg = 'XHTML' then
  111. OutputFormat := fmtXHTML
  112. else if Arg = 'LATEX' then
  113. OutputFormat := fmtLaTeX
  114. else if Arg = 'IPF' then
  115. OutputFormat := fmtIPF
  116. else if Arg = 'XML-STRUCT' then
  117. OutputFormat := fmtXMLStruct
  118. else
  119. WriteLn(StdErr, Format(SCmdLineInvalidFormat, [Arg]));
  120. end else if (Cmd = '-l') or (Cmd = '--lang') then
  121. DocLang := Arg
  122. else if (Cmd = '--latex-highlight') then
  123. LatexHighLight:=True
  124. else if (Cmd = '-i') or (Cmd = '--input') then
  125. AddToFileList(InputFiles, Arg)
  126. else if (Cmd = '-o') or (Cmd = '--output') then
  127. Engine.Output := Arg
  128. else if Cmd = '--content' then
  129. ContentFile := Arg
  130. else if Cmd = '--import' then
  131. ReadContentFile(Arg)
  132. else if Cmd = '--package' then
  133. PackageName := Arg
  134. else if Cmd = '--html-search' then
  135. SearchPage := Arg
  136. else if Cmd = '--ostarget' then
  137. OSTarget := Arg
  138. else if Cmd = '--cputarget' then
  139. CPUTarget := Arg
  140. else if Cmd = '--latex-extension' then
  141. TexExtension:=Arg
  142. else
  143. WriteLn(StdErr, Format(SCmdLineInvalidOption, [s]));
  144. end;
  145. end;
  146. procedure ParseCommandLine;
  147. var
  148. i: Integer;
  149. begin
  150. for i := 1 to ParamCount do
  151. ParseOption(ParamStr(i));
  152. end;
  153. var
  154. i: Integer;
  155. XMLDoc: TXMLDocument;
  156. Allocator: TFileAllocator;
  157. HTMLWriter: THTMLWriter;
  158. begin
  159. {$IFDEF Unix}
  160. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  161. {$ELSE}
  162. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  163. {$ENDIF}
  164. WriteLn(STitle);
  165. WriteLn(SCopyright);
  166. WriteLn;
  167. InitOptions;
  168. ParseCommandLine;
  169. if CmdLineAction = actionHelp then
  170. WriteLn(SCmdLineHelp)
  171. else
  172. begin
  173. if (PackageName='') then
  174. begin
  175. Writeln(SNeedPackageName);
  176. Halt(1);
  177. end;
  178. // Action is to create documentation
  179. // Read all description files
  180. for i := 0 to DescrFiles.Count - 1 do
  181. Engine.AddDocFile(DescrFiles[i]);
  182. // Set the name of the package to be processed
  183. Engine.SetPackageName(PackageName);
  184. // Read all source files
  185. for i := 0 to InputFiles.Count - 1 do
  186. try
  187. ParseSource(Engine, InputFiles[i], OSTarget, CPUTarget);
  188. except
  189. on e: EParserError do
  190. WriteLn(StdErr, Format('%s(%d,%d): %s',
  191. [e.Filename, e.Row, e.Column, e.Message]));
  192. end;
  193. // Translate internal documentation strings
  194. if Length(DocLang) > 0 then
  195. TranslateDocStrings(DocLang);
  196. case OutputFormat of
  197. fmtHTM:
  198. begin
  199. Allocator := TShortNameFileAllocator.Create('.htm');
  200. try
  201. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  202. try
  203. HTMLWriter.SearchPage := SearchPage;
  204. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  205. HTMLWriter.WriteHTMLPages;
  206. finally
  207. HTMLWriter.Free;
  208. end;
  209. finally
  210. Allocator.Free;
  211. end;
  212. end;
  213. fmtHTML:
  214. begin
  215. Allocator := TLongNameFileAllocator.Create('.html');
  216. try
  217. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  218. try
  219. HTMLWriter.SearchPage := SearchPage;
  220. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  221. HTMLWriter.WriteHTMLPages;
  222. finally
  223. HTMLWriter.Free;
  224. end;
  225. finally
  226. Allocator.Free;
  227. end;
  228. end;
  229. { fmtXHTML:
  230. begin
  231. Allocator := TLongNameFileAllocator.Create('.xml');
  232. try
  233. BeginXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  234. for i := 0 to InputFiles.Count - 1 do
  235. CreateXHTMLDocFromModule(Engine, XHTMLOptions, Allocator,
  236. ParseSource(Engine, InputFiles[i]));
  237. EndXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  238. finally
  239. Allocator.Free;
  240. end;
  241. end;}
  242. fmtLaTeX:
  243. begin
  244. if Length(Engine.Output) = 0 then
  245. WriteLn(SCmdLineOutputOptionMissing)
  246. else
  247. CreateLaTeXDocForPackage(Engine.Package, Engine);
  248. end;
  249. fmtIPF:
  250. begin
  251. if Length(Engine.Output) = 0 then
  252. WriteLn(SCmdLineOutputOptionMissing)
  253. else
  254. CreateIPFDocForPackage(Engine.Package, Engine);
  255. end;
  256. { fmtXMLStruct:
  257. for i := 0 to InputFiles.Count - 1 do
  258. begin
  259. XMLDoc := ModuleToXMLStruct(Module);
  260. try
  261. WriteXMLFile(XMLDoc, StdOut);
  262. finally
  263. XMLDoc.Free;
  264. end;
  265. end;}
  266. end;
  267. if Length(ContentFile) > 0 then
  268. Engine.WriteContentFile(ContentFile);
  269. end;
  270. FreeOptions;
  271. WriteLn(SDone);
  272. end.
  273. {
  274. $Log$
  275. Revision 1.5 2004-08-28 18:03:23 michael
  276. + Added warning if docnode not found (option --warn-no-node
  277. Revision 1.4 2003/10/08 11:41:54 yuri
  278. + Initial OS/2 IPF support added
  279. Revision 1.3 2003/03/27 17:14:13 sg
  280. * Added --ostarget and --cputarget
  281. Revision 1.2 2003/03/18 19:28:44 michael
  282. + Some changes to output handling, more suitable for tex output
  283. Revision 1.1 2003/03/17 23:03:20 michael
  284. + Initial import in CVS
  285. Revision 1.13 2003/03/13 22:02:13 sg
  286. * New version with many bugfixes and our own parser (now independent of the
  287. compiler source)
  288. Revision 1.12 2002/10/12 17:09:45 michael
  289. + Added check for package name
  290. Revision 1.11 2002/05/24 00:13:22 sg
  291. * much improved new version, including many linking and output fixes
  292. Revision 1.10 2002/03/12 10:58:36 sg
  293. * reworked linking engine and internal structure
  294. Revision 1.9 2002/01/08 13:00:06 michael
  295. + Added correct array handling and syntax highlighting is now optional
  296. Revision 1.8 2001/12/17 23:24:11 sg
  297. * Added "--package" switch
  298. * Now uses translation files written in lower-case
  299. Revision 1.7 2001/07/27 12:17:20 sg
  300. * Added "--html-search" command line argument
  301. Revision 1.6 2001/07/27 10:21:42 sg
  302. * Just a new, improved version ;)
  303. (detailed changelogs will be provided again with the next commits)
  304. }