summaryrefslogtreecommitdiff
path: root/script/xbel2yaml.pl
diff options
context:
space:
mode:
Diffstat (limited to 'script/xbel2yaml.pl')
-rw-r--r--script/xbel2yaml.pl77
1 files changed, 77 insertions, 0 deletions
diff --git a/script/xbel2yaml.pl b/script/xbel2yaml.pl
new file mode 100644
index 0000000..6cfb186
--- /dev/null
+++ b/script/xbel2yaml.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+use XML::SAX::ParserFactory;
+use YAML;
+
+my $handler=XBEL::Handler->new();
+my $parser=XML::SAX::ParserFactory->parser(Handler=>$handler);
+$parser->parse_uri($ARGV[0]);
+
+print Dump($handler->bookmarks());
+
+package XBEL::Handler;
+use base 'XML::SAX::Base';
+use Date::Parse;
+
+sub new {
+ my ($class)=@_;
+ return bless +{
+ bookmarks=>[],
+ status=>'out',
+ tags=>[],
+ },$class;
+}
+
+sub start_element {
+ my ($self,$el)=@_;
+
+ if ($el->{LocalName} eq 'folder') {
+ $self->{status}='folder';
+ push @{$self->{tags}},'';
+ }
+ elsif ($el->{LocalName} eq 'bookmark') {
+ $self->{status}='mark';
+ my $mark={};
+ $mark->{href}=$el->{Attributes}{'{}href'}{Value};
+ $mark->{created}=convert_date($el->{Attributes}{'{}added'}{Value});
+ $mark->{modified}=convert_date($el->{Attributes}{'{}modified'}{Value});
+ $mark->{tags}=[@{$self->{tags}}]; # force a copy
+ push @{$self->{bookmarks}},$mark;
+ }
+ elsif ($el->{LocalName} eq 'title' and $self->{status} eq 'folder') {
+ $self->{status}='folder-title';
+ }
+ elsif ($el->{LocalName} eq 'title' and $self->{status} eq 'mark') {
+ $self->{status}='mark-title';
+ }
+}
+
+sub end_element {
+ my ($self,$el)=@_;
+
+ if ($el->{LocalName} eq 'folder') {
+ pop @{$self->{tags}};
+ }
+ $self->{status}='';
+}
+
+sub characters {
+ my ($self,$data)=@_;
+
+ if ($self->{status} eq 'mark-title') {
+ $self->{bookmarks}[-1]{description}.=$data->{Data};
+ }
+ elsif ($self->{status} eq 'folder-title') {
+ $self->{tags}[-1].=$data->{Data};
+ }
+}
+
+sub convert_date {
+ return str2time($_[0]);
+}
+
+sub bookmarks {
+ my ($self)=@_;
+ return $self->{bookmarks};
+}
+
+1;