App-Followme
view release on metacpan or search on metacpan
lib/App/Followme/EditSections.pm view on Meta::CPAN
if ($count > 1) {
$lo = $mid;
if (defined $hi) {
$mid = int(0.5 * ($mid + $hi));
} else {
$mid = 2 * $mid;
}
} elsif ($count == 1) {
$pagepos += $mid unless $after;
return $pagepos;
} else {
$hi = $mid;
$mid = int(0.5 * ($mid + $lo));
}
}
return;
}
#----------------------------------------------------------------------
# Initialize the extension
sub setup {
my ($self) = @_;
$self->{extension} = $self->{web_extension};
return;
}
#----------------------------------------------------------------------
# Read page and strip comments
sub strip_comments {
my ($self, $file, $keep_sections) = @_;
my $page = fio_read_page($file);
die "Could not read page" unless length($page);
my @output;
my @tokens = split(/(<!--.*?-->)/, $page);
foreach my $token (@tokens) {
if ($token !~ /^(<!--.*?-->)$/) {
push(@output, $token);
} elsif ($token =~ /(<!--\s*end)?section\s+.*?-->/) {
push(@output, $token) if $keep_sections;
} else {
push(@output, $token) unless $self->{remove_comments};
}
}
return join('', @output);
}
#----------------------------------------------------------------------
# Strip file of comments and combine with prototype
sub update_file {
my ($self, $file, $prototype) = @_;
my $page = $self->strip_comments($file, 0);
$page = $self->update_page($page, $prototype);
fio_write_page($file, $page);
return;
}
#----------------------------------------------------------------------
# Parse prototype and page and combine them
sub update_page {
my ($self, $page, $prototype) = @_;
my @output;
my $notfound;
while ($prototype =~ /(<!--\s*(?:end)?section\s+.*?-->)/g) {
my $comment = $1;
my $pos = pos($prototype);
my $after = $comment =~ /<!--\s*end/;
# locate the position of the comment from the prototype in the page
$pos = $pos - length($comment) unless $after;
my $loc = $self->find_location($page, $prototype, $pos, $after);
unless (defined $loc) {
$notfound ++;
$loc= 0;
}
# substitute comment into page
$comment = $after ? "\n$comment" : "$comment\n";
push(@output, substr($page, 0, $loc), $comment);
$page = substr($page, $loc);
}
push(@output, $page);
die "Could not locate tags\n" if $notfound;
return join('', @output);
}
#----------------------------------------------------------------------
# Edit all files in the directory
sub update_folder {
my ($self, $folder, $prototype_file, $prototype) = @_;
my $index_file = $self->to_file($folder);
my $files = $self->{data}->build('files_by_mdate_reversed', $index_file);
unless (defined $prototype_file) {
if (@$files) {
$prototype_file = shift(@$files);
$prototype = $self->strip_comments($prototype_file, 1);
}
}
foreach my $file (@$files) {
eval {$self->update_file($file, $prototype)};
$self->check_error($@, $folder);
}
my $folders = $self->{data}->build('folders', $index_file);
for my $subfolder (@$folders) {
$self->update_folder($subfolder, $prototype_file, $prototype);
}
return;
}
1;
__END__
=encoding utf-8
=head1 NAME
App::Followme::EditSections - Edit the section tags on an html page
=head1 SYNOPSIS
use App::Followme::EditSection;
my $edit = App::Followme::EditSection->new();
$edit->run($directory);
=head1 DESCRIPTION
Followme distinguishes between sections of a web page which are the same across
the website and sections that differ between each page by html comments starting
with section and endsection. This module modifies the placement or number of these
tags across a website. It can also used to modify an existing website so it
can be maintained by followme. Before running followme with this module, edit a
page and put the section and endsection comments in the proper locations. Then
create a configuration file containing the name of this module. Then run followme
and it will modify all the pages of the website to include the same comments. Any
section and endsection tags that were previously in the file will be removed.
=head1 CONFIGURATION
The following field in the configuration file are used:
=over 4
=item remove_comments
( run in 0.493 second using v1.01-cache-2.11-cpan-99c4e6809bf )