dwlinear.pp 29 KB

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