2
0

nroff2cd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 converts an nroff file to curldown
  27. Example: cd2nroff [options] <file.md> > <file.3>
  28. Note: when converting .nf sections, this tool does not know if the
  29. section is code or just regular quotes. It then assumes and uses ~~~c
  30. for code.
  31. =end comment
  32. =cut
  33. my $nroff2cd = "0.1"; # to keep check
  34. sub single {
  35. my ($f)=@_;
  36. open(F, "<:crlf", "$f") ||
  37. return 1;
  38. my $line;
  39. my $title;
  40. my $section;
  41. my $source;
  42. my @seealso;
  43. my @desc;
  44. my $header; # non-zero when TH is passed
  45. my $quote = 0; # quote state
  46. while(<F>) {
  47. $line++;
  48. my $d = $_;
  49. if($_ =~ /^.\\\"/) {
  50. # a comment we can ignore
  51. next;
  52. }
  53. if(!$header) {
  54. if($d =~ /.so (.*)/) {
  55. # this is basically an include, so do that
  56. my $f = $1;
  57. # remove leading directory
  58. $f =~ s/(.*?\/)//;
  59. close(F);
  60. open(F, "<:crlf", "$f") || return 1;
  61. }
  62. if($d =~ /^\.TH ([^ ]*) (\d) \"(.*?)\" ([^ \n]*)/) {
  63. # header, this needs to be the first thing after leading comments
  64. $title = $1;
  65. $section = $2;
  66. # date is $3
  67. $source = $4;
  68. # if there are enclosing quotes around source, remove them
  69. $source =~ s/[\"\'](.*)[\"\']\z/$1/;
  70. $header = 1;
  71. print <<HEAD
  72. ---
  73. c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  74. SPDX-License-Identifier: curl
  75. Title: $title
  76. Section: $section
  77. Source: $source
  78. HEAD
  79. ;
  80. }
  81. next;
  82. }
  83. if($quote) {
  84. if($d =~ /^\.SH/) {
  85. #end of quote without an .fi
  86. $quote = 0;
  87. push @desc, "~~~\n";
  88. }
  89. elsif($d =~ /^\.fi/) {
  90. #end of quote
  91. $quote = 0;
  92. push @desc, "~~~\n";
  93. next;
  94. }
  95. else {
  96. # double-backslashes converted to single ones
  97. $d =~ s/\\\\/\\/g;
  98. push @desc, $d;
  99. next;
  100. }
  101. }
  102. if($d =~ /^\.SH (.*)/) {
  103. my $word = $1;
  104. # if there are enclosing quotes, remove them first
  105. $word =~ s/[\"\'](.*)[\"\']\z/$1/;
  106. if($word eq "SEE ALSO") {
  107. # we just slurp up this section
  108. next;
  109. }
  110. push @desc, "\n# $word\n\n";
  111. }
  112. elsif($d =~ /^\.(RS|RE)/) {
  113. # ignore these
  114. }
  115. elsif($d =~ /^\.IP (.*)/) {
  116. my $word = $1;
  117. # if there are enclosing quotes, remove them first
  118. $word =~ s/[\"\'](.*)[\"\']\z/$1/;
  119. push @desc, "\n## $word\n\n";
  120. }
  121. elsif($d =~ /^\.IP/) {
  122. # .IP with no text we just skip
  123. }
  124. elsif($d =~ /^\.BR (.*)/) {
  125. # only used for SEE ALSO
  126. my $word = $1;
  127. # remove trailing comma
  128. $word =~ s/,\z//;
  129. for my $s (split(/,/, $word)) {
  130. # remove all double quotes
  131. $s =~ s/\"//g;
  132. # tream leading whitespace
  133. $s =~ s/^ +//g;
  134. push @seealso, $s;
  135. }
  136. }
  137. elsif($d =~ /^\.I (.*)/) {
  138. push @desc, "*$1*\n";
  139. }
  140. elsif($d =~ /^\.B (.*)/) {
  141. push @desc, "**$1**\n";
  142. }
  143. elsif($d =~ /^\.nf/) {
  144. push @desc, "~~~c\n";
  145. $quote = 1;
  146. }
  147. else {
  148. # embolden
  149. $d =~ s/\\fB(.*?)\\fP/**$1**/g;
  150. # links to "curl.*()" are left bare since cd2nroff handles them
  151. # specially
  152. $d =~ s/\\fI(curl.*?\(3\))\\fP/$1/ig;
  153. # emphasize
  154. $d =~ s/\\fI(.*?)\\fP/*$1*/g;
  155. # emphasize on a split line
  156. $d =~ s/\\fI/*/g;
  157. # bold on a split line
  158. $d =~ s/\\fB/**/g;
  159. # remove backslash amp
  160. $d =~ s/\\&//g;
  161. # remove backslashes
  162. $d =~ s/\\//g;
  163. # fix single quotes
  164. $d =~ s/\(aq/'/g;
  165. # fix double quotes
  166. $d =~ s/\(dq/\"/g;
  167. push @desc, $d;
  168. }
  169. }
  170. close(F);
  171. print "See-also:\n";
  172. for my $s (sort @seealso) {
  173. print " - $s\n" if($s);
  174. }
  175. print "---\n";
  176. print @desc;
  177. return !$header;
  178. }
  179. exit single($ARGV[0]);