dglobals.pp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. function CanWeExit(AResult: string): boolean;
  973. var
  974. s: string;
  975. begin
  976. s := StringReplace(Lowercase(ALinkDest), '.', '_', [rfReplaceAll]);
  977. Result := pos(s, AResult) > 0;
  978. end;
  979. begin
  980. //system.WriteLn('ResolveLink(', AModule.Name, ' - ', ALinkDest, ')... ');
  981. if Length(ALinkDest) = 0 then
  982. begin
  983. SetLength(Result, 0);
  984. exit;
  985. end;
  986. if (ALinkDest[1] = '#') or (not assigned(AModule)) then
  987. Result := FindAbsoluteLink(ALinkDest)
  988. else
  989. begin
  990. if Pos(AModule.Name, ALinkDest) = 1 then
  991. begin
  992. Result := ResolveLink(AModule, amodule.packagename + '.' + ALinkDest);
  993. if CanWeExit(Result) then
  994. Exit;
  995. end
  996. else
  997. begin
  998. Result := ResolveLink(AModule, AModule.PathName + '.' + ALinkDest);
  999. if CanWeExit(Result) then
  1000. Exit;
  1001. end;
  1002. { Try all packages }
  1003. SetLength(Result, 0);
  1004. ThisPackage := RootLinkNode.FirstChild;
  1005. while Assigned(ThisPackage) do
  1006. begin
  1007. Result := ResolveLink(AModule, ThisPackage.Name + '.' + ALinkDest);
  1008. if CanWeExit(Result) then
  1009. Exit;
  1010. ThisPackage := ThisPackage.NextSibling;
  1011. end;
  1012. if not CanWeExit(Result) then
  1013. begin
  1014. { Okay, then we have to try all imported units of the current module }
  1015. UnitList := AModule.InterfaceSection.UsesList;
  1016. for i := UnitList.Count - 1 downto 0 do
  1017. begin
  1018. { Try all packages }
  1019. ThisPackage := RootLinkNode.FirstChild;
  1020. while Assigned(ThisPackage) do
  1021. begin
  1022. Result := ResolveLink(AModule, ThisPackage.Name + '.' +
  1023. TPasType(UnitList[i]).Name + '.' + ALinkDest);
  1024. if CanWeExit(Result) then
  1025. Exit;
  1026. ThisPackage := ThisPackage.NextSibling;
  1027. end;
  1028. end;
  1029. end;
  1030. end;
  1031. if Length(Result) = 0 then
  1032. for i := Length(ALinkDest) downto 1 do
  1033. if ALinkDest[i] = '.' then
  1034. begin
  1035. Result := ResolveLink(AModule, Copy(ALinkDest, 1, i - 1));
  1036. exit;
  1037. end;
  1038. end;
  1039. procedure TFPDocEngine.AddDocFile(const AFilename: String);
  1040. function ReadNode(OwnerDocNode: TDocNode; Element: TDOMElement): TDocNode;
  1041. var
  1042. Subnode: TDOMNode;
  1043. begin
  1044. if OwnerDocNode = RootDocNode then
  1045. Result := OwnerDocNode.CreateChildren('#' + Element['name'])
  1046. else
  1047. Result := OwnerDocNode.CreateChildren(Element['name']);
  1048. Result.FNode := Element;
  1049. Result.FLink := Element['link'];
  1050. Result.FIsSkipped := Element['skip'] = '1';
  1051. Subnode := Element.FirstChild;
  1052. while Assigned(Subnode) do
  1053. begin
  1054. if Subnode.NodeType = ELEMENT_NODE then
  1055. begin
  1056. if Subnode.NodeName = 'short' then
  1057. Result.FShortDescr := TDOMElement(Subnode)
  1058. else if Subnode.NodeName = 'descr' then
  1059. Result.FDescr := TDOMElement(Subnode)
  1060. else if Subnode.NodeName = 'version' then
  1061. begin
  1062. Result.FVersion := TDOMElement(Subnode)
  1063. end
  1064. else if Subnode.NodeName = 'errors' then
  1065. Result.FErrorsDoc := TDOMElement(Subnode)
  1066. else if Subnode.NodeName = 'seealso' then
  1067. Result.FSeeAlso := TDOMElement(Subnode)
  1068. else if (Subnode.NodeName = 'example') and
  1069. not Assigned(Result.FirstExample) then
  1070. Result.FFirstExample := TDOMElement(Subnode);
  1071. end;
  1072. Subnode := Subnode.NextSibling;
  1073. end;
  1074. end;
  1075. Procedure ReadTopics(TopicNode : TDocNode);
  1076. Var
  1077. SubNode : TDOMNode;
  1078. begin
  1079. SubNode:=TopicNode.FNode.FirstChilD;
  1080. While Assigned(SubNode) do
  1081. begin
  1082. If (SubNode.NodeType=ELEMENT_NODE) and (SubNode.NodeName='topic') then
  1083. With ReadNode(TopicNode,TDomElement(SubNode)) do
  1084. // We could allow recursion here, but we won't, because it doesn't work on paper.
  1085. FTopicNode:=True;
  1086. SubNode:=Subnode.NextSibling;
  1087. end;
  1088. end;
  1089. var
  1090. i: Integer;
  1091. Node, Subnode, Subsubnode: TDOMNode;
  1092. Element: TDOMElement;
  1093. Doc: TXMLDocument;
  1094. PackageDocNode, TopicNode,ModuleDocNode: TDocNode;
  1095. begin
  1096. ReadXMLFile(Doc, AFilename);
  1097. DescrDocs.Add(Doc);
  1098. DescrDocNames.Add(AFilename);
  1099. Node := Doc.DocumentElement.FirstChild;
  1100. while Assigned(Node) do
  1101. begin
  1102. if (Node.NodeType = ELEMENT_NODE) and (Node.NodeName = 'package') then
  1103. begin
  1104. PackageDocNode := ReadNode(RootDocNode, TDOMElement(Node));
  1105. PackageDocNode.IncRefCount;
  1106. // Scan all 'module' elements within this package element
  1107. Subnode := Node.FirstChild;
  1108. while Assigned(Subnode) do
  1109. begin
  1110. if (Subnode.NodeType = ELEMENT_NODE) then
  1111. begin
  1112. If (Subnode.NodeName = 'module') then
  1113. begin
  1114. ModuleDocNode := ReadNode(PackageDocNode, TDOMElement(Subnode));
  1115. // Scan all 'element' elements within this module element
  1116. Subsubnode := Subnode.FirstChild;
  1117. while Assigned(Subsubnode) do
  1118. begin
  1119. if (Subsubnode.NodeType = ELEMENT_NODE) then
  1120. begin
  1121. if (Subsubnode.NodeName = 'element') then
  1122. ReadNode(ModuleDocNode, TDOMElement(Subsubnode))
  1123. else if (SubSubNode.NodeName='topic') then
  1124. begin
  1125. TopicNode:=ReadNode(ModuleDocNode,TDomElement(SubSubNode));
  1126. TopicNode.FTopicNode:=True;
  1127. ReadTopics(TopicNode);
  1128. end;
  1129. end;
  1130. Subsubnode := Subsubnode.NextSibling;
  1131. end;
  1132. end
  1133. else if (SubNode.NodeName='topic') then
  1134. begin
  1135. TopicNode:=ReadNode(PackageDocNode,TDomElement(SubNode));
  1136. TopicNode.FTopicNode:=True;
  1137. ReadTopics(TopicNode);
  1138. end;
  1139. end;
  1140. Subnode := Subnode.NextSibling;
  1141. end;
  1142. end;
  1143. Node := Node.NextSibling;
  1144. end;
  1145. end;
  1146. function TFPDocEngine.FindDocNode(AElement: TPasElement): TDocNode;
  1147. begin
  1148. Result:=Nil;
  1149. If Assigned(AElement) then
  1150. begin
  1151. if AElement.InheritsFrom(TPasUnresolvedTypeRef) then
  1152. Result := FindDocNode(AElement.GetModule, AElement.Name)
  1153. else
  1154. Result := RootDocNode.FindChild(AElement.PathName);
  1155. if (Result=Nil) and
  1156. WarnNoNode and
  1157. (Length(AElement.PathName)>0) and
  1158. (AElement.PathName[1]='#') then
  1159. Writeln('No documentation node found for identifier : ',AElement.PathName);
  1160. end;
  1161. end;
  1162. function TFPDocEngine.FindDocNode(ARefModule: TPasModule;
  1163. const AName: String): TDocNode;
  1164. var
  1165. CurPackage: TDocNode;
  1166. UnitList: TList;
  1167. i: Integer;
  1168. begin
  1169. if Length(AName) = 0 then
  1170. Result := nil
  1171. else
  1172. begin
  1173. if AName[1] = '#' then
  1174. Result := RootDocNode.FindChild(AName)
  1175. else
  1176. Result := RootDocNode.FindChild(Package.Name + '.' + AName);
  1177. if (not Assigned(Result)) and Assigned(ARefModule) then
  1178. Result := RootDocNode.FindChild(ARefModule.PathName + '.' + AName);
  1179. if (not Assigned(Result)) and (AName[1] <> '#') then
  1180. begin
  1181. CurPackage := RootDocNode.FirstChild;
  1182. while Assigned(CurPackage) do
  1183. begin
  1184. Result := RootDocNode.FindChild(CurPackage.Name + '.' + AName);
  1185. if Assigned(Result) then
  1186. break;
  1187. CurPackage := CurPackage.NextSibling;
  1188. end;
  1189. if not Assigned(Result) then
  1190. begin
  1191. { Okay, then we have to try all imported units of the current module }
  1192. UnitList := CurModule.InterfaceSection.UsesList;
  1193. for i := UnitList.Count - 1 downto 0 do
  1194. begin
  1195. { Try all packages }
  1196. CurPackage := RootDocNode.FirstChild;
  1197. while Assigned(CurPackage) do
  1198. begin
  1199. Result := RootDocNode.FindChild(CurPackage.Name + '.' +
  1200. TPasType(UnitList[i]).Name + '.' + AName);
  1201. if Assigned(Result) then
  1202. break;
  1203. CurPackage := CurPackage.NextSibling;
  1204. end;
  1205. end;
  1206. end;
  1207. end;
  1208. end;
  1209. end;
  1210. function TFPDocEngine.FindShortDescr(AElement: TPasElement): TDOMElement;
  1211. var
  1212. DocNode,N: TDocNode;
  1213. begin
  1214. DocNode := FindDocNode(AElement);
  1215. if Assigned(DocNode) then
  1216. begin
  1217. N:=FindLinkedNode(DocNode);
  1218. If (N<>Nil) then
  1219. DocNode:=N;
  1220. Result := DocNode.ShortDescr;
  1221. end
  1222. else
  1223. Result := nil;
  1224. end;
  1225. function TFPDocEngine.FindLinkedNode(ANode : TDocNode) : TDocNode;
  1226. Var
  1227. S: String;
  1228. begin
  1229. If (ANode.Link='') then
  1230. Result:=Nil
  1231. else
  1232. Result:=FindDocNode(CurModule,ANode.Link);
  1233. end;
  1234. function TFPDocEngine.FindShortDescr(ARefModule: TPasModule;
  1235. const AName: String): TDOMElement;
  1236. var
  1237. N,DocNode: TDocNode;
  1238. begin
  1239. DocNode := FindDocNode(ARefModule, AName);
  1240. if Assigned(DocNode) then
  1241. begin
  1242. N:=FindLinkedNode(DocNode);
  1243. If (N<>Nil) then
  1244. DocNode:=N;
  1245. Result := DocNode.ShortDescr;
  1246. end
  1247. else
  1248. Result := nil;
  1249. end;
  1250. function TFPDocEngine.GetExampleFilename(const ExElement: TDOMElement): String;
  1251. var
  1252. i: Integer;
  1253. fn : String;
  1254. begin
  1255. Result:='';
  1256. for i := 0 to DescrDocs.Count - 1 do
  1257. begin
  1258. Fn:=ExElement['file'];
  1259. if (FN<>'') and (TDOMDocument(DescrDocs[i]) = ExElement.OwnerDocument) then
  1260. begin
  1261. Result := ExtractFilePath(DescrDocNames[i]) + FN;
  1262. if (ExtractFileExt(Result)='') then
  1263. Result:=Result+'.pp';
  1264. end;
  1265. end;
  1266. end;
  1267. { Global helpers }
  1268. procedure TranslateDocStrings(const Lang: String);
  1269. Const
  1270. {$ifdef unix}
  1271. DefDir = '/usr/local/share/locale';
  1272. {$else}
  1273. DefDir = 'intl';
  1274. {$endif}
  1275. var
  1276. mo: TMOFile;
  1277. dir : string;
  1278. begin
  1279. dir:=modir;
  1280. If Dir='' then
  1281. Dir:=DefDir;
  1282. Dir:=IncludeTrailingPathDelimiter(Dir);
  1283. {$IFDEF Unix}
  1284. mo := TMOFile.Create(Format(Dir+'%s/LC_MESSAGES/dglobals.mo', [Lang]));
  1285. {$ELSE}
  1286. mo := TMOFile.Create(Format(Dir+'dglobals.%s.mo', [Lang]));
  1287. {$ENDIF}
  1288. try
  1289. TranslateResourceStrings(mo);
  1290. finally
  1291. mo.Free;
  1292. end;
  1293. end;
  1294. Function IsLinkNode(Node : TDomNode) : Boolean;
  1295. begin
  1296. Result:=Assigned(Node) and (Node.NodeType = ELEMENT_NODE) and (Node.NodeName = 'link');
  1297. end;
  1298. Function IsExampleNode(Example : TDomNode) : Boolean;
  1299. begin
  1300. Result:=Assigned(Example) and (Example.NodeType = ELEMENT_NODE) and (Example.NodeName = 'example')
  1301. end;
  1302. function IsLinkAbsolute(ALink: String): boolean;
  1303. var
  1304. i: integer;
  1305. begin
  1306. Result := false;
  1307. for i := low(AbsoluteLinkPrefixes) to high(AbsoluteLinkPrefixes) do
  1308. if CompareText(AbsoluteLinkPrefixes[i], copy(ALink,1,length(AbsoluteLinkPrefixes[i])))=0 then begin
  1309. Result := true;
  1310. break;
  1311. end;
  1312. end;
  1313. initialization
  1314. LEOL:=Length(LineEnding);
  1315. end.