fpdoc.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. begin
  122. Engine.Output := Arg;
  123. if (Length(Engine.Output) > 0) and not
  124. (Engine.Output[Length(Engine.Output)] in DirSeparators) then
  125. Engine.Output := Engine.Output + PathDelim;
  126. end 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
  135. WriteLn(StdErr, Format(SCmdLineInvalidOption, [s]));
  136. end;
  137. end;
  138. procedure ParseCommandLine;
  139. var
  140. i: Integer;
  141. begin
  142. for i := 1 to ParamCount do
  143. ParseOption(ParamStr(i));
  144. end;
  145. var
  146. i: Integer;
  147. XMLDoc: TXMLDocument;
  148. Allocator: TFileAllocator;
  149. HTMLWriter: THTMLWriter;
  150. begin
  151. {$IFDEF Unix}
  152. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  153. {$ELSE}
  154. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  155. {$ENDIF}
  156. WriteLn(STitle);
  157. WriteLn(SCopyright);
  158. WriteLn;
  159. InitOptions;
  160. ParseCommandLine;
  161. if CmdLineAction = actionHelp then
  162. WriteLn(SCmdLineHelp)
  163. else
  164. begin
  165. if (PackageName='') then
  166. begin
  167. Writeln(SNeedPackageName);
  168. Halt(1);
  169. end;
  170. // Action is to create documentation
  171. // Read all description files
  172. for i := 0 to DescrFiles.Count - 1 do
  173. Engine.AddDocFile(DescrFiles[i]);
  174. // Set the name of the package to be processed
  175. Engine.SetPackageName(PackageName);
  176. // Read all source files
  177. for i := 0 to InputFiles.Count - 1 do
  178. try
  179. ParseSource(Engine, InputFiles[i]);
  180. except
  181. on e: EParserError do
  182. WriteLn(StdErr, Format('%s(%d,%d): %s',
  183. [e.Filename, e.Row, e.Column, e.Message]));
  184. end;
  185. // Translate internal documentation strings
  186. if Length(DocLang) > 0 then
  187. TranslateDocStrings(DocLang);
  188. case OutputFormat of
  189. fmtHTM:
  190. begin
  191. Allocator := TShortNameFileAllocator.Create('.htm');
  192. try
  193. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  194. try
  195. HTMLWriter.SearchPage := SearchPage;
  196. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  197. HTMLWriter.WriteHTMLPages;
  198. finally
  199. HTMLWriter.Free;
  200. end;
  201. finally
  202. Allocator.Free;
  203. end;
  204. end;
  205. fmtHTML:
  206. begin
  207. Allocator := TLongNameFileAllocator.Create('.html');
  208. try
  209. HTMLWriter := THTMLWriter.Create(Engine, Allocator, Engine.Package);
  210. try
  211. HTMLWriter.SearchPage := SearchPage;
  212. WriteLn(Format(SWritingPages, [HTMLWriter.PageCount]));
  213. HTMLWriter.WriteHTMLPages;
  214. finally
  215. HTMLWriter.Free;
  216. end;
  217. finally
  218. Allocator.Free;
  219. end;
  220. end;
  221. { fmtXHTML:
  222. begin
  223. Allocator := TLongNameFileAllocator.Create('.xml');
  224. try
  225. BeginXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  226. for i := 0 to InputFiles.Count - 1 do
  227. CreateXHTMLDocFromModule(Engine, XHTMLOptions, Allocator,
  228. ParseSource(Engine, InputFiles[i]));
  229. EndXHTMLDocForPackage(Engine, XHTMLOptions, Allocator);
  230. finally
  231. Allocator.Free;
  232. end;
  233. end;}
  234. fmtLaTeX:
  235. begin
  236. if Length(Engine.Output) = 0 then
  237. WriteLn(SCmdLineOutputOptionMissing)
  238. else
  239. CreateLaTeXDocForPackage(Engine.Package, Engine);
  240. end;
  241. { fmtXMLStruct:
  242. for i := 0 to InputFiles.Count - 1 do
  243. begin
  244. XMLDoc := ModuleToXMLStruct(Module);
  245. try
  246. WriteXMLFile(XMLDoc, StdOut);
  247. finally
  248. XMLDoc.Free;
  249. end;
  250. end;}
  251. end;
  252. if Length(ContentFile) > 0 then
  253. Engine.WriteContentFile(ContentFile);
  254. end;
  255. FreeOptions;
  256. WriteLn(SDone);
  257. end.
  258. {
  259. $Log$
  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. }