# URI::Bookmarks::Netscape -- routines for Netscape bookmark files
#
# Copyright (c) 1999 Adam Spiers <adam@spiers.net>. All rights
# reserved. This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# $Id: Netscape.pm,v 1.13 2001/10/09 16:45:20 adam Exp $
#

package URI::Bookmarks::Netscape;

use strict;

require 5.005;
#use AutoLoader qw(AUTOLOAD);
use Carp;
use URI::Bookmarks;
use URI::Bookmark::Netscape;
use URI::Bookmarks::Netscape::Parser;

=head1 NAME

URI::Bookmarks::Netscape - routines for Netscape bookmark files

=head1 SYNOPSIS

  See L<URI::Bookmarks>.

=head1 DESCRIPTION

URI::Bookmarks::Netscape contains some helper routines specifically
for URI::Bookmarks objects which were originally Netscape bookmark
files.

=cut

sub import_bookmarks {
  my ($self, %p) = @_;
  my $parser = URI::Bookmarks::Netscape::Parser->new(collection => $self, %p);
  my $success = $parser->parse_file($p{file});
  $self->{_parser_debugging} = $parser->debugging();

  return $parser->parse_failed() ? $parser->failure_reason() : $success;
}

sub doctype  { $_[0]->{origin}{doctype} || '<!DOCTYPE NETSCAPE-Bookmark-file-1>' }
sub base     { $_[0]->{origin}{base} }
sub meta     { $_[0]->{origin}{meta} }

sub preamble {
  my ($self) = @_;
  return $self->{origin}{preamble} ?
           $_[0]->{origin}{preamble} . "\n" : '';
}

##############################################################################
# static routines
##############################################################################

sub export {
  my ($collection) = @_;

  my @lines = ();

  if (my $base_href = $collection->{origin}{base}{href}) {
    push @lines, qq{<BASE HREF="$base_href">\n\n};
  }

  push @lines, doctype($collection), "\n";
  push @lines, preamble($collection);
  push @lines, "<TITLE>" . $collection->title() . "</TITLE>\n",
               "<H1>" . $collection->tree_root->name() . "</H1>\n\n";

  $collection->tree_root->walk_down({
                                     lines        => \@lines,
                                     callback     => \&pre_output_node,
                                     callbackback => \&post_output_node,
                                    });

  push @lines, "</DL><p>\n";

  return @lines;
}

sub pre_output_node {
  my ($node, $options) = @_;
  my $lines = $options->{lines};

  my $indent = ' ' x (4 * ($options->{_depth} || 0));

  my $type = $node->type();

  if ($type eq 'folder') {
    my $title = $node->name();
    my $HTML_attribs =
      URI::Bookmark::Netscape::HTML_attribs($node,
                                            qw/folded newitemheader add_date/);
    push @$lines, "$indent<DT><H3$HTML_attribs>$title</H3>\n"
      if $options->{_depth};
  }
  elsif ($type eq 'bookmark') {
    my $title = $node->name;
    my $HTML_attribs =
      URI::Bookmark::Netscape::HTML_attribs($node,
                                            qw/href aliasof aliasid add_date
                                               last_visit last_modified/);
    push @$lines, "$indent<DT><A$HTML_attribs>$title</A>\n";
  }
  elsif ($type eq 'rule') {
    push @$lines, "$indent<HR>\n";
  }

  my $description = $node->attribute->{description};
  push @$lines, "<DD>$description" if $description;

  if ($type eq 'folder') {
    push @$lines, "$indent<DL><p>\n";
  }

  return 1;
}

sub post_output_node {
  my ($node, $options) = @_;
  my $lines = $options->{lines};

  my $indent = ' ' x (4 * ($options->{_depth} || 0));

  if ($node->type() eq 'folder') {
    push @$lines, "$indent</DL><p>\n"
      if $options->{_depth};
  }
}

#1;
########################    End of preloaded code    ########################
#__END__

=head1 AUTHOR

Adam Spiers <adam@spiers.net>

=head1 SEE ALSO

L<URI::Bookmarks>

=cut

