fpdoc.pp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_LaTeX,dw_XML, dw_HTML, dw_ipf, dwlinear;
  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. Allocator: TFileAllocator;
  156. HTMLWriter: THTMLWriter;
  157. begin
  158. {$IFDEF Unix}
  159. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  160. {$ELSE}
  161. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  162. {$ENDIF}
  163. WriteLn(STitle);
  164. WriteLn(SCopyright);
  165. WriteLn;
  166. InitOptions;
  167. ParseCommandLine;
  168. if CmdLineAction = actionHelp then
  169. WriteLn(SCmdLineHelp)
  170. else
  171. begin
  172. if (PackageName='') then
  173. begin
  174. Writeln(SNeedPackageName);
  175. Halt(1);
  176. end;
  177. // Action is to create documentation
  178. // Read all description files
  179. for i := 0 to DescrFiles.Count - 1 do
  180. Engine.AddDocFile(DescrFiles[i]);
  181. // Set the name of the package to be processed
  182. Engine.SetPackageName(PackageName);
  183. // Read all source files
  184. for i := 0 to InputFiles.Count - 1 do
  185. try
  186. ParseSource(Engine, InputFiles[i], OSTarget, CPUTarget);
  187. except
  188. on e: EParserError do
  189. WriteLn(StdErr, Format('%s(%d,%d): %s',
  190. [e.Filename, e.Row, e.Column, e.Message]));
  191. end;
  192. // Translate internal documentation strings
  193. if Length(DocLang) > 0 then
  194. TranslateDocStrings(DocLang);
  195. case OutputFormat of
  196. fmtHTM:
  197. begin
  198. Allocator := TShortNameFileAllocator.Create('.htm');
  199. try
  200. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  201. try
  202. HTMLWriter.SearchPage := SearchPage;
  203. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  204. HTMLWriter.WriteHTMLPages;
  205. finally
  206. HTMLWriter.Free;
  207. end;
  208. finally
  209. Allocator.Free;
  210. end;
  211. end;
  212. fmtHTML:
  213. begin
  214. Allocator := TLongNameFileAllocator.Create('.html');
  215. try
  216. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  217. try
  218. HTMLWriter.SearchPage := SearchPage;
  219. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  220. HTMLWriter.WriteHTMLPages;
  221. finally
  222. HTMLWriter.Free;
  223. end;
  224. finally
  225. Allocator.Free;
  226. end;
  227. end;
  228. { fmtXHTML:
  229. begin
  230. Allocator := TLongNameFileAllocator.Create('.xml');
  231. try
  232. BeginXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  233. for i := 0 to InputFiles.Count - 1 do
  234. CreateXHTMLDocFromModule(Engine, XHTMLOptions, Allocator,
  235. ParseSource(Engine, InputFiles[i]));
  236. EndXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  237. finally
  238. Allocator.Free;
  239. end;
  240. end;}
  241. fmtLaTeX:
  242. begin
  243. if Length(Engine.Output) = 0 then
  244. WriteLn(SCmdLineOutputOptionMissing)
  245. else
  246. CreateLaTeXDocForPackage(Engine.Package, Engine);
  247. end;
  248. fmtIPF:
  249. begin
  250. if Length(Engine.Output) = 0 then
  251. WriteLn(SCmdLineOutputOptionMissing)
  252. else
  253. CreateIPFDocForPackage(Engine.Package, Engine);
  254. end;
  255. { fmtXMLStruct:
  256. for i := 0 to InputFiles.Count - 1 do
  257. begin
  258. XMLDoc := ModuleToXMLStruct(Module);
  259. try
  260. WriteXMLFile(XMLDoc, StdOut);
  261. finally
  262. XMLDoc.Free;
  263. end;
  264. end;}
  265. end;
  266. if Length(ContentFile) > 0 then
  267. Engine.WriteContentFile(ContentFile);
  268. end;
  269. FreeOptions;
  270. WriteLn(SDone);
  271. end.
  272. {
  273. $Log$
  274. Revision 1.6 2005-01-09 15:59:50 michael
  275. + Split out latex writer to linear and latex writer
  276. Revision 1.5 2004/08/28 18:03:23 michael
  277. + Added warning if docnode not found (option --warn-no-node
  278. Revision 1.4 2003/10/08 11:41:54 yuri
  279. + Initial OS/2 IPF support added
  280. Revision 1.3 2003/03/27 17:14:13 sg
  281. * Added --ostarget and --cputarget
  282. Revision 1.2 2003/03/18 19:28:44 michael
  283. + Some changes to output handling, more suitable for tex output
  284. Revision 1.1 2003/03/17 23:03:20 michael
  285. + Initial import in CVS
  286. Revision 1.13 2003/03/13 22:02:13 sg
  287. * New version with many bugfixes and our own parser (now independent of the
  288. compiler source)
  289. Revision 1.12 2002/10/12 17:09:45 michael
  290. + Added check for package name
  291. Revision 1.11 2002/05/24 00:13:22 sg
  292. * much improved new version, including many linking and output fixes
  293. Revision 1.10 2002/03/12 10:58:36 sg
  294. * reworked linking engine and internal structure
  295. Revision 1.9 2002/01/08 13:00:06 michael
  296. + Added correct array handling and syntax highlighting is now optional
  297. Revision 1.8 2001/12/17 23:24:11 sg
  298. * Added "--package" switch
  299. * Now uses translation files written in lower-case
  300. Revision 1.7 2001/07/27 12:17:20 sg
  301. * Added "--html-search" command line argument
  302. Revision 1.6 2001/07/27 10:21:42 sg
  303. * Just a new, improved version ;)
  304. (detailed changelogs will be provided again with the next commits)
  305. }