2
0

fpdoc.pp 8.4 KB

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