dw_man.pp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  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. if (s<>'') then
  781. WriteExampleFile(S);
  782. DescrEndParaGraph;
  783. Repeat
  784. Example := TDomElement(Example.NextSibling);
  785. until (Example=Nil) or ((Example.NodeType=ELEMENT_NODE) and (Example.NodeName='example'));
  786. end;
  787. end;
  788. end;
  789. end;
  790. procedure TManWriter.WriteExampleFile(FN : String);
  791. Var
  792. L : TStringList;
  793. I : Integer;
  794. begin
  795. WriteBI(SDocExample+' \- '+ExtractFileName(FN));
  796. Writeln('');
  797. DescrWriteLineBreak;
  798. If (FN<>'') and FileExists(FN) then
  799. begin
  800. L:=TStringList.Create;
  801. Try
  802. L.LoadFromFile(FN);
  803. For I:=0 to L.Count-1 do
  804. DescrWriteCodeLine(L[i]);
  805. finally
  806. L.Free;
  807. end;
  808. end;
  809. end;
  810. { ---------------------------------------------------------------------
  811. Actual man page writing
  812. ---------------------------------------------------------------------}
  813. procedure TManWriter.WriteDoc;
  814. var
  815. i : Integer;
  816. L : TstringList;
  817. DocNode : TDocNode;
  818. begin
  819. PackageName := LowerCase(Copy(Package.Name, 2, 255));
  820. If (Engine.Output<>'') then
  821. OutputDir:=Engine.Output
  822. else
  823. OutputDir:=PackageName+'.man';
  824. If not ForceDirectories(OutputDir) then
  825. FPDocError(SErrCouldNotCreateOutputDir,[OutputDir]);
  826. OutputDir:=IncludeTrailingPathDelimiter(OutputDir);
  827. If (ManSection='') then
  828. ManSection:=IntToStr(DefaultManSection);
  829. WritePackagePage;
  830. L:=TStringList.Create;
  831. Try
  832. // Sort modules.
  833. For I:=0 to Package.Modules.Count-1 do
  834. L.AddObject(TPasModule(Package.Modules[i]).Name,TPasModule(Package.Modules[i]));
  835. L.Sorted:=True;
  836. for i:=0 to L.Count - 1 do
  837. ProcessModule(TPasModule(L.Objects[i]));
  838. Finally
  839. L.Free;
  840. end;
  841. end;
  842. { ---------------------------------------------------------------------
  843. Package man page
  844. ---------------------------------------------------------------------}
  845. Procedure TManWriter.WritePackagePage;
  846. var
  847. D,DocNode: TDocNode;
  848. M : TPasModule;
  849. I : Integer;
  850. L : TStringList;
  851. begin
  852. DocNode:=Engine.FindDocNode(Package);
  853. If (PackageDescr='') then
  854. PackageDescr:=GetDescrString(Package,DocNode.ShortDescr);
  855. StartManPage(Package,DocNode);
  856. Try
  857. PageTitle(PackageName,ManSection,PackageName,PackageDescr);
  858. StartSection(SManDocName);
  859. Writeln(PackageName+' \- '+PackageDescr);
  860. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  861. begin
  862. StartSection(SDocDescription);
  863. WriteDescr(Package, DocNode.Descr);
  864. end;
  865. StartSection(SManDocPackageUnits);
  866. L:=TStringList.Create;
  867. Try
  868. For I:=0 to Package.Modules.Count-1 do
  869. L.AddObject(TPasModule(Package.Modules[i]).Name,TPasModule(Package.Modules[i]));
  870. L.Sorted:=True;
  871. for i:=0 to L.Count - 1 do
  872. begin
  873. WriteTP;
  874. WriteB(L[i]);
  875. M:=TPasModule(L.Objects[i]);
  876. D:=Engine.FindDocNode(M);
  877. WriteLn(GetDescrString(M,D.ShortDescr))
  878. end;
  879. StartSection(SDocSeeAlso);
  880. WriteSeeAlso(DocNode,True);
  881. WriteTopicRefs(DocNode);
  882. for i:=0 to L.Count - 1 do
  883. WriteManRef(TPasModule(L.Objects[i]),I<L.Count-1);
  884. Finally
  885. L.Free;
  886. end;
  887. Finally
  888. EndManPage;
  889. end;
  890. ProcessTopics(DocNode,True);
  891. end;
  892. { ---------------------------------------------------------------------
  893. Topic support
  894. ---------------------------------------------------------------------}
  895. Procedure TManWriter.WriteTopicRefs(DocNode : TDocNode);
  896. Var
  897. Node : TDocNode;
  898. begin
  899. If Not Assigned(DocNode) then
  900. Exit;
  901. Node:=DocNode.FirstChild;
  902. While Assigned(Node) do
  903. begin
  904. If Node.TopicNode then
  905. WriteManRef(DocNode.Name,Node.NextSibling<>Nil);
  906. Node:=Node.NextSibling;
  907. end;
  908. end;
  909. Procedure TManWriter.ProcessTopics(DocNode : TDocNode; Subs : Boolean);
  910. Var
  911. Node,SubNode : TDocNode;
  912. begin
  913. If Not Assigned(DocNode) then
  914. Exit;
  915. Node:=DocNode.FirstChild;
  916. While Assigned(Node) do
  917. begin
  918. If Node.TopicNode then
  919. begin
  920. WriteTopicPage(DocNode,Node);
  921. If Subs then
  922. begin
  923. SubNode:=DocNode.FirstChild;
  924. While Assigned(SubNode) do
  925. If SubNode.TopicNode then
  926. WriteTopicPage(Node,SubNode);
  927. end;
  928. end;
  929. Node:=Node.NextSibling;
  930. end;
  931. end;
  932. Procedure TManWriter.WriteTopicPage(Parent,Node : TDocNode);
  933. Var
  934. Element : TTopicElement;
  935. begin
  936. Element:=FindTopicElement(Node);
  937. If Not Assigned(Element) then
  938. Exit;
  939. StartManPage(Element,Node) ;
  940. Try
  941. PageTitle(Node.Name,ManSection,PackageName,PackageDescr);
  942. StartSection(SManDocName);
  943. Writeln(Node.Name+' \- '+GetDescrString(Element,Node.ShortDescr));
  944. StartSection(SManDocDescription);
  945. If Assigned(Node.Descr) then
  946. WriteDescr(Element,Node.Descr);
  947. StartSection(SManDocSeeAlso);
  948. WriteSeeAlso(Node,True);
  949. WriteTopicRefs(Parent);
  950. WriteTopicRefs(Node);
  951. Finally
  952. EndManPage;
  953. end;
  954. end;
  955. { ---------------------------------------------------------------------
  956. Module man pages
  957. ---------------------------------------------------------------------}
  958. procedure TManWriter.ProcessModule(AModule : TPasModule);
  959. begin
  960. With AModule do
  961. begin
  962. Module:=AModule;
  963. ModuleName:=Name;
  964. With InterfaceSection do
  965. begin
  966. SortElementList(Declarations);
  967. SortElementList(Types);
  968. SortElementList(Consts);
  969. SortElementList(Classes);
  970. SortElementList(Functions);
  971. SortElementList(Variables);
  972. SortElementList(ResStrings);
  973. end;
  974. WriteUnitPage(AModule);
  975. WriteUnitResourceStrings(InterfaceSection);
  976. WriteUnitConsts(InterfaceSection);
  977. WriteUnitTypes(InterfaceSection);
  978. WriteUnitVars(InterfaceSection);
  979. WriteUnitClasses(InterfaceSection);
  980. WriteUnitFunctionsAndProcedures(InterfaceSection);
  981. end;
  982. end;
  983. procedure TManWriter.WriteUnitPage(AModule : TPasModule);
  984. Var
  985. DocNode : TDocNode;
  986. begin
  987. DocNode:=Engine.FindDocNode(AModule);
  988. StartManPage(AModule,DocNode);
  989. Try
  990. PageTitle(AModule.Name,ManSection,PackageName,PackageDescr);
  991. StartSection(SManDocName);
  992. Writeln(DocNode.Name+' \- '+GetDescrString(AModule,DocNode.ShortDescr));
  993. if Assigned(DocNode) and not IsDescrNodeEmpty(DocNode.Descr) then
  994. begin
  995. StartSection(SManDocDescription);
  996. WriteDescr(AModule.Parent, DocNode.Descr);
  997. end;
  998. WriteUnitUsesOverview(AModule.InterfaceSection);
  999. WriteUnitClassesOverView(AModule.InterfaceSection);
  1000. WriteUnitFunctionsAndProceduresOverView(AModule.InterfaceSection);
  1001. StartSection(SManDocSeealso);
  1002. WriteModuleSeeAlso(False);
  1003. finally
  1004. EndManPage;
  1005. end;
  1006. end;
  1007. procedure TManWriter.WriteUnitUsesOverview(ASection: TPasSection);
  1008. var
  1009. i: Integer;
  1010. UnitRef: TPasType;
  1011. DocNode: TDocNode;
  1012. begin
  1013. if ASection.UsesList.Count > 0 then
  1014. begin
  1015. StartSection(SManDocUsedUnits);
  1016. for i := 0 to ASection.UsesList.Count - 1 do
  1017. begin
  1018. UnitRef := TPasType(ASection.UsesList[i]);
  1019. WriteTP;
  1020. WriteB(UnitRef.Name);
  1021. DocNode := Engine.FindDocNode(UnitRef);
  1022. If Assigned(DocNode) then
  1023. WriteDescr(UnitRef,DocNode.ShortDescr)
  1024. end;
  1025. end;
  1026. end;
  1027. { ---------------------------------------------------------------------
  1028. Classes man pages
  1029. ---------------------------------------------------------------------}
  1030. procedure TManWriter.WriteUnitClassesOverview(ASection: TPasSection);
  1031. var
  1032. i : Integer;
  1033. DocNode : TDocNode;
  1034. ClassDecl : TPasClassType;
  1035. begin
  1036. // Overview page
  1037. if ASection.Classes.Count > 0 then
  1038. begin
  1039. StartSection(SManDocClasses);
  1040. for i := 0 to ASection.Classes.Count - 1 do
  1041. begin
  1042. ClassDecl:=TPasClassType(ASection.Classes[i]);
  1043. WriteTP;
  1044. WriteB(ClassDecl.Name);
  1045. DocNode:=Engine.FindDocNode(ClassDecl);
  1046. If Assigned(DocNode) then
  1047. WriteDescr(ClassDecl,DocNode.ShortDescr);
  1048. end;
  1049. end;
  1050. end;
  1051. procedure TManWriter.WriteUnitClasses(ASection: TPasSection);
  1052. var
  1053. i: Integer;
  1054. begin
  1055. if (ASection.Classes.Count > 0) then
  1056. begin
  1057. for i := 0 to ASection.Classes.Count - 1 do
  1058. WriteClassPage(TPasClassType(ASection.Classes[i]));
  1059. end;
  1060. end;
  1061. procedure TManWriter.WriteClassPage(ClassDecl: TPasClassType);
  1062. var
  1063. DocNode: TDocNode;
  1064. begin
  1065. DocNode:=Engine.FindDocNode(ClassDecl);
  1066. StartManPage(ClassDecl,DocNode);
  1067. Try
  1068. PageTitle(ClassDecl.Name,ManSection,PackageName,PackageDescr);
  1069. StartSection(SManDocName);
  1070. Write(ClassDecl.Name);
  1071. DocNode := Engine.FindDocNode(ClassDecl);
  1072. If Assigned(DocNode) then
  1073. begin
  1074. if not IsDescrNodeEmpty(DocNode.ShortDescr) then
  1075. begin
  1076. write(' \- ');
  1077. WriteDescr(ClassDecl,DocNode.ShortDescr);
  1078. end;
  1079. if Not (IsDescrNodeEmpty(DocNode.Descr)
  1080. and IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1081. begin
  1082. StartSection(SDocDescription);
  1083. WriteDescr(ClassDecl);
  1084. end;
  1085. end;
  1086. // Write method overview
  1087. WriteClassMethodOverView(ClassDecl);
  1088. // Write Property Overview;
  1089. WriteClassPropertyOverView(ClassDecl);
  1090. Finally
  1091. EndManPage;
  1092. end
  1093. end;
  1094. procedure TManWriter.WriteClassPropertyOverview(ClassDecl : TPasClassType);
  1095. var
  1096. Member: TPasElement;
  1097. i: Integer;
  1098. A: String;
  1099. DocNode: TDocNode;
  1100. List : TStringList;
  1101. begin
  1102. // Write property overview
  1103. List:=TStringList.Create;
  1104. Try
  1105. for i := 0 to ClassDecl.Members.Count - 1 do
  1106. begin
  1107. Member := TPasElement(ClassDecl.Members[i]);
  1108. With Member do
  1109. if InheritsFrom(TPasProperty) and SHowMember(Member) then
  1110. List.AddObject(Member.Name,Member)
  1111. end;
  1112. List.Sorted:=True;
  1113. If (List.Count>0) then
  1114. begin
  1115. StartSection(SDocPropertyOverview);
  1116. For I:=0 to List.Count-1 do
  1117. begin
  1118. Member:=TPasElement(List.objects[i]);
  1119. WriteTP;
  1120. A:='';
  1121. if Length(TPasProperty(Member).ReadAccessorName) > 0 then
  1122. a := a + 'r';
  1123. if Length(TPasProperty(Member).WriteAccessorName) > 0 then
  1124. a := a + 'w';
  1125. if Length(TPasProperty(Member).StoredAccessorName) > 0 then
  1126. a := a + 's';
  1127. WriteBI(Member.Name+' '+A);
  1128. DocNode := Engine.FindDocNode(Member);
  1129. If Assigned(DocNode) then
  1130. WriteDescr(Member, DocNode.ShortDescr)
  1131. end;
  1132. end;
  1133. Finally
  1134. List.Free;
  1135. end;
  1136. end;
  1137. { ---------------------------------------------------------------------
  1138. Resource strings man page
  1139. ---------------------------------------------------------------------}
  1140. procedure TManWriter.WriteUnitResourceStrings(ASection: TPasSection);
  1141. var
  1142. ResStrDecl: TPasResString;
  1143. i: Integer;
  1144. begin
  1145. if ASection.ResStrings.Count > 0 then
  1146. begin
  1147. StartManpage(ModuleName+'.'+SManResStr);
  1148. Try
  1149. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1150. StartSection(SManDocName);
  1151. Writeln(ModuleName+' \- '+SDocResStrings);
  1152. StartSection(SManDocResourceStrings);
  1153. Writeln('');
  1154. for i := 0 to ASection.ResStrings.Count - 1 do
  1155. begin
  1156. ResStrDecl := TPasResString(ASection.ResStrings[i]);
  1157. StartSubSection(ResStrDecl.Name);
  1158. DescrWriteCodeLine(EscapeText(ResStrDecl.GetDeclaration(False)));
  1159. end;
  1160. StartSection(SDocSeeAlso);
  1161. WriteModuleSeealso(False);
  1162. Finally
  1163. EndManPage;
  1164. end;
  1165. end;
  1166. end;
  1167. { ---------------------------------------------------------------------
  1168. Constants man page
  1169. ---------------------------------------------------------------------}
  1170. procedure TManWriter.WriteUnitConsts(ASection: TPasSection);
  1171. var
  1172. i: Integer;
  1173. ConstDecl: TPasConst;
  1174. DocNode: TDocNode;
  1175. begin
  1176. if ASection.Consts.Count > 0 then
  1177. begin
  1178. StartManpage(ModuleName+'.'+SManConsts);
  1179. Try
  1180. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1181. StartSection(SManDocName);
  1182. Writeln(ModuleName+' \- '+SDocConstants);
  1183. StartSection(SManDocConstants);
  1184. for i := 0 to ASection.Consts.Count - 1 do
  1185. begin
  1186. ConstDecl := TPasConst(ASection.Consts[i]);
  1187. StartSubSection(ConstDecl.Name);
  1188. DescrWriteCodeLine(EscapeText(ConstDecl.GetDeclaration(True)));
  1189. DocNode:=Engine.FindDocNode(ConstDecl);
  1190. WriteDescr(ConstDecl,DocNode);
  1191. end;
  1192. StartSection(SDocSeeAlso);
  1193. WriteModuleSeealso(False);
  1194. Finally
  1195. EndManPage;
  1196. end;
  1197. end;
  1198. end;
  1199. { ---------------------------------------------------------------------
  1200. Types man page
  1201. ---------------------------------------------------------------------}
  1202. procedure TManWriter.WriteUnitTypes(ASection: TPasSection);
  1203. var
  1204. i: Integer;
  1205. TypeDecl: TPasType;
  1206. DocNode : TDocNode;
  1207. begin
  1208. if ASection.Types.Count > 0 then
  1209. begin
  1210. StartManpage(ModuleName+'.'+SManTypes);
  1211. Try
  1212. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1213. StartSection(SManDocName);
  1214. Writeln(ModuleName+' \- '+SDocTypes);
  1215. StartSection(SManDocTypes);
  1216. for i := 0 to ASection.Types.Count - 1 do
  1217. begin
  1218. TypeDecl := TPasType(ASection.Types[i]);
  1219. StartSubsection(TypeDecl.Name);
  1220. DescrWriteCodeLine(EscapeText(TypeDecl.GetDeclaration(True)));
  1221. DocNode:=Engine.FindDocNode(TypeDecl);
  1222. If TypeDecl is TPasEnumType then
  1223. WriteEnumElements(TypeDecl as TPasEnumType);
  1224. WriteDescr(TypeDecl,DocNode);
  1225. end;
  1226. StartSection(SDocSeeAlso);
  1227. WriteModuleSeeAlso(False);
  1228. Finally
  1229. EndManPage;
  1230. end;
  1231. end;
  1232. end;
  1233. procedure TManWriter.WriteEnumElements(TypeDecl : TPasEnumType);
  1234. Var
  1235. EV : TPasEnumValue;
  1236. I : Integer;
  1237. DocNode : TDocNode;
  1238. begin
  1239. With TypeDecl do
  1240. begin
  1241. SortElementList(Values);
  1242. Writeln(EscapeText(Format(SDocValuesForEnum,[TypeDecl.Name])));
  1243. Try
  1244. For I:=0 to Values.Count-1 do
  1245. begin
  1246. EV:=TPasEnumValue(Values[i]);
  1247. WriteTP;
  1248. WriteB(EscapeText(EV.Name));
  1249. DocNode := Engine.FindDocNode(EV);
  1250. if Assigned(DocNode) and (not IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1251. WriteDescr(EV,DocNode.ShortDescr);
  1252. end;
  1253. Finally
  1254. NewLine;
  1255. end;
  1256. end;
  1257. end;
  1258. { ---------------------------------------------------------------------
  1259. Variables man page
  1260. ---------------------------------------------------------------------}
  1261. procedure TManWriter.WriteUnitVars(ASection: TPasSection);
  1262. var
  1263. VarDecl: TPasVariable;
  1264. i: Integer;
  1265. DocNode : TDocNode;
  1266. begin
  1267. if ASection.Variables.Count > 0 then
  1268. begin
  1269. StartManpage(ModuleName+'.'+SManVars);
  1270. Try
  1271. PageTitle(Modulename,ManSection,PackageName,PackageDescr);
  1272. StartSection(SManDocName);
  1273. Writeln(ModuleName+' \- '+SDocVariables);
  1274. StartSection(SManDocVariables);
  1275. for i := 0 to ASection.Variables.Count - 1 do
  1276. begin
  1277. VarDecl := TPasVariable(ASection.Variables[i]);
  1278. StartSubSection(VarDecl.Name);
  1279. DescrWriteCodeLine(EscapeText(VarDecl.GetDeclaration(True)));
  1280. DocNode:=Engine.FindDocNode(VarDecl);
  1281. WriteDescr(VarDecl,DocNode);
  1282. end;
  1283. StartSection(SDocSeeAlso);
  1284. WriteModuleSeeAlso(False);
  1285. Finally
  1286. EndManPage;
  1287. end;
  1288. end;
  1289. end;
  1290. { ---------------------------------------------------------------------
  1291. Procedure/Function/Method man page
  1292. ---------------------------------------------------------------------}
  1293. procedure TManWriter.WriteUnitFunctionsAndProceduresOverview(ASection: TPasSection);
  1294. var
  1295. i : Integer;
  1296. DocNode : TDocNode;
  1297. PDecl : TPasProcedureBase;
  1298. begin
  1299. // Overview page
  1300. if ASection.Functions.Count > 0 then
  1301. begin
  1302. StartSection(SManDocFunctions);
  1303. for i := 0 to ASection.Functions.Count - 1 do
  1304. begin
  1305. PDecl:=TPasProcedureBase(ASection.Functions[i]);
  1306. WriteTP;
  1307. WriteB(PDecl.Name);
  1308. DocNode:=Engine.FindDocNode(PDecl);
  1309. If Assigned(DocNode) then
  1310. WriteDescr(PDecl,DocNode.ShortDescr);
  1311. end;
  1312. end;
  1313. end;
  1314. procedure TManWriter.WriteUnitFunctionsAndProcedures(ASection: TPasSection);
  1315. var
  1316. i : Integer;
  1317. begin
  1318. // Pages for all identifiers.
  1319. for i := 0 to ASection.Functions.Count - 1 do
  1320. WriteProcedurePage(TPasProcedure(ASection.Functions[i]));
  1321. end;
  1322. procedure TManWriter.WriteProcedurePage(ProcDecl : TPasProcedureBase);
  1323. var
  1324. DocNode: TDocNode;
  1325. OP : TPasOverloadedProc;
  1326. i : integer;
  1327. D,N : String;
  1328. begin
  1329. DocNode := Engine.FindDocNode(ProcDecl);
  1330. StartManpage(ProcDecl,DocNode);
  1331. Try
  1332. PageTitle(ProcDecl.name,ManSection,PackageName,PackageDescr);
  1333. if Assigned(DocNode) then
  1334. D:=GetDescrString(ProcDecl,DocNode.ShortDescr);
  1335. // Name
  1336. StartSection(SManDocName);
  1337. Writeln(N+' \- '+D);
  1338. // Declaration
  1339. StartSection(SManDocSynopsis);
  1340. if ProcDecl is TPasOverloadedProc then
  1341. begin
  1342. OP:=TPasOverloadedProc(ProcDecl);
  1343. for i := 0 to OP.Overloads.Count - 1 do
  1344. begin
  1345. DescrWriteCodeLine(TPasProcedure(OP.Overloads[i]).GetDeclaration(True));
  1346. end;
  1347. end
  1348. else
  1349. DescrWriteCodeLine(ProcDecl.GetDeclaration(True));
  1350. // Visibility
  1351. If Assigned(ProcDecl.Parent) then
  1352. begin
  1353. StartSection(SManDocVisibility);
  1354. Writeln(VisibilityNames[ProcDecl.Visibility])
  1355. end;
  1356. // Arguments, if present.
  1357. If ProcDecl is TPasProcedure then
  1358. AppendProcedureArgsSection(TPasProcedure(ProcDecl).ProcType);
  1359. // Description
  1360. if Assigned(DocNode) then
  1361. begin
  1362. if Assigned(DocNode.Descr) then
  1363. begin
  1364. StartSection(SManDocDescription);
  1365. WriteDescr(ProcDecl,DocNode.Descr);
  1366. end;
  1367. // Errors
  1368. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  1369. begin
  1370. StartSection(SManDocErrors);
  1371. WriteDescr(ProcDecl, DocNode.ErrorsDoc);
  1372. end;
  1373. end;
  1374. // Arguments, if present.
  1375. If ProcDecl is TPasFunction then
  1376. AppendProcedureArgsSection(TPasFunction(ProcDecl).ProcType);
  1377. WriteExample(DocNode);
  1378. StartSection(SManDocSeeAlso);
  1379. WriteModuleSeeAlso(True);
  1380. WriteSeeAlso(DocNode,False);
  1381. Finally
  1382. EndManPage;
  1383. end;
  1384. end;
  1385. procedure TManWriter.AppendProcedureArgsSection(Element: TPasProcedureType);
  1386. var
  1387. IsFirst: Boolean;
  1388. DocNode: TDocNode;
  1389. i: Integer;
  1390. Arg: TPasArgument;
  1391. begin
  1392. If Not Assigned(Element) then
  1393. exit;
  1394. IsFirst := True;
  1395. for i := 0 to Element.Args.Count - 1 do
  1396. begin
  1397. Arg := TPasArgument(Element.Args[i]);
  1398. DocNode:=Engine.FindDocNode(Arg);
  1399. if Assigned(DocNode) and (Not IsDescrNodeEmpty(DocNode.ShortDescr)) then
  1400. begin
  1401. if IsFirst then
  1402. begin
  1403. IsFirst:=False;
  1404. StartSection(SManDocArguments);
  1405. end;
  1406. WriteTP;
  1407. WriteB(Arg.Name);
  1408. WriteDescr(Arg,DocNode.ShortDescr);
  1409. end;
  1410. end;
  1411. end;
  1412. procedure TManWriter.AppendFunctionResultSection(Element: TPasFunctionType);
  1413. Var
  1414. ResultEl: TPasResultElement;
  1415. DocNode: TDocNode;
  1416. begin
  1417. If Not Assigned(Element) then
  1418. exit;
  1419. ResultEl := TPasFunctionType(Element).ResultEl;
  1420. DocNode := Engine.FindDocNode(ResultEl);
  1421. If Assigned(DocNode) then
  1422. begin
  1423. if IsDescrNodeEmpty(DocNode.Descr) or not IsDescrNodeEmpty(DocNode.ShortDescr) then
  1424. begin
  1425. StartSection(SManDocResult);
  1426. WriteDescr(ResultEl,DocNode);
  1427. end;
  1428. end;
  1429. end;
  1430. { ---------------------------------------------------------------------
  1431. Property man page
  1432. ---------------------------------------------------------------------}
  1433. procedure TManWriter.WritePropertyPage(PropDecl : TPasProperty);
  1434. var
  1435. DocNode: TDocNode;
  1436. N,D: String;
  1437. begin
  1438. DocNode := Engine.FindDocNode(PropDecl);
  1439. StartManpage(PropDecl,DocNode);
  1440. Try
  1441. PageTitle(PropDecl.Name,ManSection,PackageName,PackageDescr);
  1442. if Assigned(DocNode) then
  1443. D:=GetDescrString(PropDecl,DocNode.ShortDescr);
  1444. // Name
  1445. StartSection(SManDocName);
  1446. Writeln(N+' \- '+D);
  1447. // Declaration
  1448. StartSection(SManDocSynopsis);
  1449. WriteLn(PropDecl.GetDeclaration(True));
  1450. // Visibility
  1451. If Assigned(PropDecl.Parent) then
  1452. begin
  1453. StartSection(SManDocVisibility);
  1454. Writeln(VisibilityNames[PropDecl.Visibility])
  1455. end;
  1456. StartSection(SManDocAccess);
  1457. D:='';
  1458. If Length(PropDecl.ReadAccessorName) > 0 then
  1459. D:='Read';
  1460. if Length(PropDecl.WriteAccessorName) > 0 then
  1461. begin
  1462. If D<>'' then
  1463. D:=D+',';
  1464. D:=D+'Write';
  1465. end;
  1466. Writeln(D);
  1467. if Assigned(DocNode) then
  1468. begin
  1469. // Description
  1470. if Assigned(DocNode.Descr) then
  1471. begin
  1472. StartSection(SManDocDescription);
  1473. WriteDescr(PropDecl,DocNode.Descr);
  1474. end;
  1475. // Errors
  1476. if Assigned(DocNode) and Assigned(DocNode.ErrorsDoc) then
  1477. begin
  1478. StartSection(SManDocErrors);
  1479. WriteDescr(PropDecl, DocNode.ErrorsDoc);
  1480. end;
  1481. WriteExample(DocNode);
  1482. WriteSeeAlso(DocNode,False);
  1483. end;
  1484. Finally
  1485. EndManPage;
  1486. end;
  1487. end;
  1488. Function CompareElements(P1,P2 : Pointer) : Integer;
  1489. begin
  1490. Result:=CompareText(TPasElement(P1).Name,TPasElement(P2).Name);
  1491. end;
  1492. procedure TManWriter.SortElementList(List : TList);
  1493. begin
  1494. List.Sort(@CompareElements);
  1495. end;
  1496. procedure TManWriter.WriteCommentLine;
  1497. begin
  1498. WriteComment('-------------------------------------------------------');
  1499. end;
  1500. procedure TManWriter.WriteComment(Comment : String);
  1501. begin
  1502. Writeln('." '+Comment);
  1503. end;
  1504. class function TManWriter.FileNameExtension: String;
  1505. begin
  1506. Result:=IntToStr(DefaultManSection);
  1507. end;
  1508. procedure TManWriter.WriteClassMethodOverview(ClassDecl: TPasClassType);
  1509. var
  1510. Member : TPasElement;
  1511. i : Integer;
  1512. DocNode : TDocNode;
  1513. List : TStringList;
  1514. begin
  1515. List:=TStringList.Create;
  1516. Try
  1517. GetMethodList(ClassDecl,List);
  1518. If List.Count>0 then
  1519. begin
  1520. StartSection(SManDocMethods);
  1521. For I:=0 to List.Count-1 do
  1522. begin
  1523. Member:=TPasElement(List.Objects[i]);
  1524. WriteTP;
  1525. WriteB(EscapeText(Member.Name));
  1526. DocNode := Engine.FindDocNode(Member);
  1527. If Assigned(DocNode) then
  1528. WriteDescr(Member, DocNode.ShortDescr)
  1529. end;
  1530. end;
  1531. Finally
  1532. List.Free;
  1533. end;
  1534. end;
  1535. Class procedure TManWriter.Usage(List: TStrings);
  1536. begin
  1537. List.add('--nounitprefix');
  1538. List.Add(SManUsageNoUnitPrefix);
  1539. List.add('--man-section=ASection');
  1540. List.Add(SManUsageManSection);
  1541. List.Add('--man-description=descr');
  1542. List.Add(SManUsagePackageDescription);
  1543. end;
  1544. initialization
  1545. // Do not localize.
  1546. RegisterWriter(TMANWriter,'man',SManUsageWriterDescr);
  1547. finalization
  1548. UnRegisterWriter('man');
  1549. end.