Skip to content

Commit

Permalink
Item8081: Expand Foswiki macros in command line settings, and convert…
Browse files Browse the repository at this point in the history
… image tags to filenames.

git-svn-id: http://svn.foswiki.org/trunk/GenPDFAddOn@5681 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Nov 30, 2009
1 parent 0f88bc1 commit 7b923cd
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions lib/Foswiki/Contrib/GenPDF.pm
Expand Up @@ -72,7 +72,7 @@ $VERSION = '$Rev$';
# This is a free-form string you can use to "name" your own plugin version.
# It is *not* used by the build automation tools, but is reported as part
# of the version number in PLUGINDESCRIPTIONS.
$RELEASE = 'Foswiki 1.0';
$RELEASE = '1.0.8';

$| = 1; # Autoflush buffers

Expand Down Expand Up @@ -430,7 +430,7 @@ s|<h(\d)>((?:(?!<h\d>).)*)</h\d>|'<h'.($newHead = ($1+$prefs{'shift'})>15?15:($1
return $html;
}

=head2 _fixImages($html)
=head2 _fixImages($html,nt)
Extract all local server image names and convert to temporary files
Images that are relative to the server pub path or any images
Expand All @@ -439,10 +439,13 @@ be converted to temp files to avoid http / https authorization issues
on authenticated servers, and to validate image access throught the
Foswiki access controls.
if nt is set to true, then return a simple filename instead of an html tag.
=cut

sub _fixImages {
my ($html) = @_;
my $html = $_[0];
my $notag = $_[1] || 0;
my %infoForImages;
my $Foswikiurl = Foswiki::Func::getUrlHost();
my $pubpath = Foswiki::Func::getPubUrlPath();
Expand Down Expand Up @@ -516,18 +519,30 @@ sub _fixImages {
( my $tvol, my $tdir, my $fname ) =
File::Spec->splitpath( $tempfh->filename );

$html =~ s{
<[iI][mM][gG]\s+ # starting img tag plus space
( (?: \w+ \s*=\s* $reAttrValue \s+ )* ) # 0 or more word = value - Assign to $1
[sS][rR][cC]\s*=\s* # src = with or without spaces
([\"\']?) # assign quote to $2
$imgurl # value of URL
\2 # Optional Closing quote
( (?: \s+ \w+ \s*=\s* $reAttrValue )* ) # 0 or more word = value - Assign to $3
\s*/?> # Close tag Group
}{<img $1 src=$2$fname$2 $3 >
}sgx;

# For htmldoc commandline parameters, images are passed as a filename, not an html tag.
if ($notag) {
$html =~ s{
<[iI][mM][gG]\s+ # starting img tag plus space
( (?: \w+ \s*=\s* $reAttrValue \s+ )* ) # 0 or more word = value - Assign to $1
[sS][rR][cC]\s*=\s* # src = with or without spaces
([\"\']?) # assign quote to $2
$imgurl # value of URL
\2 # Optional Closing quote
( (?: \s+ \w+ \s*=\s* $reAttrValue )* ) # 0 or more word = value - Assign to $3
\s*/?> # Close tag Group
}{$tempfh}sgx;
} else {
$html =~ s{
<[iI][mM][gG]\s+ # starting img tag plus space
( (?: \w+ \s*=\s* $reAttrValue \s+ )* ) # 0 or more word = value - Assign to $1
[sS][rR][cC]\s*=\s* # src = with or without spaces
([\"\']?) # assign quote to $2
$imgurl # value of URL
\2 # Optional Closing quote
( (?: \s+ \w+ \s*=\s* $reAttrValue )* ) # 0 or more word = value - Assign to $3
\s*/?> # Close tag Group
}{<img $1 src=$2$fname$2 $3 > }sgx;
}
}

return $html;
Expand Down Expand Up @@ -1092,7 +1107,6 @@ sub viewPDF {
my ( $cfh, $contentFile ) = tempfile(
'GenPDFAddOnXXXXXXXXXX',
DIR => $tempdir,

#UNLINK => 0, # DEBUG
SUFFIX => '.html'
);
Expand Down Expand Up @@ -1167,14 +1181,21 @@ sub viewPDF {
push @htmldocArgs, "--pagemode", "$prefs{'pagemode'}" if $prefs{'pagemode'};
push @htmldocArgs, @contentFiles;

_writeDebug("Calling htmldoc with args: @htmldocArgs");

# Disable CGI feature of newer versions of htmldoc
# (thanks to Brent Roberts for this fix)
$ENV{HTMLDOC_NOCGI} = "yes";

my $htmldocArgs = join(' ', @htmldocArgs);
$htmldocArgs = Foswiki::Func::expandCommonVariables( $htmldocArgs,
$topic, $webName );

# Command line paramters need filenames, not image tags, so strip any tags and convert to filename.
$htmldocArgs = _fixImages($htmldocArgs,1);

_writeDebug("Calling htmldoc with args: $htmldocArgs");

my ( $Output, $exit ) = Foswiki::Sandbox->sysCommand(
$htmldocCmd . ' ' . join( ' ', @htmldocArgs ) );
$htmldocCmd . ' ' . $htmldocArgs );
_writeDebug("htmldoc exited with $exit");
if ( !-e $outputFile ) {
die "error running htmldoc ($htmldocCmd): $Output\n";
Expand Down

0 comments on commit 7b923cd

Please sign in to comment.