From http://www.jwz.org/xscreensaver/xscreensaver-5.23.tar.gz
[xscreensaver] / hacks / xml2man.pl
1 #!/usr/bin/perl -w
2 # Copyright © 2002-2013 Jamie Zawinski <jwz@jwz.org>
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # Created: 30-May-2002.
13 #
14 # This creates man pages from the XML program descriptions in 
15 # xscreensaver/hacks/config/.
16 #
17 # They aren't necessarily the most accurate or well-written man pages,
18 # but at least they exist.
19
20 require 5;
21 use diagnostics;
22 use strict;
23
24 use Text::Wrap;
25
26 my $progname = $0; $progname =~ s@.*/@@g;
27 my $version = q{ $Revision: 1.5 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
28
29 my $verbose = 0;
30
31 my $default_args = ("[\\-display \\fIhost:display.screen\\fP]\n" .
32                     "[\\-visual \\fIvisual\\fP]\n" .
33                     "[\\-window]\n" .
34                     "[\\-root]\n");
35 my $default_options = (".TP 8\n" .
36                        ".B \\-visual \\fIvisual\\fP\n" .
37                        "Specify which visual to use.  Legal values " .
38                        "are the name of a visual class,\n" .
39                        "or the id number (decimal or hex) of a " .
40                        "specific visual.\n" .
41                        ".TP 8\n" .
42                        ".B \\-window\n" .
43                        "Draw on a newly-created window.  " .
44                        "This is the default.\n" .
45                        ".TP 8\n" .
46                        ".B \\-root\n" .
47                        "Draw on the root window.\n");
48
49 my $man_suffix = (".SH ENVIRONMENT\n" .
50                   ".PP\n" .
51                   ".TP 8\n" .
52                   ".B DISPLAY\n" .
53                   "to get the default host and display number.\n" .
54                   ".TP 8\n" .
55                   ".B XENVIRONMENT\n" .
56                   "to get the name of a resource file that overrides " .
57                   "the global resources\n" .
58                   "stored in the RESOURCE_MANAGER property.\n" .
59                   ".SH SEE ALSO\n" .
60                   ".BR X (1),\n" .
61                   ".BR xscreensaver (1)\n" .
62                   ".SH COPYRIGHT\n" .
63                   "Copyright \\(co %YEAR% by %AUTHOR%.  " .
64                   "Permission to use, copy, modify, \n" .
65                   "distribute, and sell this software and its " .
66                   "documentation for any purpose is \n" .
67                   "hereby granted without fee, provided that " .
68                   "the above copyright notice appear \n" .
69                   "in all copies and that both that copyright " .
70                   "notice and this permission notice\n" .
71                   "appear in supporting documentation.  No " .
72                   "representations are made about the \n" .
73                   "suitability of this software for any purpose.  " .
74                   "It is provided \"as is\" without\n" .
75                   "express or implied warranty.\n" .
76                   ".SH AUTHOR\n" .
77                   "%AUTHOR%.\n");
78
79 sub xml2man($) {
80   my ($exe) = @_;
81   $exe =~ s/\.xml$//s;
82   my $cfgdir = (-d "config" ? "config" : "../config");
83   my $xml = "$cfgdir/$exe.xml";
84   my $man = "$exe.man";
85
86   error ("$exe does not exist") if (! -f $exe);
87   error ("$xml does not exist") if (! -f $xml);
88   error ("$man already exists") if (-f $man);
89
90   local *IN;
91   open (IN, "<$xml") || error ("$xml: $!");
92   my $xmltxt = "";
93   while (<IN>) { $xmltxt .= $_; }
94   close IN;
95
96   my $args = "";
97   my $body = "";
98   my $desc;
99
100   $xmltxt =~ s/\s+/ /gs;
101   $xmltxt =~ s/<!--.*?-->//g;
102   $xmltxt =~ s@(<[^/])@\n$1@gs;
103
104   foreach (split ('\n', $xmltxt)) {
105     next if m/^$/;
106     next if m/^<\?xml\b/;
107     next if m/^<screensaver\b/;
108     next if m/^<command\b/;
109     next if m/^<[hv]group\b/;
110     next if m/^<select\b/;
111
112     my ($x,$arg) = m@\barg(|-unset|-set)=\"([^\"]+)\"@;
113     my ($label)  = m@\b_?label=\"([^\"]+)\"@;
114     my ($low)    = m@\blow=\"([^\"]+)\"@;
115     my ($hi)     = m@\bhigh=\"([^\"]+)\"@;
116     my ($def)    = m@\bdefault=\"([^\"]+)\"@;
117
118     $arg =~ s@\s*\%\s*@ \\fInumber\\fP@g if ($arg);
119                          
120     my $carg = $arg;
121     my $boolp = m/^<boolean/;
122     my $novalsp = 0;
123
124     if ($arg && $arg =~ m/^-no(-.*)/) {
125       $arg = "$1 | \\$arg";
126     } elsif ($boolp && $arg) {
127       $arg = "$arg | \\-no$arg";
128     }
129
130     if ($carg && $carg =~ m/colors/) {
131       $hi = $low = undef;
132     }
133
134     if (!$carg) {
135     } elsif ($carg eq '-move' || $carg eq '-no-move' ||
136              $carg eq '-wander' || $carg eq '-no-wander') {
137       $label = "Whether the object should wander around the screen.";
138     } elsif ($boolp && ($carg eq '-spin' || $carg eq '-no-spin')) {
139       $label = "Whether the object should spin.";
140     } elsif ($carg eq '-spin X') {
141       $carg = '-spin \fI[XYZ]\fP';
142       $arg = $carg;
143       $label = "Around which axes should the object spin?";
144     } elsif ($carg eq '-fps' || $carg eq '-no-fps') {
145       $label = "Whether to show a frames-per-second display " .
146                "at the bottom of the screen.";
147     } elsif ($carg eq '-wireframe' || $carg eq '-wire') {
148       $label = "Render in wireframe instead of solid.";
149     } elsif ($carg =~ m/^-delay/ && $hi && $hi >= 10000) {
150       $label = "Per-frame delay, in microseconds.";
151       $def = sprintf ("%d (%0.2f seconds)", $def, ($def/1000000.0));
152       $low = $hi = undef;
153     } elsif ($carg eq '-speed \fInumber\fP') {
154       $label = "Animation speed.  2.0 means twice as fast, " .
155                "0.5 means half as fast.";
156       $novalsp = 1;
157     } elsif ($boolp) {
158       $label .= ".  Boolean.";
159     } elsif ($label) {
160       $label .= ".";
161     }
162
163     if (m/^<(number|boolean|option)/) {
164
165       next if (!$arg && m/<option/);
166       if (!$label) {
167         print STDERR "$progname: ERROR: no label: $_\n";
168         $label = "???";
169       }
170
171       $args .= "[\\$carg]\n";
172
173       if (! $novalsp) {
174         $label .= "  $low - $hi." if (defined($low) && defined($hi));
175         $label .= "  Default: $def." if (defined ($def));
176       }
177       $label = wrap ("", "", $label);
178
179       $body .= ".TP 8\n.B \\$arg\n$label";
180       $body .= "\n";
181
182     } elsif (m@^<_description>\s*(.*)\s*</_description>@) {
183       $desc = $1;
184     } else {
185       print STDERR "$progname: ERROR: UNKNOWN: $_\n";
186     }
187   }
188
189   $desc = "Something pretty." unless $desc;
190
191   my $author = undef;
192   if ($desc =~ m@^(.*?)\s*(Written by|By) ([^.]+\.?\s*)$@s) {
193     $desc = $1;
194     $author = $3;
195     $author =~ s/\s*[.]\s*$//;
196   }
197
198   if (!$author) {
199     print STDERR "$progname: $exe: WARNING: unknown author\n";
200     $author = "UNKNOWN";
201   }
202
203   $desc =~ s@http://en\.wikipedia\.org/[^\s]+@@gs;
204
205   $desc = wrap ("", "", $desc);
206
207   $body = (".TH XScreenSaver 1 \"\" \"X Version 11\"\n" .
208            ".SH NAME\n" .
209            "$exe - screen saver.\n" .
210            ".SH SYNOPSIS\n" .
211            ".B $exe\n" .
212            $default_args .
213            $args .
214            ".SH DESCRIPTION\n" .
215            $desc . "\n" .
216            ".SH OPTIONS\n" .
217            $default_options .
218            $body .
219            $man_suffix);
220
221   my $year = $1 if ($author =~ s/; (\d{4})$//s);
222   $year = (localtime)[5] + 1900 unless $year;
223
224   $body =~ s/%AUTHOR%/$author/g;
225   $body =~ s/%YEAR%/$year/g;
226
227 #print $body; exit 0;
228
229   local *OUT;
230   open (OUT, ">$man") || error ("$man: $!");
231   print OUT $body || error ("$man: $!");
232   close OUT || error ("$man: $!");
233   print STDERR "$progname: wrote $man\n";
234 }
235
236
237 sub error($) {
238   my ($err) = @_;
239   print STDERR "$progname: $err\n";
240   exit 1;
241 }
242
243 sub usage() {
244   print STDERR "usage: $progname [--verbose] programs...\n";
245   exit 1;
246 }
247
248 sub main() {
249   my @progs = ();
250   while ($_ = $ARGV[0]) {
251     shift @ARGV;
252     if ($_ eq "--verbose") { $verbose++; }
253     elsif (m/^-v+$/) { $verbose += length($_)-1; }
254     elsif (m/^-./) { usage; }
255     else { push @progs, $_; }
256   }
257
258   usage() if ($#progs < 0);
259
260   foreach (@progs) { xml2man($_); }
261 }
262
263 main();
264 exit 0;