Skip to content
Snippets Groups Projects
Commit 9ea5cf1c authored by Gerhard Gonter's avatar Gerhard Gonter :speech_balloon:
Browse files

added module Tomboy::Directory to scan dir with notes in Tomboy format

parent 55c097ad
Branches
No related tags found
No related merge requests found
Tomboy-TOC.csv
#
# scan directory with note files in Tomboy format
#
=head1 NAME
Tomboy::Directory;
=head1 SYNOPSIS
=head1 DESCRIPTION
process a directory containing note files in Tomboy format
=cut
package Tomboy::Directory;
use strict;
use Tomboy::Note::Simple;
my @TB_note_attrs= qw(title create-date last-change-date last-metadata-change-date);
my @TB_meta_attrs= qw(uid notebook);
sub TB_attrs { return (@TB_meta_attrs, @TB_note_attrs) }
sub new
{
my $class= shift;
my %par= @_;
my $obj= {};
bless $obj, $class;
foreach my $par (keys %par)
{
$obj->{$par}= $par{$par};
if ($par eq 'dir') { $obj->scan_dir ($par{$par}) }
}
$obj;
}
sub scan_dir
{
my $obj= shift;
my $dir= shift;
unless (opendir (DIR, $dir))
{
print "ATTN: can't read directory [$dir]\n";
return undef;
}
my @res= ();
NOTE: while (my $e= readdir (DIR))
{
next NOTE if ($e eq '.' || $e eq '..');
next NOTE unless ($e =~ /(.+)\.note$/);
my $uid= $1;
my $fp= join ('/', $dir, $e);
# print "reading note [$fp]\n"; # TODO: if verbose...
my $n= parse Tomboy::Note::Simple ($fp);
unless (defined ($n))
{
print "ATTN: parsing [$fp] returned undefined note!\n";
next NOTE;
}
my %rec= map { $_ => $n->{$_} } @TB_note_attrs;
$rec{'uid'}= $uid;
foreach my $tag (@{$n->{'tags'}})
{
if ($tag =~ m#system:notebook:(.+)#) { $rec{'notebook'}= $1 }
}
push (@res, \%rec);
}
closedir (DIR);
(wantarray) ? @res : \@res;
}
1;
...@@ -88,10 +88,11 @@ sub parse ...@@ -88,10 +88,11 @@ sub parse
my $fnm= shift; my $fnm= shift;
my $note; my $note;
if (ref ($c) eq 'Tomboy::Note::Simple') { $note= $c; } if (ref ($c) eq 'Tomboy::Note::Simple') { $note= $c; }
elsif (ref ($c) eq '') elsif (ref ($c) eq '')
{ {
print "create new c=[$c]\n"; # print "create new c=[$c]\n";
$note= new Tomboy::Note::Simple; $note= new Tomboy::Note::Simple;
} }
else else
...@@ -99,6 +100,7 @@ sub parse ...@@ -99,6 +100,7 @@ sub parse
print "unknown c=[$c] r=[", ref ($c), "]\n"; print "unknown c=[$c] r=[", ref ($c), "]\n";
} }
# print "note=[$note]\n"; # print "note=[$note]\n";
$note->{'fnm'}= $fnm; $note->{'fnm'}= $fnm;
my $p= new XML::Parser (Style => 'Tree'); my $p= new XML::Parser (Style => 'Tree');
...@@ -170,4 +172,9 @@ __END__ ...@@ -170,4 +172,9 @@ __END__
Gerhard Gonter <ggonter@gmail.com> Gerhard Gonter <ggonter@gmail.com>
=head1 BUGS
* XML::Parser throws exceptions, these are currently not handled.
=cut =cut
#!/usr/bin/perl
use strict;
use lib 'lib';
use Util::Simple_CSV;
use Data::Dumper;
$Data::Dumper::Indent= 1;
# use Tomboy::Note::Simple;
use Tomboy::Directory;
my $note_dir= shift (@ARGV);
my $toc_file= 'Tomboy-TOC.csv';
die "no note dir specified" unless ($note_dir);
my $tb_d= new Tomboy::Directory ('dir' => $note_dir);
print "tb_d: ", Dumper ($tb_d);
my $toc_data= $tb_d->scan_dir ($note_dir);
# TODO: if verbose or so print "toc_data: ", Dumper ($toc_data);
my $toc= new Util::Simple_CSV ('UTF8' => 1, 'no_hash' => 1);
$toc->define_columns (Tomboy::Directory::TB_attrs());
$toc->{'data'}= $toc_data;
$toc->save_csv_file ('filename' => $toc_file);
exit (0);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment