# Copyright 2002-2008 Josh Clark and Global Moxie, LLC. This code cannot be
# redistributed without permission from globalmoxie.com.  For more
# information, consult your Big Medium license.
#
# $Id: PageUtils.pm 3043 2008-03-31 14:00:38Z josh $

package BigMed::PageUtils;
use strict;
use warnings;
use utf8;
use Carp;
use BigMed::DiskUtil qw(bm_load_file);

sub page_to_cgi {
    my ( $site, $text, $base_url ) = @_;
    $text = replace_all_includes( $site, $text );
    if ($base_url) {
        $text =~ s{(<head>)(\s+)}{$1$2<base href="$base_url"></base>$2}ms;
    }
    else {
        $text = replace_slash_urls( $site, $text );
    }
    return $text;
}

sub site_web_root {
    my $site = shift;
    my $page_path_from_root = $site->html_url;
    $page_path_from_root =~ s{\Ahttps?://[^/]*}{}msi;
    my $web_root = $site->html_dir;
    $web_root =~ s/\Q$page_path_from_root\E\z//ms;
    return $web_root;   
}

sub site_web_root_url {
    my $site = shift;
    my $domain = $site->html_url;
    $domain =~ s{\A(https?://[^/]*).*}{$1};
    return $domain;
}

sub replace_all_includes {
    my ($site, $text) = @_;
    #scans for include virtual SSI tags and brings in the files they
    #include. This is not recursive and no other SSI is processed.
    my $webroot = site_web_root($site);
    $text =~ s{(\Q<!--#include virtual="\E[^"]+"\s+\-\->)}{
        _replace_include_tag($webroot,$1);
    }msge;
    
    #todo: smarter handling of includes; for now, just remove remaining
    #include tags.
    $text =~ s{<!--\#[^>]*-->}{}msg;
    return $text;
}

sub _replace_include_tag {
    my ($webroot, $tag) = @_;
    if ($tag =~ /virtual\s*=\s*"([^"]+)/ms) {
        return join("\n", bm_load_file($webroot . $1) );
    }
    return '<!-- include will appear here -->'; #couldn't sort it out
}

sub replace_slash_urls {
    my ($site, $text) = @_;
    my $webroot = site_web_root_url($site);
	$text =~ s{
	    (\s)
	    (href|src|data|<param[^>]+?value)
	    \s*=\s*
	    "(/[^"]*)"
	  }{$1$2="$webroot$3"}msigx;
	
    return $text;
}

1;

__END__
