Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
2.0 KiB

#!/usr/bin/env perl
=pod
=head1 NAME
notes-links-for - show links for a given target in my notes
=head1 SYNOPSIS
Currently this is vimwiki-dependent.
# Show links for foo
vimwiki-links-for --target foo
# Show links for foo.wiki:
vimwiki-links-for --file ./foo.wiki
=head1 AUTHOR
Brennen Bearnes
=cut
use warnings;
use strict;
use 5.10.0;
use Cwd;
use Data::Dumper;
use DBI;
use File::Basename;
use File::Spec;
use Getopt::Long;
use Pod::Usage;
use SQL::Abstract;
use Sys::Hostname;
# Handle options, including help generated from the POD above.
my $debug = 0;
my $target;
my $file;
GetOptions(
'debug' => \$debug,
'target=s' => \$target,
'file=s' => \$file,
'help' => sub { pod2usage(0) },
) or pod2usage(2);
if (defined $file) {
my ($name, $path, $suffix) = fileparse($file, '.wiki');
$target = $name;
}
unless (defined $target) {
die "Must give a --target or a --file";
}
my %where = ();
$where{target} = $target;
my $dbfile = $ENV{HOME} . "/notes/metadata.db";
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
my $sql = SQL::Abstract->new;
my ($query, @bind) = $sql->select(
'links JOIN pages ON links.page = pages.page',
'*',
\%where,
{-desc => 'target'}
);
if ($debug) {
say STDERR $query;
say STDERR Dumper(%where);
}
my $sth = $dbh->prepare($query);
$sth->execute(@bind);
# TODO: Note that the "1" here is, theoretically, a line number.
# Unfortunately, there's no obvious way to extract that from Pandoc's parsing
# of the original file, and thus no non-hacky way for notes-collect-metadata to
# store an approximate value in the database. We _could_ read the file
# line-by-line, or run grep, looking for the file's basename, but this is
# tricky to get right.
#
# If I keep using this long-term, I should solve this problem.
while (my $data = $sth->fetchrow_hashref()) {
print "$ENV{HOME}/notes/vimwiki/$data->{page}.wiki";
if ($data->{title}) {
print ":1:$data->{title}";
} else {
print ":1:$data->{page}"
}
print "\n";
}
$sth->execute();