dw_man.pp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  1. {$mode objfpc}
  2. {$H+}
  3. unit dw_man;
  4. interface
  5. uses
  6. Classes, SysUtils, DGlobals, dWriter, pastree, dom;
  7. Const
  8. DefaultManSection = 3;
  9. MaxListLevel = 4;
  10. // Suffixes for overview man pages.
  11. SManConsts = 'consts';
  12. SManVars = 'variables';
  13. SManTypes = 'types';
  14. SManResStr = 'resstr';
  15. // Standard man sections.
  16. SManDocName = 'NAME';
  17. SManDocSynopsis = 'SYNOPSIS';
  18. SManDocDescription = 'DESCRIPTION';
  19. SManDocErrors = 'ERRORS';
  20. SManDocSeeAlso = 'SEE ALSO';
  21. // FPDoc man sections.
  22. SManDocPackageUnits = 'PACKAGE UNITS';
  23. SManDocUsedUnits = 'USED UNITS';
  24. SManDocResourceStrings = 'RESOURCE STRINGS';
  25. SManDocVariables = 'VARIABLES';
  26. SManDocTypes = 'TYPES';
  27. SManDocConstants = 'CONSTANTS';
  28. SManDocFunctions = 'PROCEDURES AND FUNCTIONS';
  29. SManDocClasses = 'CLASSES';
  30. SManDocExamples = 'EXAMPLES';
  31. SManDocVisibility = 'VISIBILITY';
  32. SManDocArguments = 'ARGUMENTS';
  33. SManDocResult = 'RETURN VALUE';
  34. SManDocAccess = 'ACCESSIBILITY';
  35. SManDocMethods = 'METHODS';
  36. SManDocProperties = 'PROPERTIES';
  37. // Used to start listing
  38. SManDocListing = 'Listing:';
  39. Type
  40. { TManWriter }
  41. TManWriter = Class(TFPDocWriter)
  42. SkipUnitPrefix,
  43. FSkipTrim : Boolean;
  44. OutputDir,
  45. ModuleName,
  46. ManSection,
  47. PackageDescr,
  48. PackageName: String;
  49. FAtLineStart,
  50. FCheckEOL : Boolean;
  51. FStream : TStream;
  52. Module: TPasModule;
  53. FListLevel : Integer;
  54. FLists : Array [0..MaxListLevel] of integer;
  55. Protected
  56. // Writing support.
  57. procedure Write(const s: String);
  58. procedure WriteF(const s: String; const Args: array of const);
  59. procedure WriteLn(const s: String);
  60. procedure WriteLnF(const s: String; const Args: array of const);
  61. Procedure NewLine;
  62. Function PushWriteContext(S : TStream) : TStream;
  63. Procedure PopWriteContext(S : TSTream);
  64. Function EscapeText(const s : String) : String;
  65. // Formatting
  66. procedure WriteTP;
  67. procedure WriteB(Const Msg : String);
  68. procedure WriteBI(Const Msg : String);
  69. procedure NewListLevel(Initial : Integer);
  70. procedure DecListLevel;
  71. procedure StartListing(Frames: Boolean);
  72. // Sectioning routines
  73. Procedure StartManPage(AElement : TPasElement; ADocNode : TDocNode);
  74. Procedure StartManPage(FN : String);
  75. Procedure EndManPage;
  76. procedure StartSection(Const SectionName : String);
  77. procedure StartSubSection(Const SubSectionName : String);
  78. procedure PageTitle(Const ATitle,ASection,ASource,Amanual : String);
  79. // Referencing
  80. Function ElementToManPage(APasElement : TPasElement) : String;
  81. procedure WriteManRef(Const ManPage : String; Comma : Boolean);
  82. procedure WriteManRef(APasElement : TPasElement; Comma : Boolean);
  83. procedure WriteModuleSeealso(Comma : Boolean);
  84. procedure SortElementList(List : TFPList);
  85. Function GetDescrString(AContext: TPasElement; DescrNode: TDOMElement) : String;
  86. function ConstValue(ConstDecl: TPasConst): String; virtual;
  87. procedure WriteCommentLine;
  88. procedure WriteComment(Comment : String);
  89. Procedure WriteExampleFile(FN : String); virtual;
  90. procedure WriteExample(ADocNode: TDocNode);
  91. procedure WriteSeeAlso(ADocNode: TDocNode; Comma : Boolean);
  92. Public
  93. Constructor Create(APackage: TPasPackage; AEngine: TFPDocEngine); override;
  94. procedure WriteDoc; override;
  95. // Documentation writing methods.
  96. // Package
  97. Procedure WritePackagePage;
  98. // Topics
  99. Procedure ProcessTopics(DocNode : TDocNode; Subs : Boolean);
  100. Procedure WriteTopicRefs(DocNode : TDocNode);
  101. Procedure WriteTopicPage(Parent,Node : TDocNode);
  102. // Module
  103. procedure ProcessModule(AModule: TPasModule);
  104. procedure WriteUnitPage(AModule: TPasModule);
  105. procedure WriteUnitUsesOverview(ASection: TPasSection);
  106. procedure WriteUnitFunctionsAndProceduresOverview(ASection: TPasSection);
  107. procedure WriteUnitClassesOverview(ASection: TPasSection);
  108. procedure WriteUnitResourceStrings(ASection: TPasSection);
  109. procedure WriteUnitConsts(ASection: TPasSection);
  110. procedure WriteUnitTypes(ASection: TPasSection);
  111. procedure WriteUnitVars(ASection: TPasSection);
  112. procedure WriteUnitClasses(ASection: TPasSection);
  113. procedure WriteUnitFunctionsAndProcedures(ASection: TPasSection);
  114. // Smaller elements
  115. procedure WriteEnumElements(TypeDecl : TPasEnumType);
  116. procedure WriteClassPage(ClassDecl: TPasClassType);
  117. procedure WriteClassMethodOverview(ClassDecl: TPasClassType);
  118. procedure WriteClassPropertyOverview(ClassDecl: TPasClassType);
  119. procedure WriteProcedurePage(ProcDecl: TPasProcedureBase);
  120. procedure AppendProcedureArgsSection(Element: TPasProcedureType);
  121. procedure AppendFunctionResultSection(Element: TPasFunctionType);
  122. procedure WritePropertyPage(PropDecl: TPasProperty);
  123. // Overriden from fpdocwriter;
  124. procedure DescrWriteText(const AText: DOMString); override;
  125. procedure DescrBeginBold; override;
  126. procedure DescrEndBold; override;
  127. procedure DescrBeginItalic; override;
  128. procedure DescrEndItalic; override;
  129. procedure DescrBeginEmph; override;
  130. procedure DescrEndEmph; override;
  131. procedure DescrWriteFileEl(const AText: DOMString); override;
  132. procedure DescrWriteKeywordEl(const AText: DOMString); override;
  133. procedure DescrWriteVarEl(const AText: DOMString); override;
  134. procedure DescrBeginLink(const AId: DOMString); override;
  135. procedure DescrEndLink; override;
  136. procedure DescrWriteLinebreak; override;
  137. procedure DescrBeginParagraph; override;
  138. procedure DescrBeginCode(HasBorder: Boolean; const AHighlighterName: String); override;
  139. procedure DescrWriteCodeLine(const ALine: String); override;
  140. procedure DescrEndCode; override;
  141. procedure DescrEndParagraph; override;
  142. procedure DescrBeginOrderedList; override;
  143. procedure DescrEndOrderedList; override;
  144. procedure DescrBeginUnorderedList; override;
  145. procedure DescrEndUnorderedList; override;
  146. procedure DescrBeginDefinitionList; override;
  147. procedure DescrEndDefinitionList; override;
  148. procedure DescrBeginListItem; override;
  149. procedure DescrEndListItem; override;
  150. procedure DescrBeginDefinitionTerm; override;
  151. procedure DescrEndDefinitionTerm; override;
  152. procedure DescrBeginDefinitionEntry; override;
  153. procedure DescrEndDefinitionEntry; override;
  154. procedure DescrBeginSectionTitle; override;
  155. procedure DescrBeginSectionBody; override;
  156. procedure DescrEndSection; override;
  157. procedure DescrBeginRemark; override;
  158. procedure DescrEndRemark; override;
  159. procedure DescrBeginTable(ColCount: Integer; HasBorder: Boolean); override;
  160. procedure DescrEndTable; override;
  161. procedure DescrBeginTableCaption; override;
  162. procedure DescrEndTableCaption; override;
  163. procedure DescrBeginTableHeadRow; override;
  164. procedure DescrEndTableHeadRow; override;
  165. procedure DescrBeginTableRow; override;
  166. procedure DescrEndTableRow; override;
  167. procedure DescrBeginTableCell; override;
  168. procedure DescrEndTableCell; override;
  169. Function InterPretOption(Const Cmd,Arg : String) : boolean; override;
  170. Class Function FileNameExtension : String; override;
  171. Class procedure Usage(List: TStrings); override;
  172. end;
  173. implementation
  174. { TManWriter }
  175. constructor TManWriter.Create(APackage: TPasPackage; AEngine: TFPDocEngine);
  176. procedure AddLabel(AElement: TPasElement);
  177. begin
  178. Engine.AddLink(AElement.PathName, ElementToManPage(AElement));
  179. end;
  180. procedure AddList(AElement: TPasElement; AList: TFPList);
  181. var
  182. i: Integer;
  183. begin
  184. for i := 0 to AList.Count - 1 do
  185. AddLabel(TPasElement(AList[i]));
  186. end;
  187. procedure AddTopicPages(AElement: TPasElement);
  188. var
  189. PreviousTopic,
  190. TopicElement : TTopicElement;
  191. DocNode,
  192. TopicNode : TDocNode;
  193. begin
  194. DocNode:=Engine.FindDocNode(AElement);
  195. If not Assigned(DocNode) then
  196. exit;
  197. TopicNode:=DocNode.FirstChild;
  198. PreviousTopic:=Nil;
  199. While Assigned(TopicNode) do
  200. begin
  201. If TopicNode.TopicNode then
  202. begin
  203. TopicElement:=TTopicElement.Create(TopicNode.Name,AElement);
  204. Topics.Add(TopicElement);
  205. TopicElement.TopicNode:=TopicNode;
  206. TopicElement.Previous:=PreviousTopic;
  207. If Assigned(PreviousTopic) then
  208. PreviousTopic.Next:=TopicElement;
  209. PreviousTopic:=TopicElement;
  210. if AElement is TTopicElement then
  211. TTopicElement(AElement).SubTopics.Add(TopicElement);
  212. Engine.AddLink(TopicElement.PathName, ElementToManPage(TopicElement));
  213. if AElement is TTopicElement then
  214. TTopicElement(AElement).SubTopics.Add(TopicElement)
  215. else // Only one level of recursion.
  216. AddTopicPages(TopicElement);
  217. end;
  218. TopicNode:=TopicNode.NextSibling;
  219. end;
  220. end;
  221. procedure ScanModule(AModule: TPasModule);
  222. var
  223. i, j, k: Integer;
  224. ClassEl: TPasClassType;
  225. FPEl, AncestorMemberEl: TPasElement;
  226. DocNode: TDocNode;
  227. DidAutolink: Boolean;
  228. begin
  229. AddLabel(AModule);
  230. AddTopicPages(AModule);
  231. with AModule do
  232. begin
  233. AddList(AModule, InterfaceSection.ResStrings);
  234. AddList(AModule, InterfaceSection.Consts);
  235. AddList(AModule, InterfaceSection.Types);
  236. if InterfaceSection.Classes.Count > 0 then
  237. begin
  238. for i := 0 to InterfaceSection.Classes.Count - 1 do
  239. begin
  240. ClassEl := TPasClassType(InterfaceSection.Classes[i]);
  241. AddLabel(ClassEl);
  242. for j := 0 to ClassEl.Members.Count - 1 do
  243. begin
  244. FPEl := TPasElement(ClassEl.Members[j]);
  245. if Not Engine.ShowElement(FPEl) then
  246. continue;
  247. DocNode := Engine.FindDocNode(FPEl);
  248. if not Assigned(DocNode) then
  249. begin
  250. DidAutolink := False;
  251. if Assigned(ClassEl.AncestorType) and
  252. (ClassEl.AncestorType.ClassType = TPasClassType) then
  253. begin
  254. for k := 0 to TPasClassType(ClassEl.AncestorType).Members.Count - 1 do
  255. begin
  256. AncestorMemberEl :=
  257. TPasElement(TPasClassType(ClassEl.AncestorType).Members[k]);
  258. if AncestorMemberEl.Name = FPEl.Name then
  259. begin
  260. DocNode := Engine.FindDocNode(AncestorMemberEl);
  261. if Assigned(DocNode) then
  262. begin
  263. DidAutolink := True;
  264. Engine.AddLink(FPEl.PathName,
  265. Engine.FindAbsoluteLink(AncestorMemberEl.PathName));
  266. break;
  267. end;
  268. end;
  269. end;
  270. end;
  271. if not DidAutolink then
  272. AddLabel(FPEl);
  273. end else
  274. AddLabel(FPEl);
  275. end;
  276. end;
  277. end;
  278. AddList(AModule, InterfaceSection.Functions);
  279. AddList(AModule, InterfaceSection.Variables);
  280. end;
  281. end;
  282. var
  283. i: Integer;
  284. begin
  285. inherited;
  286. if Length(Package.Name) > 1 then
  287. AddTopicPages(Package);
  288. for i := 0 to Package.Modules.Count - 1 do
  289. ScanModule(TPasModule(Package.Modules[i]));
  290. end;
  291. { ---------------------------------------------------------------------
  292. Writing support
  293. ---------------------------------------------------------------------}
  294. Function TManWriter.PushWriteContext(S : TStream) : TStream;
  295. begin
  296. Result:=FStream;
  297. FStream:=S;
  298. end;
  299. Procedure TManWriter.PopWriteContext(S : TSTream);
  300. begin
  301. FStream:=S;
  302. end;
  303. function TManWriter.EscapeText(const s: String): String;
  304. begin
  305. Result:=S;
  306. end;
  307. procedure TManWriter.Write(const s: String);
  308. Var
  309. W : String;
  310. L : Integer;
  311. begin
  312. W:=S;
  313. If FAtLineStart and not FSKipTrim then
  314. W:=TrimLeft(W);
  315. L:=Length(W);
  316. If (L>0) then
  317. FStream.Write(PChar(W)^,L);
  318. FAtLineStart:=false;
  319. If FCheckEOL then
  320. begin
  321. If (L>=LEOL) then
  322. FAtLineStart:=(Copy(W,L-LEOL+1,LEOL)=LineEnding);
  323. end;
  324. end;
  325. Procedure TManWriter.NewLine;
  326. begin
  327. if Not FAtLineStart then
  328. Writeln('');
  329. end;
  330. procedure TManWriter.WriteF(const s: String; const Args: array of const);
  331. begin
  332. Write(Format(S,Args));
  333. end;
  334. procedure TManWriter.WriteLn(const s: String);
  335. begin
  336. FCheckEOL:=False;
  337. Try
  338. Write(S);
  339. Write(LineEnding);
  340. FAtLineStart:=True;
  341. finally
  342. FCheckEOL:=True;
  343. end;
  344. end;
  345. procedure TManWriter.WriteLnF(const s: String; const Args: array of const);
  346. begin
  347. Write(Format(S,Args));
  348. Write(LineEnding);
  349. end;
  350. procedure TManWriter.DescrWriteText(const AText: DOMString);
  351. begin
  352. self.Write(EscapeText(AText));
  353. end;
  354. procedure TManWriter.DescrBeginBold;
  355. begin
  356. NewLine;
  357. Write('.B ');
  358. end;
  359. procedure TManWriter.DescrEndBold;
  360. begin
  361. NewLine;
  362. end;
  363. procedure TManWriter.DescrBeginItalic;
  364. begin
  365. NewLine;
  366. Write('.I ');
  367. end;
  368. procedure TManWriter.DescrEndItalic;
  369. begin
  370. NewLine;
  371. end;
  372. procedure TManWriter.DescrBeginEmph;
  373. begin
  374. NewLine;
  375. Write('.I ');
  376. end;
  377. procedure TManWriter.DescrEndEmph;
  378. begin
  379. NewLine;
  380. end;
  381. procedure TManWriter.DescrWriteFileEl(const AText: DOMString);
  382. Var
  383. S : AnsiString;
  384. begin
  385. NewLine;
  386. S:=AText;
  387. Writeln('.I '+S);
  388. end;
  389. procedure TManWriter.DescrWriteKeywordEl(const AText: DOMString);
  390. Var
  391. S : AnsiString;
  392. begin
  393. NewLine;
  394. S:=AText;
  395. Writeln('.B '+S);
  396. end;
  397. procedure TManWriter.DescrWriteVarEl(const AText: DOMString);
  398. Var
  399. S : AnsiString;
  400. begin
  401. NewLine;
  402. S:=AText;
  403. Writeln('.B '+S);
  404. end;
  405. procedure TManWriter.DescrBeginLink(const AId: DOMString);
  406. begin
  407. // Do nothing
  408. end;
  409. procedure TManWriter.DescrEndLink;
  410. begin
  411. // Do nothing
  412. end;
  413. procedure TManWriter.DescrWriteLinebreak;
  414. begin
  415. NewLine;
  416. Writeln('.br');
  417. end;
  418. procedure TManWriter.DescrBeginParagraph;
  419. begin
  420. NewLine;
  421. Writeln('.PP');
  422. end;
  423. procedure TManWriter.DescrBeginCode(HasBorder: Boolean;
  424. const AHighlighterName: String);
  425. begin
  426. NewLine;
  427. Writeln('');
  428. end;
  429. procedure TManWriter.DescrWriteCodeLine(const ALine: String);
  430. begin
  431. FSkipTrim:=True;
  432. Try
  433. Writeln(ALine);
  434. Finally
  435. FSkipTrim:=False;
  436. end;
  437. DescrWriteLinebreak;
  438. end;
  439. procedure TManWriter.DescrEndCode;
  440. begin
  441. NewLine;
  442. Writeln('');
  443. end;
  444. procedure TManWriter.DescrEndParagraph;
  445. begin
  446. NewLine;
  447. Writeln('');
  448. end;
  449. procedure TManWriter.NewListLevel(Initial : Integer);
  450. begin
  451. Inc(FListLevel);
  452. If (FListLevel<MaxListLevel) then
  453. FLists[FListLevel]:=0;
  454. NewLine;
  455. Writeln('.RS');
  456. end;
  457. procedure TManWriter.DecListLevel;
  458. begin
  459. NewLine;
  460. Writeln('.RE');
  461. If (FListLevel>0) then
  462. Dec(FListLevel)
  463. end;
  464. procedure TManWriter.DescrBeginOrderedList;
  465. begin
  466. NewListLevel(0);
  467. end;
  468. procedure TManWriter.DescrEndOrderedList;
  469. begin
  470. DecListLevel;
  471. end;
  472. procedure TManWriter.DescrBeginUnorderedList;
  473. begin
  474. NewListLevel(-1);
  475. end;
  476. procedure TManWriter.DescrEndUnorderedList;
  477. begin
  478. DecListlevel;
  479. end;
  480. procedure TManWriter.DescrBeginDefinitionList;
  481. begin
  482. NewListLevel(-2);
  483. end;
  484. procedure TManWriter.DescrEndDefinitionList;
  485. begin
  486. DecListLevel
  487. end;
  488. procedure TManWriter.DescrBeginListItem;
  489. begin
  490. WriteTP
  491. end;
  492. procedure TManWriter.DescrEndListItem;
  493. begin
  494. NewLine;
  495. end;
  496. procedure TManWriter.DescrBeginDefinitionTerm;
  497. begin
  498. WriteTP;
  499. Write('.B ');
  500. end;
  501. procedure TManWriter.DescrEndDefinitionTerm;
  502. begin
  503. NewLine;
  504. end;
  505. procedure TManWriter.DescrBeginDefinitionEntry;
  506. begin
  507. NewLine;
  508. end;
  509. procedure TManWriter.DescrEndDefinitionEntry;
  510. begin
  511. NewLine;
  512. end;
  513. procedure TManWriter.DescrBeginSectionTitle;
  514. begin
  515. Write('.SH ');
  516. end;
  517. procedure TManWriter.DescrBeginSectionBody;
  518. begin
  519. NewLine;
  520. end;
  521. procedure TManWriter.DescrEndSection;
  522. begin
  523. NewLine;
  524. end;
  525. procedure TManWriter.DescrBeginRemark;
  526. begin
  527. WriteTP;
  528. WriteB(SDocRemark);
  529. end;
  530. procedure TManWriter.DescrEndRemark;
  531. begin
  532. NewLine;
  533. end;
  534. procedure TManWriter.DescrBeginTable(ColCount: Integer; HasBorder: Boolean);
  535. begin
  536. NewLine;
  537. end;
  538. procedure TManWriter.DescrEndTable;
  539. begin
  540. NewLine;
  541. end;
  542. procedure TManWriter.DescrBeginTableCaption;
  543. begin
  544. NewLine;
  545. end;
  546. procedure TManWriter.DescrEndTableCaption;
  547. begin
  548. NewLine;
  549. end;
  550. procedure TManWriter.DescrBeginTableHeadRow;
  551. begin
  552. DescrBeginParagraph;
  553. end;
  554. procedure TManWriter.DescrEndTableHeadRow;
  555. begin
  556. DescrEndParagraph;
  557. end;
  558. procedure TManWriter.DescrBeginTableRow;
  559. begin
  560. DescrBeginParagraph;
  561. end;
  562. procedure TManWriter.DescrEndTableRow;
  563. begin
  564. DescrEndParagraph;
  565. end;
  566. procedure TManWriter.DescrBeginTableCell;
  567. begin
  568. // Do nothing
  569. end;
  570. procedure TManWriter.DescrEndTableCell;
  571. begin
  572. Writeln(#9);
  573. end;
  574. function TManWriter.InterPretOption(const Cmd, Arg: String): boolean;
  575. begin
  576. Result:=True;
  577. if (Cmd='--man-section') then
  578. ManSection:=Arg
  579. else if (Cmd='--man-description') then
  580. PackageDescr:=Arg
  581. else if (Cmd='--nounitprefix') then
  582. SkipUnitPrefix:=True
  583. else
  584. Result:=inherited InterPretOption(Cmd, Arg);
  585. end;
  586. Function TManWriter.GetDescrString(AContext: TPasElement; DescrNode: TDOMElement) : String;
  587. Var
  588. S : TStringStream;
  589. F : TStream;
  590. begin
  591. Result:='';
  592. if Assigned(DescrNode) then
  593. begin
  594. S:=TStringStream.Create('');
  595. Try
  596. F:=PushWriteContext(S);
  597. Try
  598. ConvertDescr(AContext, DescrNode, False);
  599. Result:=S.DataString;
  600. FInally
  601. PopWriteContext(F);
  602. end;
  603. finally
  604. S.FRee;
  605. end;
  606. end;
  607. end;
  608. { ---------------------------------------------------------------------
  609. Formatting routines
  610. ---------------------------------------------------------------------}
  611. procedure TManWriter.WriteTP;
  612. begin
  613. NewLine;
  614. Writeln('.TP');
  615. end;
  616. procedure TManWriter.WriteB(Const Msg : String);
  617. begin
  618. NewLine;
  619. Writeln('.B '+MSG);
  620. end;
  621. procedure TManWriter.WriteBI(Const Msg : String);
  622. begin
  623. NewLine;
  624. Writeln('.BI '+MSG);
  625. end;
  626. { ---------------------------------------------------------------------
  627. Sectioning routines
  628. ---------------------------------------------------------------------}
  629. procedure TManWriter.StartListing(Frames: Boolean);
  630. begin
  631. Writeln('');
  632. WriteB(SManDocListing);
  633. end;
  634. procedure TManWriter.StartManPage(AElement: TPasElement; ADocNode: TDocNode);
  635. begin
  636. StartManPage(ElementToManPage(AElement))
  637. end;
  638. procedure TManWriter.StartManPage(FN: String);
  639. begin
  640. FN:=LowerCase(FN+'.'+mansection);
  641. FStream:=TFileStream.Create(OutputDir+FN,fmCreate);
  642. end;
  643. procedure TManWriter.EndManPage;
  644. begin
  645. FreeAndNil(FStream);
  646. end;
  647. procedure TManWriter.StartSection(Const SectionName: String);
  648. begin
  649. NewLine;
  650. Writeln('');
  651. Writeln('.SH '+SectionName);
  652. end;
  653. procedure TManWriter.StartSubSection(Const SubSectionName: String);
  654. begin
  655. NewLine;
  656. Writeln('.SS '+SubSectionName);
  657. end;
  658. procedure TManWriter.PageTitle(Const ATitle,ASection,ASource,Amanual : String);
  659. Var
  660. D : String;
  661. begin
  662. D:=FormatDateTime('mmmm yyyy',Date);
  663. WritelnF('.TH "%s" "%s" "%s" "%s" "%s"',[ATitle,ASection,D,ASource,AManual]);
  664. end;
  665. procedure TManWriter.WriteManRef(Const ManPage : String; Comma : Boolean);
  666. begin
  667. If Comma then
  668. Writeln(Lowercase(Format('%s (%s),',[ManPage,ManSection])))
  669. else
  670. Writeln(Lowercase(Format('%s (%s)',[ManPage,ManSection])));
  671. end;
  672. Function TManWriter.ElementToManPage(APasElement : TPasElement) : String;
  673. Var
  674. E : TPasElement;
  675. begin
  676. E:=APasElement; // Make code more readable
  677. If (E is TPasPackage) or (E is TPasModule) then
  678. begin
  679. Result:=APasElement.Name;
  680. If (Length(Result)>0) and (Result[1]='#') then
  681. Delete(Result,1,1);
  682. end
  683. else if E is TTopicElement then
  684. begin
  685. // Todo : Check for package
  686. Result:=ModuleName+E.name;
  687. end
  688. else
  689. begin
  690. If Not SkipUnitprefix then
  691. Result:=ModuleName+'.';
  692. If (E.Parent<>Nil) and (not (E.Parent is TPasSection)) then
  693. begin
  694. If (E.Parent.Name<>'') then
  695. Result:=Result+E.Parent.Name;
  696. If (E is TPasProperty) or (E is TPasProcedure) or (E is TPasClassType) then
  697. Result:=Result+E.Name;
  698. end
  699. else
  700. begin
  701. If (E is TPasConst) then
  702. Result:=Result+SManConsts
  703. else If E is TPasVariable then
  704. Result:=Result+SManVars
  705. else If E is TPasClassType then
  706. Result:=Result+E.Name
  707. else If E is TPasType then
  708. Result:=Result+SManTypes
  709. else If E is TPasResString then
  710. Result:=Result+SManResStr
  711. else
  712. Result:=Result+E.Name;
  713. end;
  714. end;
  715. Result:=LowerCase(Result);
  716. end;
  717. procedure TManWriter.WriteManRef(APasElement : TPasElement; Comma : Boolean);
  718. begin
  719. WriteManRef(ElementToManPage(APasElement),Comma);
  720. end;
  721. procedure TManWriter.WriteModuleSeealso(Comma : Boolean);
  722. Var
  723. HC,HT,HV,HR : Boolean;
  724. begin
  725. HC:=Module.InterfaceSection.Consts.Count>0;
  726. HR:=Module.InterfaceSection.ResStrings.Count>0;
  727. HV:=Module.InterfaceSection.Variables.Count>0;
  728. HT:=Module.InterfaceSection.Types.Count>0;
  729. WriteManRef(ModuleName,HC or HR or HV or HT or comma);
  730. if HC then
  731. WriteManRef(ModuleName+'.'+SManConsts,HR or HV or HT or comma);
  732. if HR then
  733. WriteManRef(ModuleName+'.'+SManResStr, HV or HT or comma);
  734. if HV then
  735. WriteManRef(ModuleName+'.'+SManVars, HT or comma);
  736. if HT then
  737. WriteManRef(ModuleName+'.'+SManTypes,comma);
  738. end;
  739. procedure TManWriter.WriteSeeAlso(ADocNode: TDocNode; Comma: Boolean);
  740. var
  741. Node: TDOMNode;
  742. s: String;
  743. begin
  744. if Not (Assigned(ADocNode) and Assigned(ADocNode.SeeAlso)) then
  745. Exit;
  746. Node := ADocNode.SeeAlso.FirstChild;
  747. while Assigned(Node) do
  748. begin
  749. if IsLinkNode(Node) then
  750. begin
  751. S:=TDomElement(Node)['id'];
  752. WriteManRef(S,(Node.NextSibling<>Nil) or Comma);
  753. end;
  754. Node:=Node.NextSibling;
  755. end;
  756. end;
  757. function TManWriter.ConstValue(ConstDecl: TPasConst): String;
  758. begin
  759. if Assigned(ConstDecl) then
  760. Result := ConstDecl.ClassName
  761. else
  762. Result := '<nil>';
  763. end;
  764. procedure TManWriter.WriteExample(ADocNode: TDocNode);
  765. var
  766. Example: TDOMElement;
  767. S : string;
  768. begin
  769. S:='';
  770. if Assigned(ADocNode) then
  771. begin
  772. Example := ADocNode.FirstExample;
  773. If Assigned(Example) then
  774. begin
  775. StartSection(SManDocExamples);
  776. while Assigned(Example) do
  777. begin
  778. s:=Engine.GetExampleFileName(Example);
  779. if (s<>'') then
  780. WriteExampleFile(S);
  781. DescrEndParaGraph;
  782. Repeat
  783. Example := TDomElement(Example.NextSibling);
  784. until (Example=Nil) or ((Example.NodeType=ELEMENT_NODE) and (Example.NodeName='example'));
  785. end;
  786. end;
  787. end;
  788. end;
  789. procedure TManWriter.WriteExampleFile(FN : String);
  790. Var
  791. L : TStringList;
  792. I : Integer;
  793. begin
  794. WriteBI(SDocExample+' \- '+ExtractFileName(FN));
  795. Writeln('');
  796. DescrWriteLineBreak;
  797. If (FN<>'') and FileExists(FN) then
  798. begin
  799. L:=TStringList.Create;
  800. Try
  801. L.LoadFromFile(FN);
  802. For I:=0 to L.Count-1 do
  803. DescrWriteCodeLine(L[i]);
  804. finally
  805. L.Free;
  806. end;
  807. end;
  808. end;
  809. { ---------------------------------------------------------------------
  810. Actual man page writing
  811. ---------------------------------------------------------------------}
  812. procedure TManWriter.WriteDoc;
  813. var
  814. i : Integer;
  815. L : TstringList;
  816. DocNode : TDocNode;
  817. begin
  818. PackageName := LowerCase(Copy(Package.Name, 2, 255));
  819. If (Engine.Output<>'') then
  820. OutputDir:=Engine.Output
  821. else
  822. OutputDir:=PackageName+'.man';
  823. If not ForceDirectories(OutputDir) then
  824. FPDocError(SErrCouldNotCreateOutputDir,[OutputDir]);
  825. OutputDir:=IncludeTrailingPathDelimiter(OutputDir);
  826. If (ManSection='') then
  827. ManSection:=IntToStr(DefaultManSection);
  828. WritePackagePage;
  829. L:=TStringList.Create;
  830. Try
  831. // Sort modules.
  832. For I:=0 to Package.Modules.Count-1 do
  833. L.AddObject(TPasModule(Package.Modules[i]).Name,TPasModule(Package.Modules[i]));
  834. L.Sorted:=True;
  835. for i:=0 to L.Count - 1 do
  836. ProcessModule(TPasModule(L.Objects[i]));
  837. Finally
  838. L.Free;
  839. end;
  840. end;
  841. { ---------------------------------------------------------------------
  842. Package man page
  843. ---------------------------------------------------------------------}
  844. Procedure TManWriter.WritePackagePage;
  845. var
  846. D,DocNode: TDocNode;
  847. M : TPasModule;
  848. I : Integer;
  849. L : TStringList;
  850. begin
  851. DocNode:=Engine.FindDocNode(Package);
  852. If (PackageDescr='') then
  853. PackageDescr:=GetDescrString(Package,DocNode.ShortDescr);
  854. StartManPage(Package,DocNode);
  855. Try
  856. PageTitle(PackageName,ManSection,PackageName,PackageDescr);
  857. StartSection(SManDocName);
  858. Writeln(PackageName+' \- '+PackageDescr);
  859. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  860. begin
  861. StartSection(SDocDescription);
  862. WriteDescr(Package, DocNode.Descr);
  863. end;
  864. StartSection(SManDocPackageUnits);
  865. L:=TStringList.Create;
  866. Try
  867. For I:=0 to Package.Modules.Count-1 do
  868. L.AddObject(TPasModule(Package.Modules[i]).Name,TPasModule(Package.Modules[i]));
  869. L.Sorted:=True;
  870. for i:=0 to L.Count - 1 do
  871. begin
  872. WriteTP;
  873. WriteB(L[i]);
  874. M:=TPasModule(L.Objects[i]);
  875. D:=Engine.FindDocNode(M);
  876. WriteLn(GetDescrString(M,D.ShortDescr))
  877. end;
  878. StartSection(SDocSeeAlso);
  879. WriteSeeAlso(DocNode,True);
  880. WriteTopicRefs(DocNode);
  881. for i:=0 to L.Count - 1 do
  882. WriteManRef(TPasModule(L.Objects[i]),I<L.Count-1);
  883. Finally
  884. L.Free;
  885. end;
  886. Finally
  887. EndManPage;
  888. end;
  889. ProcessTopics(DocNode,True);
  890. end;
  891. { ---------------------------------------------------------------------
  892. Topic support
  893. ---------------------------------------------------------------------}
  894. Procedure TManWriter.WriteTopicRefs(DocNode : TDocNode);
  895. Var
  896. Node : TDocNode;
  897. begin
  898. If Not Assigned(DocNode) then
  899. Exit;
  900. Node:=DocNode.FirstChild;
  901. While Assigned(Node) do
  902. begin
  903. If Node.TopicNode then
  904. WriteManRef(DocNode.Name,Node.NextSibling<>Nil);
  905. Node:=Node.NextSibling;
  906. end;
  907. end;
  908. Procedure TManWriter.ProcessTopics(DocNode : TDocNode; Subs : Boolean);
  909. Var
  910. Node,SubNode : TDocNode;
  911. begin
  912. If Not Assigned(DocNode) then
  913. Exit;
  914. Node:=DocNode.FirstChild;
  915. While Assigned(Node) do
  916. begin
  917. If Node.TopicNode then
  918. begin
  919. WriteTopicPage(DocNode,Node);
  920. If Subs then
  921. begin
  922. SubNode:=DocNode.FirstChild;
  923. While Assigned(SubNode) do
  924. If SubNode.TopicNode then
  925. WriteTopicPage(Node,SubNode);
  926. end;
  927. end;
  928. Node:=Node.NextSibling;
  929. end;
  930. end;
  931. Procedure TManWriter.WriteTopicPage(Parent,Node : TDocNode);
  932. Var
  933. Element : TTopicElement;
  934. begin
  935. Element:=FindTopicElement(Node);
  936. If Not Assigned(Element) then
  937. Exit;
  938. StartManPage(Element,Node) ;
  939. Try
  940. PageTitle(Node.Name,ManSection,PackageName,PackageDescr);
  941. StartSection(SManDocName);
  942. Writeln(Node.Name+' \- '+GetDescrString(Element,Node.ShortDescr));
  943. StartSection(SManDocDescription);
  944. If Assigned(Node.Descr) then
  945. WriteDescr(Element,Node.Descr);
  946. StartSection(SManDocSeeAlso);
  947. WriteSeeAlso(Node,True);
  948. WriteTopicRefs(Parent);
  949. WriteTopicRefs(Node);
  950. Finally
  951. EndManPage;
  952. end;
  953. end;
  954. { ---------------------------------------------------------------------
  955. Module man pages
  956. ---------------------------------------------------------------------}
  957. procedure TManWriter.ProcessModule(AModule : TPasModule);
  958. begin
  959. With AModule do
  960. begin
  961. Module:=AModule;
  962. ModuleName:=Name;
  963. With InterfaceSection do
  964. begin
  965. SortElementList(Declarations);
  966. SortElementList(Types);
  967. SortElementList(Consts);
  968. SortElementList(Classes);
  969. SortElementList(Functions);
  970. SortElementList(Variables);
  971. SortElementList(ResStrings);
  972. end;
  973. WriteUnitPage(AModule);
  974. WriteUnitResourceStrings(InterfaceSection);
  975. WriteUnitConsts(InterfaceSection);
  976. WriteUnitTypes(InterfaceSection);
  977. WriteUnitVars(InterfaceSection);
  978. WriteUnitClasses(InterfaceSection);
  979. WriteUnitFunctionsAndProcedures(InterfaceSection);
  980. end;
  981. end;
  982. procedure TManWriter.WriteUnitPage(AModule : TPasModule);
  983. Var
  984. DocNode : TDocNode;
  985. begin
  986. DocNode:=Engine.FindDocNode(AModule);
  987. StartManPage(AModule,DocNode);
  988. Try
  989. PageTitle(AModule.Name,ManSection,PackageName,PackageDescr);
  990. StartSection(SManDocName);
  991. Writeln(DocNode.Name+' \- '+GetDescrString(AModule,DocNode.ShortDescr));
  992. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  993. begin
  994. StartSection(SManDocDescription);
  995. WriteDescr(AModule.Parent, DocNode.Descr);
  996. end;
  997. WriteUnitUsesOverview(AModule.InterfaceSection);
  998. WriteUnitClassesOverView(AModule.InterfaceSection);
  999. WriteUnitFunctionsAndProceduresOverView(AModule.InterfaceSection);
  1000. StartSection(SManDocSeealso);
  1001. WriteModuleSeeAlso(False);
  1002. finally
  1003. EndManPage;
  1004. end;
  1005. end;
  1006. procedure TManWriter.WriteUnitUsesOverview(ASection: TPasSection);
  1007. var
  1008. i: Integer;
  1009. UnitRef: TPasType;
  1010. DocNode: TDocNode;
  1011. begin
  1012. if ASection.UsesList.Count > 0 then
  1013. begin
  1014. StartSection(SManDocUsedUnits);
  1015. for i := 0 to ASection.UsesList.Count - 1 do
  1016. begin
  1017. UnitRef := TPasType(ASection.UsesList[i]);
  1018. WriteTP;
  1019. WriteB(UnitRef.Name);
  1020. DocNode := Engine.FindDocNode(UnitRef);
  1021. If Assigned(DocNode) then
  1022. WriteDescr(UnitRef,DocNode.ShortDescr)
  1023. end;
  1024. end;
  1025. end;
  1026. { ---------------------------------------------------------------------
  1027. Classes man pages
  1028. ---------------------------------------------------------------------}
  1029. procedure TManWriter.WriteUnitClassesOverview(ASection: TPasSection);
  1030. var
  1031. i : Integer;
  1032. DocNode : TDocNode;
  1033. ClassDecl : TPasClassType;
  1034. begin
  1035. // Overview page
  1036. if ASection.Classes.Count > 0 then
  1037. begin
  1038. StartSection(SManDocClasses);
  1039. for i := 0 to ASection.Classes.Count - 1 do
  1040. begin
  1041. ClassDecl:=TPasClassType(ASection.Classes[i]);
  1042. WriteTP;
  1043. WriteB(ClassDecl.Name);
  1044. DocNode:=Engine.FindDocNode(ClassDecl);
  1045. If Assigned(DocNode) then
  1046. WriteDescr(ClassDecl,DocNode.ShortDescr);
  1047. end;
  1048. end;
  1049. end;
  1050. procedure TManWriter.WriteUnitClasses(ASection: TPasSection);
  1051. var
  1052. i: Integer;
  1053. begin
  1054. if (ASection.Classes.Count > 0) then
  1055. begin
  1056. for i := 0 to ASection.Classes.Count - 1 do
  1057. WriteClassPage(TPasClassType(ASection.Classes[i]));
  1058. end;
  1059. end;
  1060. procedure TManWriter.WriteClassPage(ClassDecl: TPasClassType);
  1061. var
  1062. DocNode: TDocNode;
  1063. begin
  1064. DocNode:=Engine.FindDocNode(ClassDecl);
  1065. StartManPage(ClassDecl,DocNode);
  1066. Try
  1067. PageTitle(ClassDecl.Name,ManSection,PackageName,PackageDescr);
  1068. StartSection(SManDocName);
  1069. Write(ClassDecl.Name);
  1070. DocNode := Engine.FindDocNode(ClassDecl);
  1071. If Assigned(DocNode) then
  1072. begin
  1073. if not IsDescrNodeEmpty(DocNode.ShortDescr) then
  1074. begin
  1075. write(' \- ');
  1076. WriteDescr(ClassDecl,DocNode.ShortDescr);
  1077. end;
  1078. if Not (IsDescrNodeEmpty(DocNode.Descr)
  1079. and IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1080. begin
  1081. StartSection(SDocDescription);
  1082. WriteDescr(ClassDecl);
  1083. end;
  1084. end;
  1085. // Write method overview
  1086. WriteClassMethodOverView(ClassDecl);
  1087. // Write Property Overview;
  1088. WriteClassPropertyOverView(ClassDecl);
  1089. Finally
  1090. EndManPage;
  1091. end
  1092. end;
  1093. procedure TManWriter.WriteClassPropertyOverview(ClassDecl : TPasClassType);
  1094. var
  1095. Member: TPasElement;
  1096. i: Integer;
  1097. A: String;
  1098. DocNode: TDocNode;
  1099. List : TStringList;
  1100. begin
  1101. // Write property overview
  1102. List:=TStringList.Create;
  1103. Try
  1104. for i := 0 to ClassDecl.Members.Count - 1 do
  1105. begin
  1106. Member := TPasElement(ClassDecl.Members[i]);
  1107. With Member do
  1108. if InheritsFrom(TPasProperty) and SHowMember(Member) then
  1109. List.AddObject(Member.Name,Member)
  1110. end;
  1111. List.Sorted:=True;
  1112. If (List.Count>0) then
  1113. begin
  1114. StartSection(SDocPropertyOverview);
  1115. For I:=0 to List.Count-1 do
  1116. begin
  1117. Member:=TPasElement(List.objects[i]);
  1118. WriteTP;
  1119. A:='';
  1120. if Length(TPasProperty(Member).ReadAccessorName) > 0 then
  1121. a := a + 'r';
  1122. if Length(TPasProperty(Member).WriteAccessorName) > 0 then
  1123. a := a + 'w';
  1124. if Length(TPasProperty(Member).StoredAccessorName) > 0 then
  1125. a := a + 's';
  1126. WriteBI(Member.Name+' '+A);
  1127. DocNode := Engine.FindDocNode(Member);
  1128. If Assigned(DocNode) then
  1129. WriteDescr(Member, DocNode.ShortDescr)
  1130. end;
  1131. end;
  1132. Finally
  1133. List.Free;
  1134. end;
  1135. end;
  1136. { ---------------------------------------------------------------------
  1137. Resource strings man page
  1138. ---------------------------------------------------------------------}
  1139. procedure TManWriter.WriteUnitResourceStrings(ASection: TPasSection);
  1140. var
  1141. ResStrDecl: TPasResString;
  1142. i: Integer;
  1143. begin
  1144. if ASection.ResStrings.Count > 0 then
  1145. begin
  1146. StartManpage(ModuleName+'.'+SManResStr);
  1147. Try
  1148. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1149. StartSection(SManDocName);
  1150. Writeln(ModuleName+' \- '+SDocResStrings);
  1151. StartSection(SManDocResourceStrings);
  1152. Writeln('');
  1153. for i := 0 to ASection.ResStrings.Count - 1 do
  1154. begin
  1155. ResStrDecl := TPasResString(ASection.ResStrings[i]);
  1156. StartSubSection(ResStrDecl.Name);
  1157. DescrWriteCodeLine(EscapeText(ResStrDecl.GetDeclaration(False)));
  1158. end;
  1159. StartSection(SDocSeeAlso);
  1160. WriteModuleSeealso(False);
  1161. Finally
  1162. EndManPage;
  1163. end;
  1164. end;
  1165. end;
  1166. { ---------------------------------------------------------------------
  1167. Constants man page
  1168. ---------------------------------------------------------------------}
  1169. procedure TManWriter.WriteUnitConsts(ASection: TPasSection);
  1170. var
  1171. i: Integer;
  1172. ConstDecl: TPasConst;
  1173. DocNode: TDocNode;
  1174. begin
  1175. if ASection.Consts.Count > 0 then
  1176. begin
  1177. StartManpage(ModuleName+'.'+SManConsts);
  1178. Try
  1179. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1180. StartSection(SManDocName);
  1181. Writeln(ModuleName+' \- '+SDocConstants);
  1182. StartSection(SManDocConstants);
  1183. for i := 0 to ASection.Consts.Count - 1 do
  1184. begin
  1185. ConstDecl := TPasConst(ASection.Consts[i]);
  1186. StartSubSection(ConstDecl.Name);
  1187. DescrWriteCodeLine(EscapeText(ConstDecl.GetDeclaration(True)));
  1188. DocNode:=Engine.FindDocNode(ConstDecl);
  1189. WriteDescr(ConstDecl,DocNode);
  1190. end;
  1191. StartSection(SDocSeeAlso);
  1192. WriteModuleSeealso(False);
  1193. Finally
  1194. EndManPage;
  1195. end;
  1196. end;
  1197. end;
  1198. { ---------------------------------------------------------------------
  1199. Types man page
  1200. ---------------------------------------------------------------------}
  1201. procedure TManWriter.WriteUnitTypes(ASection: TPasSection);
  1202. var
  1203. i: Integer;
  1204. TypeDecl: TPasType;
  1205. DocNode : TDocNode;
  1206. begin
  1207. if ASection.Types.Count > 0 then
  1208. begin
  1209. StartManpage(ModuleName+'.'+SManTypes);
  1210. Try
  1211. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1212. StartSection(SManDocName);
  1213. Writeln(ModuleName+' \- '+SDocTypes);
  1214. StartSection(SManDocTypes);
  1215. for i := 0 to ASection.Types.Count - 1 do
  1216. begin
  1217. TypeDecl := TPasType(ASection.Types[i]);
  1218. StartSubsection(TypeDecl.Name);
  1219. DescrWriteCodeLine(EscapeText(TypeDecl.GetDeclaration(True)));
  1220. DocNode:=Engine.FindDocNode(TypeDecl);
  1221. If TypeDecl is TPasEnumType then
  1222. WriteEnumElements(TypeDecl as TPasEnumType);
  1223. WriteDescr(TypeDecl,DocNode);
  1224. end;
  1225. StartSection(SDocSeeAlso);
  1226. WriteModuleSeeAlso(False);
  1227. Finally
  1228. EndManPage;
  1229. end;
  1230. end;
  1231. end;
  1232. procedure TManWriter.WriteEnumElements(TypeDecl : TPasEnumType);
  1233. Var
  1234. EV : TPasEnumValue;
  1235. I : Integer;
  1236. DocNode : TDocNode;
  1237. begin
  1238. With TypeDecl do
  1239. begin
  1240. SortElementList(Values);
  1241. Writeln(EscapeText(Format(SDocValuesForEnum,[TypeDecl.Name])));
  1242. Try
  1243. For I:=0 to Values.Count-1 do
  1244. begin
  1245. EV:=TPasEnumValue(Values[i]);
  1246. WriteTP;
  1247. WriteB(EscapeText(EV.Name));
  1248. DocNode := Engine.FindDocNode(EV);
  1249. if Assigned(DocNode) and (not IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1250. WriteDescr(EV,DocNode.ShortDescr);
  1251. end;
  1252. Finally
  1253. NewLine;
  1254. end;
  1255. end;
  1256. end;
  1257. { ---------------------------------------------------------------------
  1258. Variables man page
  1259. ---------------------------------------------------------------------}
  1260. procedure TManWriter.WriteUnitVars(ASection: TPasSection);
  1261. var
  1262. VarDecl: TPasVariable;
  1263. i: Integer;
  1264. DocNode : TDocNode;
  1265. begin
  1266. if ASection.Variables.Count > 0 then
  1267. begin
  1268. StartManpage(ModuleName+'.'+SManVars);
  1269. Try
  1270. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1271. StartSection(SManDocName);
  1272. Writeln(ModuleName+' \- '+SDocVariables);
  1273. StartSection(SManDocVariables);
  1274. for i := 0 to ASection.Variables.Count - 1 do
  1275. begin
  1276. VarDecl := TPasVariable(ASection.Variables[i]);
  1277. StartSubSection(VarDecl.Name);
  1278. DescrWriteCodeLine(EscapeText(VarDecl.GetDeclaration(True)));
  1279. DocNode:=Engine.FindDocNode(VarDecl);
  1280. WriteDescr(VarDecl,DocNode);
  1281. end;
  1282. StartSection(SDocSeeAlso);
  1283. WriteModuleSeeAlso(False);
  1284. Finally
  1285. EndManPage;
  1286. end;
  1287. end;
  1288. end;
  1289. { ---------------------------------------------------------------------
  1290. Procedure/Function/Method man page
  1291. ---------------------------------------------------------------------}
  1292. procedure TManWriter.WriteUnitFunctionsAndProceduresOverview(ASection: TPasSection);
  1293. var
  1294. i : Integer;
  1295. DocNode : TDocNode;
  1296. PDecl : TPasProcedureBase;
  1297. begin
  1298. // Overview page
  1299. if ASection.Functions.Count > 0 then
  1300. begin
  1301. StartSection(SManDocFunctions);
  1302. for i := 0 to ASection.Functions.Count - 1 do
  1303. begin
  1304. PDecl:=TPasProcedureBase(ASection.Functions[i]);
  1305. WriteTP;
  1306. WriteB(PDecl.Name);
  1307. DocNode:=Engine.FindDocNode(PDecl);
  1308. If Assigned(DocNode) then
  1309. WriteDescr(PDecl,DocNode.ShortDescr);
  1310. end;
  1311. end;
  1312. end;
  1313. procedure TManWriter.WriteUnitFunctionsAndProcedures(ASection: TPasSection);
  1314. var
  1315. i : Integer;
  1316. begin
  1317. // Pages for all identifiers.
  1318. for i := 0 to ASection.Functions.Count - 1 do
  1319. WriteProcedurePage(TPasProcedure(ASection.Functions[i]));
  1320. end;
  1321. procedure TManWriter.WriteProcedurePage(ProcDecl : TPasProcedureBase);
  1322. var
  1323. DocNode: TDocNode;
  1324. OP : TPasOverloadedProc;
  1325. i : integer;
  1326. D,N : String;
  1327. begin
  1328. DocNode := Engine.FindDocNode(ProcDecl);
  1329. StartManpage(ProcDecl,DocNode);
  1330. Try
  1331. PageTitle(ProcDecl.name,ManSection,PackageName,PackageDescr);
  1332. if Assigned(DocNode) then
  1333. D:=GetDescrString(ProcDecl,DocNode.ShortDescr);
  1334. // Name
  1335. StartSection(SManDocName);
  1336. Writeln(N+' \- '+D);
  1337. // Declaration
  1338. StartSection(SManDocSynopsis);
  1339. if ProcDecl is TPasOverloadedProc then
  1340. begin
  1341. OP:=TPasOverloadedProc(ProcDecl);
  1342. for i := 0 to OP.Overloads.Count - 1 do
  1343. begin
  1344. DescrWriteCodeLine(TPasProcedure(OP.Overloads[i]).GetDeclaration(True));
  1345. end;
  1346. end
  1347. else
  1348. DescrWriteCodeLine(ProcDecl.GetDeclaration(True));
  1349. // Visibility
  1350. If Assigned(ProcDecl.Parent) then
  1351. begin
  1352. StartSection(SManDocVisibility);
  1353. Writeln(VisibilityNames[ProcDecl.Visibility])
  1354. end;
  1355. // Arguments, if present.
  1356. If ProcDecl is TPasProcedure then
  1357. AppendProcedureArgsSection(TPasProcedure(ProcDecl).ProcType);
  1358. // Description
  1359. if Assigned(DocNode) then
  1360. begin
  1361. if Assigned(DocNode.Descr) then
  1362. begin
  1363. StartSection(SManDocDescription);
  1364. WriteDescr(ProcDecl,DocNode.Descr);
  1365. end;
  1366. // Errors
  1367. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  1368. begin
  1369. StartSection(SManDocErrors);
  1370. WriteDescr(ProcDecl, DocNode.ErrorsDoc);
  1371. end;
  1372. end;
  1373. // Arguments, if present.
  1374. If ProcDecl is TPasFunction then
  1375. AppendProcedureArgsSection(TPasFunction(ProcDecl).ProcType);
  1376. WriteExample(DocNode);
  1377. StartSection(SManDocSeeAlso);
  1378. WriteModuleSeeAlso(True);
  1379. WriteSeeAlso(DocNode,False);
  1380. Finally
  1381. EndManPage;
  1382. end;
  1383. end;
  1384. procedure TManWriter.AppendProcedureArgsSection(Element: TPasProcedureType);
  1385. var
  1386. IsFirst: Boolean;
  1387. DocNode: TDocNode;
  1388. i: Integer;
  1389. Arg: TPasArgument;
  1390. begin
  1391. If Not Assigned(Element) then
  1392. exit;
  1393. IsFirst := True;
  1394. for i := 0 to Element.Args.Count - 1 do
  1395. begin
  1396. Arg := TPasArgument(Element.Args[i]);
  1397. DocNode:=Engine.FindDocNode(Arg);
  1398. if Assigned(DocNode) and (Not IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1399. begin
  1400. if IsFirst then
  1401. begin
  1402. IsFirst:=False;
  1403. StartSection(SManDocArguments);
  1404. end;
  1405. WriteTP;
  1406. WriteB(Arg.Name);
  1407. WriteDescr(Arg,DocNode.ShortDescr);
  1408. end;
  1409. end;
  1410. end;
  1411. procedure TManWriter.AppendFunctionResultSection(Element: TPasFunctionType);
  1412. Var
  1413. ResultEl: TPasResultElement;
  1414. DocNode: TDocNode;
  1415. begin
  1416. If Not Assigned(Element) then
  1417. exit;
  1418. ResultEl := TPasFunctionType(Element).ResultEl;
  1419. DocNode := Engine.FindDocNode(ResultEl);
  1420. If Assigned(DocNode) then
  1421. begin
  1422. if IsDescrNodeEmpty(DocNode.Descr) or not IsDescrNodeEmpty(DocNode.ShortDescr) then
  1423. begin
  1424. StartSection(SManDocResult);
  1425. WriteDescr(ResultEl,DocNode);
  1426. end;
  1427. end;
  1428. end;
  1429. { ---------------------------------------------------------------------
  1430. Property man page
  1431. ---------------------------------------------------------------------}
  1432. procedure TManWriter.WritePropertyPage(PropDecl : TPasProperty);
  1433. var
  1434. DocNode: TDocNode;
  1435. N,D: String;
  1436. begin
  1437. DocNode := Engine.FindDocNode(PropDecl);
  1438. StartManpage(PropDecl,DocNode);
  1439. Try
  1440. PageTitle(PropDecl.Name,ManSection,PackageName,PackageDescr);
  1441. if Assigned(DocNode) then
  1442. D:=GetDescrString(PropDecl,DocNode.ShortDescr);
  1443. // Name
  1444. StartSection(SManDocName);
  1445. Writeln(N+' \- '+D);
  1446. // Declaration
  1447. StartSection(SManDocSynopsis);
  1448. WriteLn(PropDecl.GetDeclaration(True));
  1449. // Visibility
  1450. If Assigned(PropDecl.Parent) then
  1451. begin
  1452. StartSection(SManDocVisibility);
  1453. Writeln(VisibilityNames[PropDecl.Visibility])
  1454. end;
  1455. StartSection(SManDocAccess);
  1456. D:='';
  1457. If Length(PropDecl.ReadAccessorName) > 0 then
  1458. D:='Read';
  1459. if Length(PropDecl.WriteAccessorName) > 0 then
  1460. begin
  1461. If D<>'' then
  1462. D:=D+',';
  1463. D:=D+'Write';
  1464. end;
  1465. Writeln(D);
  1466. if Assigned(DocNode) then
  1467. begin
  1468. // Description
  1469. if Assigned(DocNode.Descr) then
  1470. begin
  1471. StartSection(SManDocDescription);
  1472. WriteDescr(PropDecl,DocNode.Descr);
  1473. end;
  1474. // Errors
  1475. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  1476. begin
  1477. StartSection(SManDocErrors);
  1478. WriteDescr(PropDecl, DocNode.ErrorsDoc);
  1479. end;
  1480. WriteExample(DocNode);
  1481. WriteSeeAlso(DocNode,False);
  1482. end;
  1483. Finally
  1484. EndManPage;
  1485. end;
  1486. end;
  1487. Function CompareElements(P1,P2 : Pointer) : Integer;
  1488. begin
  1489. Result:=CompareText(TPasElement(P1).Name,TPasElement(P2).Name);
  1490. end;
  1491. procedure TManWriter.SortElementList(List : TFPList);
  1492. begin
  1493. List.Sort(@CompareElements);
  1494. end;
  1495. procedure TManWriter.WriteCommentLine;
  1496. begin
  1497. WriteComment('-------------------------------------------------------');
  1498. end;
  1499. procedure TManWriter.WriteComment(Comment : String);
  1500. begin
  1501. Writeln('." '+Comment);
  1502. end;
  1503. class function TManWriter.FileNameExtension: String;
  1504. begin
  1505. Result:=IntToStr(DefaultManSection);
  1506. end;
  1507. procedure TManWriter.WriteClassMethodOverview(ClassDecl: TPasClassType);
  1508. var
  1509. Member : TPasElement;
  1510. i : Integer;
  1511. DocNode : TDocNode;
  1512. List : TStringList;
  1513. begin
  1514. List:=TStringList.Create;
  1515. Try
  1516. GetMethodList(ClassDecl,List);
  1517. If List.Count>0 then
  1518. begin
  1519. StartSection(SManDocMethods);
  1520. For I:=0 to List.Count-1 do
  1521. begin
  1522. Member:=TPasElement(List.Objects[i]);
  1523. WriteTP;
  1524. WriteB(EscapeText(Member.Name));
  1525. DocNode := Engine.FindDocNode(Member);
  1526. If Assigned(DocNode) then
  1527. WriteDescr(Member, DocNode.ShortDescr)
  1528. end;
  1529. end;
  1530. Finally
  1531. List.Free;
  1532. end;
  1533. end;
  1534. Class procedure TManWriter.Usage(List: TStrings);
  1535. begin
  1536. List.add('--nounitprefix');
  1537. List.Add(SManUsageNoUnitPrefix);
  1538. List.add('--man-section=ASection');
  1539. List.Add(SManUsageManSection);
  1540. List.Add('--man-description=descr');
  1541. List.Add(SManUsagePackageDescription);
  1542. end;
  1543. initialization
  1544. // Do not localize.
  1545. RegisterWriter(TMANWriter,'man',SManUsageWriterDescr);
  1546. finalization
  1547. UnRegisterWriter('man');
  1548. end.