#!/usr/bin/perl -w

############################################################################
#                       refresh_lynx_bookmarks.pl                          #
#                                                                          #
#             Copyright (C) 2004 by Salvador Blasco Llopis                 #
#               <salvador D0T blasco 4T gmail D07 com>                     #
#                                                                          #
#     This script can be copied, distributed and modified under the terms  #
# of the  GNU  General Public License version 2.  Use it at your own risk. #
# The author provides neither warranty nor support.                        #
############################################################################

# USAGE:
# $ refresh_lynx_bookmarks.pl > lynx_bookmarks.html

use strict;
use XML::Parser;
use Getopt::Long;

my $AUTHOR = "Salvador Blasco <salvador.blasco\@gmail.com>";
my $VERSION = "0.3";

my $usage =<<"END";
REFRESH_LYNX_BOOKMARKS.PL: The script for exporting your konqueror
bookmarks to lynx. By $AUTHOR 2004. version $VERSION.

USAGE: \$ refresh_lynx_bookmarks.pl > lynx_bookmarks
       \$ refresh_lynx_bookmarks.pl --help
END

print "$usage" and exit if $ARGV[0] =~ /--help|-h|--usage|-\?/;

our $debug = 0;

my $output =<<"END";
<head>
<META http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Bookmark file</title>
</head>
	 You can delete links using the remove bookmark command.  It is usually
	 the 'R' key but may have been remapped by you or your system
		 administrator.<br>
	 This file also may be edited with a standard text editor to delete
	 outdated or invalid links, or to change their order.

<!--
Note: if you edit this file manually
	  you should not change the format within the lines
	  or add other HTML markup.
	  Make sure any bookmark link is saved as a single line.
-->

<p>
<ol>
END

my $filename = "/home/salvador/.kde/share/apps/konqueror/bookmarks.xml";
my $parser = new XML::Parser(Handlers => {
	Start	=> \&handle_start,
	End		=> \&handle_end,
	Char	=> \&handle_char,
});

my $sectioning = '';

GetOptions(
	'sectioning!' => \$sectioning, 
	'debug' => \$debug
);

my $href = "";
my $txt  = "";
my $title = "";
my $level = 0;
my $mode = "";
my $istitle = 0;
my $current_tag = "";

$parser->parsefile($filename);

print $output;

exit;

sub handle_start
{
	my ( $expat, $element, %alter ) = @_;
	
	# When bookmark, get the URL
	if ( $element eq "bookmark" )
	{
		$mode = "bookmark";
		$href = $alter{'href'};
	}

	# When folder, count up a level
	if ( $element eq "folder" )
	{
		$mode = $element;
		$level++;
		print "\t\$level++\n" if $debug;
	}
 
	$istitle = "$element" eq "title" ? 1 : 0;

	print "handle_start: \$element -> `$element', \$istitle -> $istitle, " . 
		"\$level = $level\n" if $debug;
}

sub handle_end
{	
	my ($p, $element) = @_;
	
	print "handle_end: \$element -> `$element', \$title = `$title'" .
		", \$level = $level\n" if $debug;

	$output .= "\t<li><a href=\"$href\">$title</a></li>\n"
		if "$element" eq "bookmark";
	$output .= "<h$level>$title<\\h$level>\n" and $level-- 
		if "$mode" eq "folder" and $sectioning;
		
	$istitle = 0;
}

sub handle_char
{
	my ($p, $txt) = @_;
	$title = $txt if $istitle or $sectioning;
	
	print "handle_char: \$txt -> `$txt', \$title -> $title\n" if $debug;
	return;
}


