dw_man.pp 44 KB

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