dw_man.pp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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 : TList);
  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. Class Function FileNameExtension : String;virtual;
  91. procedure WriteExample(ADocNode: TDocNode);
  92. procedure WriteSeeAlso(ADocNode: TDocNode; Comma : Boolean);
  93. Public
  94. Constructor Create(APackage: TPasPackage; AEngine: TFPDocEngine); override;
  95. procedure WriteDoc; override;
  96. // Documentation writing methods.
  97. // Package
  98. Procedure WritePackagePage;
  99. // Topics
  100. Procedure ProcessTopics(DocNode : TDocNode; Subs : Boolean);
  101. Procedure WriteTopicRefs(DocNode : TDocNode);
  102. Procedure WriteTopicPage(Parent,Node : TDocNode);
  103. // Module
  104. procedure ProcessModule(AModule: TPasModule);
  105. procedure WriteUnitPage(AModule: TPasModule);
  106. procedure WriteUnitUsesOverview(ASection: TPasSection);
  107. procedure WriteUnitFunctionsAndProceduresOverview(ASection: TPasSection);
  108. procedure WriteUnitClassesOverview(ASection: TPasSection);
  109. procedure WriteUnitResourceStrings(ASection: TPasSection);
  110. procedure WriteUnitConsts(ASection: TPasSection);
  111. procedure WriteUnitTypes(ASection: TPasSection);
  112. procedure WriteUnitVars(ASection: TPasSection);
  113. procedure WriteUnitClasses(ASection: TPasSection);
  114. procedure WriteUnitFunctionsAndProcedures(ASection: TPasSection);
  115. // Smaller elements
  116. procedure WriteEnumElements(TypeDecl : TPasEnumType);
  117. procedure WriteClassPage(ClassDecl: TPasClassType);
  118. procedure WriteClassMethodOverview(ClassDecl: TPasClassType);
  119. procedure WriteClassPropertyOverview(ClassDecl: TPasClassType);
  120. procedure WriteProcedurePage(ProcDecl: TPasProcedureBase);
  121. procedure AppendProcedureArgsSection(Element: TPasProcedureType);
  122. procedure AppendFunctionResultSection(Element: TPasFunctionType);
  123. procedure WritePropertyPage(PropDecl: TPasProperty);
  124. // Overriden from fpdocwriter;
  125. procedure DescrWriteText(const AText: DOMString); override;
  126. procedure DescrBeginBold; override;
  127. procedure DescrEndBold; override;
  128. procedure DescrBeginItalic; override;
  129. procedure DescrEndItalic; override;
  130. procedure DescrBeginEmph; override;
  131. procedure DescrEndEmph; override;
  132. procedure DescrWriteFileEl(const AText: DOMString); override;
  133. procedure DescrWriteKeywordEl(const AText: DOMString); override;
  134. procedure DescrWriteVarEl(const AText: DOMString); override;
  135. procedure DescrBeginLink(const AId: DOMString); override;
  136. procedure DescrEndLink; override;
  137. procedure DescrWriteLinebreak; override;
  138. procedure DescrBeginParagraph; override;
  139. procedure DescrBeginCode(HasBorder: Boolean; const AHighlighterName: String); override;
  140. procedure DescrWriteCodeLine(const ALine: String); override;
  141. procedure DescrEndCode; override;
  142. procedure DescrEndParagraph; override;
  143. procedure DescrBeginOrderedList; override;
  144. procedure DescrEndOrderedList; override;
  145. procedure DescrBeginUnorderedList; override;
  146. procedure DescrEndUnorderedList; override;
  147. procedure DescrBeginDefinitionList; override;
  148. procedure DescrEndDefinitionList; override;
  149. procedure DescrBeginListItem; override;
  150. procedure DescrEndListItem; override;
  151. procedure DescrBeginDefinitionTerm; override;
  152. procedure DescrEndDefinitionTerm; override;
  153. procedure DescrBeginDefinitionEntry; override;
  154. procedure DescrEndDefinitionEntry; override;
  155. procedure DescrBeginSectionTitle; override;
  156. procedure DescrBeginSectionBody; override;
  157. procedure DescrEndSection; override;
  158. procedure DescrBeginRemark; override;
  159. procedure DescrEndRemark; override;
  160. procedure DescrBeginTable(ColCount: Integer; HasBorder: Boolean); override;
  161. procedure DescrEndTable; override;
  162. procedure DescrBeginTableCaption; override;
  163. procedure DescrEndTableCaption; override;
  164. procedure DescrBeginTableHeadRow; override;
  165. procedure DescrEndTableHeadRow; override;
  166. procedure DescrBeginTableRow; override;
  167. procedure DescrEndTableRow; override;
  168. procedure DescrBeginTableCell; override;
  169. procedure DescrEndTableCell; override;
  170. Function InterPretOption(Const Cmd,Arg : String) : boolean; 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: TList);
  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 ((FPEl.Visibility = visPrivate) and Engine.HidePrivate) or
  246. ((FPEl.Visibility = visProtected) and Engine.HideProtected) then
  247. continue;
  248. DocNode := Engine.FindDocNode(FPEl);
  249. if not Assigned(DocNode) then
  250. begin
  251. DidAutolink := False;
  252. if Assigned(ClassEl.AncestorType) and
  253. (ClassEl.AncestorType.ClassType = TPasClassType) then
  254. begin
  255. for k := 0 to TPasClassType(ClassEl.AncestorType).Members.Count - 1 do
  256. begin
  257. AncestorMemberEl :=
  258. TPasElement(TPasClassType(ClassEl.AncestorType).Members[k]);
  259. if AncestorMemberEl.Name = FPEl.Name then
  260. begin
  261. DocNode := Engine.FindDocNode(AncestorMemberEl);
  262. if Assigned(DocNode) then
  263. begin
  264. DidAutolink := True;
  265. Engine.AddLink(FPEl.PathName,
  266. Engine.FindAbsoluteLink(AncestorMemberEl.PathName));
  267. break;
  268. end;
  269. end;
  270. end;
  271. end;
  272. if not DidAutolink then
  273. AddLabel(FPEl);
  274. end else
  275. AddLabel(FPEl);
  276. end;
  277. end;
  278. end;
  279. AddList(AModule, InterfaceSection.Functions);
  280. AddList(AModule, InterfaceSection.Variables);
  281. end;
  282. end;
  283. var
  284. i: Integer;
  285. begin
  286. inherited;
  287. if Length(Package.Name) > 1 then
  288. AddTopicPages(Package);
  289. for i := 0 to Package.Modules.Count - 1 do
  290. ScanModule(TPasModule(Package.Modules[i]));
  291. end;
  292. { ---------------------------------------------------------------------
  293. Writing support
  294. ---------------------------------------------------------------------}
  295. Function TManWriter.PushWriteContext(S : TStream) : TStream;
  296. begin
  297. Result:=FStream;
  298. FStream:=S;
  299. end;
  300. Procedure TManWriter.PopWriteContext(S : TSTream);
  301. begin
  302. FStream:=S;
  303. end;
  304. function TManWriter.EscapeText(const s: String): String;
  305. begin
  306. Result:=S;
  307. end;
  308. procedure TManWriter.Write(const s: String);
  309. Var
  310. W : String;
  311. L : Integer;
  312. begin
  313. W:=S;
  314. If FAtLineStart and not FSKipTrim then
  315. W:=TrimLeft(W);
  316. L:=Length(W);
  317. If (L>0) then
  318. FStream.Write(PChar(W)^,L);
  319. FAtLineStart:=false;
  320. If FCheckEOL then
  321. begin
  322. If (L>=LEOL) then
  323. FAtLineStart:=(Copy(W,L-LEOL+1,LEOL)=LineEnding);
  324. end;
  325. end;
  326. Procedure TManWriter.NewLine;
  327. begin
  328. if Not FAtLineStart then
  329. Writeln('');
  330. end;
  331. procedure TManWriter.WriteF(const s: String; const Args: array of const);
  332. begin
  333. Write(Format(S,Args));
  334. end;
  335. procedure TManWriter.WriteLn(const s: String);
  336. begin
  337. FCheckEOL:=False;
  338. Try
  339. Write(S);
  340. Write(LineEnding);
  341. FAtLineStart:=True;
  342. finally
  343. FCheckEOL:=True;
  344. end;
  345. end;
  346. procedure TManWriter.WriteLnF(const s: String; const Args: array of const);
  347. begin
  348. Write(Format(S,Args));
  349. Write(LineEnding);
  350. end;
  351. procedure TManWriter.DescrWriteText(const AText: DOMString);
  352. begin
  353. self.Write(EscapeText(AText));
  354. end;
  355. procedure TManWriter.DescrBeginBold;
  356. begin
  357. NewLine;
  358. Write('.B ');
  359. end;
  360. procedure TManWriter.DescrEndBold;
  361. begin
  362. NewLine;
  363. end;
  364. procedure TManWriter.DescrBeginItalic;
  365. begin
  366. NewLine;
  367. Write('.I ');
  368. end;
  369. procedure TManWriter.DescrEndItalic;
  370. begin
  371. NewLine;
  372. end;
  373. procedure TManWriter.DescrBeginEmph;
  374. begin
  375. NewLine;
  376. Write('.I ');
  377. end;
  378. procedure TManWriter.DescrEndEmph;
  379. begin
  380. NewLine;
  381. end;
  382. procedure TManWriter.DescrWriteFileEl(const AText: DOMString);
  383. Var
  384. S : AnsiString;
  385. begin
  386. NewLine;
  387. S:=AText;
  388. Writeln('.I '+S);
  389. end;
  390. procedure TManWriter.DescrWriteKeywordEl(const AText: DOMString);
  391. Var
  392. S : AnsiString;
  393. begin
  394. NewLine;
  395. S:=AText;
  396. Writeln('.B '+S);
  397. end;
  398. procedure TManWriter.DescrWriteVarEl(const AText: DOMString);
  399. Var
  400. S : AnsiString;
  401. begin
  402. NewLine;
  403. S:=AText;
  404. Writeln('.B '+S);
  405. end;
  406. procedure TManWriter.DescrBeginLink(const AId: DOMString);
  407. begin
  408. // Do nothing
  409. end;
  410. procedure TManWriter.DescrEndLink;
  411. begin
  412. // Do nothing
  413. end;
  414. procedure TManWriter.DescrWriteLinebreak;
  415. begin
  416. NewLine;
  417. Writeln('.br');
  418. end;
  419. procedure TManWriter.DescrBeginParagraph;
  420. begin
  421. NewLine;
  422. Writeln('.PP');
  423. end;
  424. procedure TManWriter.DescrBeginCode(HasBorder: Boolean;
  425. const AHighlighterName: String);
  426. begin
  427. NewLine;
  428. Writeln('');
  429. end;
  430. procedure TManWriter.DescrWriteCodeLine(const ALine: String);
  431. begin
  432. FSkipTrim:=True;
  433. Try
  434. Writeln(ALine);
  435. Finally
  436. FSkipTrim:=False;
  437. end;
  438. DescrWriteLinebreak;
  439. end;
  440. procedure TManWriter.DescrEndCode;
  441. begin
  442. NewLine;
  443. Writeln('');
  444. end;
  445. procedure TManWriter.DescrEndParagraph;
  446. begin
  447. NewLine;
  448. Writeln('');
  449. end;
  450. procedure TManWriter.NewListLevel(Initial : Integer);
  451. begin
  452. Inc(FListLevel);
  453. If (FListLevel<MaxListLevel) then
  454. FLists[FListLevel]:=0;
  455. NewLine;
  456. Writeln('.RS');
  457. end;
  458. procedure TManWriter.DecListLevel;
  459. begin
  460. NewLine;
  461. Writeln('.RE');
  462. If (FListLevel>0) then
  463. Dec(FListLevel)
  464. end;
  465. procedure TManWriter.DescrBeginOrderedList;
  466. begin
  467. NewListLevel(0);
  468. end;
  469. procedure TManWriter.DescrEndOrderedList;
  470. begin
  471. DecListLevel;
  472. end;
  473. procedure TManWriter.DescrBeginUnorderedList;
  474. begin
  475. NewListLevel(-1);
  476. end;
  477. procedure TManWriter.DescrEndUnorderedList;
  478. begin
  479. DecListlevel;
  480. end;
  481. procedure TManWriter.DescrBeginDefinitionList;
  482. begin
  483. NewListLevel(-2);
  484. end;
  485. procedure TManWriter.DescrEndDefinitionList;
  486. begin
  487. DecListLevel
  488. end;
  489. procedure TManWriter.DescrBeginListItem;
  490. begin
  491. WriteTP
  492. end;
  493. procedure TManWriter.DescrEndListItem;
  494. begin
  495. NewLine;
  496. end;
  497. procedure TManWriter.DescrBeginDefinitionTerm;
  498. begin
  499. WriteTP;
  500. Write('.B ');
  501. end;
  502. procedure TManWriter.DescrEndDefinitionTerm;
  503. begin
  504. NewLine;
  505. end;
  506. procedure TManWriter.DescrBeginDefinitionEntry;
  507. begin
  508. NewLine;
  509. end;
  510. procedure TManWriter.DescrEndDefinitionEntry;
  511. begin
  512. NewLine;
  513. end;
  514. procedure TManWriter.DescrBeginSectionTitle;
  515. begin
  516. Write('.SH ');
  517. end;
  518. procedure TManWriter.DescrBeginSectionBody;
  519. begin
  520. NewLine;
  521. end;
  522. procedure TManWriter.DescrEndSection;
  523. begin
  524. NewLine;
  525. end;
  526. procedure TManWriter.DescrBeginRemark;
  527. begin
  528. WriteTP;
  529. WriteB(SDocRemark);
  530. end;
  531. procedure TManWriter.DescrEndRemark;
  532. begin
  533. NewLine;
  534. end;
  535. procedure TManWriter.DescrBeginTable(ColCount: Integer; HasBorder: Boolean);
  536. begin
  537. NewLine;
  538. end;
  539. procedure TManWriter.DescrEndTable;
  540. begin
  541. NewLine;
  542. end;
  543. procedure TManWriter.DescrBeginTableCaption;
  544. begin
  545. NewLine;
  546. end;
  547. procedure TManWriter.DescrEndTableCaption;
  548. begin
  549. NewLine;
  550. end;
  551. procedure TManWriter.DescrBeginTableHeadRow;
  552. begin
  553. DescrBeginParagraph;
  554. end;
  555. procedure TManWriter.DescrEndTableHeadRow;
  556. begin
  557. DescrEndParagraph;
  558. end;
  559. procedure TManWriter.DescrBeginTableRow;
  560. begin
  561. DescrBeginParagraph;
  562. end;
  563. procedure TManWriter.DescrEndTableRow;
  564. begin
  565. DescrEndParagraph;
  566. end;
  567. procedure TManWriter.DescrBeginTableCell;
  568. begin
  569. // Do nothing
  570. end;
  571. procedure TManWriter.DescrEndTableCell;
  572. begin
  573. Writeln(#9);
  574. end;
  575. function TManWriter.InterPretOption(const Cmd, Arg: String): boolean;
  576. begin
  577. Result:=True;
  578. if (Cmd='--man-section') then
  579. ManSection:=Arg
  580. else if (Cmd='--man-description') then
  581. PackageDescr:=Arg
  582. else if (Cmd='--nounitprefix') then
  583. SkipUnitPrefix:=True
  584. else
  585. Result:=inherited InterPretOption(Cmd, Arg);
  586. end;
  587. Function TManWriter.GetDescrString(AContext: TPasElement; DescrNode: TDOMElement) : String;
  588. Var
  589. S : TStringStream;
  590. F : TStream;
  591. begin
  592. Result:='';
  593. if Assigned(DescrNode) then
  594. begin
  595. S:=TStringStream.Create('');
  596. Try
  597. F:=PushWriteContext(S);
  598. Try
  599. ConvertDescr(AContext, DescrNode, False);
  600. Result:=S.DataString;
  601. FInally
  602. PopWriteContext(F);
  603. end;
  604. finally
  605. S.FRee;
  606. end;
  607. end;
  608. end;
  609. { ---------------------------------------------------------------------
  610. Formatting routines
  611. ---------------------------------------------------------------------}
  612. procedure TManWriter.WriteTP;
  613. begin
  614. NewLine;
  615. Writeln('.TP');
  616. end;
  617. procedure TManWriter.WriteB(Const Msg : String);
  618. begin
  619. NewLine;
  620. Writeln('.B '+MSG);
  621. end;
  622. procedure TManWriter.WriteBI(Const Msg : String);
  623. begin
  624. NewLine;
  625. Writeln('.BI '+MSG);
  626. end;
  627. { ---------------------------------------------------------------------
  628. Sectioning routines
  629. ---------------------------------------------------------------------}
  630. procedure TManWriter.StartListing(Frames: Boolean);
  631. begin
  632. Writeln('');
  633. WriteB(SManDocListing);
  634. end;
  635. procedure TManWriter.StartManPage(AElement: TPasElement; ADocNode: TDocNode);
  636. begin
  637. StartManPage(ElementToManPage(AElement))
  638. end;
  639. procedure TManWriter.StartManPage(FN: String);
  640. begin
  641. FN:=LowerCase(FN+'.'+mansection);
  642. FStream:=TFileStream.Create(OutputDir+FN,fmCreate);
  643. end;
  644. procedure TManWriter.EndManPage;
  645. begin
  646. FreeAndNil(FStream);
  647. end;
  648. procedure TManWriter.StartSection(Const SectionName: String);
  649. begin
  650. NewLine;
  651. Writeln('');
  652. Writeln('.SH '+SectionName);
  653. end;
  654. procedure TManWriter.StartSubSection(Const SubSectionName: String);
  655. begin
  656. NewLine;
  657. Writeln('.SS '+SubSectionName);
  658. end;
  659. procedure TManWriter.PageTitle(Const ATitle,ASection,ASource,Amanual : String);
  660. Var
  661. D : String;
  662. begin
  663. D:=FormatDateTime('mmmm yyyy',Date);
  664. WritelnF('.TH "%s" "%s" "%s" "%s" "%s"',[ATitle,ASection,D,ASource,AManual]);
  665. end;
  666. procedure TManWriter.WriteManRef(Const ManPage : String; Comma : Boolean);
  667. begin
  668. If Comma then
  669. Writeln(Lowercase(Format('%s (%s),',[ManPage,ManSection])))
  670. else
  671. Writeln(Lowercase(Format('%s (%s)',[ManPage,ManSection])));
  672. end;
  673. Function TManWriter.ElementToManPage(APasElement : TPasElement) : String;
  674. Var
  675. E : TPasElement;
  676. begin
  677. E:=APasElement; // Make code more readable
  678. If (E is TPasPackage) or (E is TPasModule) then
  679. begin
  680. Result:=APasElement.Name;
  681. If (Length(Result)>0) and (Result[1]='#') then
  682. Delete(Result,1,1);
  683. end
  684. else if E is TTopicElement then
  685. begin
  686. // Todo : Check for package
  687. Result:=ModuleName+E.name;
  688. end
  689. else
  690. begin
  691. If Not SkipUnitprefix then
  692. Result:=ModuleName+'.';
  693. If (E.Parent<>Nil) and (not (E.Parent is TPasSection)) then
  694. begin
  695. If (E.Parent.Name<>'') then
  696. Result:=Result+E.Parent.Name;
  697. If (E is TPasProperty) or (E is TPasProcedure) or (E is TPasClassType) then
  698. Result:=Result+E.Name;
  699. end
  700. else
  701. begin
  702. If (E is TPasConst) then
  703. Result:=Result+SManConsts
  704. else If E is TPasVariable then
  705. Result:=Result+SManVars
  706. else If E is TPasClassType then
  707. Result:=Result+E.Name
  708. else If E is TPasType then
  709. Result:=Result+SManTypes
  710. else If E is TPasResString then
  711. Result:=Result+SManResStr
  712. else
  713. Result:=Result+E.Name;
  714. end;
  715. end;
  716. Result:=LowerCase(Result);
  717. end;
  718. procedure TManWriter.WriteManRef(APasElement : TPasElement; Comma : Boolean);
  719. begin
  720. WriteManRef(ElementToManPage(APasElement),Comma);
  721. end;
  722. procedure TManWriter.WriteModuleSeealso(Comma : Boolean);
  723. Var
  724. HC,HT,HV,HR : Boolean;
  725. begin
  726. HC:=Module.InterfaceSection.Consts.Count>0;
  727. HR:=Module.InterfaceSection.ResStrings.Count>0;
  728. HV:=Module.InterfaceSection.Variables.Count>0;
  729. HT:=Module.InterfaceSection.Types.Count>0;
  730. WriteManRef(ModuleName,HC or HR or HV or HT or comma);
  731. if HC then
  732. WriteManRef(ModuleName+'.'+SManConsts,HR or HV or HT or comma);
  733. if HR then
  734. WriteManRef(ModuleName+'.'+SManResStr, HV or HT or comma);
  735. if HV then
  736. WriteManRef(ModuleName+'.'+SManVars, HT or comma);
  737. if HT then
  738. WriteManRef(ModuleName+'.'+SManTypes,comma);
  739. end;
  740. procedure TManWriter.WriteSeeAlso(ADocNode: TDocNode; Comma: Boolean);
  741. var
  742. Node: TDOMNode;
  743. s: String;
  744. begin
  745. if Not (Assigned(ADocNode) and Assigned(ADocNode.SeeAlso)) then
  746. Exit;
  747. Node := ADocNode.SeeAlso.FirstChild;
  748. while Assigned(Node) do
  749. begin
  750. if IsLinkNode(Node) then
  751. begin
  752. S:=TDomElement(Node)['id'];
  753. WriteManRef(S,(Node.NextSibling<>Nil) or Comma);
  754. end;
  755. Node:=Node.NextSibling;
  756. end;
  757. end;
  758. function TManWriter.ConstValue(ConstDecl: TPasConst): String;
  759. begin
  760. if Assigned(ConstDecl) then
  761. Result := ConstDecl.ClassName
  762. else
  763. Result := '<nil>';
  764. end;
  765. procedure TManWriter.WriteExample(ADocNode: TDocNode);
  766. var
  767. Example: TDOMElement;
  768. S : string;
  769. begin
  770. S:='';
  771. if Assigned(ADocNode) then
  772. begin
  773. Example := ADocNode.FirstExample;
  774. If Assigned(Example) then
  775. begin
  776. StartSection(SManDocExamples);
  777. while Assigned(Example) do
  778. begin
  779. s:=Engine.GetExampleFileName(Example);
  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 : TList);
  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. 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.