fpdoc.pp 8.1 KB

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