dwlinear.pp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. DocNode : TDocNode;
  599. begin
  600. if ASection.Types.Count > 0 then
  601. begin
  602. StartSubSection(SDocTypes,ModuleName+'Types');
  603. for i := 0 to ASection.Types.Count - 1 do
  604. begin
  605. DescrBeginParaGraph;
  606. TypeDecl := TPasType(ASection.Types[i]);
  607. StartListing(False,'');
  608. DocNode := Engine.FindDocNode(TypeDecl);
  609. If Assigned(DocNode) and
  610. Assigned(DocNode.Node) and
  611. (Docnode.Node['opaque']='1') then
  612. Writeln(TypeDecl.Name+' = '+SDocOpaque)
  613. else
  614. Writeln(EscapeText(TypeDecl.GetDeclaration(True)));
  615. EndListing;
  616. WriteLabel(TypeDecl);
  617. WriteIndex(TypeDecl);
  618. If TypeDecl is TPasEnumType then
  619. begin
  620. WriteENumElements(TypeDecl as TPasEnumType);
  621. end;
  622. WriteDescr(TypeDecl);
  623. DescrEndParaGraph;
  624. end;
  625. end;
  626. end;
  627. procedure TLinearWriter.WriteVars(ASection: TPasSection);
  628. var
  629. VarDecl: TPasVariable;
  630. i: Integer;
  631. begin
  632. if ASection.Variables.Count > 0 then
  633. begin
  634. StartSubsection(SDocVariables,ModuleName+'Variables');
  635. for i := 0 to ASection.Variables.Count - 1 do
  636. begin
  637. DescrBeginParaGraph;
  638. VarDecl := TPasVariable(ASection.Variables[i]);
  639. StartListing(False,'');
  640. WriteLn(EscapeText(VarDecl.GetDeclaration(True)));
  641. EndListing;
  642. WriteLabel(VarDecl);
  643. WriteIndex(VarDecl);
  644. WriteDescr(VarDecl);
  645. DescrEndParaGraph;
  646. end;
  647. end;
  648. end;
  649. procedure TLinearWriter.WriteProcedure(ProcDecl : TPasProcedureBase);
  650. var
  651. DocNode: TDocNode;
  652. OP : TPasOverloadedProc;
  653. i : integer;
  654. begin
  655. With ProcDecl do
  656. begin
  657. if Not (Assigned(Parent) and Parent.InheritsFrom(TPasClassType)) then
  658. begin
  659. StartSubSection(Name);
  660. WriteLabel(ProcDecl);
  661. WriteIndex(ProcDecl);
  662. end
  663. else
  664. begin // Parent assigned and hence method.
  665. StartSubSection(Parent.Name+'.'+Name);
  666. WriteLabel(ProcDecl);
  667. WriteIndex(Parent.Name+'.'+Name);
  668. end;
  669. StartProcedure;
  670. DocNode := Engine.FindDocNode(ProcDecl);
  671. if Assigned(DocNode) and Assigned(DocNode.ShortDescr) then
  672. begin
  673. StartSynopsis;
  674. WriteDescr(ProcDecl, DocNode.ShortDescr);
  675. end;
  676. StartDeclaration;
  677. StartListing(False);
  678. if ClassType = TPasOverloadedProc then
  679. begin
  680. OP:=TPasOverloadedProc(ProcDecl);
  681. for i := 0 to OP.Overloads.Count - 1 do
  682. begin
  683. WriteLn(TPasProcedure(OP.Overloads[i]).GetDeclaration(True));
  684. end;
  685. end
  686. else
  687. WriteLn(GetDeclaration(True));
  688. EndListing;
  689. If Assigned(Parent) then
  690. begin
  691. StartVisibility;
  692. Writeln(VisibilityNames[Visibility])
  693. end;
  694. if Assigned(DocNode) and Assigned(DocNode.Descr) then
  695. begin
  696. StartDescription;
  697. WriteDescr(ProcDecl);
  698. end;
  699. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  700. begin
  701. StartErrors;
  702. WriteDescr(ProcDecl, DocNode.ErrorsDoc);
  703. end;
  704. WriteSeeAlso(DocNode);
  705. EndProcedure;
  706. WriteExample(DocNode);
  707. end;
  708. end;
  709. procedure TLinearWriter.WriteFunctionsAndProcedures(ASection: TPasSection);
  710. var
  711. i: Integer;
  712. begin
  713. if ASection.Functions.Count > 0 then
  714. begin
  715. StartSection(SDocProceduresAndFunctions,ModuleName+'Functions');
  716. for i := 0 to ASection.Functions.Count - 1 do
  717. WriteProcedure(TPasProcedureBase(ASection.Functions[i]));
  718. end;
  719. end;
  720. procedure TLinearWriter.WriteExample(ADocNode: TDocNode);
  721. var
  722. Example: TDOMElement;
  723. S : string;
  724. begin
  725. S:='';
  726. if Assigned(ADocNode) then
  727. begin
  728. Example := ADocNode.FirstExample;
  729. while Assigned(Example) do
  730. begin
  731. if IsExampleNode(Example) then
  732. begin
  733. if (S<>'') then // not first example, start new paragraph
  734. DescrBeginParagraph;
  735. s:=Engine.GetExampleFileName(Example);
  736. WriteExampleFile(S);
  737. if Assigned(Example.NextSibling) then
  738. DescrEndParaGraph;
  739. end;
  740. Example := TDomElement(Example.NextSibling);
  741. end;
  742. end;
  743. end;
  744. procedure TLinearWriter.WriteProperty(PropDecl : TPasProperty);
  745. var
  746. DocNode: TDocNode;
  747. S: String;
  748. begin
  749. With PropDecl do
  750. begin
  751. StartSubSection(Parent.Name+'.'+Name);
  752. WriteLabel(PropDecl);
  753. WriteIndex(Parent.Name+'.'+Name);
  754. StartProperty;
  755. DocNode := Engine.FindDocNode(PropDecl);
  756. if Assigned(DocNode) and Assigned(DocNode.ShortDescr) then
  757. begin
  758. StartSynopsis;
  759. WriteDescr(PropDecl, DocNode.ShortDescr);
  760. end;
  761. StartDeclaration;
  762. StartListing(False);
  763. WriteLn('Property '+GetDeclaration(True));
  764. EndListing;
  765. If Assigned(Parent) then
  766. begin
  767. StartVisibility;
  768. Writeln(VisibilityNames[Visibility])
  769. end;
  770. StartAccess;
  771. Setlength(S,0);
  772. If Length(ReadAccessorName) > 0 then
  773. S:='Read';
  774. if Length(WriteAccessorName) > 0 then
  775. begin
  776. If S<>'' then
  777. S:=S+',';
  778. S:=S+'Write';
  779. end;
  780. Writeln(S);
  781. if Assigned(DocNode) and Assigned(DocNode.Descr) then
  782. begin
  783. StartDescription;
  784. WriteDescr(PropDecl);
  785. end;
  786. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  787. begin
  788. StartErrors;
  789. WriteDescr(PropDecl, DocNode.ErrorsDoc);
  790. end;
  791. WriteSeeAlso(DocNode);
  792. EndProperty;
  793. WriteExample(DocNode);
  794. end;
  795. end;
  796. procedure TLinearWriter.WriteSeeAlso(ADocNode: TDocNode);
  797. var
  798. Node: TDOMNode;
  799. s: String;
  800. First : Boolean;
  801. begin
  802. if Not (Assigned(ADocNode) and Assigned(ADocNode.SeeAlso)) then
  803. Exit;
  804. Node := ADocNode.SeeAlso.FirstChild;
  805. First:=True;
  806. while Assigned(Node) do
  807. begin
  808. if IsLinkNode(Node) then
  809. begin
  810. If First then
  811. begin
  812. StartSeealso;
  813. First:=False;
  814. end
  815. else
  816. Writeln(',');
  817. S:=TDomElement(Node)['id'];
  818. DescrBeginLink(S);
  819. Write(EscapeText(S));
  820. DescrEndLink();
  821. end;
  822. Node:=Node.NextSibling;
  823. end;
  824. If Not First then
  825. EndSeeAlso
  826. end;
  827. Function CompareElements(P1,P2 : Pointer) : Integer;
  828. begin
  829. Result:=CompareText(TPasElement(P1).Name,TPasElement(P2).Name);
  830. end;
  831. procedure TLinearWriter.SortElementList(List : TList);
  832. begin
  833. List.Sort(@CompareElements);
  834. end;
  835. Function TLinearWriter.ShowMember(M : TPasElement) : boolean;
  836. begin
  837. Result:=not ((M.Visibility=visPrivate) and Engine.HidePrivate);
  838. If Result then
  839. Result:=Not ((M.Visibility=visProtected) and Engine.HideProtected)
  840. end;
  841. procedure TLinearWriter.WriteClasses(ASection: TPasSection);
  842. var
  843. i: Integer;
  844. begin
  845. if (ASection.Classes.Count > 0) then
  846. for i := 0 to ASection.Classes.Count - 1 do
  847. WriteClassDecl(TPasClassType(ASection.Classes[i]));
  848. end;
  849. procedure TLinearWriter.WriteClassMethodOverview(ClassDecl: TPasClassType);
  850. var
  851. Member : TPasElement;
  852. i : Integer;
  853. L,N,S : String;
  854. DocNode : TDocNode;
  855. List : TStringList;
  856. begin
  857. List:=TStringList.Create;
  858. Try
  859. List.Sorted:=True;
  860. for i := 0 to ClassDecl.Members.Count - 1 do
  861. begin
  862. Member := TPasElement(ClassDecl.Members[i]);
  863. With Member do
  864. if InheritsFrom(TPasProcedureBase) and ShowMember(Member) then
  865. List.AddObject(Member.Name,Member);
  866. end;
  867. If List.Count>0 then
  868. begin
  869. StartSubSection(SDocMethodOverview);
  870. WriteLabel(GetLabel(ClassDecl) + ':Methods');
  871. StartOverview(False);
  872. For I:=0 to List.Count-1 do
  873. begin
  874. Member:=TPasElement(List.Objects[i]);
  875. L:=StripText(GetLabel(Member));
  876. N:=EscapeText(Member.Name);
  877. DocNode := Engine.FindDocNode(Member);
  878. If Assigned(DocNode) then
  879. S:=GetDescrString(Member, DocNode.ShortDescr)
  880. else
  881. S:='';
  882. WriteOverviewMember(L,N,S);
  883. end;
  884. EndOverview;
  885. end;
  886. Finally
  887. List.Free;
  888. end;
  889. end;
  890. constructor TLinearWriter.Create(APackage: TPasPackage; AEngine: TFPDocEngine);
  891. procedure AddLabel(AElement: TPasElement);
  892. begin
  893. Engine.AddLink(AElement.PathName, GetLabel(AElement));
  894. end;
  895. procedure AddList(AElement: TPasElement; AList: TList);
  896. var
  897. i: Integer;
  898. begin
  899. for i := 0 to AList.Count - 1 do
  900. AddLabel(TPasElement(AList[i]));
  901. end;
  902. procedure AddTopicPages(AElement: TPasElement);
  903. var
  904. PreviousTopic,
  905. TopicElement : TTopicElement;
  906. DocNode,
  907. TopicNode : TDocNode;
  908. begin
  909. DocNode:=Engine.FindDocNode(AElement);
  910. If not Assigned(DocNode) then
  911. exit;
  912. TopicNode:=DocNode.FirstChild;
  913. PreviousTopic:=Nil;
  914. While Assigned(TopicNode) do
  915. begin
  916. If TopicNode.TopicNode then
  917. begin
  918. TopicElement:=TTopicElement.Create(TopicNode.Name,AElement);
  919. Topics.Add(TopicElement);
  920. TopicElement.TopicNode:=TopicNode;
  921. TopicElement.Previous:=PreviousTopic;
  922. If Assigned(PreviousTopic) then
  923. PreviousTopic.Next:=TopicElement;
  924. PreviousTopic:=TopicElement;
  925. if AElement is TTopicElement then
  926. TTopicElement(AElement).SubTopics.Add(TopicElement);
  927. Engine.AddLink(TopicElement.PathName, GetLabel(TopicElement));
  928. if AElement is TTopicElement then
  929. TTopicElement(AElement).SubTopics.Add(TopicElement)
  930. else // Only one level of recursion.
  931. AddTopicPages(TopicElement);
  932. end;
  933. TopicNode:=TopicNode.NextSibling;
  934. end;
  935. end;
  936. procedure ScanModule(AModule: TPasModule);
  937. var
  938. i, j, k: Integer;
  939. ClassEl: TPasClassType;
  940. FPEl, AncestorMemberEl: TPasElement;
  941. DocNode: TDocNode;
  942. DidAutolink: Boolean;
  943. begin
  944. AddLabel(AModule);
  945. AddTopicPages(AModule);
  946. with AModule do
  947. begin
  948. AddList(AModule, InterfaceSection.ResStrings);
  949. AddList(AModule, InterfaceSection.Consts);
  950. AddList(AModule, InterfaceSection.Types);
  951. if InterfaceSection.Classes.Count > 0 then
  952. begin
  953. for i := 0 to InterfaceSection.Classes.Count - 1 do
  954. begin
  955. ClassEl := TPasClassType(InterfaceSection.Classes[i]);
  956. AddLabel(ClassEl);
  957. for j := 0 to ClassEl.Members.Count - 1 do
  958. begin
  959. FPEl := TPasElement(ClassEl.Members[j]);
  960. if ((FPEl.Visibility = visPrivate) and Engine.HidePrivate) or
  961. ((FPEl.Visibility = visProtected) and Engine.HideProtected) then
  962. continue;
  963. DocNode := Engine.FindDocNode(FPEl);
  964. if not Assigned(DocNode) then
  965. begin
  966. DidAutolink := False;
  967. if Assigned(ClassEl.AncestorType) and
  968. (ClassEl.AncestorType.ClassType = TPasClassType) then
  969. begin
  970. for k := 0 to TPasClassType(ClassEl.AncestorType).Members.Count - 1 do
  971. begin
  972. AncestorMemberEl :=
  973. TPasElement(TPasClassType(ClassEl.AncestorType).Members[k]);
  974. if AncestorMemberEl.Name = FPEl.Name then
  975. begin
  976. DocNode := Engine.FindDocNode(AncestorMemberEl);
  977. if Assigned(DocNode) then
  978. begin
  979. DidAutolink := True;
  980. Engine.AddLink(FPEl.PathName,
  981. Engine.FindAbsoluteLink(AncestorMemberEl.PathName));
  982. break;
  983. end;
  984. end;
  985. end;
  986. end;
  987. if not DidAutolink then
  988. AddLabel(FPEl);
  989. end else
  990. AddLabel(FPEl);
  991. end;
  992. end;
  993. end;
  994. AddList(AModule, InterfaceSection.Functions);
  995. AddList(AModule, InterfaceSection.Variables);
  996. end;
  997. end;
  998. var
  999. i: Integer;
  1000. begin
  1001. inherited ;
  1002. { Allocate labels for all elements for which we are going to create
  1003. documentation. This is needed for links to work correctly. }
  1004. // Allocate label for the package itself, if a name is given (i.e. <> '#')
  1005. if Length(Package.Name) > 1 then
  1006. begin
  1007. AddLabel(Package);
  1008. AddTopicPages(Package);
  1009. end;
  1010. for i := 0 to Package.Modules.Count - 1 do
  1011. ScanModule(TPasModule(Package.Modules[i]));
  1012. end;
  1013. end.