dwlinear.pp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. end;
  764. end;
  765. procedure TLinearWriter.WriteFunctionsAndProcedures(ASection: TPasSection);
  766. var
  767. i: Integer;
  768. begin
  769. if ASection.Functions.Count > 0 then
  770. begin
  771. StartSection(SDocProceduresAndFunctions,ModuleName+'Functions');
  772. for i := 0 to ASection.Functions.Count - 1 do
  773. WriteProcedure(TPasProcedureBase(ASection.Functions[i]));
  774. end;
  775. end;
  776. procedure TLinearWriter.WriteExample(ADocNode: TDocNode);
  777. var
  778. Example: TDOMElement;
  779. S : string;
  780. begin
  781. S:='';
  782. if Assigned(ADocNode) then
  783. begin
  784. Example := ADocNode.FirstExample;
  785. while Assigned(Example) do
  786. begin
  787. if IsExampleNode(Example) then
  788. begin
  789. if (S<>'') then // not first example, start new paragraph
  790. DescrBeginParagraph;
  791. s:=Engine.GetExampleFileName(Example);
  792. if (S<>'') then
  793. WriteExampleFile(S);
  794. if Assigned(Example.NextSibling) then
  795. DescrEndParaGraph;
  796. end;
  797. Example := TDomElement(Example.NextSibling);
  798. end;
  799. end;
  800. end;
  801. procedure TLinearWriter.WriteProperty(PropDecl : TPasProperty);
  802. var
  803. DocNode: TDocNode;
  804. S: String;
  805. begin
  806. With PropDecl do
  807. begin
  808. StartSubSection(Parent.Name+'.'+Name);
  809. WriteLabel(PropDecl);
  810. WriteIndex(Parent.Name+'.'+Name);
  811. StartProperty;
  812. DocNode := Engine.FindDocNode(PropDecl);
  813. if Assigned(DocNode) and Assigned(DocNode.ShortDescr) then
  814. begin
  815. StartSynopsis;
  816. WriteDescr(PropDecl, DocNode.ShortDescr);
  817. end;
  818. StartDeclaration;
  819. StartListing(False);
  820. WriteLn('Property '+GetDeclaration(True));
  821. EndListing;
  822. If Assigned(Parent) then
  823. begin
  824. StartVisibility;
  825. Writeln(VisibilityNames[Visibility])
  826. end;
  827. StartAccess;
  828. Setlength(S,0);
  829. If Length(ReadAccessorName) > 0 then
  830. S:='Read';
  831. if Length(WriteAccessorName) > 0 then
  832. begin
  833. If S<>'' then
  834. S:=S+',';
  835. S:=S+'Write';
  836. end;
  837. Writeln(S);
  838. if Assigned(DocNode) then
  839. begin
  840. if Assigned(DocNode.Descr) then
  841. begin
  842. StartDescription;
  843. WriteDescr(PropDecl);
  844. end;
  845. if Assigned(DocNode.ErrorsDoc) then
  846. begin
  847. StartErrors;
  848. WriteDescr(PropDecl, DocNode.ErrorsDoc);
  849. end;
  850. if Assigned(DocNode.Version) then
  851. begin
  852. StartVersion;
  853. WriteDescr(PropDecl, DocNode.Version);
  854. end;
  855. WriteSeeAlso(DocNode);
  856. EndProperty;
  857. WriteExample(DocNode);
  858. end;
  859. end;
  860. end;
  861. procedure TLinearWriter.WriteSeeAlso(ADocNode: TDocNode);
  862. var
  863. Node: TDOMNode;
  864. s: String;
  865. First : Boolean;
  866. begin
  867. if Not (Assigned(ADocNode) and Assigned(ADocNode.SeeAlso)) then
  868. Exit;
  869. Node := ADocNode.SeeAlso.FirstChild;
  870. First:=True;
  871. while Assigned(Node) do
  872. begin
  873. if IsLinkNode(Node) then
  874. begin
  875. If First then
  876. begin
  877. StartSeealso;
  878. First:=False;
  879. end
  880. else
  881. Writeln(',');
  882. S:=TDomElement(Node)['id'];
  883. DescrBeginLink(S);
  884. Write(EscapeText(S));
  885. DescrEndLink();
  886. end;
  887. Node:=Node.NextSibling;
  888. end;
  889. If Not First then
  890. EndSeeAlso
  891. end;
  892. Function CompareElements(P1,P2 : Pointer) : Integer;
  893. begin
  894. Result:=CompareText(TPasElement(P1).Name,TPasElement(P2).Name);
  895. end;
  896. procedure TLinearWriter.SortElementList(List : TList);
  897. begin
  898. List.Sort(@CompareElements);
  899. end;
  900. Function TLinearWriter.ShowMember(M : TPasElement) : boolean;
  901. begin
  902. Result:=not ((M.Visibility=visPrivate) and Engine.HidePrivate);
  903. If Result then
  904. Result:=Not ((M.Visibility=visProtected) and Engine.HideProtected)
  905. end;
  906. procedure TLinearWriter.WriteClasses(ASection: TPasSection);
  907. var
  908. i: Integer;
  909. begin
  910. if (ASection.Classes.Count > 0) then
  911. for i := 0 to ASection.Classes.Count - 1 do
  912. WriteClassDecl(TPasClassType(ASection.Classes[i]));
  913. end;
  914. procedure TLinearWriter.WriteClassMethodOverview(ClassDecl: TPasClassType);
  915. var
  916. Member : TPasElement;
  917. i : Integer;
  918. L,N,S : String;
  919. DocNode : TDocNode;
  920. List : TStringList;
  921. begin
  922. List:=TStringList.Create;
  923. Try
  924. List.Sorted:=True;
  925. for i := 0 to ClassDecl.Members.Count - 1 do
  926. begin
  927. Member := TPasElement(ClassDecl.Members[i]);
  928. With Member do
  929. if InheritsFrom(TPasProcedureBase) and ShowMember(Member) then
  930. List.AddObject(Member.Name,Member);
  931. end;
  932. If List.Count>0 then
  933. begin
  934. StartSubSection(SDocMethodOverview);
  935. WriteLabel(GetLabel(ClassDecl) + ':Methods');
  936. StartOverview(False);
  937. For I:=0 to List.Count-1 do
  938. begin
  939. Member:=TPasElement(List.Objects[i]);
  940. L:=StripText(GetLabel(Member));
  941. N:=EscapeText(Member.Name);
  942. DocNode := Engine.FindDocNode(Member);
  943. If Assigned(DocNode) then
  944. S:=GetDescrString(Member, DocNode.ShortDescr)
  945. else
  946. S:='';
  947. WriteOverviewMember(L,N,S);
  948. end;
  949. EndOverview;
  950. end;
  951. Finally
  952. List.Free;
  953. end;
  954. end;
  955. constructor TLinearWriter.Create(APackage: TPasPackage; AEngine: TFPDocEngine);
  956. procedure AddLabel(AElement: TPasElement);
  957. begin
  958. Engine.AddLink(AElement.PathName, GetLabel(AElement));
  959. end;
  960. procedure AddList(AElement: TPasElement; AList: TList);
  961. var
  962. i: Integer;
  963. begin
  964. for i := 0 to AList.Count - 1 do
  965. AddLabel(TPasElement(AList[i]));
  966. end;
  967. procedure AddTopicPages(AElement: TPasElement);
  968. var
  969. PreviousTopic,
  970. TopicElement : TTopicElement;
  971. DocNode,
  972. TopicNode : TDocNode;
  973. begin
  974. DocNode:=Engine.FindDocNode(AElement);
  975. If not Assigned(DocNode) then
  976. exit;
  977. TopicNode:=DocNode.FirstChild;
  978. PreviousTopic:=Nil;
  979. While Assigned(TopicNode) do
  980. begin
  981. If TopicNode.TopicNode then
  982. begin
  983. TopicElement:=TTopicElement.Create(TopicNode.Name,AElement);
  984. Topics.Add(TopicElement);
  985. TopicElement.TopicNode:=TopicNode;
  986. TopicElement.Previous:=PreviousTopic;
  987. If Assigned(PreviousTopic) then
  988. PreviousTopic.Next:=TopicElement;
  989. PreviousTopic:=TopicElement;
  990. if AElement is TTopicElement then
  991. TTopicElement(AElement).SubTopics.Add(TopicElement);
  992. Engine.AddLink(TopicElement.PathName, GetLabel(TopicElement));
  993. if AElement is TTopicElement then
  994. TTopicElement(AElement).SubTopics.Add(TopicElement)
  995. else // Only one level of recursion.
  996. AddTopicPages(TopicElement);
  997. end;
  998. TopicNode:=TopicNode.NextSibling;
  999. end;
  1000. end;
  1001. procedure ScanModule(AModule: TPasModule);
  1002. var
  1003. i, j, k: Integer;
  1004. ClassEl: TPasClassType;
  1005. FPEl, AncestorMemberEl: TPasElement;
  1006. DocNode: TDocNode;
  1007. DidAutolink: Boolean;
  1008. begin
  1009. AddLabel(AModule);
  1010. AddTopicPages(AModule);
  1011. with AModule do
  1012. begin
  1013. AddList(AModule, InterfaceSection.ResStrings);
  1014. AddList(AModule, InterfaceSection.Consts);
  1015. AddList(AModule, InterfaceSection.Types);
  1016. if InterfaceSection.Classes.Count > 0 then
  1017. begin
  1018. for i := 0 to InterfaceSection.Classes.Count - 1 do
  1019. begin
  1020. ClassEl := TPasClassType(InterfaceSection.Classes[i]);
  1021. AddLabel(ClassEl);
  1022. for j := 0 to ClassEl.Members.Count - 1 do
  1023. begin
  1024. FPEl := TPasElement(ClassEl.Members[j]);
  1025. if ((FPEl.Visibility = visPrivate) and Engine.HidePrivate) or
  1026. ((FPEl.Visibility = visProtected) and Engine.HideProtected) then
  1027. continue;
  1028. DocNode := Engine.FindDocNode(FPEl);
  1029. if not Assigned(DocNode) then
  1030. begin
  1031. DidAutolink := False;
  1032. if Assigned(ClassEl.AncestorType) and
  1033. (ClassEl.AncestorType.ClassType = TPasClassType) then
  1034. begin
  1035. for k := 0 to TPasClassType(ClassEl.AncestorType).Members.Count - 1 do
  1036. begin
  1037. AncestorMemberEl :=
  1038. TPasElement(TPasClassType(ClassEl.AncestorType).Members[k]);
  1039. if AncestorMemberEl.Name = FPEl.Name then
  1040. begin
  1041. DocNode := Engine.FindDocNode(AncestorMemberEl);
  1042. if Assigned(DocNode) then
  1043. begin
  1044. DidAutolink := True;
  1045. Engine.AddLink(FPEl.PathName,
  1046. Engine.FindAbsoluteLink(AncestorMemberEl.PathName));
  1047. break;
  1048. end;
  1049. end;
  1050. end;
  1051. end;
  1052. if not DidAutolink then
  1053. AddLabel(FPEl);
  1054. end else
  1055. AddLabel(FPEl);
  1056. end;
  1057. end;
  1058. end;
  1059. AddList(AModule, InterfaceSection.Functions);
  1060. AddList(AModule, InterfaceSection.Variables);
  1061. end;
  1062. end;
  1063. var
  1064. i: Integer;
  1065. begin
  1066. inherited ;
  1067. { Allocate labels for all elements for which we are going to create
  1068. documentation. This is needed for links to work correctly. }
  1069. // Allocate label for the package itself, if a name is given (i.e. <> '#')
  1070. if Length(Package.Name) > 1 then
  1071. begin
  1072. AddLabel(Package);
  1073. AddTopicPages(Package);
  1074. end;
  1075. for i := 0 to Package.Modules.Count - 1 do
  1076. ScanModule(TPasModule(Package.Modules[i]));
  1077. end;
  1078. procedure TLinearWriter.WriteBeginDocument;
  1079. begin
  1080. WriteComment('This file has been created automatically by FPDoc.');
  1081. WriteComment('Linear output (c) 2005 Michael Van Canneyt');
  1082. end;
  1083. procedure TLinearWriter.WriteEndDocument;
  1084. begin
  1085. end;
  1086. end.