#!/usr/bin/perl -w use strict; use URI::file; BEGIN { package MyFilter; use base qw(HTML::Filter); use Image::Size; use HTML::Entities; use LWP::Simple; sub new { my $package = shift; my $uri = shift; my $self = $package->SUPER::new(@_); $self->{_uri} = $uri; $self; } sub start { my $self = shift; my($tag, $attr, $attrseq, $origtext) = @_; if ($tag eq 'base' and exists $attr->{href}) { $self->{_uri} = URI->new($attr->{href}); } { last unless $tag eq 'img'; last unless exists $attr->{src}; last if exists $attr->{width}; last if exists $attr->{height}; my $src = $attr->{src}; my $src_uri = URI->new_abs($src, $self->{_uri}); my @xy = $src_uri->scheme eq "file" ? imgsize($src_uri->path) : imgsize(\get($src_uri)); last unless defined $xy[0]; @$attr{qw(width height)} = @xy[0,1]; my $tmp = "<$tag"; for (@$attrseq, qw(width height)) { $tmp .= qq/ $_="/.encode_entities($attr->{$_}).q/"/; } $tmp .= ">"; $self->output($tmp); return; } $self->output($origtext); } } undef $/; shift, $^I = ($1 || "~") if @ARGV and $ARGV[0] =~ /^-i(.*)/; while (<>) { my $file = URI::file->new_abs($ARGV); print STDOUT "===== $ARGV =====\n"; MyFilter->new($file)->parse($_)->eof; }