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.

102 lines
2.0 KiB

  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. notes-links-for - show links for a given target in my notes
  5. =head1 SYNOPSIS
  6. Currently this is vimwiki-dependent.
  7. # Show links for foo
  8. vimwiki-links-for --target foo
  9. # Show links for foo.wiki:
  10. vimwiki-links-for --file ./foo.wiki
  11. =head1 AUTHOR
  12. Brennen Bearnes
  13. =cut
  14. use warnings;
  15. use strict;
  16. use 5.10.0;
  17. use Cwd;
  18. use Data::Dumper;
  19. use DBI;
  20. use File::Basename;
  21. use File::Spec;
  22. use Getopt::Long;
  23. use Pod::Usage;
  24. use SQL::Abstract;
  25. use Sys::Hostname;
  26. use App::CommandLog;
  27. # Handle options, including help generated from the POD above.
  28. my $debug = 0;
  29. my $target;
  30. my $file;
  31. GetOptions(
  32. 'debug' => \$debug,
  33. 'target=s' => \$target,
  34. 'file=s' => \$file,
  35. 'help' => sub { pod2usage(0) },
  36. ) or pod2usage(2);
  37. if (defined $file) {
  38. my ($name, $path, $suffix) = fileparse($file, '.wiki');
  39. $target = $name;
  40. }
  41. unless (defined $target) {
  42. die "Must give a --target or a --file";
  43. }
  44. my %where = ();
  45. $where{target} = $target;
  46. my $dbfile = $ENV{HOME} . "/notes/metadata.db";
  47. my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
  48. my $sql = SQL::Abstract->new;
  49. my ($query, @bind) = $sql->select(
  50. 'links JOIN pages ON links.page = pages.page',
  51. '*',
  52. \%where,
  53. {-desc => 'target'}
  54. );
  55. if ($debug) {
  56. say STDERR $query;
  57. say STDERR Dumper(%where);
  58. }
  59. my $sth = $dbh->prepare($query);
  60. $sth->execute(@bind);
  61. # TODO: Note that the "1" here is, theoretically, a line number.
  62. # Unfortunately, there's no obvious way to extract that from Pandoc's parsing
  63. # of the original file, and thus no non-hacky way for notes-collect-metadata to
  64. # store an approximate value in the database. We _could_ read the file
  65. # line-by-line, or run grep, looking for the file's basename, but this is
  66. # tricky to get right.
  67. #
  68. # If I keep using this long-term, I should solve this problem.
  69. while (my $data = $sth->fetchrow_hashref()) {
  70. print "$ENV{HOME}/notes/vimwiki/$data->{page}.wiki";
  71. if ($data->{title}) {
  72. print ":1:$data->{title}";
  73. } else {
  74. print ":1:$data->{page}"
  75. }
  76. print "\n";
  77. }
  78. $sth->execute();