2
0

dw_man.pp 44 KB

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