diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-10-07 12:13:12 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-10-07 12:16:59 +0200 |
commit | 59065ff6e02b605cddb55c15436b294f553c8619 (patch) | |
tree | 71768453bb50b772a607a012a03365eb2c4cf745 | |
parent | a928def5ed6fff777d32a19c635274aa33635655 (diff) | |
download | mansrv-59065ff6e02b605cddb55c15436b294f553c8619.tar.gz mansrv-59065ff6e02b605cddb55c15436b294f553c8619.tar.bz2 |
Various improvements
* mansrv: Interpret material appearing between {% and %}
in template files as Perl expression.
Avoid using hardcoded URLs.
-rwxr-xr-x | mansrv | 70 | ||||
-rw-r--r-- | top.html | 4 |
2 files changed, 54 insertions, 20 deletions
@@ -16,15 +16,16 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. use strict; use File::Basename; use sigtrap; use Sys::Syslog; +use Safe; -my $server = "mansrv"; -my $version = "1.0"; +my $package_name = "mansrv"; +our $VERSION = "1.1"; my $cf = "/etc/mansrv.conf"; our $docdir; our $manref = $ENV{'SCRIPT_NAME'}."?"; our $htmltop; our $htmlbot; @@ -107,23 +108,40 @@ sub build_manpath() { grep { !/^\./ && -d "$docdir/$_" && -d "$docdir/$_/man" } readdir($d); closedir($d); $ENV{'MANPATH'} = "$dirs:$ENV{'MANPATH'}" if ($dirs); } +sub expand_template { + my ($comp, $code, $file, $line) = @_; + my $r = $comp->reval($code); + unless (defined($r)) { + syslog("LOG_ERR", "%s:%d: error expanding template expression %s", + $file, $line, $code); + $r = ''; + } + return $r; +} + sub interpret_file($$) { my ($ofd, $file) = @_; my $ifd; open($ifd, $file) or syserror("cannot open $file: $!"); + my $s = new Safe 'Root' ; + %{$s->varglob('ENV')} = %ENV; + ${$s->varglob('TITLE')} = $ARGV[1]; + ${$s->varglob('SECTION')} = $ARGV[0]; + ${$s->varglob('PACKAGE')} = $package_name; + ${$s->varglob('VERSION')} = $VERSION; + ${$s->varglob('SERVER')} = "$proto://$ENV{SERVER_NAME}"; + while (<$ifd>) { - s/\@TITLE\@/$ARGV[1]/g; - s/\@SECTION\@/$ARGV[0]/g; - s/\@SERVER\@/$server/g; - s/\@VERSION\@/$version/g; - print $ofd $_; + chomp; + s/\{%(.*)%\}/expand_template($s, $1, $file, $.)/ex; + print $ofd "$_\n"; } close($ifd); } sub interpret { my $fd = shift; @@ -156,15 +174,13 @@ sub checkdeps($) { } return 1; } # ############################################################################# -my $script; # This script name. -($script = $0) =~ s/.*\///; -openlog($script, "ndelay,pid", "daemon"); +openlog(basename($0), "ndelay,pid", "daemon"); if ($ENV{'MANSRV_CONF'}) { $cf = $ENV{'MANSRV_CONF'}; } read_config($cf); push @deps, $cf; @@ -175,17 +191,27 @@ if ($manref =~ /\?$/) { } else { $manref .= "?"; $mansep = '+'; } # Set up environment -&build_manpath; +build_manpath(); + +my $proto; +if (exists($ENV{HTTP_X_FORWARDED_PROTO})) { + $proto = $ENV{HTTP_X_FORWARDED_PROTO}; +} elsif ($ENV{HTTPS} eq 'on') { + $proto = 'https'; +} else { + $proto = 'http'; +} +$ENV{REQUEST_SCHEME} = $proto; $ENV{'MANCGI'}='WEBDOC'; if ($#ARGV != 1) { - print "Location: http://man.gnu.org.ua\n"; + print "Location: $ENV{REQUEST_SCHEME}://$ENV{SERVER_NAME}\n"; print "\n"; exit 0; } # Begin output print "Content-Type: text/html\n"; @@ -454,30 +480,38 @@ Include path for B<groff> (list of directories separated with semicolons). =back =head1 TEMPLATE SUBSTITUTIONS While interpreting the contents of the files B<htmltop>, B<htmlbot> and -B<errpage>, the following character sequences are removed and replaced with -the corresponding expansions: +B<errpage>, the material between B<{%> and B<%}> is evaluated as a Perl +expression. It can make references to the following variables: =over 4 -=item B<@SERVER@> +=item B<$PACKAGE> Canonical name of the program (B<mansrv>). -=item B<@VERSION@> +=item B<$VERSION> Version of B<mansrv>. -=item B<@TITLE@> +=item B<%ENV> + +Trimmed environment from the master process. + +=item B<$SERVER> + +Base server URL. + +=item B<$TITLE> Manpage title. -=item B<@SECTION@> +=item B<$SECTION> Requested manpage section. =back =head1 LOGGING @@ -1,10 +1,10 @@ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> -<meta name="generator" content="@SERVER@ @VERSION@"> +<meta name="generator" content="{% "$PACKAGE $VERSION" %}"> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> -<title>@TITLE@</title> +<title>{% $TITLE %}</title> </head> <body> <div id="content"> |