2
0

cd2cd 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. =begin comment
  26. This script updates a curldown file to current/better curldown.
  27. Example: cd2cd [--in-place] <file.md> > <file.md>
  28. --in-place: if used, it replaces the original file with the cleaned up
  29. version. When this is used, cd2cd accepts multiple files to work
  30. on and it ignores errors on single files.
  31. =end comment
  32. =cut
  33. my $cd2cd = "0.1"; # to keep check
  34. my $dir;
  35. my $extension;
  36. my $inplace = 0;
  37. while(1) {
  38. if($ARGV[0] eq "--in-place") {
  39. shift @ARGV;
  40. $inplace = 1;
  41. }
  42. else {
  43. last;
  44. }
  45. }
  46. use POSIX qw(strftime);
  47. my @ts;
  48. if (defined($ENV{SOURCE_DATE_EPOCH})) {
  49. @ts = localtime($ENV{SOURCE_DATE_EPOCH});
  50. } else {
  51. @ts = localtime;
  52. }
  53. my $date = strftime "%B %d %Y", @ts;
  54. sub outseealso {
  55. my (@sa) = @_;
  56. my $comma = 0;
  57. my @o;
  58. push @o, ".SH SEE ALSO\n";
  59. for my $s (sort @sa) {
  60. push @o, sprintf "%s.BR $s", $comma ? ",\n": "";
  61. $comma = 1;
  62. }
  63. push @o, "\n";
  64. return @o;
  65. }
  66. sub single {
  67. my @head;
  68. my @seealso;
  69. my ($f)=@_;
  70. my $title;
  71. my $section;
  72. my $source;
  73. my $start = 0;
  74. my $d;
  75. my $line = 0;
  76. open(F, "<:crlf", "$f") ||
  77. return 1;
  78. while(<F>) {
  79. $line++;
  80. $d = $_;
  81. if(!$start) {
  82. if(/^---/) {
  83. # header starts here
  84. $start = 1;
  85. push @head, $d;
  86. }
  87. next;
  88. }
  89. if(/^Title: *(.*)/i) {
  90. $title=$1;
  91. }
  92. elsif(/^Section: *(.*)/i) {
  93. $section=$1;
  94. }
  95. elsif(/^Source: *(.*)/i) {
  96. $source=$1;
  97. }
  98. elsif(/^See-also: +(.*)/i) {
  99. $salist = 0;
  100. push @seealso, $1;
  101. }
  102. elsif(/^See-also: */i) {
  103. if($seealso[0]) {
  104. print STDERR "$f:$line:1:ERROR: bad See-Also, needs list\n";
  105. return 2;
  106. }
  107. $salist = 1;
  108. }
  109. elsif(/^ +- (.*)/i) {
  110. # the only list we support is the see-also
  111. if($salist) {
  112. push @seealso, $1;
  113. }
  114. }
  115. # REUSE-IgnoreStart
  116. elsif(/^C: (.*)/i) {
  117. $copyright=$1;
  118. }
  119. elsif(/^SPDX-License-Identifier: (.*)/i) {
  120. $spdx=$1;
  121. }
  122. # REUSE-IgnoreEnd
  123. elsif(/^---/) {
  124. # end of the header section
  125. if(!$title) {
  126. print STDERR "ERROR: no 'Title:' in $f\n";
  127. return 1;
  128. }
  129. if(!$section) {
  130. print STDERR "ERROR: no 'Section:' in $f\n";
  131. return 2;
  132. }
  133. if(!$seealso[0]) {
  134. print STDERR "$f:$line:1:ERROR: no 'See-also:' present\n";
  135. return 2;
  136. }
  137. if(!$copyright) {
  138. print STDERR "$f:$line:1:ERROR: no 'C:' field present\n";
  139. return 2;
  140. }
  141. if(!$spdx) {
  142. print STDERR "$f:$line:1:ERROR: no 'SPDX-License-Identifier:' field present\n";
  143. return 2;
  144. }
  145. last;
  146. }
  147. else {
  148. chomp;
  149. print STDERR "WARN: unrecognized line in $f, ignoring:\n:'$_';"
  150. }
  151. }
  152. if(!$start) {
  153. print STDERR "$f:$line:1:ERROR: no header present\n";
  154. return 2;
  155. }
  156. my @desc;
  157. push @desc, sprintf <<HEAD
  158. ---
  159. c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  160. SPDX-License-Identifier: curl
  161. Title: $title
  162. Section: $section
  163. Source: $source
  164. HEAD
  165. ;
  166. push @desc, "See-also:\n";
  167. for my $s (sort @seealso) {
  168. push @desc, " - $s\n" if($s);
  169. }
  170. push @desc, "---\n";
  171. my $blankline = 0;
  172. while(<F>) {
  173. $d = $_;
  174. $line++;
  175. if($d =~ /^[ \t]*\n/) {
  176. $blankline++;
  177. }
  178. else {
  179. $blankline = 0;
  180. }
  181. # *italics* for curl symbol links get the asterisks removed
  182. $d =~ s/\*((lib|)curl[^ ]*\(3\))\*/$1/gi;
  183. if(length($d) > 90) {
  184. print STDERR "$f:$line:1:WARN: excessive line length\n";
  185. }
  186. push @desc, $d if($blankline < 2);
  187. }
  188. close(F);
  189. if($inplace) {
  190. open(O, ">$f") || return 1;
  191. print O @desc;
  192. close(O);
  193. }
  194. else {
  195. print @desc;
  196. }
  197. return 0;
  198. }
  199. if($inplace) {
  200. for my $a (@ARGV) {
  201. # this ignores errors
  202. single($a);
  203. }
  204. }
  205. else {
  206. exit single($ARGV[0]);
  207. }