# Example ~/.elinks/hooks.pl
#
# Copyleft by Russ Rowan (See the file "COPYING" for details.)
#
# To get documentation for this file:
#   pod2html hooks.pl > hooks.html && elinks hooks.html
# or
#   perldoc hooks.pl

=head1 NAME

hooks.pl -- Perl hooks for the ELinks text WWW browser

=head1 DESCRIPTION

This file contains the Perl hooks for the ELinks text WWW browser.

These hooks change the browser's behavior in various ways.  They allow
shortcuts to be used in the Goto URL dialog, modifying the source of a page,
proxy handling, and other things such as displaying a fortune at exit.

=cut
#use strict;
#use warnings;
#use diagnostics;
use Data::Dumper;
use HTML::TreeBuilder;

=head1 PRE FORMAT HTML HOOK

When an HTML document is downloaded and is about to undergo the final
rendering, this hook is called.  This is frequently used to get rid of ads, but
also various ELinks-unfriendly HTML code and HTML snippets which are irrelevant
to ELinks but can obfuscate the rendered document.

Note that these hooks are applied B<only> before the final rendering, not
before the gradual re-renderings which happen when only part of the document is
available.

=over

I<Developer's usage>: The function I<pre_format_html_hook> is called when the
hook is triggered, taking the document's URL and the HTML source as its two
arguments.  It returns the rewritten HTML code.

=cut
################################################################################
### pre_format_html_hook #######################################################
sub pre_format_html_hook
{
	my $url = shift;
	my $html = shift;
#	my $content_type = shift;


=item Slashdot Sanitation

Kills Slashdot's Advertisements.  (This one is disabled due to weird behavior
with fragments.)

=cut
	if ($url =~ 'slashdot\.org')
	{
		$html =~ s/.*<header class/<header class/smg;
		$html =~ s/Oops! You do.*There are no comments related to the filter you selected.//smg;
		my $tree = HTML::TreeBuilder->new;
		$tree->parse_content($html);
		foreach $point ('-1','0','1','2')
		{
			@bad = $tree->look_down('data-points',$point);
			foreach(@bad)
			{
				$_->delete();
			}
		}
		foreach $point ('3','4','5')
		{
			@good = $tree->look_down('data-points',$point);
			foreach(@good)
			{
				my $textje = HTML::Element->new( '~literal', 'text', '<br><br>');
				$_->push_content($textje);
			}
		}
		@bad = $tree->look_down('class','comment-footer-controls btn-group btn-group-sm');
		foreach(@bad)
		{
			$_->delete();
		}
		@h3 = $tree->look_down('class','comment-title');
		foreach(@h3)
		{
			$_->attr('_tag','font');
			my $textje = HTML::Element->new( '~literal', 'text', ' ');
			$_->push_content($textje);
		}
		@h4 = $tree->look_down('class','avatar-tiny');
		foreach(@h4)
		{
			$_->attr('_tag','font');
		}
		$html = $tree->as_HTML('<>&',"\t");
	}

=head1 SEE ALSO

elinks(1), perl(1)


=head1 AUTHORS

Russ Rowan, Petr Baudis

=cut

# vim: ts=4 sw=4 sts=0 nowrap
