#!/usr/bin/perl
#Mozilla ブックマークから風博士のブックマークファイルを作る
#
#このスクリプトと同じディレクトリに Mozilla のブックマークファイル(bookmarks.html)をコピーした状態で
#このスクリプトを実行すると，カレントディレクトリに bookmarks.xml を作成します．

my $tab = 0;
my $folder = '';
my $title = '';
my $bookmark = '';
my $url = '';

my $head1 = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
my $head2 = '<xbel version="1.0" folded="none">' . "\n";
my $foot = "</xbel>\n";

open MOZ , 'bookmarks.html' or die "Can't open Mozilla bookmark file";
my @moz = <MOZ>;
close MOZ;
chomp @moz;

open FILE , '>bookmarks.xml';
print FILE $head1;
print FILE $head2;

foreach my $temp(@moz)
{
	$temp =~ s/\&/&amp;/g;
	if($temp =~ /ID=.*?>(.*?)<\/H3>/i)
	{
		$folder = $1;
		$tab++;
		print FILE "\t" x $tab , "<folder>\n";
		print FILE "\t" x $tab , "<title>$folder</title>\n";
	}
	if($temp =~ /<\/DL>/)
	{
		print FILE "\t" x $tab , "</folder>\n";
		$tab--;
	}
	if($temp =~ /<HR>/i)
	{
		print FILE "\t" x $tab , "<separater />\n";
	}

	if($temp =~ /A HREF=(\".*?\").*?ID=.*?>(.*?)<\/A>/i)
	{
		$url = $1;
		$title = $2;
		print FILE "\t" x $tab , "<bookmark href=$url>\n";
		print FILE "\t" x ($tab+1) , "<title>$title</title>\n";
		print FILE "\t" x $tab , "</bookmark>\n";
	}
}
print FILE $foot;
print FILE $head2;
print FILE "<title>ブックマーク</title>\n";
print FILE $foot;
close FILE;
