vim2html.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/bin/env perl
  2. # converts vim documentation to simple html
  3. # Sirtaj Singh Kang (taj@kde.org)
  4. # Sun Feb 24 14:49:17 CET 2002
  5. use strict;
  6. use vars qw/%url $date/;
  7. %url = ();
  8. $date = `date`;
  9. chop $date;
  10. sub maplink
  11. {
  12. my $tag = shift;
  13. if( exists $url{ $tag } ){
  14. return $url{ $tag };
  15. } else {
  16. #warn "Unknown hyperlink target: $tag\n";
  17. $tag =~ s/\.txt//;
  18. $tag =~ s/</&lt;/g;
  19. $tag =~ s/>/&gt;/g;
  20. return "<code class=\"badlink\">$tag</code>";
  21. }
  22. }
  23. sub readTagFile
  24. {
  25. my($tagfile) = @_;
  26. my( $tag, $file, $name );
  27. open(TAGS,"$tagfile") || die "can't read tags\n";
  28. while( <TAGS> ) {
  29. next unless /^(\S+)\s+(\S+)\s+/;
  30. $tag = $1;
  31. my $label = $tag;
  32. ($file= $2) =~ s/.txt$/.html/g;
  33. $label =~ s/\.txt//;
  34. $url{ $tag } = "<a href=\"$file#".escurl($tag)."\">".esctext($label)."</a>";
  35. }
  36. close( TAGS );
  37. }
  38. sub esctext
  39. {
  40. my $text = shift;
  41. $text =~ s/&/&amp;/g;
  42. $text =~ s/</&lt;/g;
  43. $text =~ s/>/&gt;/g;
  44. return $text;
  45. }
  46. sub escurl
  47. {
  48. my $url = shift;
  49. $url =~ s/"/%22/g;
  50. $url =~ s/~/%7E/g;
  51. $url =~ s/</%3C/g;
  52. $url =~ s/>/%3E/g;
  53. $url =~ s/=/%20/g;
  54. $url =~ s/#/%23/g;
  55. $url =~ s/\//%2F/g;
  56. return $url;
  57. }
  58. sub vim2html
  59. {
  60. my( $infile ) = @_;
  61. my( $outfile );
  62. open(IN, "$infile" ) || die "Couldn't read from $infile: $!.\n";
  63. ($outfile = $infile) =~ s:.*/::g;
  64. $outfile =~ s/\.txt$//g;
  65. open( OUT, ">$outfile.html" )
  66. || die "Couldn't write to $outfile.html: $!.\n";
  67. my $head = uc( $outfile );
  68. print OUT<<EOF;
  69. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  70. <html>
  71. <head>
  72. <title>VIM: $outfile</title>
  73. <link rel="stylesheet" href="vim-stylesheet.css" type="text/css">
  74. </head>
  75. <body>
  76. <h2>$head</h2>
  77. <pre>
  78. EOF
  79. my $inexample = 0;
  80. while( <IN> ) {
  81. chop;
  82. if ( /^\s*[-=]+\s*$/ ) {
  83. print OUT "</pre><hr><pre>";
  84. next;
  85. }
  86. # examples
  87. elsif( /^>$/ || /\s>$/ ) {
  88. $inexample = 1;
  89. chop;
  90. }
  91. elsif ( $inexample && /^([<\S])/ ) {
  92. $inexample = 0;
  93. $_ = $' if $1 eq "<";
  94. }
  95. s/\s+$//g;
  96. # Various vim highlights. note that < and > have already been escaped
  97. # so that HTML doesn't get screwed up.
  98. my @out = ();
  99. # print "Text: $_\n";
  100. LOOP:
  101. foreach my $token ( split /((?:\|[^\|]+\|)|(?:\*[^\*]+\*))/ ) {
  102. if ( $token =~ /^\|([^\|]+)\|/ ) {
  103. # link
  104. push( @out, "|".maplink( $1 )."|" );
  105. next LOOP;
  106. }
  107. elsif ( $token =~ /^\*([^\*]+)\*/ ) {
  108. # target
  109. push( @out,
  110. "<b class=\"vimtag\">\*<a name=\"".escurl($1)."\">".esctext($1)."<\/a>\*<\/b>");
  111. next LOOP;
  112. }
  113. $_ = esctext($token);
  114. s/CTRL-(\w+)/<code class="keystroke">CTRL-$1<\/code>/g;
  115. # parameter <...>
  116. s/&lt;(.*?)&gt;/<code class="special">&lt;$1&gt;<\/code>/g;
  117. # parameter {...}
  118. s/\{([^}]*)\}/<code class="special">{$1}<\/code>/g;
  119. # parameter [...]
  120. s/\[(range|line|count|offset|cmd|[-+]?num)\]/<code class="special">\[$1\]<\/code>/g;
  121. # note
  122. s/(Note:?)/<code class="note">$1<\/code>/gi;
  123. # local heading
  124. s/^(.*)\~$/<code class="section">$1<\/code>/g;
  125. push( @out, $_ );
  126. }
  127. $_ = join( "", @out );
  128. if( $inexample == 2 ) {
  129. print OUT "<code class=\"example\">$_</code>\n";
  130. } else {
  131. print OUT $_,"\n";
  132. }
  133. $inexample = 2 if $inexample == 1;
  134. }
  135. print OUT<<EOF;
  136. </pre>
  137. <p><i>Generated by vim2html on $date</i></p>
  138. </body>
  139. </html>
  140. EOF
  141. }
  142. sub usage
  143. {
  144. die<<EOF;
  145. vim2html.pl: converts vim documentation to HTML.
  146. usage:
  147. vim2html.pl <tag file> <text files>
  148. EOF
  149. }
  150. sub writeCSS
  151. {
  152. open( CSS, ">vim-stylesheet.css" ) || die "Couldn't write stylesheet: $!\n";
  153. print CSS<<EOF;
  154. body { background-color: white; color: black;}
  155. :link { color: rgb(0,137,139); }
  156. :visited { color: rgb(0,100,100);
  157. background-color: white; /* should be inherit */ }
  158. :active { color: rgb(0,200,200);
  159. background-color: white; /* should be inherit */ }
  160. B.vimtag { color : rgb(250,0,250); }
  161. h1, h2 { color: rgb(82,80,82); text-align: center; }
  162. h3, h4, h5, h6 { color: rgb(82,80,82); }
  163. .headline { color: rgb(0,137,139); }
  164. .header { color: rgb(164, 32, 246); }
  165. .section { color: rgb(164, 32, 246); }
  166. .keystroke { color: rgb(106, 89, 205); }
  167. .vim { }
  168. .example { color: rgb(0, 0, 255); }
  169. .option { }
  170. .notvi { }
  171. .special { color: rgb(106, 89, 205); }
  172. .note { color: blue; background-color: yellow; }
  173. .sub {}
  174. .badlink { color: rgb(0,37,39); }
  175. EOF
  176. }
  177. # main
  178. usage() if $#ARGV < 1;
  179. print "Processing tags...\n";
  180. readTagFile( $ARGV[ 0 ] );
  181. foreach my $file ( 1..$#ARGV ) {
  182. print "Processing ".$ARGV[ $file ]."...\n";
  183. vim2html( $ARGV[ $file ] );
  184. }
  185. print "Writing stylesheet...\n";
  186. writeCSS();
  187. print "done.\n"