fpdoc.pp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 = '--show-private' then
  86. Engine.HidePrivate := False
  87. else
  88. begin
  89. i := Pos('=', s);
  90. if i > 0 then
  91. begin
  92. Cmd := Copy(s, 1, i - 1);
  93. Arg := Copy(s, i + 1, Length(s));
  94. end else
  95. begin
  96. Cmd := s;
  97. SetLength(Arg, 0);
  98. end;
  99. if Cmd = '--descr' then
  100. AddToFileList(DescrFiles, Arg)
  101. else if (Cmd = '-f') or (Cmd = '--format') then
  102. begin
  103. Arg := UpperCase(Arg);
  104. if Arg = 'HTM' then
  105. OutputFormat := fmtHTM
  106. else if Arg = 'HTML' then
  107. OutputFormat := fmtHTML
  108. else if Arg = 'XHTML' then
  109. OutputFormat := fmtXHTML
  110. else if Arg = 'LATEX' then
  111. OutputFormat := fmtLaTeX
  112. else if Arg = 'IPF' then
  113. OutputFormat := fmtIPF
  114. else if Arg = 'XML-STRUCT' then
  115. OutputFormat := fmtXMLStruct
  116. else
  117. WriteLn(StdErr, Format(SCmdLineInvalidFormat, [Arg]));
  118. end else if (Cmd = '-l') or (Cmd = '--lang') then
  119. DocLang := Arg
  120. else if (Cmd = '--latex-highlight') then
  121. LatexHighLight:=True
  122. else if (Cmd = '-i') or (Cmd = '--input') then
  123. AddToFileList(InputFiles, Arg)
  124. else if (Cmd = '-o') or (Cmd = '--output') then
  125. Engine.Output := Arg
  126. else if Cmd = '--content' then
  127. ContentFile := Arg
  128. else if Cmd = '--import' then
  129. ReadContentFile(Arg)
  130. else if Cmd = '--package' then
  131. PackageName := Arg
  132. else if Cmd = '--html-search' then
  133. SearchPage := Arg
  134. else if Cmd = '--ostarget' then
  135. OSTarget := Arg
  136. else if Cmd = '--cputarget' then
  137. CPUTarget := Arg
  138. else if Cmd = '--latex-extension' then
  139. TexExtension:=Arg
  140. else
  141. WriteLn(StdErr, Format(SCmdLineInvalidOption, [s]));
  142. end;
  143. end;
  144. procedure ParseCommandLine;
  145. var
  146. i: Integer;
  147. begin
  148. for i := 1 to ParamCount do
  149. ParseOption(ParamStr(i));
  150. end;
  151. var
  152. i: Integer;
  153. XMLDoc: TXMLDocument;
  154. Allocator: TFileAllocator;
  155. HTMLWriter: THTMLWriter;
  156. begin
  157. {$IFDEF Unix}
  158. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  159. {$ELSE}
  160. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  161. {$ENDIF}
  162. WriteLn(STitle);
  163. WriteLn(SCopyright);
  164. WriteLn;
  165. InitOptions;
  166. ParseCommandLine;
  167. if CmdLineAction = actionHelp then
  168. WriteLn(SCmdLineHelp)
  169. else
  170. begin
  171. if (PackageName='') then
  172. begin
  173. Writeln(SNeedPackageName);
  174. Halt(1);
  175. end;
  176. // Action is to create documentation
  177. // Read all description files
  178. for i := 0 to DescrFiles.Count - 1 do
  179. Engine.AddDocFile(DescrFiles[i]);
  180. // Set the name of the package to be processed
  181. Engine.SetPackageName(PackageName);
  182. // Read all source files
  183. for i := 0 to InputFiles.Count - 1 do
  184. try
  185. ParseSource(Engine, InputFiles[i], OSTarget, CPUTarget);
  186. except
  187. on e: EParserError do
  188. WriteLn(StdErr, Format('%s(%d,%d): %s',
  189. [e.Filename, e.Row, e.Column, e.Message]));
  190. end;
  191. // Translate internal documentation strings
  192. if Length(DocLang) > 0 then
  193. TranslateDocStrings(DocLang);
  194. case OutputFormat of
  195. fmtHTM:
  196. begin
  197. Allocator := TShortNameFileAllocator.Create('.htm');
  198. try
  199. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  200. try
  201. HTMLWriter.SearchPage := SearchPage;
  202. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  203. HTMLWriter.WriteHTMLPages;
  204. finally
  205. HTMLWriter.Free;
  206. end;
  207. finally
  208. Allocator.Free;
  209. end;
  210. end;
  211. fmtHTML:
  212. begin
  213. Allocator := TLongNameFileAllocator.Create('.html');
  214. try
  215. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  216. try
  217. HTMLWriter.SearchPage := SearchPage;
  218. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  219. HTMLWriter.WriteHTMLPages;
  220. finally
  221. HTMLWriter.Free;
  222. end;
  223. finally
  224. Allocator.Free;
  225. end;
  226. end;
  227. { fmtXHTML:
  228. begin
  229. Allocator := TLongNameFileAllocator.Create('.xml');
  230. try
  231. BeginXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  232. for i := 0 to InputFiles.Count - 1 do
  233. CreateXHTMLDocFromModule(Engine, XHTMLOptions, Allocator,
  234. ParseSource(Engine, InputFiles[i]));
  235. EndXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  236. finally
  237. Allocator.Free;
  238. end;
  239. end;}
  240. fmtLaTeX:
  241. begin
  242. if Length(Engine.Output) = 0 then
  243. WriteLn(SCmdLineOutputOptionMissing)
  244. else
  245. CreateLaTeXDocForPackage(Engine.Package, Engine);
  246. end;
  247. fmtIPF:
  248. begin
  249. if Length(Engine.Output) = 0 then
  250. WriteLn(SCmdLineOutputOptionMissing)
  251. else
  252. CreateIPFDocForPackage(Engine.Package, Engine);
  253. end;
  254. { fmtXMLStruct:
  255. for i := 0 to InputFiles.Count - 1 do
  256. begin
  257. XMLDoc := ModuleToXMLStruct(Module);
  258. try
  259. WriteXMLFile(XMLDoc, StdOut);
  260. finally
  261. XMLDoc.Free;
  262. end;
  263. end;}
  264. end;
  265. if Length(ContentFile) > 0 then
  266. Engine.WriteContentFile(ContentFile);
  267. end;
  268. FreeOptions;
  269. WriteLn(SDone);
  270. end.
  271. {
  272. $Log$
  273. Revision 1.4 2003-10-08 11:41:54 yuri
  274. + Initial OS/2 IPF support added
  275. Revision 1.3 2003/03/27 17:14:13 sg
  276. * Added --ostarget and --cputarget
  277. Revision 1.2 2003/03/18 19:28:44 michael
  278. + Some changes to output handling, more suitable for tex output
  279. Revision 1.1 2003/03/17 23:03:20 michael
  280. + Initial import in CVS
  281. Revision 1.13 2003/03/13 22:02:13 sg
  282. * New version with many bugfixes and our own parser (now independent of the
  283. compiler source)
  284. Revision 1.12 2002/10/12 17:09:45 michael
  285. + Added check for package name
  286. Revision 1.11 2002/05/24 00:13:22 sg
  287. * much improved new version, including many linking and output fixes
  288. Revision 1.10 2002/03/12 10:58:36 sg
  289. * reworked linking engine and internal structure
  290. Revision 1.9 2002/01/08 13:00:06 michael
  291. + Added correct array handling and syntax highlighting is now optional
  292. Revision 1.8 2001/12/17 23:24:11 sg
  293. * Added "--package" switch
  294. * Now uses translation files written in lower-case
  295. Revision 1.7 2001/07/27 12:17:20 sg
  296. * Added "--html-search" command line argument
  297. Revision 1.6 2001/07/27 10:21:42 sg
  298. * Just a new, improved version ;)
  299. (detailed changelogs will be provided again with the next commits)
  300. }