#!/usr/bin/perl -w
use strict;
use XML::Writer;

# Small utility to parse Opera bookmark files.
# Originally written in Python by Lars Marius Garshol
# perl version - Briac Pilpré
# version to output docbook xml that can be converted
# eg to a PDF file - Marc Baaden
# (comments for the latter to baaden@smplinux.de)

# We wrap everything in this element
my $wraptag = 'section';

my $version = <>;
#die "Doesn't seem to be a Opera Hotlist\n" if $version !~ /^Opera/;

#my $version = 2.0;
chomp( $version );

my $xml = new XML::Writer(DATA_MODE=>1, DATA_INDENT=>1);
   $xml->xmlDecl();
   $xml->doctype(
       $wraptag,
       '-//OASIS//DTD DocBook XML V4.2//EN',
       'http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd');
   $xml->startTag($wraptag);
   $xml->dataElement('title',$version);

while (<>){
        next unless /\S/;
        chomp;
        if ( $_ eq '#FOLDER' || $_ eq '#URL' ){
                my $node = $_;

                my $line;
                {
                        local $/ = "";  # Paragraph mode
                        $line    = <>;
                }

                if ($node eq '#FOLDER'){
                        my $name    = $1 if $line =~ /NAME=(.*)/;
                        my $created = $1 if $line =~ /CREATED=(.*)/;
                        $xml->startTag('section');
                        $xml->dataElement('title', $name);
                }

                elsif ($node eq '#URL') {
                        my $name    = $1 if $line =~ /NAME=(.*)/;
                        my $created = $1 if $line =~ /CREATED=(.*)/;
                        my $order   = $1 if $line =~ /ORDER=(.*)/;
                        my $visited = $1 if $line =~ /VISITED=(.*)/;
                        my $url     = $1 if $line =~ /URL=(.*)/;

                        $xml->startTag('para');
                        $xml->startTag('ulink', url => $url);
                        $xml->characters($name);
                        $xml->endTag('ulink');
                        $xml->endTag('para');
                }

        }
        elsif ( $_ eq '-'){
                $xml->endTag('section');
        }
}

$xml->endTag($wraptag);
