From: Eric S. Raymond Date: Wed, 30 Aug 2000 18:55:48 +0000 (+0000) Subject: Michael Toren'ssecond patch. X-Git-Tag: 1.8~1 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=f8cb1d08916235198ae09325c4b16b05c1546b4e;p=imgsizer.git Michael Toren'ssecond patch. --- diff --git a/imgsizer b/imgsizer index 8d9e364..4b6e38d 100755 --- a/imgsizer +++ b/imgsizer @@ -1,43 +1,103 @@ #!/usr/bin/perl -# Works with either Perl4 or Perl5, Perl4 is much faster. # # imgsizer -- correct image sizes in WWW pages +# by Eric S. Raymond +# +# Fix up IMG tags in given documents to contain correct sizes. No longer +# requires the identify(1) program from the ImageMagick suite if you have +# a decent file(1) and rdjpgcom(1). To fix up sizes for non-local images +# it requires the httpget(1) script. +# +# Works with either Perl4 or Perl5, Perl4 is much faster. +# +# Copy, use, and redistribute freely, but don't take my name off it and +# clearly mark an altered version. Fixes and enhancements cheerfully +# accepted. # -# by Eric S. Raymond 30 Jul 1996 +# Changelog: +# +# Originally created by Eric S. Raymond 30 Jul 1996 # # Modified by Erik Rossen 15 May 1999 # +# Added the --nomagick switch, to use file(1) and rdjpgcom(1) +# to determine the image size instead of identify(1) from the +# ImageMagick suite. +# # Modified by Michael C. Toren 18 Aug 2000; -# Fixed bug where the SRC attribute needed to be in quotes, improved -# command line argument parsing (but it could still use some work), -# added -q switch to omit quotes when generating tags, and -l switch -# to generate lowercase tags. # -# Fix up IMG tags in given documents to contain correct sizes. -# No longer requires the identify(1) program from the ImageMagick suite if -# you have a decent file(1) and rdjpgcom(1). -# To fix up sizes for non-local images it requires the httpget(1) script. +# Fixed bug where the SRC attribute's value needed to be in quotes, +# improved command line parsing (but it could still use some work), +# added -q switch to omit quotes when generating tags, and -l switch +# to generate lowercase tags. -mct # -# Copy, use, and redistribute freely, but don't take my name off it and -# clearly mark an altered version. Fixes and enhancements cheerfully -# accepted. +# Modified by Michael C. Toren 19 Aug 2000; # -# This is version 1.6pre+mct +# Improved the command line parsing some more, now looks for additional +# arguments via an IMGSIZER envrionmental variable, added the -d switch +# to set the DocumentRoot, -v switch to display version information, +# and -h switch to display usage information. -mct # +$version = "1.8, 30 Aug 2000"; + +$splash = < +See my home page at for updates. +EOT + +$usage = <, --document-root + + Directory where aboslute image filenames (i.e, ones which contain + a leading "/") may be found. + + -l, --lowercase + + Output HTML tag attributes imgsizer generates in lowercase. + + -q, --no-quotes + + Omit the quotes in HTML tag attributes imgsizer generates. +EOT + # Anything that can fetch a URL defined on the command line # and dump it to stdout will do here. $fetcher = 'httpget'; -# Output uppercase tags, surrounded by quotes, by default +# Output uppercase tags, surrounded by quotes, by default. $lower = 0; $quotes = 1; +# Set the default DocumentRoot to the current working directory. +$root = "."; + $out = "imgsizer-out$$"; $dir = "."; # NOTE: if you are doing '; # define INPUT_RECORD_SEPARATOR to split on HTML marker boundaries +# Look in IMGSIZER environmental variable for additional command line options +unshift @ARGV, split /\s+/, $ENV{IMGSIZER} + if ($ENV{IMGSIZER}); + while ($i = shift @ARGV) { last if ($i eq "--"); @@ -48,12 +108,50 @@ while ($i = shift @ARGV) last; } - if ($i =~ /^(--nomagick|-m)$/i) + if ($i =~ /^-(\w{2,})$/i) + { + # Hack to parse "-foo" as "-f -o -o" + unshift @ARGV, map { "-$_" } split //, $1; + next; + } + + if ($i =~ /^(--ver(sion)?|-v)$/i) + { + die "\n$splash\n"; + } + + if ($i =~ /^(--help|--usage|-h)$/i) + { + die "\n$splash\n$usage\n"; + } + + if ($i =~ /^(--no-?magick|-m)$/i) { $magick = 0; next; } + if ($i =~ /^(--document-?root(=.*)?|-d)$/i) + { + if ($2 =~ /^=(.*)/) + { + $root = $1; + } + + else + { + die "Error: --document-root requires a directory name\n" + unless (@ARGV); + + $root = shift @ARGV; + } + + die "Error: --document-root, \"$root\", is not a directory ", + "or does not exist\n" + unless (-d $root); + next; + } + if ($i =~ /^(--lower-?(case)?|-l)$/i) { $lower = 1; @@ -66,7 +164,7 @@ while ($i = shift @ARGV) next; } - warn "Warning: Ignoring unknown option: $i\n"; + die "Error: Unknown option: $i\n"; } if (@ARGV) { @@ -152,8 +250,8 @@ sub attrfix return $attribs; } } - elsif ($url =~ /^\//) { - $image = "./$url"; # This catches local files that are in + elsif ($url =~ m!^/!) { + $image = $root . $url; # This catches local files that are in # virtual root, assuming we're called # from /var/www or equivalent. } diff --git a/imgsizer.xml b/imgsizer.xml index 9623fc4..dfc91b4 100644 --- a/imgsizer.xml +++ b/imgsizer.xml @@ -1,10 +1,31 @@ -.TH IMGSIZER 1 "28 Jul 1997" +.TH IMGSIZER 1 "19 Aug 2000" .AT 3 .SH NAME imgsizer \- automatically splice in height and width params for HTML IMG tags .SH SYNOPSIS -.B imgsizer [--nomagick] [-q] [-l] [file...] +.B imgsizer [OPTIONS] [HTML File] .PP +.SH OPTIONS +.TP +.B \-v, \-\-version +Display version information and exit. +.TP +.B \-h, \-\-help +Display usage information. +.TP +.B \-m, \-\-no\-magick +Use file(1) and rdjpgcom(1) to determine the image size, instead +of identify(1) from the ImageMagick suite, which is the default. +.TP +.B \-d , \-\-document\-root +Directory where aboslute image filenames (i.e, ones which contain +a leading "/") may be found. +.TP +.B \-l, \-\-lowercase +Output HTML tag attributes imgsizer generates in lowercase. +.TP +.B \-q, \-\-no\-quotes +Omit the quotes in HTML tag attributes imgsizer generates. .SH DESCRIPTION The .B imgsizer @@ -28,17 +49,37 @@ arguments, it will attempt to transform each file in place. Each argument file is not actually modified until the script completes a successful conversion pass. .PP -The -q option suppresses generation of string quotes in generated -attributes. The -l option generates attributes in lower case. +The \-q option suppresses generation of string quotes in generated +attributes. The \-l option generates attributes in lower case. +The \-d option sets the DocumentRoot, which images with an +absolute filename (i.e., ones which contain a leading "/") may be found. +If none is specified, the DocumentRoot defaults to the current working +directory. +.PP +Additional options may also be specified in the environmental variable +"IMGSIZER". For example, to avoid typing "imgsizer -options ..." each +time imgsizer is invoked, you might tell sh (or one of it's decendents): +.PP +.RS +IMGSIZER="\-options"; export IMGSIZER +.RE +.PP +or, if you use csh: +.PP +.RS +setenv IMGSIZER "\-options" +.RE .PP This script is written on Perl, and thus requires a Perl interpreter on the host system. It also requires either the .IR identify (1) utility distributed in the freeware ImageMagick suite of image-display -and manipulation tools or (with --nomagick) a modern version of +and manipulation tools, or, with the \-\-no\-magick option, a modern +version of .IR file (1) and -.IR rdjpgcom (the --nomagick mode is faster). These +.IR rdjpgcom (1) +(the --no-magick mode is faster). These utilities are used to extract sizes from the images; .B imgsizer itself has no knowledge of graphics formats. The script will handle any @@ -57,8 +98,10 @@ will complain and fail if either the required size-reporting tools or .IR httpget (1) are needed and not present. .SH AUTHOR -Eric S. Raymond . The --nomagick option is by Erik Rossen. -See my home page at http://www.tuxedo.org/~esr for updates. +Originally created by Eric\ S.\ Raymond\ . Additional code +contributed by Erik\ Rossen and Michael\ C.\ Toren\ . +.SH AVAILABILITY +For updates, see .SH SEE ALSO .IR identify (1), .IR httpget (1).