dglobals.pp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. {
  2. FPDoc - Free Pascal Documentation Tool
  3. Copyright (C) 2000 - 2002 by
  4. Areca Systems GmbH / Sebastian Guenther, [email protected]
  5. * Global declarations
  6. * Link list management
  7. * Document node tree
  8. * Main engine
  9. See the file COPYING, included in this distribution,
  10. for details about the copyright.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. }
  15. {$MODE objfpc}
  16. {$H+}
  17. unit dGlobals;
  18. interface
  19. uses Classes, DOM, PasTree, PParser, StrUtils;
  20. Var
  21. LEOL : Integer;
  22. modir : string;
  23. resourcestring
  24. // Output strings
  25. SDocPackageTitle = 'Reference for package ''%s''';
  26. SDocPrograms = 'Programs';
  27. SDocUnits = 'Units';
  28. SDocUnitTitle = 'Reference for unit ''%s''';
  29. SDocInterfaceSection = 'Interface section';
  30. SDocImplementationSection = 'Implementation section';
  31. SDocUsedUnits = 'Used units';
  32. SDocUsedUnitsByUnitXY = 'Used units by unit ''%s''';
  33. SDocConstsTypesVars = 'Constants, types and variables';
  34. SDocResStrings = 'Resource strings';
  35. SDocTypes = 'Types';
  36. SDocConstants = 'Constants';
  37. SDocClasses = 'Classes';
  38. SDocProceduresAndFunctions = 'Procedures and functions';
  39. SDocVariables = 'Variables';
  40. SDocIdentifierIndex = 'Index';
  41. SDocModuleIndex = 'Index of all identifiers in unit ''%s''';
  42. SDocPackageIndex = 'Index of all identifiers in package ''%s''';
  43. SDocUnitOverview = 'Overview of unit ''%s''';
  44. SDocOverview = 'Overview';
  45. SDocSearch = 'Search';
  46. SDocDeclaration = 'Declaration';
  47. SDocDescription = 'Description';
  48. SDocErrors = 'Errors';
  49. SDocVersion = 'Version info';
  50. SDocSeeAlso = 'See also';
  51. SDocExample = 'Example';
  52. SDocArguments = 'Arguments';
  53. SDocFunctionResult = 'Function result';
  54. SDocRemark = 'Remark: ';
  55. SDocMethodOverview = 'Method overview';
  56. SDocPropertyOverview = 'Property overview';
  57. SDocInterfacesOverview = 'Interfaces overview';
  58. SDocPage = 'Page';
  59. SDocMethod = 'Method';
  60. SDocProperty = 'Property';
  61. SDocAccess = 'Access';
  62. SDocInheritance = 'Inheritance';
  63. SDocProperties = 'Properties';
  64. SDocMethods = 'Methods';
  65. SDocEvents = 'Events';
  66. SDocByName = 'by Name';
  67. SDocValue = 'Value';
  68. SDocExplanation = 'Explanation';
  69. SDocProcedure = 'Procedure';
  70. SDocValuesForEnum = 'Enumeration values for type %s';
  71. SDocSourcePosition = 'Source position: %s line %d';
  72. SDocSynopsis = 'Synopsis';
  73. SDocVisibility = 'Visibility';
  74. SDocOpaque = 'Opaque type';
  75. SDocDateGenerated = 'Documentation generated on: %s';
  76. // Topics
  77. SDocRelatedTopics = 'Related topics';
  78. SDocUp = 'Up';
  79. SDocNext = 'Next';
  80. SDocPrevious = 'Previous';
  81. // Various backend constants
  82. SDocChapter = 'Chapter';
  83. SDocSection = 'Section';
  84. SDocSubSection = 'Subsection';
  85. SDocTable = 'Table';
  86. SDocListing = 'Listing';
  87. // Man page usage
  88. SManUsageManSection = 'Use ASection as the man page section';
  89. SManUsageNoUnitPrefix = 'Do not prefix man pages with unit name.';
  90. SManUsageWriterDescr = 'UNIX man page output.';
  91. SManUsagePackageDescription = 'Use descr as the description of man pages';
  92. // HTML usage
  93. SHTMLUsageFooter = 'Append xhtml from file as footer to html page';
  94. SHTMLUsageFooterDate = 'Append footer with date. fmt is Optional format for FormatDateTime';
  95. SHTMLUsageCharset = 'Set the HTML character set';
  96. SHTMLHtmlSearch = 'Add search page with given name to the menu bar';
  97. SHTMLIndexColcount = 'Use N columns in the identifier index pages';
  98. SHTMLImageUrl = 'Prefix image URLs with url';
  99. // CHM usage
  100. SCHMUsageTOC = 'Use [File] as the table of contents. Usually a .hhc file.';
  101. SCHMUsageIndex = 'Use [File] as the index. Usually a .hhk file.';
  102. SCHMUsageDefPage = 'Set the "Home" page relative to where it lives in the chm. i.e. "/index.html"';
  103. SCHMUsageOtrFiles= 'A txt file containing a list of files to be added relative to the working directory.';
  104. SCHMUsageCSSFile = 'Filename of a .css file to be included in the chm.';
  105. SCHMUsageAutoTOC = 'Automatically generate a Table of Contents. Ignores --toc-file';
  106. SCHMUsageAutoIDX = 'Automatically generate an Index. Ignores --index-file';
  107. SCHMUsageMakeSearch = 'Automatically generate a Search Index from filenames that match *.htm*';
  108. // Linear usage
  109. SLinearUsageDupLinkedDocsP1 = 'Duplicate linked element documentation in';
  110. SLinearUsageDupLinkedDocsP2 = 'descendant classes.';
  111. STitle = 'FPDoc - Free Pascal Documentation Tool';
  112. SVersion = 'Version %s [%s]';
  113. SCopyright = '(c) 2000 - 2003 Areca Systems GmbH / Sebastian Guenther, [email protected]';
  114. SCmdLineHelp = 'Usage: %s [options]';
  115. SUsageOption010 = '--content Create content file for package cross-references';
  116. SUsageOption020 = '--cputarget=value Set the target CPU for the scanner.';
  117. SUsageOption030 = '--descr=name use name as description file. ';
  118. SUsageOption040 = ' This option is allowed more than once';
  119. SUsageOption050 = '--format=fmt Select output format.';
  120. SUsageOption060 = '--help Show this help.';
  121. SUsageOption070 = '--hide-protected Do not show protected methods in overview';
  122. SUsageOption080 = '--import=file Import content file for package cross-references';
  123. SUsageOption090 = '--input=cmd use cmd as input for the parser.';
  124. SUsageOption100 = ' At least one input option is required.';
  125. SUsageOption110 = '--lang=lng Select output language.';
  126. SUsageOption120 = '--ostarget=value Set the target OS for the scanner.';
  127. SUsageOption130 = '--output=name use name as the output name.';
  128. SUsageOption140 = ' Each backend interpretes this as needed.';
  129. SUsageOption150 = '--package=name Set the package name for which to create output';
  130. SUsageOption160 = '--show-private Show private methods.';
  131. SUsageOption170 = '--warn-no-node Warn if no documentation node was found.';
  132. SUsageOption180 = '--mo-dir=dir Set directory where language files reside to dir';
  133. SUsageOption190 = '--parse-impl (Experimental) try to parse implementation too';
  134. SUsageFormats = 'The following output formats are supported by this fpdoc:';
  135. SUsageBackendHelp = 'Specify an output format, combined with --help to get more help for this backend.';
  136. SUsageFormatSpecific = 'Output format "%s" supports the following options:';
  137. SCmdLineInvalidOption = 'Ignoring unknown option "%s"';
  138. SCmdLineInvalidFormat = 'Invalid format "%s" specified';
  139. SCmdLineOutputOptionMissing = 'Need an output filename, please specify one with --output=<filename>';
  140. SWritingPages = 'Writing %d pages...';
  141. SNeedPackageName = 'No package name specified. Please specify one using the --package option.';
  142. SDone = 'Done.';
  143. SErrCouldNotCreateOutputDir = 'Could not create output directory "%s"';
  144. SErrCouldNotCreateFile = 'Could not create file "%s": %s';
  145. SSeeURL = '(See %s)'; // For linear text writers.
  146. Const
  147. SVisibility: array[TPasMemberVisibility] of string =
  148. ('Default', 'Private', 'Protected', 'Public',
  149. 'Published', 'Automated','Strict Private','Strict Protected');
  150. type
  151. // Assumes a list of TObject instances and frees them on destruction
  152. TObjectList = class(TList)
  153. public
  154. destructor Destroy; override;
  155. end;
  156. { Link entry tree
  157. TFPDocEngine stores the root of the entry tree in its property
  158. "RootLinkNode". The root has one child node for each package, for which
  159. documentation links are available. The children of a package node
  160. are module nodes; and the children of a module node are the top-level
  161. declarations of this module; the next level in the tree stores e.g. record
  162. members, and so on...
  163. }
  164. TLinkNode = class
  165. private
  166. FFirstChild, FNextSibling: TLinkNode;
  167. FName: String;
  168. FLink: String;
  169. public
  170. constructor Create(const AName, ALink: String);
  171. destructor Destroy; override;
  172. function FindChild(const APathName: String): TLinkNode;
  173. function CreateChildren(const APathName, ALinkTo: String): TLinkNode;
  174. // Properties for tree structure
  175. property FirstChild: TLinkNode read FFirstChild;
  176. property NextSibling: TLinkNode read FNextSibling;
  177. // Link properties
  178. property Name: String read FName;
  179. property Link: String read FLink;
  180. end;
  181. { Documentation entry tree
  182. TFPDocEngine stores the root of the entry tree in its property
  183. "RootDocNode". The root has one child node for each package, for which
  184. documentation is being provided by the user. The children of a package node
  185. are module nodes; and the children of a module node are the top-level
  186. declarations of this module; the next level in the tree stores e.g. record
  187. members, and so on...
  188. }
  189. { TDocNode }
  190. TDocNode = class
  191. private
  192. FFirstChild, FNextSibling: TDocNode;
  193. FName: String;
  194. FNode: TDOMElement;
  195. FIsSkipped: Boolean;
  196. FShortDescr: TDOMElement;
  197. FDescr: TDOMElement;
  198. FErrorsDoc: TDOMElement;
  199. FSeeAlso: TDOMElement;
  200. FFirstExample: TDOMElement;
  201. FLink: String;
  202. FTopicNode : Boolean;
  203. FRefCount : Integer;
  204. FVersion: TDomElement;
  205. public
  206. constructor Create(const AName: String; ANode: TDOMElement);
  207. destructor Destroy; override;
  208. Function IncRefcount : Integer;
  209. function FindChild(const APathName: String): TDocNode;
  210. function CreateChildren(const APathName: String): TDocNode;
  211. // Properties for tree structure
  212. property FirstChild: TDocNode read FFirstChild;
  213. property NextSibling: TDocNode read FNextSibling;
  214. // Basic properties
  215. property Name: String read FName;
  216. property Node: TDOMElement read FNode;
  217. // Data fetched from the XML document
  218. property IsSkipped: Boolean read FIsSkipped;
  219. property ShortDescr: TDOMElement read FShortDescr;
  220. property Descr: TDOMElement read FDescr;
  221. property ErrorsDoc: TDOMElement read FErrorsDoc;
  222. Property Version : TDomElement Read FVersion;
  223. property SeeAlso: TDOMElement read FSeeAlso;
  224. property FirstExample: TDOMElement read FFirstExample;
  225. property Link: String read FLink;
  226. Property TopicNode : Boolean Read FTopicNode;
  227. Property RefCount : Integer Read FRefCount;
  228. end;
  229. // The main FPDoc engine
  230. { TFPDocEngine }
  231. TFPDocEngine = class(TPasTreeContainer)
  232. private
  233. protected
  234. DescrDocs: TObjectList; // List of XML documents
  235. DescrDocNames: TStringList; // Names of the XML documents
  236. FRootLinkNode: TLinkNode;
  237. FRootDocNode: TDocNode;
  238. FPackages: TList; // List of TFPPackage objects
  239. CurModule: TPasModule;
  240. CurPackageDocNode: TDocNode;
  241. public
  242. Output: String;
  243. HasContentFile: Boolean;
  244. HidePrivate: Boolean; // Hide private class members in output?
  245. HideProtected: Boolean; // Hide protected class members in output?
  246. WarnNoNode : Boolean; // Warn if no description node found for element.
  247. constructor Create;
  248. destructor Destroy; override;
  249. procedure SetPackageName(const APackageName: String);
  250. procedure ReadContentFile(const AFilename, ALinkPrefix: String);
  251. procedure WriteContentFile(const AFilename: String);
  252. function CreateElement(AClass: TPTreeElement; const AName: String;
  253. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  254. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  255. override;
  256. function FindElement(const AName: String): TPasElement; override;
  257. function FindModule(const AName: String): TPasModule; override;
  258. // Link tree support
  259. procedure AddLink(const APathName, ALinkTo: String);
  260. function FindAbsoluteLink(const AName: String): String;
  261. function ResolveLink(AModule: TPasModule; const ALinkDest: String): String;
  262. function FindLinkedNode(ANode: TDocNode): TDocNode;
  263. // Documentation file support
  264. procedure AddDocFile(const AFilename: String);
  265. // Documentation retrieval
  266. function FindDocNode(AElement: TPasElement): TDocNode;
  267. function FindDocNode(ARefModule: TPasModule; const AName: String): TDocNode;
  268. function FindShortDescr(AElement: TPasElement): TDOMElement;
  269. function FindShortDescr(ARefModule: TPasModule;
  270. const AName: String): TDOMElement;
  271. function GetExampleFilename(const ExElement: TDOMElement): String;
  272. property RootLinkNode: TLinkNode read FRootLinkNode;
  273. property RootDocNode: TDocNode read FRootDocNode;
  274. property Package: TPasPackage read FPackage;
  275. end;
  276. procedure TranslateDocStrings(const Lang: String);
  277. Function IsLinkNode(Node : TDomNode) : Boolean;
  278. Function IsExampleNode(Example : TDomNode) : Boolean;
  279. // returns true is link is an absolute URI
  280. Function IsLinkAbsolute(ALink: String): boolean;
  281. implementation
  282. uses SysUtils, Gettext, XMLRead;
  283. const
  284. AbsoluteLinkPrefixes : array[0..2] of string = ('/', 'http://', 'ms-its:');
  285. { TObjectList }
  286. destructor TObjectList.Destroy;
  287. var
  288. i: Integer;
  289. begin
  290. for i := 0 to Count - 1 do
  291. TObject(Items[i]).Free;
  292. inherited Destroy;
  293. end;
  294. { TLinkNode }
  295. constructor TLinkNode.Create(const AName, ALink: String);
  296. begin
  297. inherited Create;
  298. FName := AName;
  299. FLink := ALink;
  300. end;
  301. destructor TLinkNode.Destroy;
  302. begin
  303. if Assigned(FirstChild) then
  304. FirstChild.Free;
  305. if Assigned(NextSibling) then
  306. NextSibling.Free;
  307. inherited Destroy;
  308. end;
  309. function TLinkNode.FindChild(const APathName: String): TLinkNode;
  310. var
  311. DotPos: Integer;
  312. ChildName: String;
  313. Child: TLinkNode;
  314. begin
  315. if Length(APathName) = 0 then
  316. Result := Self
  317. else
  318. begin
  319. DotPos := Pos('.', APathName);
  320. if DotPos = 0 then
  321. ChildName := APathName
  322. else
  323. ChildName := Copy(APathName, 1, DotPos - 1);
  324. Child := FirstChild;
  325. while Assigned(Child) do
  326. begin
  327. if CompareText(Child.Name, ChildName) = 0 then
  328. begin
  329. if DotPos = 0 then
  330. Result := Child
  331. else
  332. Result := Child.FindChild(
  333. Copy(APathName, DotPos + 1, Length(APathName)));
  334. exit;
  335. end;
  336. Child := Child.NextSibling;
  337. end;
  338. Result := nil;
  339. end;
  340. end;
  341. function TLinkNode.CreateChildren(const APathName, ALinkTo: String): TLinkNode;
  342. var
  343. DotPos: Integer;
  344. ChildName: String;
  345. Child, LastChild: TLinkNode;
  346. begin
  347. if Length(APathName) = 0 then
  348. Result := Self
  349. else
  350. begin
  351. DotPos := Pos('.', APathName);
  352. if DotPos = 0 then
  353. ChildName := APathName
  354. else
  355. ChildName := Copy(APathName, 1, DotPos - 1);
  356. Child := FirstChild;
  357. LastChild := nil;
  358. while Assigned(Child) do
  359. begin
  360. if CompareText(Child.Name, ChildName) = 0 then
  361. begin
  362. if DotPos = 0 then
  363. Result := Child
  364. else
  365. Result := Child.CreateChildren(
  366. Copy(APathName, DotPos + 1, Length(APathName)), ALinkTo);
  367. exit;
  368. end;
  369. LastChild := Child;
  370. Child := Child.NextSibling;
  371. end;
  372. { No child found, let's create one if we are at the end of the path }
  373. if DotPos > 0 then
  374. // !!!: better throw an exception
  375. WriteLn('Link path does not exist: ', APathName);
  376. Result := TLinkNode.Create(ChildName, ALinkTo);
  377. if Assigned(LastChild) then
  378. LastChild.FNextSibling := Result
  379. else
  380. FFirstChild := Result;
  381. end;
  382. end;
  383. { TDocNode }
  384. constructor TDocNode.Create(const AName: String; ANode: TDOMElement);
  385. begin
  386. inherited Create;
  387. FName := AName;
  388. FNode := ANode;
  389. end;
  390. destructor TDocNode.Destroy;
  391. begin
  392. if Assigned(FirstChild) then
  393. FirstChild.Free;
  394. if Assigned(NextSibling) then
  395. NextSibling.Free;
  396. inherited Destroy;
  397. end;
  398. Function TDocNode.IncRefcount : Integer;
  399. begin
  400. Inc(FRefCount);
  401. Result:=FRefCount;
  402. end;
  403. function TDocNode.FindChild(const APathName: String): TDocNode;
  404. var
  405. DotPos: Integer;
  406. ChildName: String;
  407. Child: TDocNode;
  408. begin
  409. if Length(APathName) = 0 then
  410. Result := Self
  411. else
  412. begin
  413. DotPos := Pos('.', APathName);
  414. if DotPos = 0 then
  415. ChildName := APathName
  416. else
  417. ChildName := Copy(APathName, 1, DotPos - 1);
  418. Child := FirstChild;
  419. while Assigned(Child) do
  420. begin
  421. if CompareText(Child.Name, ChildName) = 0 then
  422. begin
  423. if DotPos = 0 then
  424. Result := Child
  425. else
  426. Result := Child.FindChild(
  427. Copy(APathName, DotPos + 1, Length(APathName)));
  428. exit;
  429. end;
  430. Child := Child.NextSibling;
  431. end;
  432. Result := nil;
  433. end;
  434. end;
  435. function TDocNode.CreateChildren(const APathName: String): TDocNode;
  436. var
  437. DotPos: Integer;
  438. ChildName: String;
  439. Child: TDocNode;
  440. begin
  441. if Length(APathName) = 0 then
  442. Result := Self
  443. else
  444. begin
  445. DotPos := Pos('.', APathName);
  446. if DotPos = 0 then
  447. ChildName := APathName
  448. else
  449. ChildName := Copy(APathName, 1, DotPos - 1);
  450. Child := FirstChild;
  451. while Assigned(Child) do
  452. begin
  453. if CompareText(Child.Name, ChildName) = 0 then
  454. begin
  455. if DotPos = 0 then
  456. Result := Child
  457. else
  458. Result := Child.CreateChildren(
  459. Copy(APathName, DotPos + 1, Length(APathName)));
  460. exit;
  461. end;
  462. Child := Child.NextSibling;
  463. end;
  464. // No child found, let's create one
  465. Result := TDocNode.Create(ChildName, nil);
  466. if Assigned(FirstChild) then
  467. begin
  468. Result.FNextSibling := FirstChild;
  469. FFirstChild := Result;
  470. end else
  471. FFirstChild := Result;
  472. if DotPos > 0 then
  473. Result := Result.CreateChildren(
  474. Copy(APathName, DotPos + 1, Length(APathName)));
  475. end;
  476. end;
  477. { TFPDocEngine }
  478. constructor TFPDocEngine.Create;
  479. begin
  480. inherited Create;
  481. DescrDocs := TObjectList.Create;
  482. DescrDocNames := TStringList.Create;
  483. FRootLinkNode := TLinkNode.Create('', '');
  484. FRootDocNode := TDocNode.Create('', nil);
  485. HidePrivate := True;
  486. InterfaceOnly:=True;
  487. FPackages := TList.Create;
  488. end;
  489. destructor TFPDocEngine.Destroy;
  490. var
  491. i: Integer;
  492. begin
  493. for i := 0 to FPackages.Count - 1 do
  494. TPasPackage(FPackages[i]).Release;
  495. FPackages.Free;
  496. FRootDocNode.Free;
  497. FRootLinkNode.Free;
  498. DescrDocNames.Free;
  499. DescrDocs.Free;
  500. inherited Destroy;
  501. end;
  502. procedure TFPDocEngine.SetPackageName(const APackageName: String);
  503. begin
  504. ASSERT(not Assigned(Package));
  505. FPackage := TPasPackage(inherited CreateElement(TPasPackage,
  506. '#' + APackageName, nil, '', 0));
  507. FPackages.Add(FPackage);
  508. CurPackageDocNode := RootDocNode.FindChild('#' + APackageName);
  509. If Assigned(CurPackageDocNode) then
  510. CurPackageDocNode.IncRefCount;
  511. end;
  512. procedure TFPDocEngine.ReadContentFile(const AFilename, ALinkPrefix: String);
  513. var
  514. f: Text;
  515. inheritanceinfo : TStringlist;
  516. procedure ReadLinkTree;
  517. var
  518. s: String;
  519. PrevSpaces, ThisSpaces, i, StackIndex: Integer;
  520. CurParent, PrevSibling, NewNode: TLinkNode;
  521. ParentStack, SiblingStack: array[0..7] of TLinkNode;
  522. begin
  523. PrevSpaces := 0;
  524. CurParent := RootLinkNode;
  525. PrevSibling := CurParent.FirstChild;
  526. if assigned(PrevSibling) then
  527. while assigned(PrevSibling.NextSibling) do
  528. PrevSibling := PrevSibling.NextSibling;
  529. StackIndex := 0;
  530. while True do
  531. begin
  532. ReadLn(f, s);
  533. if Length(s) = 0 then
  534. break;
  535. ThisSpaces := 0;
  536. while s[ThisSpaces + 1] = ' ' do
  537. Inc(ThisSpaces);
  538. if ThisSpaces <> PrevSpaces then
  539. begin
  540. if ThisSpaces > PrevSpaces then
  541. begin
  542. { Dive down one level }
  543. ParentStack[StackIndex] := CurParent;
  544. SiblingStack[StackIndex] := PrevSibling;
  545. Inc(StackIndex);
  546. CurParent := PrevSibling;
  547. PrevSibling := nil;
  548. end else
  549. while PrevSpaces > ThisSpaces do
  550. begin
  551. Dec(StackIndex);
  552. CurParent := ParentStack[StackIndex];
  553. PrevSibling := SiblingStack[StackIndex];
  554. Dec(PrevSpaces);
  555. end;
  556. PrevSpaces := ThisSpaces;
  557. end;
  558. i := ThisSpaces + 1;
  559. while s[i] <> ' ' do
  560. Inc(i);
  561. NewNode := TLinkNode.Create(Copy(s, ThisSpaces + 1, i - ThisSpaces - 1),
  562. ALinkPrefix + Copy(s, i + 1, Length(s)));
  563. if Assigned(PrevSibling) then
  564. PrevSibling.FNextSibling := NewNode
  565. else
  566. CurParent.FFirstChild := NewNode;
  567. PrevSibling := NewNode;
  568. end;
  569. end;
  570. function ResolvePackageModule(AName:String;var pkg:TPasPackage;var module:TPasModule;createnew:boolean):String;
  571. var
  572. DotPos, DotPos2, i,j: Integer;
  573. s: String;
  574. HPackage: TPasPackage;
  575. begin
  576. pkg:=nil; module:=nil; result:='';
  577. // Find or create package
  578. DotPos := Pos('.', AName);
  579. s := Copy(AName, 1, DotPos - 1);
  580. HPackage := nil;
  581. for i := 0 to FPackages.Count - 1 do
  582. if CompareText(TPasPackage(FPackages[i]).Name, s) = 0 then
  583. begin
  584. HPackage := TPasPackage(FPackages[i]);
  585. break;
  586. end;
  587. if not Assigned(HPackage) then
  588. begin
  589. if not CreateNew then
  590. exit;
  591. HPackage := TPasPackage(inherited CreateElement(TPasPackage, s, nil,
  592. '', 0));
  593. FPackages.Add(HPackage);
  594. end;
  595. // Find or create module
  596. DotPos2 := DotPos;
  597. repeat
  598. Inc(DotPos2);
  599. until AName[DotPos2] = '.';
  600. s := Copy(AName, DotPos + 1, DotPos2 - DotPos - 1);
  601. Module := nil;
  602. for i := 0 to HPackage.Modules.Count - 1 do
  603. if CompareText(TPasModule(HPackage.Modules[i]).Name, s) = 0 then
  604. begin
  605. Module := TPasModule(HPackage.Modules[i]);
  606. break;
  607. end;
  608. if not Assigned(Module) then
  609. begin
  610. if not CreateNew then
  611. exit;
  612. Module := TPasModule.Create(s, HPackage);
  613. Module.InterfaceSection := TInterfaceSection.Create('', Module);
  614. HPackage.Modules.Add(Module);
  615. end;
  616. pkg:=hpackage;
  617. result:=Copy(AName, DotPos2 + 1, length(AName)-dotpos2);
  618. end;
  619. function ResolveClassType(AName:String):TPasClassType;
  620. var
  621. pkg : TPasPackage;
  622. module : TPasModule;
  623. s : string;
  624. clslist : TList;
  625. ClassEl : TPasClassType;
  626. i : Integer;
  627. begin
  628. Result:=nil;
  629. s:=ResolvePackageModule(AName,pkg,module,False);
  630. if not assigned(module) then
  631. exit;
  632. clslist:=module.InterfaceSection.Classes;
  633. for i:=0 to clslist.count-1 do
  634. begin
  635. ClassEl := TPasClassType(clslist[i]);
  636. if CompareText(ClassEl.Name,s) =0 then
  637. exit(Classel);
  638. end;
  639. end;
  640. procedure ReadClasses;
  641. function CreateClass(const AName: String;InheritanceStr:String): TPasClassType;
  642. var
  643. DotPos, DotPos2, i,j: Integer;
  644. s: String;
  645. HPackage: TPasPackage;
  646. Module: TPasModule;
  647. begin
  648. s:= ResolvePackageModule(AName,HPackage,Module,True);
  649. // Create node for class
  650. Result := TPasClassType.Create(s, Module.InterfaceSection);
  651. Result.ObjKind := okClass;
  652. Module.InterfaceSection.Declarations.Add(Result);
  653. Module.InterfaceSection.Classes.Add(Result);
  654. // defer processing inheritancestr till all classes are loaded.
  655. if inheritancestr<>'' then
  656. InheritanceInfo.AddObject(Inheritancestr,result);
  657. end;
  658. procedure ProcessInheritanceStrings(inhInfo:TStringList);
  659. var i,j : integer;
  660. cls : TPasClassType;
  661. cls2: TPasClassType;
  662. inhclass : TStringList;
  663. begin
  664. inhclass:=TStringList.Create;
  665. inhclass.delimiter:=',';
  666. if InhInfo.Count>0 then
  667. for i:=0 to InhInfo.Count-1 do
  668. begin
  669. cls:=TPasClassType(InhInfo.Objects[i]);
  670. inhclass.clear;
  671. inhclass.delimitedtext:=InhInfo[i];
  672. for j:= 0 to inhclass.count-1 do
  673. begin
  674. writeln('processing',inhclass[j]);
  675. cls2:=TPasClassType(ResolveClassType(inhclass[j]));
  676. if assigned(cls2) and not (cls=cls2) then // save from tobject=implicit tobject
  677. begin
  678. cls2.addref;
  679. if j=0 then
  680. cls.ancestortype:=cls2
  681. else
  682. cls.interfaces.add(cls2);
  683. { if j=0 then
  684. writeln(cls.name, ' has as ancestor ',cls2.pathname)
  685. else
  686. writeln(cls.name, ' implements ',cls2.pathname)
  687. }
  688. end
  689. else
  690. if cls<>cls2 then
  691. writeln(cls.name,'''s dependancy ' ,inhclass[j],' ',j,' could not be resolved');
  692. end;
  693. end;
  694. end;
  695. var
  696. s, Name: String;
  697. CurClass: TPasClassType;
  698. i: Integer;
  699. Member: TPasElement;
  700. begin
  701. inheritanceinfo :=TStringlist.Create;
  702. Try
  703. CurClass := nil;
  704. while True do
  705. begin
  706. ReadLn(f, s);
  707. if Length(s) = 0 then
  708. break;
  709. if s[1] = '#' then
  710. begin
  711. // New class
  712. i := Pos(' ', s);
  713. CurClass := CreateClass(Copy(s, 1, i - 1), copy(s,i+1,length(s)));
  714. end else
  715. begin
  716. i := Pos(' ', s);
  717. if i = 0 then
  718. Name := Copy(s, 3, Length(s))
  719. else
  720. Name := Copy(s, 3, i - 3);
  721. case s[2] of
  722. 'M':
  723. Member := TPasProcedure.Create(Name, CurClass);
  724. 'P':
  725. begin
  726. Member := TPasProperty.Create(Name, CurClass);
  727. if i > 0 then
  728. while i <= Length(s) do
  729. begin
  730. case s[i] of
  731. 'r':
  732. TPasProperty(Member).ReadAccessorName := '<dummy>';
  733. 'w':
  734. TPasProperty(Member).WriteAccessorName := '<dummy>';
  735. 's':
  736. TPasProperty(Member).StoredAccessorName := '<dummy>';
  737. end;
  738. Inc(i);
  739. end;
  740. end;
  741. 'V':
  742. Member := TPasVariable.Create(Name, CurClass);
  743. else
  744. raise Exception.Create('Invalid member type: ' + s[2]);
  745. end;
  746. CurClass.Members.Add(Member);
  747. end;
  748. end;
  749. ProcessInheritanceStrings(Inheritanceinfo);
  750. finally
  751. inheritanceinfo.Free;
  752. end;
  753. end;
  754. var
  755. s: String;
  756. begin
  757. if not FileExists(AFileName) then
  758. raise EInOutError.Create('File not found: ' + AFileName);
  759. Assign(f, AFilename);
  760. Reset(f);
  761. while not EOF(f) do
  762. begin
  763. ReadLn(f, s);
  764. if (Length(s) = 0) or (s[1] = '#') then
  765. continue;
  766. if s = ':link tree' then
  767. ReadLinkTree
  768. else if s = ':classes' then
  769. ReadClasses
  770. else
  771. repeat
  772. ReadLn(f, s);
  773. until EOF(f) or (Length(s) = 0);
  774. end;
  775. Close(f);
  776. end;
  777. procedure TFPDocEngine.WriteContentFile(const AFilename: String);
  778. var
  779. ContentFile: Text;
  780. procedure ProcessLinkNode(ALinkNode: TLinkNode; const AIdent: String);
  781. var
  782. ChildNode: TLinkNode;
  783. begin
  784. WriteLn(ContentFile, AIdent, ALinkNode.Name, ' ', ALinkNode.Link);
  785. ChildNode := ALinkNode.FirstChild;
  786. while Assigned(ChildNode) do
  787. begin
  788. ProcessLinkNode(ChildNode, AIdent + ' ');
  789. ChildNode := ChildNode.NextSibling;
  790. end;
  791. end;
  792. var
  793. LinkNode: TLinkNode;
  794. i, j, k: Integer;
  795. Module: TPasModule;
  796. ClassDecl: TPasClassType;
  797. Member: TPasElement;
  798. s: String;
  799. begin
  800. Assign(ContentFile, AFilename);
  801. Rewrite(ContentFile);
  802. try
  803. WriteLn(ContentFile, '# FPDoc Content File');
  804. WriteLn(ContentFile, ':link tree');
  805. LinkNode := RootLinkNode.FirstChild;
  806. while Assigned(LinkNode) do
  807. begin
  808. if LinkNode.Name = Package.Name then
  809. begin
  810. ProcessLinkNode(LinkNode, '');
  811. end;
  812. LinkNode := LinkNode.NextSibling;
  813. end;
  814. if Assigned(Package) then
  815. begin
  816. WriteLn(ContentFile);
  817. WriteLn(ContentFile, ':classes');
  818. for i := 0 to Package.Modules.Count - 1 do
  819. begin
  820. Module := TPasModule(Package.Modules[i]);
  821. for j := 0 to Module.InterfaceSection.Classes.Count - 1 do
  822. begin
  823. ClassDecl := TPasClassType(Module.InterfaceSection.Classes[j]);
  824. Write(ContentFile, ClassDecl.PathName, ' ');
  825. if Assigned(ClassDecl.AncestorType) then
  826. Write(ContentFile, ClassDecl.AncestorType.PathName)
  827. else if ClassDecl.ObjKind = okClass then
  828. Write(ContentFile, '#rtl.System.TObject')
  829. else if ClassDecl.ObjKind = okInterface then
  830. Write(ContentFile, '#rtl.System.IUnknown');
  831. if ClassDecl.Interfaces.Count>0 then
  832. begin
  833. for k:=0 to ClassDecl.Interfaces.count-1 do
  834. write(contentfile,',',TPasClassType(ClassDecl.Interfaces[k]).PathName);
  835. end;
  836. writeln(contentfile);
  837. for k := 0 to ClassDecl.Members.Count - 1 do
  838. begin
  839. Member := TPasElement(ClassDecl.Members[k]);
  840. Write(ContentFile, Chr(Ord(Member.Visibility) + Ord('0')));
  841. SetLength(s, 0);
  842. if Member.ClassType = TPasVariable then
  843. Write(ContentFile, 'V')
  844. else if Member.ClassType = TPasProperty then
  845. begin
  846. Write(ContentFile, 'P');
  847. if Length(TPasProperty(Member).ReadAccessorName) > 0 then
  848. s := s + 'r';
  849. if Length(TPasProperty(Member).WriteAccessorName) > 0 then
  850. s := s + 'w';
  851. if Length(TPasProperty(Member).StoredAccessorName) > 0 then
  852. s := s + 's';
  853. end else
  854. Write(ContentFile, 'M'); // Member must be a method
  855. Write(ContentFile, Member.Name);
  856. if Length(s) > 0 then
  857. WriteLn(ContentFile, ' ', s)
  858. else
  859. WriteLn(ContentFile);
  860. end;
  861. end;
  862. end;
  863. end;
  864. finally
  865. Close(ContentFile);
  866. end;
  867. end;
  868. function TFPDocEngine.CreateElement(AClass: TPTreeElement; const AName: String;
  869. AParent: TPasElement; AVisibility: TPasMemberVisibility;
  870. const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
  871. begin
  872. Result := AClass.Create(AName, AParent);
  873. Result.Visibility := AVisibility;
  874. if AClass.InheritsFrom(TPasModule) then
  875. CurModule := TPasModule(Result);
  876. Result.SourceFilename := ASourceFilename;
  877. Result.SourceLinenumber := ASourceLinenumber;
  878. end;
  879. function TFPDocEngine.FindElement(const AName: String): TPasElement;
  880. function FindInModule(AModule: TPasModule; const LocalName: String): TPasElement;
  881. var
  882. l: TList;
  883. i: Integer;
  884. begin
  885. If assigned(AModule.InterfaceSection) and
  886. Assigned(AModule.InterfaceSection.Declarations) then
  887. begin
  888. l:=AModule.InterfaceSection.Declarations;
  889. for i := 0 to l.Count - 1 do
  890. begin
  891. Result := TPasElement(l[i]);
  892. if CompareText(Result.Name, LocalName) = 0 then
  893. exit;
  894. end;
  895. end;
  896. Result := nil;
  897. end;
  898. var
  899. i: Integer;
  900. //ModuleName, LocalName: String;
  901. Module: TPasElement;
  902. begin
  903. {!!!: Don't know if we ever will have to use the following:
  904. i := Pos('.', AName);
  905. if i <> 0 then
  906. begin
  907. WriteLn('Dot found in name: ', AName);
  908. Result := nil;
  909. end else
  910. begin}
  911. Result := FindInModule(CurModule, AName);
  912. if not Assigned(Result) then
  913. for i := CurModule.InterfaceSection.UsesList.Count - 1 downto 0 do
  914. begin
  915. Module := TPasElement(CurModule.InterfaceSection.UsesList[i]);
  916. if Module.ClassType = TPasModule then
  917. begin
  918. Result := FindInModule(TPasModule(Module), AName);
  919. if Assigned(Result) then
  920. exit;
  921. end;
  922. end;
  923. {end;}
  924. end;
  925. function TFPDocEngine.FindModule(const AName: String): TPasModule;
  926. function FindInPackage(APackage: TPasPackage): TPasModule;
  927. var
  928. i: Integer;
  929. begin
  930. for i := 0 to APackage.Modules.Count - 1 do
  931. begin
  932. Result := TPasModule(APackage.Modules[i]);
  933. if CompareText(Result.Name, AName) = 0 then
  934. exit;
  935. end;
  936. Result := nil;
  937. end;
  938. var
  939. i: Integer;
  940. begin
  941. Result := FindInPackage(Package);
  942. if not Assigned(Result) then
  943. for i := FPackages.Count - 1 downto 0 do
  944. begin
  945. if TPasPackage(FPackages[i]) = Package then
  946. continue;
  947. Result := FindInPackage(TPasPackage(FPackages[i]));
  948. if Assigned(Result) then
  949. exit;
  950. end;
  951. end;
  952. procedure TFPDocEngine.AddLink(const APathName, ALinkTo: String);
  953. begin
  954. RootLinkNode.CreateChildren(APathName, ALinkTo);
  955. end;
  956. function TFPDocEngine.FindAbsoluteLink(const AName: String): String;
  957. var
  958. LinkNode: TLinkNode;
  959. begin
  960. LinkNode := RootLinkNode.FindChild(AName);
  961. if Assigned(LinkNode) then
  962. Result := LinkNode.Link
  963. else
  964. SetLength(Result, 0);
  965. end;
  966. function TFPDocEngine.ResolveLink(AModule: TPasModule;
  967. const ALinkDest: String): String;
  968. var
  969. i: Integer;
  970. ThisPackage: TLinkNode;
  971. UnitList: TList;
  972. begin
  973. //WriteLn('ResolveLink(', ALinkDest, ')... ');
  974. if Length(ALinkDest) = 0 then
  975. begin
  976. SetLength(Result, 0);
  977. exit;
  978. end;
  979. if (ALinkDest[1] = '#') or (not assigned(AModule)) then
  980. Result := FindAbsoluteLink(ALinkDest)
  981. else
  982. begin
  983. Result := ResolveLink(AModule, amodule.packagename + '.' + ALinkDest);
  984. if Length(Result) > 0 then
  985. exit;
  986. Result := ResolveLink(AModule, AModule.PathName + '.' + ALinkDest);
  987. if Length(Result) > 0 then
  988. exit;
  989. { Try all packages }
  990. SetLength(Result, 0);
  991. ThisPackage := RootLinkNode.FirstChild;
  992. while Assigned(ThisPackage) do
  993. begin
  994. Result := ResolveLink(AModule, ThisPackage.Name + '.' + ALinkDest);
  995. if Length(Result) > 0 then
  996. exit;
  997. ThisPackage := ThisPackage.NextSibling;
  998. end;
  999. if Length(Result) = 0 then
  1000. begin
  1001. { Okay, then we have to try all imported units of the current module }
  1002. UnitList := AModule.InterfaceSection.UsesList;
  1003. for i := UnitList.Count - 1 downto 0 do
  1004. begin
  1005. { Try all packages }
  1006. ThisPackage := RootLinkNode.FirstChild;
  1007. while Assigned(ThisPackage) do
  1008. begin
  1009. Result := ResolveLink(AModule, ThisPackage.Name + '.' +
  1010. TPasType(UnitList[i]).Name + '.' + ALinkDest);
  1011. if Length(Result) > 0 then
  1012. exit;
  1013. ThisPackage := ThisPackage.NextSibling;
  1014. end;
  1015. end;
  1016. end;
  1017. end;
  1018. if Length(Result) = 0 then
  1019. for i := Length(ALinkDest) downto 1 do
  1020. if ALinkDest[i] = '.' then
  1021. begin
  1022. Result := ResolveLink(AModule, Copy(ALinkDest, 1, i - 1));
  1023. exit;
  1024. end;
  1025. end;
  1026. procedure TFPDocEngine.AddDocFile(const AFilename: String);
  1027. function ReadNode(OwnerDocNode: TDocNode; Element: TDOMElement): TDocNode;
  1028. var
  1029. Subnode: TDOMNode;
  1030. begin
  1031. if OwnerDocNode = RootDocNode then
  1032. Result := OwnerDocNode.CreateChildren('#' + Element['name'])
  1033. else
  1034. Result := OwnerDocNode.CreateChildren(Element['name']);
  1035. Result.FNode := Element;
  1036. Result.FLink := Element['link'];
  1037. Result.FIsSkipped := Element['skip'] = '1';
  1038. Subnode := Element.FirstChild;
  1039. while Assigned(Subnode) do
  1040. begin
  1041. if Subnode.NodeType = ELEMENT_NODE then
  1042. begin
  1043. if Subnode.NodeName = 'short' then
  1044. Result.FShortDescr := TDOMElement(Subnode)
  1045. else if Subnode.NodeName = 'descr' then
  1046. Result.FDescr := TDOMElement(Subnode)
  1047. else if Subnode.NodeName = 'version' then
  1048. begin
  1049. Result.FVersion := TDOMElement(Subnode)
  1050. end
  1051. else if Subnode.NodeName = 'errors' then
  1052. Result.FErrorsDoc := TDOMElement(Subnode)
  1053. else if Subnode.NodeName = 'seealso' then
  1054. Result.FSeeAlso := TDOMElement(Subnode)
  1055. else if (Subnode.NodeName = 'example') and
  1056. not Assigned(Result.FirstExample) then
  1057. Result.FFirstExample := TDOMElement(Subnode);
  1058. end;
  1059. Subnode := Subnode.NextSibling;
  1060. end;
  1061. end;
  1062. Procedure ReadTopics(TopicNode : TDocNode);
  1063. Var
  1064. SubNode : TDOMNode;
  1065. begin
  1066. SubNode:=TopicNode.FNode.FirstChilD;
  1067. While Assigned(SubNode) do
  1068. begin
  1069. If (SubNode.NodeType=ELEMENT_NODE) and (SubNode.NodeName='topic') then
  1070. With ReadNode(TopicNode,TDomElement(SubNode)) do
  1071. // We could allow recursion here, but we won't, because it doesn't work on paper.
  1072. FTopicNode:=True;
  1073. SubNode:=Subnode.NextSibling;
  1074. end;
  1075. end;
  1076. var
  1077. i: Integer;
  1078. Node, Subnode, Subsubnode: TDOMNode;
  1079. Element: TDOMElement;
  1080. Doc: TXMLDocument;
  1081. PackageDocNode, TopicNode,ModuleDocNode: TDocNode;
  1082. begin
  1083. ReadXMLFile(Doc, AFilename);
  1084. DescrDocs.Add(Doc);
  1085. DescrDocNames.Add(AFilename);
  1086. Node := Doc.DocumentElement.FirstChild;
  1087. while Assigned(Node) do
  1088. begin
  1089. if (Node.NodeType = ELEMENT_NODE) and (Node.NodeName = 'package') then
  1090. begin
  1091. PackageDocNode := ReadNode(RootDocNode, TDOMElement(Node));
  1092. PackageDocNode.IncRefCount;
  1093. // Scan all 'module' elements within this package element
  1094. Subnode := Node.FirstChild;
  1095. while Assigned(Subnode) do
  1096. begin
  1097. if (Subnode.NodeType = ELEMENT_NODE) then
  1098. begin
  1099. If (Subnode.NodeName = 'module') then
  1100. begin
  1101. ModuleDocNode := ReadNode(PackageDocNode, TDOMElement(Subnode));
  1102. // Scan all 'element' elements within this module element
  1103. Subsubnode := Subnode.FirstChild;
  1104. while Assigned(Subsubnode) do
  1105. begin
  1106. if (Subsubnode.NodeType = ELEMENT_NODE) then
  1107. begin
  1108. if (Subsubnode.NodeName = 'element') then
  1109. ReadNode(ModuleDocNode, TDOMElement(Subsubnode))
  1110. else if (SubSubNode.NodeName='topic') then
  1111. begin
  1112. TopicNode:=ReadNode(ModuleDocNode,TDomElement(SubSubNode));
  1113. TopicNode.FTopicNode:=True;
  1114. ReadTopics(TopicNode);
  1115. end;
  1116. end;
  1117. Subsubnode := Subsubnode.NextSibling;
  1118. end;
  1119. end
  1120. else if (SubNode.NodeName='topic') then
  1121. begin
  1122. TopicNode:=ReadNode(PackageDocNode,TDomElement(SubNode));
  1123. TopicNode.FTopicNode:=True;
  1124. ReadTopics(TopicNode);
  1125. end;
  1126. end;
  1127. Subnode := Subnode.NextSibling;
  1128. end;
  1129. end;
  1130. Node := Node.NextSibling;
  1131. end;
  1132. end;
  1133. function TFPDocEngine.FindDocNode(AElement: TPasElement): TDocNode;
  1134. begin
  1135. Result:=Nil;
  1136. If Assigned(AElement) then
  1137. begin
  1138. if AElement.InheritsFrom(TPasUnresolvedTypeRef) then
  1139. Result := FindDocNode(AElement.GetModule, AElement.Name)
  1140. else
  1141. Result := RootDocNode.FindChild(AElement.PathName);
  1142. if (Result=Nil) and
  1143. WarnNoNode and
  1144. (Length(AElement.PathName)>0) and
  1145. (AElement.PathName[1]='#') then
  1146. Writeln('No documentation node found for identifier : ',AElement.PathName);
  1147. end;
  1148. end;
  1149. function TFPDocEngine.FindDocNode(ARefModule: TPasModule;
  1150. const AName: String): TDocNode;
  1151. var
  1152. CurPackage: TDocNode;
  1153. UnitList: TList;
  1154. i: Integer;
  1155. begin
  1156. if Length(AName) = 0 then
  1157. Result := nil
  1158. else
  1159. begin
  1160. if AName[1] = '#' then
  1161. Result := RootDocNode.FindChild(AName)
  1162. else
  1163. Result := RootDocNode.FindChild(Package.Name + '.' + AName);
  1164. if (not Assigned(Result)) and Assigned(ARefModule) then
  1165. Result := RootDocNode.FindChild(ARefModule.PathName + '.' + AName);
  1166. if (not Assigned(Result)) and (AName[1] <> '#') then
  1167. begin
  1168. CurPackage := RootDocNode.FirstChild;
  1169. while Assigned(CurPackage) do
  1170. begin
  1171. Result := RootDocNode.FindChild(CurPackage.Name + '.' + AName);
  1172. if Assigned(Result) then
  1173. break;
  1174. CurPackage := CurPackage.NextSibling;
  1175. end;
  1176. if not Assigned(Result) then
  1177. begin
  1178. { Okay, then we have to try all imported units of the current module }
  1179. UnitList := CurModule.InterfaceSection.UsesList;
  1180. for i := UnitList.Count - 1 downto 0 do
  1181. begin
  1182. { Try all packages }
  1183. CurPackage := RootDocNode.FirstChild;
  1184. while Assigned(CurPackage) do
  1185. begin
  1186. Result := RootDocNode.FindChild(CurPackage.Name + '.' +
  1187. TPasType(UnitList[i]).Name + '.' + AName);
  1188. if Assigned(Result) then
  1189. break;
  1190. CurPackage := CurPackage.NextSibling;
  1191. end;
  1192. end;
  1193. end;
  1194. end;
  1195. end;
  1196. end;
  1197. function TFPDocEngine.FindShortDescr(AElement: TPasElement): TDOMElement;
  1198. var
  1199. DocNode,N: TDocNode;
  1200. begin
  1201. DocNode := FindDocNode(AElement);
  1202. if Assigned(DocNode) then
  1203. begin
  1204. N:=FindLinkedNode(DocNode);
  1205. If (N<>Nil) then
  1206. DocNode:=N;
  1207. Result := DocNode.ShortDescr;
  1208. end
  1209. else
  1210. Result := nil;
  1211. end;
  1212. function TFPDocEngine.FindLinkedNode(ANode : TDocNode) : TDocNode;
  1213. Var
  1214. S: String;
  1215. begin
  1216. If (ANode.Link='') then
  1217. Result:=Nil
  1218. else
  1219. Result:=FindDocNode(CurModule,ANode.Link);
  1220. end;
  1221. function TFPDocEngine.FindShortDescr(ARefModule: TPasModule;
  1222. const AName: String): TDOMElement;
  1223. var
  1224. N,DocNode: TDocNode;
  1225. begin
  1226. DocNode := FindDocNode(ARefModule, AName);
  1227. if Assigned(DocNode) then
  1228. begin
  1229. N:=FindLinkedNode(DocNode);
  1230. If (N<>Nil) then
  1231. DocNode:=N;
  1232. Result := DocNode.ShortDescr;
  1233. end
  1234. else
  1235. Result := nil;
  1236. end;
  1237. function TFPDocEngine.GetExampleFilename(const ExElement: TDOMElement): String;
  1238. var
  1239. i: Integer;
  1240. fn : String;
  1241. begin
  1242. Result:='';
  1243. for i := 0 to DescrDocs.Count - 1 do
  1244. begin
  1245. Fn:=ExElement['file'];
  1246. if (FN<>'') and (TDOMDocument(DescrDocs[i]) = ExElement.OwnerDocument) then
  1247. begin
  1248. Result := ExtractFilePath(DescrDocNames[i]) + FN;
  1249. if (ExtractFileExt(Result)='') then
  1250. Result:=Result+'.pp';
  1251. end;
  1252. end;
  1253. end;
  1254. { Global helpers }
  1255. procedure TranslateDocStrings(const Lang: String);
  1256. Const
  1257. {$ifdef unix}
  1258. DefDir = '/usr/local/share/locale';
  1259. {$else}
  1260. DefDir = 'intl';
  1261. {$endif}
  1262. var
  1263. mo: TMOFile;
  1264. dir : string;
  1265. begin
  1266. dir:=modir;
  1267. If Dir='' then
  1268. Dir:=DefDir;
  1269. Dir:=IncludeTrailingPathDelimiter(Dir);
  1270. {$IFDEF Unix}
  1271. mo := TMOFile.Create(Format(Dir+'%s/LC_MESSAGES/dglobals.mo', [Lang]));
  1272. {$ELSE}
  1273. mo := TMOFile.Create(Format(Dir+'dglobals.%s.mo', [Lang]));
  1274. {$ENDIF}
  1275. try
  1276. TranslateResourceStrings(mo);
  1277. finally
  1278. mo.Free;
  1279. end;
  1280. end;
  1281. Function IsLinkNode(Node : TDomNode) : Boolean;
  1282. begin
  1283. Result:=Assigned(Node) and (Node.NodeType = ELEMENT_NODE) and (Node.NodeName = 'link');
  1284. end;
  1285. Function IsExampleNode(Example : TDomNode) : Boolean;
  1286. begin
  1287. Result:=Assigned(Example) and (Example.NodeType = ELEMENT_NODE) and (Example.NodeName = 'example')
  1288. end;
  1289. function IsLinkAbsolute(ALink: String): boolean;
  1290. var
  1291. i: integer;
  1292. begin
  1293. Result := false;
  1294. for i := low(AbsoluteLinkPrefixes) to high(AbsoluteLinkPrefixes) do
  1295. if CompareText(AbsoluteLinkPrefixes[i], copy(ALink,1,length(AbsoluteLinkPrefixes[i])))=0 then begin
  1296. Result := true;
  1297. break;
  1298. end;
  1299. end;
  1300. initialization
  1301. LEOL:=Length(LineEnding);
  1302. end.