source: trunk/third/xscreensaver/hacks/xml2man.pl @ 20148

Revision 20148, 7.5 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20147, which included commits to RCS files with non-trunk default branches.
  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2# Copyright © 2002 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
20require 5;
21use diagnostics;
22use strict;
23
24use Text::Wrap;
25
26my $progname = $0; $progname =~ s@.*/@@g;
27my $version = q{ $Revision: 1.1.1.1 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
28
29my $verbose = 0;
30
31my $default_args = ("[\\-display \\fIhost:display.screen\\fP]\n" .
32                    "[\\-visual \\fIvisual\\fP]\n" .
33                    "[\\-window]\n" .
34                    "[\\-root]\n");
35my $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
49my $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 2002 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
79sub xml2man {
80  my ($exe) = @_;
81  my $cfgdir = (-d "config" ? "config" : "../config");
82  my $xml = "$cfgdir/$exe.xml";
83  my $man = "$exe.man";
84
85  error ("$exe does not exist") if (! -f $exe);
86  error ("$xml does not exist") if (! -f $xml);
87  error ("$man already exists") if (-f $man);
88
89  local *IN;
90  open (IN, "<$xml") || error ("$xml: $!");
91  my $xmltxt = "";
92  while (<IN>) { $xmltxt .= $_; }
93  close IN;
94
95  my $args = "";
96  my $body = "";
97  my $desc;
98
99  $xmltxt =~ s/\s+/ /gs;
100  $xmltxt =~ s/<!--.*?-->//g;
101  $xmltxt =~ s@(<[^/])@\n$1@gs;
102
103  foreach (split ('\n', $xmltxt)) {
104    next if m/^$/;
105    next if m/^<\?xml\b/;
106    next if m/^<screensaver\b/;
107    next if m/^<command\b/;
108    next if m/^<[hv]group\b/;
109    next if m/^<select\b/;
110
111    my ($x,$arg) = m@\barg(|-unset|-set)=\"([^\"]+)\"@;
112    my ($label)  = m@\b_?label=\"([^\"]+)\"@;
113    my ($low)    = m@\blow=\"([^\"]+)\"@;
114    my ($hi)     = m@\bhigh=\"([^\"]+)\"@;
115    my ($def)    = m@\bdefault=\"([^\"]+)\"@;
116
117    $arg =~ s@\s*\%\s*@ \\fInumber\\fP@g if ($arg);
118                         
119    my $carg = $arg;
120    my $boolp = m/^<boolean/;
121
122    if ($arg && $arg =~ m/^-no(-.*)/) {
123      $arg = "$1 | \\$arg";
124    } elsif ($boolp && $arg) {
125      $arg = "$arg | \\-no$arg";
126    }
127
128    if ($carg && $carg =~ m/colors/) {
129      $hi = $low = undef;
130    }
131
132    if (!$carg) {
133    } elsif ($carg eq '-move' || $carg eq '-no-move' ||
134             $carg eq '-wander' || $carg eq '-no-wander') {
135      $label = "Whether the object should wander around the screen.";
136    } elsif ($boolp && ($carg eq '-spin' || $carg eq '-no-spin')) {
137      $label = "Whether the object should spin.";
138    } elsif ($carg eq '-spin X') {
139      $carg = '-spin \fI[XYZ]\fP';
140      $arg = $carg;
141      $label = "Around which axes should the object spin?";
142    } elsif ($carg eq '-fps' || $carg eq '-no-fps') {
143      $label = "Whether to show a frames-per-second display " .
144               "at the bottom of the screen.";
145    } elsif ($carg eq '-wireframe' || $carg eq '-wire') {
146      $label = "Render in wireframe instead of solid.";
147    } elsif ($carg =~ m/^-delay/ && $hi && $hi >= 10000) {
148      $label = "Per-frame delay, in microseconds.";
149      $def = sprintf ("%d (%0.2f seconds.)", $def, ($def/1000000.0));
150      $low = $hi = undef;
151    } elsif ($boolp) {
152      $label .= ".  Boolean.";
153    } elsif ($label) {
154      $label .= ".";
155    }
156
157    if (m/^<(number|boolean|option)/) {
158
159      next if (!$arg && m/<option/);
160      if (!$label) {
161        print STDERR "$progname: ERROR: no label: $_\n";
162        $label = "???";
163      }
164
165      $args .= "[\\$carg]\n";
166
167      $label .= "  $low - $hi." if (defined($low) && defined($hi));
168      $label .= "  Default: $def." if (defined ($def));
169      $label = wrap ("", "", $label);
170
171      $body .= ".TP 8\n.B \\$arg\n$label";
172      $body .= "\n";
173
174    } elsif (m@^<_description>\s*(.*)\s*</_description>@) {
175      $desc = $1;
176    } else {
177      print STDERR "$progname: ERROR: UNKNOWN: $_\n";
178    }
179  }
180
181  $desc = "Something pretty." unless $desc;
182
183  my $author = undef;
184  if ($desc =~ m@^(.*?)\s*(Written by|By) ([^.]+\.?\s*)$@s) {
185    $desc = $1;
186    $author = $3;
187    $author =~ s/\s*[.]\s*$//;
188  }
189
190  if (!$author) {
191    print STDERR "$progname: $exe: WARNING: unknown author\n";
192    $author = "UNKNOWN";
193  }
194
195  $desc = wrap ("", "", $desc);
196
197  $body = (".TH XScreenSaver 1 \"\" \"X Version 11\"\n" .
198           ".SH NAME\n" .
199           "$exe - screen saver.\n" .
200           ".SH SYNOPSIS\n" .
201           ".B $exe\n" .
202           $default_args .
203           $args .
204           ".SH DESCRIPTION\n" .
205           $desc . "\n" .
206           ".SH OPTIONS\n" .
207           $default_options .
208           $body .
209           $man_suffix);
210
211  $body =~ s/%AUTHOR%/$author/g;
212
213#print $body; exit 0;
214
215  local *OUT;
216  open (OUT, ">$man") || error ("$man: $!");
217  print OUT $body || error ("$man: $!");
218  close OUT || error ("$man: $!");
219  print STDERR "$progname: wrote $man\n";
220}
221
222
223sub error {
224  ($_) = @_;
225  print STDERR "$progname: $_\n";
226  exit 1;
227}
228
229sub usage {
230  print STDERR "usage: $progname [--verbose] programs...\n";
231  exit 1;
232}
233
234sub main {
235  my @progs = ();
236  while ($_ = $ARGV[0]) {
237    shift @ARGV;
238    if ($_ eq "--verbose") { $verbose++; }
239    elsif (m/^-v+$/) { $verbose += length($_)-1; }
240    elsif (m/^-./) { usage; }
241    else { push @progs, $_; }
242  }
243
244  usage() if ($#progs < 0);
245
246  foreach (@progs) { xml2man($_); }
247}
248
249main;
250exit 0;
Note: See TracBrowser for help on using the repository browser.