fpdocstripper.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. program fpdocstripper;
  2. {
  3. fpdocstripper - Free Pascal fpdoc file stripper
  4. Copyright (C) 2012-2013 by Reinier Olislagers
  5. * Takes an FPDoc XML file and removes all elements that have no documentation in them
  6. * Useful before submitting a documentation patch as it keeps file size down and
  7. makes it clearer what exactly is documented.
  8. See the file COPYING, included in this distribution,
  9. for details about the copyright and license.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. To do: currently parses the raw XML; it may be possible to reuse the fpdoc format
  14. reading code in other units.
  15. }
  16. {$mode objfpc}{$H+}
  17. uses
  18. {$IFDEF UNIX}
  19. cwstring,
  20. {$ENDIF}
  21. Classes, SysUtils, CustApp,
  22. DOM, xmlread, xmlwrite;
  23. type
  24. { TFPDocStripper }
  25. TFPDocStripper = class(TCustomApplication)
  26. protected
  27. FInputFile: string;
  28. FOutputFile: string;
  29. FStripComments : Boolean;
  30. procedure DoRun; override;
  31. public
  32. constructor Create(TheOwner: TComponent); override;
  33. destructor Destroy; override;
  34. procedure StripEmptyXML(Node:TDOMNode);
  35. procedure WriteHelp; virtual;
  36. Property StripComments : Boolean Read FStripComments Write FStripComments;
  37. end;
  38. procedure TFPDocStripper.StripEmptyXML(Node:TDOMNode);
  39. // Recursive function to process a node and all its child nodes
  40. var
  41. i: integer;
  42. E : TDomElement;
  43. CN : TDomNode;
  44. B : Boolean;
  45. begin
  46. // Exit procedure if no more nodes to process
  47. if Node = nil then Exit;
  48. for i:=Node.ChildNodes.Count-1 downto 0 do
  49. begin
  50. StripEmptyXML(Node.ChildNodes[i]);
  51. end;
  52. for i:=Node.ChildNodes.Count-1 downto 0 do
  53. begin
  54. CN:=Node.ChildNodes[i];
  55. // Remove all comments
  56. B:=StripComments and (CN.NodeType=COMMENT_NODE);
  57. if not B then
  58. begin
  59. // Remove children without children or attributes
  60. B:=(CN.HasChildNodes=false) and
  61. (CN.HasAttributes=false) and
  62. (CN.TextContent='');
  63. // Empty elements that do not link to others
  64. if not B then
  65. begin
  66. if (CN is TDomElement) then
  67. begin
  68. E:=CN as TDomElement;
  69. B:=(E.NodeName='element')
  70. and (E.HasChildNodes=false)
  71. and (E['name']<>'') and (E['link']='');
  72. end;
  73. end;
  74. end;
  75. if B then
  76. Node.RemoveChild(CN);
  77. end;
  78. end;
  79. { TFPDocStripper }
  80. procedure TFPDocStripper.DoRun;
  81. var
  82. ErrorMsg: String;
  83. Doc: TXMLDocument;
  84. begin
  85. // check parameters
  86. ErrorMsg:=CheckOptions('h','help input: output: keepcomments');
  87. if ErrorMsg<>'' then begin
  88. writeln(ErrorMsg);
  89. writeln();
  90. Terminate;
  91. Exit;
  92. end;
  93. // parse parameters
  94. if HasOption('h','help') then begin
  95. WriteHelp;
  96. Terminate;
  97. Exit;
  98. end;
  99. if HasOption('input') then begin
  100. FInputFile:=ExpandFileName(GetOptionValue('input'));
  101. end else begin
  102. writeln('Error: no input file specified.');
  103. writeln();
  104. WriteHelp;
  105. Terminate;
  106. Exit;
  107. end;
  108. FStripComments:=not HasOption('keepcomments');
  109. if HasOption('output') then begin
  110. FOutputFile:=ExpandFileName(GetOptionValue('output'));
  111. end else begin
  112. writeln('Error: no output file specified.');
  113. writeln();
  114. WriteHelp;
  115. Terminate;
  116. Exit;
  117. end;
  118. if FInputFile=FOutputfile then
  119. raise Exception.CreateFmt('Input file %s must not be the same as output file.',[FInputFile]);
  120. if fileexists(FInputFile)=false then
  121. raise Exception.CreateFmt('Input file %s does not exist.',[FInputFile]);
  122. try
  123. ReadXMLFile(Doc,FInputFile);
  124. StripEmptyXML(Doc.DocumentElement);
  125. WriteXMLFile(Doc,FOutputFile);
  126. finally
  127. Doc.Free;
  128. end;
  129. Terminate;
  130. end;
  131. constructor TFPDocStripper.Create(TheOwner: TComponent);
  132. begin
  133. inherited Create(TheOwner);
  134. StopOnException:=True;
  135. end;
  136. destructor TFPDocStripper.Destroy;
  137. begin
  138. inherited Destroy;
  139. end;
  140. procedure TFPDocStripper.WriteHelp;
  141. begin
  142. writeln('Strips undocumented elements and comments');
  143. writeln('from an fpdoc XML (description/documentation) file.');
  144. writeln('');
  145. writeln('Useful before submitting a documentation patch as');
  146. writeln('it keeps file size down and makes it clear what exactly');
  147. writeln('is documented.');
  148. writeln('');
  149. writeln('Usage: ',ExeName,' -h');
  150. writeln('--keepcomments');
  151. writeln(' Do not strip comments');
  152. writeln('--input=file');
  153. writeln(' Read specified fpdoc XML file.');
  154. writeln('--output=file');
  155. writeln(' Write cleaned output to this file.');
  156. end;
  157. var
  158. Application: TFPDocStripper;
  159. begin
  160. Application:=TFPDocStripper.Create(nil);
  161. Application.Run;
  162. Application.Free;
  163. end.