dwlinear.pp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. {$mode objfpc}
  2. {$H+}
  3. unit dwlinear;
  4. interface
  5. uses
  6. Classes, SysUtils, DGlobals, dWriter, pastree, dom;
  7. Type
  8. { TLinearWriter }
  9. TLinearWriter = Class(TFPDocWriter)
  10. FStream : TStream;
  11. PackageName: String;
  12. Module: TPasModule;
  13. ModuleName: String;
  14. FLastURL : DomString;
  15. private
  16. Protected
  17. // Writing support.
  18. procedure Write(const s: String); virtual;
  19. procedure WriteLn(const s: String); virtual;
  20. procedure WriteF(const s: String; const Args: array of const);
  21. procedure WriteLnF(const s: String; const Args: array of const);
  22. Function PushWriteContext(S : TStream) : TStream;
  23. Procedure PopWriteContext(S : TSTream);
  24. procedure WriteLabel(El: TPasElement);
  25. procedure WriteIndex(El: TPasElement);
  26. // Auxiliary routines
  27. procedure DescrBeginURL(const AURL: DOMString); override; // Provides a default implementation
  28. procedure DescrEndURL; override;
  29. procedure SortElementList(List : TList);
  30. procedure StartListing(Frames: Boolean);
  31. Function ShowMember(M : TPasElement) : boolean;
  32. procedure StartChapter(ChapterName : String; ChapterLabel : String); virtual;
  33. procedure StartSection(SectionName : String; SectionLabel : String); virtual;
  34. procedure StartSubSection(SubSectionName : String; SubSectionLabel : String); virtual;
  35. procedure StartSubSubSection(SubSubSectionName : String; SubSubSectionLabel : String); virtual;
  36. Function GetDescrString(AContext: TPasElement; DescrNode: TDOMElement) : String;
  37. function ConstValue(ConstDecl: TPasConst): String; virtual;
  38. procedure ProcessSection(ASection: TPasSection); virtual;
  39. // Procedures which MAY be overridden in descendents
  40. procedure WriteBeginDocument; virtual;
  41. procedure WriteEndDocument; virtual;
  42. Function EscapeText(S : String) : String; virtual;
  43. Function StripText(S : String) : String; virtual;
  44. Procedure StartProcedure; Virtual;
  45. Procedure EndProcedure; Virtual;
  46. Procedure StartProperty; Virtual;
  47. Procedure EndProperty; Virtual;
  48. Procedure StartSynopsis; Virtual;
  49. Procedure StartDeclaration; Virtual;
  50. Procedure StartVisibility; Virtual;
  51. Procedure StartDescription; Virtual;
  52. Procedure StartAccess; Virtual;
  53. Procedure StartErrors; Virtual;
  54. Procedure StartVersion; Virtual;
  55. Procedure StartSeealso; Virtual;
  56. Procedure EndSeealso; Virtual;
  57. // Procedures which MUST be overridden in descendents;
  58. procedure WriteCommentLine; virtual; abstract;
  59. procedure WriteComment(Comment : String); virtual; abstract;
  60. function GetLabel(AElement: TPasElement): String; virtual; abstract;
  61. procedure WriteLabel(Const S : String); virtual; abstract;
  62. procedure WriteIndex(Const S : String); virtual; abstract;
  63. procedure StartChapter(ChapterName : String); virtual; abstract;
  64. procedure StartSection(SectionName : String); virtual; abstract;
  65. procedure StartSubSection(SubSectionName : String); virtual; abstract;
  66. procedure StartSubSubSection(SubSubSectionName : String); virtual; abstract;
  67. procedure StartListing(Frames: Boolean; const name: String); virtual; abstract;
  68. procedure EndListing; virtual; abstract;
  69. Procedure WriteExampleFile(FN : String); virtual; abstract;
  70. procedure StartOverview(WithAccess : Boolean); virtual; Abstract;
  71. procedure EndOverview; virtual; Abstract;
  72. procedure WriteOverviewMember(ALabel,AName,Access,ADescr : String); virtual; Abstract;
  73. procedure WriteOverviewMember(ALabel,AName,ADescr : String); virtual; Abstract;
  74. procedure StartUnitOverview(AModuleName,AModuleLabel : String);virtual; Abstract;
  75. procedure WriteUnitEntry(UnitRef : TPasType);virtual; Abstract;
  76. procedure EndUnitOverview; virtual; Abstract;
  77. Class Function FileNameExtension : String;virtual; Abstract;
  78. Property LastURL : DomString Read FLastURL Write FLastURL;
  79. Public
  80. Constructor Create(APackage: TPasPackage; AEngine: TFPDocEngine); override;
  81. procedure WriteDoc; override;
  82. // Linear Documentation writing methods.
  83. Procedure ProcessPackage;
  84. Procedure ProcessTopics(DocNode : TDocNode; Alevel : Integer);
  85. procedure WriteResourceStrings(ASection: TPasSection);
  86. procedure WriteUnitOverview(ASection: TPasSection);
  87. procedure WriteVarsConstsTypes(ASection: TPasSection);
  88. procedure WriteConsts(ASection: TPasSection);
  89. procedure WriteTypes(ASection: TPasSection);
  90. procedure WriteEnumElements(TypeDecl : TPasEnumType);
  91. procedure WriteVars(ASection: TPasSection);
  92. procedure WriteFunctionsAndProcedures(ASection: TPasSection);
  93. procedure WriteProcedure(ProcDecl: TPasProcedureBase);
  94. procedure WriteClasses(ASection: TPasSection);
  95. procedure WriteClassDecl(ClassDecl: TPasClassType);
  96. procedure WriteClassMethodOverview(ClassDecl: TPasClassType);
  97. procedure WriteClassPropertyOverview(ClassDecl: TPasClassType);
  98. procedure WriteProperty(PropDecl: TPasProperty);
  99. procedure WriteExample(ADocNode: TDocNode);
  100. procedure WriteSeeAlso(ADocNode: TDocNode);
  101. Procedure WriteTopicNode(Node : TDocNode; Level : Integer);
  102. // Overriden from fpdocwriter;
  103. procedure DescrWriteText(const AText: DOMString); override;
  104. end;
  105. implementation
  106. { TLinearWriter }
  107. { ---------------------------------------------------------------------
  108. Writing support
  109. ---------------------------------------------------------------------}
  110. Function TLinearWriter.PushWriteContext(S : TStream) : TStream;
  111. begin
  112. Result:=FStream;
  113. FStream:=S;
  114. end;
  115. Procedure TLinearWriter.PopWriteContext(S : TSTream);
  116. begin
  117. FStream:=S;
  118. end;
  119. procedure TLinearWriter.Write(const s: String);
  120. Var
  121. L : Integer;
  122. begin
  123. L:=Length(S);
  124. If (L>0) then
  125. FStream.Write(PChar(S)^,L);
  126. end;
  127. procedure TLinearWriter.WriteF(const s: String; const Args: array of const);
  128. begin
  129. Write(Format(S,Args));
  130. end;
  131. procedure TLinearWriter.WriteLn(const s: String);
  132. begin
  133. Write(S);
  134. Write(LineEnding);
  135. end;
  136. procedure TLinearWriter.WriteLnF(const s: String; const Args: array of const);
  137. begin
  138. Write(Format(S,Args));
  139. Write(LineEnding);
  140. end;
  141. procedure TLinearWriter.DescrWriteText(const AText: DOMString);
  142. begin
  143. self.Write(EscapeText(AText));
  144. end;
  145. Function TLinearWriter.GetDescrString(AContext: TPasElement; DescrNode: TDOMElement) : String;
  146. Var
  147. S : TStringStream;
  148. F : TStream;
  149. begin
  150. Result:='';
  151. if Assigned(DescrNode) then
  152. begin
  153. S:=TStringStream.Create('');
  154. Try
  155. F:=PushWriteContext(S);
  156. Try
  157. ConvertDescr(AContext, DescrNode, False);
  158. Result:=S.DataString;
  159. FInally
  160. PopWriteContext(F);
  161. end;
  162. finally
  163. S.FRee;
  164. end;
  165. end;
  166. end;
  167. { ---------------------------------------------------------------------
  168. Auxiliary routines
  169. ---------------------------------------------------------------------}
  170. procedure TLinearWriter.WriteLabel(El: TPasElement);
  171. begin
  172. WriteLabel(GetLabel(El));
  173. end;
  174. procedure TLinearWriter.WriteIndex(El: TPasElement);
  175. begin
  176. WriteIndex(EL.Name);
  177. end;
  178. procedure TLinearWriter.DescrBeginURL(const AURL: DOMString);
  179. begin
  180. FLastURL:=AURL;
  181. end;
  182. procedure TLinearWriter.DescrEndURL;
  183. begin
  184. If (FLastURL<>'') then
  185. Writeln(Format(SSeeURL,[EscapeText(FLastURL)]));
  186. FLastURL:='';
  187. end;
  188. procedure TLinearWriter.StartListing(Frames: Boolean);
  189. begin
  190. StartListing(Frames,'');
  191. end;
  192. procedure TLinearWriter.StartChapter(ChapterName: String; ChapterLabel: String);
  193. begin
  194. StartChapter(ChapterName);
  195. WriteLabel(ChapterLabel);
  196. end;
  197. procedure TLinearWriter.StartSection(SectionName: String; SectionLabel: String);
  198. begin
  199. StartSection(SectionName);
  200. WriteLabel(SectionLabel);
  201. end;
  202. procedure TLinearWriter.StartSubSection(SubSectionName: String; SubSectionLabel: String);
  203. begin
  204. StartSubSection(SubSectionName);
  205. WriteLabel(SubSectionLabel);
  206. end;
  207. procedure TLinearWriter.StartSubSubSection(SubSubSectionName: String;
  208. SubSubSectionLabel: String);
  209. begin
  210. StartSubSubSection(SubSubSectionName);
  211. WriteLabel(SubSubSectionLabel);
  212. end;
  213. { ---------------------------------------------------------------------
  214. Default implementations, may be overridden in descendents
  215. ---------------------------------------------------------------------}
  216. Function TLinearWriter.EscapeText(S : String) : String;
  217. begin
  218. Result:=S;
  219. end;
  220. Function TLinearWriter.StripText(S : String) : String;
  221. begin
  222. Result:=S;
  223. end;
  224. Procedure TLinearWriter.StartProcedure;
  225. begin
  226. Writeln(SDocProcedure+':');
  227. end;
  228. Procedure TLinearWriter.StartSynopsis;
  229. begin
  230. Writeln('');
  231. Writeln(SDocSynopsis+':');
  232. end;
  233. Procedure TLinearWriter.StartDeclaration;
  234. begin
  235. Writeln('');
  236. Writeln(SDocDeclaration+':');
  237. end;
  238. Procedure TLinearWriter.StartVisibility;
  239. begin
  240. Writeln('');
  241. Writeln(SDocVisibility+':');
  242. end;
  243. Procedure TLinearWriter.StartDescription;
  244. begin
  245. Writeln('');
  246. Writeln(SDocDescription+':');
  247. end;
  248. Procedure TLinearWriter.StartAccess;
  249. begin
  250. Writeln('');
  251. Writeln(SDocAccess+':');
  252. end;
  253. Procedure TLinearWriter.StartErrors;
  254. begin
  255. Writeln('');
  256. Writeln(SDocErrors+':');
  257. end;
  258. Procedure TLinearWriter.StartVersion;
  259. begin
  260. Writeln('');
  261. Writeln(SDocVersion+':');
  262. end;
  263. Procedure TLinearWriter.StartSeealso;
  264. begin
  265. Writeln('');
  266. Writeln(SDocSeeAlso+':');
  267. end;
  268. Procedure TLinearWriter.StartProperty;
  269. begin
  270. Writeln('');
  271. Writeln(SDocProperty+':');
  272. end;
  273. Procedure TLinearWriter.EndProcedure;
  274. begin
  275. Writeln('');
  276. end;
  277. Procedure TLinearWriter.EndProperty;
  278. begin
  279. Writeln('');
  280. end;
  281. procedure TLinearWriter.EndSeealso;
  282. begin
  283. Writeln('');
  284. end;
  285. procedure TLinearWriter.WriteClassDecl(ClassDecl: TPasClassType);
  286. var
  287. DocNode: TDocNode;
  288. Vis: TPasMemberVisibilities;
  289. Member: TPasElement;
  290. i: Integer;
  291. begin
  292. StartSection(ClassDecl.Name);
  293. WriteLabel(ClassDecl);
  294. WriteIndex(ClassDecl);
  295. DocNode := Engine.FindDocNode(ClassDecl);
  296. if Assigned(DocNode) and ((not IsDescrNodeEmpty(DocNode.Descr)) or
  297. (not IsDescrNodeEmpty(DocNode.ShortDescr))) then
  298. begin
  299. StartSubSection(SDocDescription);
  300. WriteDescr(ClassDecl,DocNode);
  301. If Assigned(DocNode.Version) then
  302. begin
  303. StartSubSection(SDocVersion);
  304. WriteDescr(ClassDecl,DocNode.Version);
  305. end;
  306. end;
  307. // Write method overview
  308. WriteClassMethodOverView(ClassDecl);
  309. // Write Property Overview;
  310. WriteClassPropertyOverView(ClassDecl);
  311. // Write method & property descriptions
  312. // Determine visibilities
  313. Vis := AllVisibilities;
  314. if Engine.HidePrivate then
  315. Exclude(Vis,visPrivate);
  316. if Engine.HideProtected then
  317. Exclude(Vis,visProtected);
  318. for i := 0 to ClassDecl.Members.Count - 1 do
  319. begin
  320. Member := TPasElement(ClassDecl.Members[i]);
  321. if ((Member.InheritsFrom(TPasProcedureBase)) and
  322. (Member.Visibility in Vis)) then
  323. WriteProcedure(TPasProcedureBase(Member));
  324. end;
  325. // properties.
  326. for i := 0 to ClassDecl.Members.Count - 1 do
  327. begin
  328. Member := TPasElement(ClassDecl.Members[i]);
  329. if ((Member.InheritsFrom(TPasProperty)) and
  330. (Member.Visibility in Vis)) then
  331. WriteProperty(TPasProperty(Member));
  332. end;
  333. end;
  334. procedure TLinearWriter.WriteClassPropertyOverview(ClassDecl : TPasClassType);
  335. var
  336. Member: TPasElement;
  337. i: Integer;
  338. L,N,S,A: String;
  339. DocNode: TDocNode;
  340. List : TStringList;
  341. begin
  342. // Write property overview
  343. List:=TStringList.Create;
  344. Try
  345. List.Sorted:=True;
  346. for i := 0 to ClassDecl.Members.Count - 1 do
  347. begin
  348. Member := TPasElement(ClassDecl.Members[i]);
  349. With Member do
  350. if InheritsFrom(TPasProperty) and SHowMember(Member) then
  351. List.AddObject(Member.Name,Member)
  352. end;
  353. If (List.Count>0) then
  354. begin
  355. StartSubSection(SDocPropertyOverview);
  356. WriteLabel(GetLabel(ClassDecl) + ':Properties');
  357. StartOverView(True);
  358. For I:=0 to List.Count-1 do
  359. begin
  360. Member:=TPasElement(List.objects[i]);
  361. L:=StripText(GetLabel(Member));
  362. N:=EscapeText(Member.Name);
  363. DocNode := Engine.FindDocNode(Member);
  364. If Assigned(DocNode) then
  365. S:=GetDescrString(Member, DocNode.ShortDescr)
  366. else
  367. S:='';
  368. A:='';
  369. if Length(TPasProperty(Member).ReadAccessorName) > 0 then
  370. a := a + 'r';
  371. if Length(TPasProperty(Member).WriteAccessorName) > 0 then
  372. a := a + 'w';
  373. if Length(TPasProperty(Member).StoredAccessorName) > 0 then
  374. a := a + 's';
  375. WriteOverviewMember(L,N,A,S);
  376. end;
  377. EndOverview;
  378. end;
  379. Finally
  380. List.Free;
  381. end;
  382. end;
  383. function TLinearWriter.ConstValue(ConstDecl: TPasConst): String;
  384. begin
  385. if Assigned(ConstDecl) then
  386. Result := ConstDecl.ClassName
  387. else
  388. Result := '<nil>';
  389. end;
  390. procedure TLinearWriter.WriteDoc;
  391. var
  392. i : Integer;
  393. L : TstringList;
  394. begin
  395. PackageName := LowerCase(Copy(Package.Name, 2, 255));
  396. If (Engine.OutPut='') then
  397. Engine.Output:=PackageName+FileNameExtension;
  398. FStream:=TFileStream.Create(Engine.Output,fmCreate);
  399. try
  400. WriteBeginDocument;
  401. ProcessPackage;
  402. L:=TStringList.Create;
  403. Try
  404. L.Sorted:=True;
  405. // Sort modules.
  406. For I:=0 to Package.Modules.Count-1 do
  407. L.AddObject(TPasModule(Package.Modules[i]).Name,TPasModule(Package.Modules[i]));
  408. // Now create table.
  409. for i:=0 to L.Count - 1 do
  410. begin
  411. Module := TPasModule(L.Objects[i]);
  412. ModuleName := LowerCase(Module.Name);
  413. WriteCommentLine;
  414. StartChapter(Format(SDocUnitTitle, [Module.Name]));
  415. WriteLabel(Module);
  416. // extra Topics now get processed in ProcessSection()
  417. ProcessSection(Module.InterfaceSection);
  418. end;
  419. Finally
  420. L.Free;
  421. end;
  422. WriteEndDocument;
  423. finally
  424. FSTream.Free;
  425. end;
  426. end;
  427. procedure TLinearWriter.ProcessSection(ASection: TPasSection);
  428. var
  429. DocNode: TDocNode;
  430. begin
  431. With ASection do
  432. begin
  433. SortElementList(UsesList);
  434. SortElementList(Declarations);
  435. SortElementList(ResStrings);
  436. SortElementList(Types);
  437. SortElementList(Consts);
  438. SortElementList(Classes);
  439. SortElementList(Functions);
  440. SortElementList(Variables);
  441. end;
  442. WriteUnitOverView(ASection);
  443. // Now process unit (extra) Topics
  444. DocNode:=Engine.FindDocNode(Module);
  445. If Assigned(DocNode) then
  446. ProcessTopics(DocNode,1);
  447. WriteVarsConstsTypes(ASection);
  448. WriteFunctionsAndProcedures(ASection);
  449. WriteClasses(ASection);
  450. end;
  451. procedure TLinearWriter.WriteVarsConstsTypes(ASection: TPasSection);
  452. begin
  453. With Asection do
  454. if (Consts.Count>0) or (Types.Count>0) or
  455. (Variables.Count>0) or (ResStrings.Count>0) then
  456. begin
  457. StartSection(SDocConstsTypesVars, ModuleName+'ConstsTypesVars');
  458. WriteResourceStrings(ASection);
  459. WriteConsts(ASection);
  460. WriteTypes(ASection);
  461. WriteVars(ASection);
  462. end;
  463. end;
  464. procedure TLinearWriter.WriteResourceStrings(ASection: TPasSection);
  465. var
  466. ResStrDecl: TPasResString;
  467. i: Integer;
  468. DocNode : TDocNode;
  469. begin
  470. if ASection.ResStrings.Count > 0 then
  471. begin
  472. StartSubSection(SDocResStrings,ModuleName+'ResStrings');
  473. for i := 0 to ASection.ResStrings.Count - 1 do
  474. begin
  475. ResStrDecl := TPasResString(ASection.ResStrings[i]);
  476. StartListing(false, '');
  477. Writeln(ResStrDecl.GetDeclaration(True));
  478. EndListing;
  479. WriteLabel(ResStrDecl);
  480. WriteIndex(ResStrDecl);
  481. DocNode:=WriteDescr(ResStrDecl);
  482. If Assigned(DocNode) and Assigned(DocNode.Version) then
  483. begin
  484. Writeln(Format('%s : ',[SDocVersion]));
  485. WriteDescr(ResStrDecl, DocNode.Version);
  486. end;
  487. end;
  488. end;
  489. end;
  490. procedure TLinearWriter.WriteUnitOverview(ASection: TPasSection);
  491. var
  492. i: Integer;
  493. UnitRef: TPasType;
  494. DocNode: TDocNode;
  495. begin
  496. if ASection.UsesList.Count > 0 then
  497. begin
  498. StartSection(SDocUsedUnits);
  499. StartUnitOverview(Module.Name,ModuleName);
  500. for i := 0 to ASection.UsesList.Count - 1 do
  501. begin
  502. UnitRef := TPasType(ASection.UsesList[i]);
  503. WriteUnitEntry(UnitRef);
  504. end;
  505. EndUnitOverview;
  506. end;
  507. DocNode := Engine.FindDocNode(ASection.Parent);
  508. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  509. begin
  510. StartSection(SDocOverview);
  511. WriteDescr(ASection.Parent, DocNode.Descr);
  512. end;
  513. end;
  514. Procedure TLinearWriter.ProcessPackage;
  515. var
  516. DocNode: TDocNode;
  517. begin
  518. DocNode:=Engine.FindDocNode(Package);
  519. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  520. begin
  521. StartSection(SDocOverview);
  522. WriteDescr(Package, DocNode.Descr);
  523. end;
  524. WriteSeeAlso(DocNode);
  525. ProcessTopics(DocNode,1);
  526. end;
  527. Procedure TLinearWriter.ProcessTopics(DocNode : TDocNode; Alevel : Integer);
  528. Var
  529. Node : TDocNode;
  530. begin
  531. If Not Assigned(DocNode) then
  532. Exit;
  533. Node:=DocNode.FirstChild;
  534. While Assigned(Node) do
  535. begin
  536. If Node.TopicNode then
  537. WriteTopicNode(Node,ALevel);
  538. Node:=Node.NextSibling;
  539. end;
  540. end;
  541. Procedure TLinearWriter.WriteTopicNode(Node : TDocNode; Level : Integer);
  542. Var
  543. Element : TTopicElement;
  544. SubNode : TDocNode;
  545. S : String;
  546. begin
  547. Element:=FindTopicElement(Node);
  548. If Not Assigned(Element) then
  549. Exit;
  550. S:=GetDescrString(Element,Node.ShortDescr);
  551. Case Level of
  552. 1 : StartSection(S);
  553. 2 : StartSubSection(S);
  554. 3 : StartSubSubSection(S);
  555. end;
  556. WriteLabel(Element);
  557. If Assigned(Node.Descr) then
  558. WriteDescr(Element,Node.Descr);
  559. WriteSeeAlso(Node);
  560. If Level<3 then
  561. begin
  562. SubNode:=Node.FirstChild;
  563. While Assigned(SubNode) do
  564. begin
  565. If SubNode.TopicNode then
  566. WriteTopicNode(SubNode,Level+1);
  567. SubNode:=SubNode.NextSibling;
  568. end;
  569. end;
  570. end;
  571. procedure TLinearWriter.WriteConsts(ASection: TPasSection);
  572. var
  573. i: Integer;
  574. ConstDecl: TPasConst;
  575. begin
  576. if ASection.Consts.Count > 0 then
  577. begin
  578. StartSubSection(SDocConstants,EscapeText(ModuleName));
  579. for i := 0 to ASection.Consts.Count - 1 do
  580. begin
  581. DescrBeginParaGraph;
  582. ConstDecl := TPasConst(ASection.Consts[i]);
  583. StartListing(False,'');
  584. WriteLn(EscapeText(ConstDecl.GetDeclaration(True)));
  585. EndListing;
  586. WriteLabel(ConstDecl);
  587. WriteIndex(ConstDecl);
  588. WriteDescr(ConstDecl);
  589. DescrEndParaGraph;
  590. end;
  591. end;
  592. end;
  593. procedure TLinearWriter.WriteEnumElements(TypeDecl : TPasEnumType);
  594. Var
  595. EV : TPasEnumValue;
  596. I : Integer;
  597. DocNode : TDocNode;
  598. begin
  599. With TypeDecl do
  600. begin
  601. SortElementList(Values);
  602. DescrBeginTable(2,True);
  603. DescrBeginTableCaption;
  604. Writeln(EscapeText(Format(SDocValuesForEnum,[TypeDecl.Name])));
  605. DescrEndTableCaption;
  606. DescrBeginTableHeadRow;
  607. DescrBeginTableCell;
  608. Writeln(EscapeText(SDocValue));
  609. DescrEndTableCell;
  610. DescrBeginTableCell;
  611. Writeln(EscapeText(SDocExplanation));
  612. DescrEndTableCell;
  613. DescrEndTableHeadRow;
  614. For I:=0 to Values.Count-1 do
  615. begin
  616. EV:=TPasEnumValue(Values[i]);
  617. DescrBeginTableRow;
  618. DescrBeginTableCell;
  619. Writeln(EscapeText(EV.Name));
  620. DescrEndTableCell;
  621. DescrBeginTableCell;
  622. DocNode := Engine.FindDocNode(EV);
  623. if Assigned(DocNode) and (not IsDescrNodeEmpty(DocNode.ShortDescr)) then
  624. WriteDescr(EV,DocNode.ShortDescr);
  625. DescrEndTableCell;
  626. DescrEndTableRow;
  627. end;
  628. DescrEndTable;
  629. end;
  630. end;
  631. procedure TLinearWriter.WriteTypes(ASection: TPasSection);
  632. var
  633. i: Integer;
  634. TypeDecl: TPasType;
  635. DocNode : TDocNode;
  636. begin
  637. if ASection.Types.Count > 0 then
  638. begin
  639. StartSubSection(SDocTypes,ModuleName+'Types');
  640. for i := 0 to ASection.Types.Count - 1 do
  641. begin
  642. DescrBeginParaGraph;
  643. TypeDecl := TPasType(ASection.Types[i]);
  644. StartListing(False,'');
  645. DocNode := Engine.FindDocNode(TypeDecl);
  646. If Assigned(DocNode) and
  647. Assigned(DocNode.Node) and
  648. (Docnode.Node['opaque']='1') then
  649. Writeln(TypeDecl.Name+' = '+SDocOpaque)
  650. else
  651. Writeln(EscapeText(TypeDecl.GetDeclaration(True)));
  652. EndListing;
  653. WriteLabel(TypeDecl);
  654. WriteIndex(TypeDecl);
  655. If TypeDecl is TPasEnumType then
  656. begin
  657. WriteENumElements(TypeDecl as TPasEnumType);
  658. end;
  659. WriteDescr(TypeDecl);
  660. If Assigned(DocNode) and Assigned(DocNode.Version) then
  661. begin
  662. Writeln(Format('%s : ',[SDocVersion]));
  663. WriteDescr(TypeDecl, DocNode.Version);
  664. end;
  665. DescrEndParaGraph;
  666. end;
  667. end;
  668. end;
  669. procedure TLinearWriter.WriteVars(ASection: TPasSection);
  670. var
  671. VarDecl: TPasVariable;
  672. i: Integer;
  673. DocNode : TDocNode;
  674. begin
  675. if ASection.Variables.Count > 0 then
  676. begin
  677. StartSubsection(SDocVariables,ModuleName+'Variables');
  678. for i := 0 to ASection.Variables.Count - 1 do
  679. begin
  680. DescrBeginParaGraph;
  681. VarDecl := TPasVariable(ASection.Variables[i]);
  682. StartListing(False,'');
  683. WriteLn(EscapeText(VarDecl.GetDeclaration(True)));
  684. EndListing;
  685. WriteLabel(VarDecl);
  686. WriteIndex(VarDecl);
  687. DocNode:=WriteDescr(VarDecl);
  688. If Assigned(DocNode) and Assigned(DocNode.Version) then
  689. begin
  690. Writeln(Format('%s : ',[SDocVersion]));
  691. WriteDescr(VarDecl, DocNode.Version);
  692. end;
  693. DescrEndParaGraph;
  694. end;
  695. end;
  696. end;
  697. procedure TLinearWriter.WriteProcedure(ProcDecl : TPasProcedureBase);
  698. var
  699. DocNode: TDocNode;
  700. OP : TPasOverloadedProc;
  701. i : integer;
  702. begin
  703. With ProcDecl do
  704. begin
  705. if Not (Assigned(Parent) and Parent.InheritsFrom(TPasClassType)) then
  706. begin
  707. StartSubSection(Name);
  708. WriteLabel(ProcDecl);
  709. WriteIndex(ProcDecl);
  710. end
  711. else
  712. begin // Parent assigned and hence method.
  713. StartSubSection(Parent.Name+'.'+Name);
  714. WriteLabel(ProcDecl);
  715. WriteIndex(Parent.Name+'.'+Name);
  716. end;
  717. StartProcedure;
  718. DocNode := Engine.FindDocNode(ProcDecl);
  719. if Assigned(DocNode) and Assigned(DocNode.ShortDescr) then
  720. begin
  721. StartSynopsis;
  722. WriteDescr(ProcDecl, DocNode.ShortDescr);
  723. end;
  724. StartDeclaration;
  725. StartListing(False);
  726. if ClassType = TPasOverloadedProc then
  727. begin
  728. OP:=TPasOverloadedProc(ProcDecl);
  729. for i := 0 to OP.Overloads.Count - 1 do
  730. begin
  731. WriteLn(TPasProcedure(OP.Overloads[i]).GetDeclaration(True));
  732. end;
  733. end
  734. else
  735. WriteLn(GetDeclaration(True));
  736. EndListing;
  737. If Assigned(Parent) then
  738. begin
  739. StartVisibility;
  740. Writeln(VisibilityNames[Visibility])
  741. end;
  742. if Assigned(DocNode) then
  743. begin
  744. If Assigned(DocNode.Descr) then
  745. begin
  746. StartDescription;
  747. WriteDescr(ProcDecl);
  748. end;
  749. if Assigned(DocNode.ErrorsDoc) then
  750. begin
  751. StartErrors;
  752. WriteDescr(ProcDecl, DocNode.ErrorsDoc);
  753. end;
  754. if Assigned(DocNode.Version) then
  755. begin
  756. StartVersion;
  757. WriteDescr(ProcDecl, DocNode.Version);
  758. end;
  759. WriteSeeAlso(DocNode);
  760. EndProcedure;
  761. WriteExample(DocNode);
  762. end
  763. else
  764. EndProcedure;
  765. end;
  766. end;
  767. procedure TLinearWriter.WriteFunctionsAndProcedures(ASection: TPasSection);
  768. var
  769. i: Integer;
  770. begin
  771. if ASection.Functions.Count > 0 then
  772. begin
  773. StartSection(SDocProceduresAndFunctions,ModuleName+'Functions');
  774. for i := 0 to ASection.Functions.Count - 1 do
  775. WriteProcedure(TPasProcedureBase(ASection.Functions[i]));
  776. end;
  777. end;
  778. procedure TLinearWriter.WriteExample(ADocNode: TDocNode);
  779. var
  780. Example: TDOMElement;
  781. S : string;
  782. begin
  783. S:='';
  784. if Assigned(ADocNode) then
  785. begin
  786. Example := ADocNode.FirstExample;
  787. while Assigned(Example) do
  788. begin
  789. if IsExampleNode(Example) then
  790. begin
  791. if (S<>'') then // not first example, start new paragraph
  792. DescrBeginParagraph;
  793. s:=Engine.GetExampleFileName(Example);
  794. if (S<>'') then
  795. WriteExampleFile(S);
  796. if Assigned(Example.NextSibling) then
  797. DescrEndParaGraph;
  798. end;
  799. Example := TDomElement(Example.NextSibling);
  800. end;
  801. end;
  802. end;
  803. procedure TLinearWriter.WriteProperty(PropDecl : TPasProperty);
  804. var
  805. DocNode: TDocNode;
  806. S: String;
  807. begin
  808. With PropDecl do
  809. begin
  810. StartSubSection(Parent.Name+'.'+Name);
  811. WriteLabel(PropDecl);
  812. WriteIndex(Parent.Name+'.'+Name);
  813. StartProperty;
  814. DocNode := Engine.FindDocNode(PropDecl);
  815. if Assigned(DocNode) and Assigned(DocNode.ShortDescr) then
  816. begin
  817. StartSynopsis;
  818. WriteDescr(PropDecl, DocNode.ShortDescr);
  819. end;
  820. StartDeclaration;
  821. StartListing(False);
  822. WriteLn('Property '+GetDeclaration(True));
  823. EndListing;
  824. If Assigned(Parent) then
  825. begin
  826. StartVisibility;
  827. Writeln(VisibilityNames[Visibility])
  828. end;
  829. StartAccess;
  830. Setlength(S,0);
  831. If Length(ReadAccessorName) > 0 then
  832. S:='Read';
  833. if Length(WriteAccessorName) > 0 then
  834. begin
  835. If S<>'' then
  836. S:=S+',';
  837. S:=S+'Write';
  838. end;
  839. Writeln(S);
  840. if Assigned(DocNode) then
  841. begin
  842. if Assigned(DocNode.Descr) then
  843. begin
  844. StartDescription;
  845. WriteDescr(PropDecl);
  846. end;
  847. if Assigned(DocNode.ErrorsDoc) then
  848. begin
  849. StartErrors;
  850. WriteDescr(PropDecl, DocNode.ErrorsDoc);
  851. end;
  852. if Assigned(DocNode.Version) then
  853. begin
  854. StartVersion;
  855. WriteDescr(PropDecl, DocNode.Version);
  856. end;
  857. WriteSeeAlso(DocNode);
  858. EndProperty;
  859. WriteExample(DocNode);
  860. end
  861. else
  862. EndProperty;
  863. end;
  864. end;
  865. procedure TLinearWriter.WriteSeeAlso(ADocNode: TDocNode);
  866. var
  867. Node: TDOMNode;
  868. s: String;
  869. First : Boolean;
  870. begin
  871. if Not (Assigned(ADocNode) and Assigned(ADocNode.SeeAlso)) then
  872. Exit;
  873. Node := ADocNode.SeeAlso.FirstChild;
  874. First:=True;
  875. while Assigned(Node) do
  876. begin
  877. if IsLinkNode(Node) then
  878. begin
  879. If First then
  880. begin
  881. StartSeealso;
  882. First:=False;
  883. end
  884. else
  885. Writeln(',');
  886. S:=TDomElement(Node)['id'];
  887. DescrBeginLink(S);
  888. Write(EscapeText(S));
  889. DescrEndLink();
  890. end;
  891. Node:=Node.NextSibling;
  892. end;
  893. If Not First then
  894. EndSeeAlso
  895. end;
  896. Function CompareElements(P1,P2 : Pointer) : Integer;
  897. begin
  898. Result:=CompareText(TPasElement(P1).Name,TPasElement(P2).Name);
  899. end;
  900. procedure TLinearWriter.SortElementList(List : TList);
  901. begin
  902. List.Sort(@CompareElements);
  903. end;
  904. Function TLinearWriter.ShowMember(M : TPasElement) : boolean;
  905. begin
  906. Result:=not ((M.Visibility=visPrivate) and Engine.HidePrivate);
  907. If Result then
  908. Result:=Not ((M.Visibility=visProtected) and Engine.HideProtected)
  909. end;
  910. procedure TLinearWriter.WriteClasses(ASection: TPasSection);
  911. var
  912. i: Integer;
  913. begin
  914. if (ASection.Classes.Count > 0) then
  915. for i := 0 to ASection.Classes.Count - 1 do
  916. WriteClassDecl(TPasClassType(ASection.Classes[i]));
  917. end;
  918. procedure TLinearWriter.WriteClassMethodOverview(ClassDecl: TPasClassType);
  919. var
  920. Member : TPasElement;
  921. i : Integer;
  922. L,N,S : String;
  923. DocNode : TDocNode;
  924. List : TStringList;
  925. begin
  926. List:=TStringList.Create;
  927. Try
  928. List.Sorted:=True;
  929. for i := 0 to ClassDecl.Members.Count - 1 do
  930. begin
  931. Member := TPasElement(ClassDecl.Members[i]);
  932. With Member do
  933. if InheritsFrom(TPasProcedureBase) and ShowMember(Member) then
  934. List.AddObject(Member.Name,Member);
  935. end;
  936. If List.Count>0 then
  937. begin
  938. StartSubSection(SDocMethodOverview);
  939. WriteLabel(GetLabel(ClassDecl) + ':Methods');
  940. StartOverview(False);
  941. For I:=0 to List.Count-1 do
  942. begin
  943. Member:=TPasElement(List.Objects[i]);
  944. L:=StripText(GetLabel(Member));
  945. N:=EscapeText(Member.Name);
  946. DocNode := Engine.FindDocNode(Member);
  947. If Assigned(DocNode) then
  948. S:=GetDescrString(Member, DocNode.ShortDescr)
  949. else
  950. S:='';
  951. WriteOverviewMember(L,N,S);
  952. end;
  953. EndOverview;
  954. end;
  955. Finally
  956. List.Free;
  957. end;
  958. end;
  959. constructor TLinearWriter.Create(APackage: TPasPackage; AEngine: TFPDocEngine);
  960. procedure AddLabel(AElement: TPasElement);
  961. begin
  962. Engine.AddLink(AElement.PathName, GetLabel(AElement));
  963. end;
  964. procedure AddList(AElement: TPasElement; AList: TList);
  965. var
  966. i: Integer;
  967. begin
  968. for i := 0 to AList.Count - 1 do
  969. AddLabel(TPasElement(AList[i]));
  970. end;
  971. procedure AddTopicPages(AElement: TPasElement);
  972. var
  973. PreviousTopic,
  974. TopicElement : TTopicElement;
  975. DocNode,
  976. TopicNode : TDocNode;
  977. begin
  978. DocNode:=Engine.FindDocNode(AElement);
  979. If not Assigned(DocNode) then
  980. exit;
  981. TopicNode:=DocNode.FirstChild;
  982. PreviousTopic:=Nil;
  983. While Assigned(TopicNode) do
  984. begin
  985. If TopicNode.TopicNode then
  986. begin
  987. TopicElement:=TTopicElement.Create(TopicNode.Name,AElement);
  988. Topics.Add(TopicElement);
  989. TopicElement.TopicNode:=TopicNode;
  990. TopicElement.Previous:=PreviousTopic;
  991. If Assigned(PreviousTopic) then
  992. PreviousTopic.Next:=TopicElement;
  993. PreviousTopic:=TopicElement;
  994. if AElement is TTopicElement then
  995. TTopicElement(AElement).SubTopics.Add(TopicElement);
  996. Engine.AddLink(TopicElement.PathName, GetLabel(TopicElement));
  997. if AElement is TTopicElement then
  998. TTopicElement(AElement).SubTopics.Add(TopicElement)
  999. else // Only one level of recursion.
  1000. AddTopicPages(TopicElement);
  1001. end;
  1002. TopicNode:=TopicNode.NextSibling;
  1003. end;
  1004. end;
  1005. procedure ScanModule(AModule: TPasModule);
  1006. var
  1007. i, j, k: Integer;
  1008. ClassEl: TPasClassType;
  1009. FPEl, AncestorMemberEl: TPasElement;
  1010. DocNode: TDocNode;
  1011. DidAutolink: Boolean;
  1012. begin
  1013. AddLabel(AModule);
  1014. AddTopicPages(AModule);
  1015. with AModule do
  1016. begin
  1017. AddList(AModule, InterfaceSection.ResStrings);
  1018. AddList(AModule, InterfaceSection.Consts);
  1019. AddList(AModule, InterfaceSection.Types);
  1020. if InterfaceSection.Classes.Count > 0 then
  1021. begin
  1022. for i := 0 to InterfaceSection.Classes.Count - 1 do
  1023. begin
  1024. ClassEl := TPasClassType(InterfaceSection.Classes[i]);
  1025. AddLabel(ClassEl);
  1026. for j := 0 to ClassEl.Members.Count - 1 do
  1027. begin
  1028. FPEl := TPasElement(ClassEl.Members[j]);
  1029. if ((FPEl.Visibility = visPrivate) and Engine.HidePrivate) or
  1030. ((FPEl.Visibility = visProtected) and Engine.HideProtected) then
  1031. continue;
  1032. DocNode := Engine.FindDocNode(FPEl);
  1033. if not Assigned(DocNode) then
  1034. begin
  1035. DidAutolink := False;
  1036. if Assigned(ClassEl.AncestorType) and
  1037. (ClassEl.AncestorType.ClassType = TPasClassType) then
  1038. begin
  1039. for k := 0 to TPasClassType(ClassEl.AncestorType).Members.Count - 1 do
  1040. begin
  1041. AncestorMemberEl :=
  1042. TPasElement(TPasClassType(ClassEl.AncestorType).Members[k]);
  1043. if AncestorMemberEl.Name = FPEl.Name then
  1044. begin
  1045. DocNode := Engine.FindDocNode(AncestorMemberEl);
  1046. if Assigned(DocNode) then
  1047. begin
  1048. DidAutolink := True;
  1049. Engine.AddLink(FPEl.PathName,
  1050. Engine.FindAbsoluteLink(AncestorMemberEl.PathName));
  1051. break;
  1052. end;
  1053. end;
  1054. end;
  1055. end;
  1056. if not DidAutolink then
  1057. AddLabel(FPEl);
  1058. end else
  1059. AddLabel(FPEl);
  1060. end;
  1061. end;
  1062. end;
  1063. AddList(AModule, InterfaceSection.Functions);
  1064. AddList(AModule, InterfaceSection.Variables);
  1065. end;
  1066. end;
  1067. var
  1068. i: Integer;
  1069. begin
  1070. inherited ;
  1071. { Allocate labels for all elements for which we are going to create
  1072. documentation. This is needed for links to work correctly. }
  1073. // Allocate label for the package itself, if a name is given (i.e. <> '#')
  1074. if Length(Package.Name) > 1 then
  1075. begin
  1076. AddLabel(Package);
  1077. AddTopicPages(Package);
  1078. end;
  1079. for i := 0 to Package.Modules.Count - 1 do
  1080. ScanModule(TPasModule(Package.Modules[i]));
  1081. end;
  1082. procedure TLinearWriter.WriteBeginDocument;
  1083. begin
  1084. WriteComment('This file has been created automatically by FPDoc.');
  1085. WriteComment('Linear output (c) 2005 Michael Van Canneyt');
  1086. end;
  1087. procedure TLinearWriter.WriteEndDocument;
  1088. begin
  1089. end;
  1090. end.