Skip to content

Commit

Permalink
Item14547: added base parameter
Browse files Browse the repository at this point in the history
- ... to override the default BaseUrl configured in LSC
- fixed url rewrite again
- make sure even non wikiword topic links are rewritten
  • Loading branch information
MichaelDaum committed Dec 6, 2017
1 parent f97f852 commit ad7f829
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/Foswiki/Plugins/ExportPlugin/Config.spec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $Foswiki::cfg{ExportPlugin}{Dir} = '$Foswiki::cfg{PubDir}/export';
$Foswiki::cfg{ExportPlugin}{URL} = '$Foswiki::cfg{DefaultUrlHost}$Foswiki::cfg{PubUrlPath}/export';

# **STRING**
$Foswiki::cfg{ExportPlugin}{BaseURL} = 'http://localhost';
$Foswiki::cfg{ExportPlugin}{BaseURL} = '$Foswiki::cfg{PubDir}/export';

# **BOOLEAN**
$Foswiki::cfg{ExportPlugin}{Debug} = 0;
Expand Down
28 changes: 17 additions & 11 deletions lib/Foswiki/Plugins/ExportPlugin/Exporter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ sub new {
$this->{htmlUrl} = $this->{exportUrl}.'/html';

$this->{baseUrl} ||= $this->{exportUrl};
$this->{baseUrl} .= '/' unless $this->{baseUrl} =~ /\/$/;

return $this;
}
Expand Down Expand Up @@ -122,10 +123,15 @@ sub getElapsedTime {
sub export {
my ($this, $session, $subject, $verb, $response) = @_;


my $debug = $this->param("debug");
$this->{debug} = Foswiki::Func::isTrue($debug, 0) if defined $debug;

$this->writeDebug("called export()");

my $base = $this->param("base");
$this->{baseUrl} = $base if defined $base;

my $result = '';

my $web = $this->param("web");
Expand Down Expand Up @@ -379,29 +385,29 @@ sub renderHTML {

#$this->writeDebug("host=$host, viewUrl=$viewUrl, viewUrlPath=$viewUrlPath");

# Remove <base.../> tag
$result =~ s/^<base[^>]+>.*?<\/base>.*$//im;
$result =~ s/^base[^>]+\/>.*$//im;

# remove non-macros and leftovers
$result =~ s/%(?:REVISIONS|REVTITLE|REVARG|QUERYPARAMSTRING)%//g;
$result =~ s/^%META:\w+{.*}%$//gm;

# copy assets and rewrite urls
my $depth = scalar(split(/[\/\.]/, $web)) + 1;
my $assetPrefix = "../" x $depth . "assets/";
$result =~ s!(['"\(])($Foswiki::cfg{DefaultUrlHost}|https?://$host)?$pub/(.*?)(\1|\))!$1.$assetPrefix.$this->copyAsset($3).$4!ge;
$result =~ s!(['"\(])($Foswiki::cfg{DefaultUrlHost}|https?://$host)?$pub/(.*?)(\1|\))!$1.$this->copyAsset($3).$4!ge;

# rewrite view links
my $htmlPrefix = "../" x $depth . "html/";
$result =~ s!href=(["'])(?:$viewUrl|$viewUrlPath)/($Foswiki::regex{webNameRegex}(?:\.|/)$Foswiki::regex{topicNameRegex})(\?.*?)?\1!'href='.$1.$htmlPrefix.$2.'.html'.($3||'').$1!ge;
my $htmlPrefix = $this->{htmlUrl};
$htmlPrefix .= '/' unless $htmlPrefix =~ /\/$/;
$result =~ s!href=(["'])(?:$viewUrl|$viewUrlPath)/($Foswiki::regex{webNameRegex}(?:\.|/)[[:upper:]]+[[:alnum:]]*)(\?.*?)?\1!'href='.$1.$htmlPrefix.$2.'.html'.($3||'').$1!ge;

# convert absolute to relative urls
$result =~ s/$host//g;

# fix anchors
$result =~ s!href=(["'])\?.*?#!href=$1#!g;

# replace <base.../> tag
$result =~ s/^<base[^>]+>.*?<\/base>.*$//im;
$result =~ s/^base[^>]+\/>.*$//im;
$result =~ s/<head>/<head>\n<base href="$this->{baseUrl}" \/>/;

return $result;
}

Expand Down Expand Up @@ -431,8 +437,8 @@ sub copyAsset {
$src =~ s/\/+/\//g;
$dst =~ s/\/+/\//g;

#$url = $this->{assetsUrl}.'/'.$path.'/'.$file;
$url = $path.'/'.$file;
$url = $this->{assetsUrl}.'/'.$path.'/'.$file;
#$url = $path.'/'.$file;

if (-r $src) {

Expand Down
9 changes: 5 additions & 4 deletions lib/Foswiki/Plugins/ExportPlugin/Pdf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ sub convertToPdf {

# create print command
my $baseUrl = $this->{baseUrl};
$baseUrl = Foswiki::Func::getPubUrlPath(undef, undef, undef, absolute=>1);

my $cmd = $Foswiki::cfg{ExportPlugin}{PdfCmd}
|| $Foswiki::cfg{GenPDFWeasyPlugin}{WeasyCmd}
|| '/usr/local/bin/weasyprint --base-url %BASEURL|U% --media-type print --encoding utf-8 %INFILE|F% %OUTFILE|F%';

#$this->writeDebug("cmd=$cmd");
#$this->writeDebug("BASEURL=$baseUrl");
#$this->writeDebug("htmlFile=" . $htmlFile);
#$this->writeDebug("pdfFile=$pdfFile");
$this->writeDebug("cmd=$cmd");
$this->writeDebug("BASEURL=$baseUrl");
$this->writeDebug("htmlFile=" . $htmlFile);
$this->writeDebug("pdfFile=$pdfFile");

# execute
my ($output, $exit, $error) = Foswiki::Sandbox->sysCommand(
Expand Down

0 comments on commit ad7f829

Please sign in to comment.