Changeset 25555

Show
Ignore:
Timestamp:
09.05.2012 14:33:11 (7 years ago)
Author:
sjm84
Message:

We want to be able to get associated files from CSS files

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/plugins/HTMLPlugin.pm

    r24431 r25555  
    628628    } 
    629629 
    630  
     630    $self->{'css_assoc_files'} = {}; 
     631     
    631632    # single section document 
    632633    $self->process_section($textref, $upgraded_base_dir, $upgraded_file, $doc_obj, $cursection); 
     634     
     635    #my $upgraded_filename_dirname = &File::Basename::dirname($upgraded_filename); 
     636         
     637    $self->acquire_css_associated_files($doc_obj, $cursection); 
     638     
     639    $self->{'css_assoc_files'} = {}; 
    633640    } 
    634641 
     
    698705    my $self = shift (@_); 
    699706    my ($textref, $base_dir, $file, $doc_obj, $cursection) = @_; 
     707 
     708    my @styleTagsText = ($$textref =~ m/<style[^>]*>([^<]*)<\/style>/sg); 
     709    if(scalar(@styleTagsText) > 0) 
     710    { 
     711        my $css_filename_dirname = &File::Basename::dirname(&util::filename_cat($base_dir, $file)); 
     712        foreach my $styleText (@styleTagsText) 
     713        { 
     714            $self->acquire_css_associated_files_from_text_block($styleText, $css_filename_dirname); 
     715        } 
     716    } 
     717 
    700718    # trap links 
    701719    if (!$self->{'nolinks'}) { 
     
    797815    my $self = shift (@_); 
    798816    my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_; 
    799  
     817     
     818    if($front =~ m/^<link / && $link =~ m/\.css"$/) 
     819    { 
     820        my $actual_link = $link; 
     821        $actual_link =~ s/^"(.*)"$/$1/; 
     822         
     823        my $directory = &File::Basename::dirname($file); 
     824         
     825        my $css_filename = &util::filename_cat($base_dir, $directory, $actual_link);     
     826        $self->retrieve_css_associated_files($css_filename); 
     827    } 
     828     
    800829    # remove quotes from link at start and end if necessary 
    801830    if ($link=~/^[\"\']/) { 
     
    885914    return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back; 
    886915    } 
     916} 
     917 
     918sub retrieve_css_associated_files { 
     919    my $self = shift (@_); 
     920    my ($css_filename) = @_; 
     921     
     922    my $css_filename_dirname = &File::Basename::dirname($css_filename); 
     923     
     924    open (CSSFILE, $css_filename) || return; 
     925    sysread (CSSFILE, my $file_string, -s CSSFILE); 
     926     
     927    $self->acquire_css_associated_files_from_text_block($file_string, $css_filename_dirname) unless !defined $file_string; 
     928     
     929    close CSSFILE; 
     930} 
     931 
     932sub acquire_css_associated_files_from_text_block { 
     933    my $self = shift (@_); 
     934    my ($text, $css_filename_dirname) = @_; 
     935     
     936    my @image_urls = ($text =~ m/background-image:\s*url[^;]*;/sg); 
     937    foreach my $img_url (@image_urls) 
     938    { 
     939        $img_url =~ s/^.*url.*\((.*)\).*$/$1/; 
     940        $img_url =~ s/^\s*"?([^"]*)"?\s*$/$1/; 
     941 
     942        $self->{'css_assoc_files'}->{&util::filename_cat($css_filename_dirname, $img_url)} = $img_url; 
     943    } 
     944} 
     945 
     946sub acquire_css_associated_files { 
     947    my $self = shift(@_); 
     948     
     949    my ($doc_obj, $section) = @_; 
     950     
     951    foreach my $image_filename (keys %{$self->{'css_assoc_files'}}) 
     952    { 
     953        $doc_obj->associate_file($image_filename, $self->{'css_assoc_files'}->{$image_filename}, undef, $section);   
     954    } 
    887955} 
    888956