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.

200 lines
3.8 KiB

  1. #!/usr/bin/env perl
  2. =pod
  3. =head1 NAME
  4. notes-tag-summary - show a summary of a given tag
  5. =head1 SYNOPSIS
  6. Currently this is vimwiki-dependent.
  7. # Show tag info for warelogging
  8. notes-tag-summary warelogging
  9. =head1 AUTHOR
  10. Brennen Bearnes
  11. =cut
  12. use warnings;
  13. use strict;
  14. use 5.10.0;
  15. use Carp;
  16. use Cwd;
  17. use Data::Dumper;
  18. use DBI;
  19. use File::Basename;
  20. use File::Spec;
  21. use Getopt::Long;
  22. use Pod::Usage;
  23. use File::HomeDir;
  24. use Term::ANSIColor;
  25. # Handle options, including help generated from the POD above.
  26. my $debug = 0;
  27. my $use_color = 0;
  28. my $from_filename = 0;
  29. GetOptions(
  30. 'color' => \$use_color,
  31. 'file' => \$from_filename,
  32. 'debug' => \$debug,
  33. 'help' => sub { pod2usage(0) },
  34. ) or pod2usage(2);
  35. my $tag = $ARGV[0];
  36. unless ($tag) {
  37. pod2usage(2);
  38. die;
  39. }
  40. # Pull a tag name out of a path to a wiki page:
  41. if ($from_filename) {
  42. my ($name, $path, $suffix) = fileparse($tag, '.wiki');
  43. $tag = $name;
  44. }
  45. my $HOME = File::HomeDir->my_home;
  46. {
  47. my $dbfile = $HOME . "/notes/metadata.db";
  48. my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
  49. sub query_onerow {
  50. return query(@_)->fetchrow_hashref();
  51. }
  52. sub query {
  53. my ($query, @bind) = @_;
  54. my $sth = $dbh->prepare($query);
  55. $sth->execute(@bind);
  56. return $sth;
  57. }
  58. }
  59. sub on_p1k3 {
  60. state %lines = map { $_ => 1 } file_get_contents_arr(
  61. File::Spec->catfile($HOME, 'notes', 'p1k3-index')
  62. );
  63. my ($tag) = @_;
  64. return exists $lines{$tag};
  65. }
  66. sub on_pinboard {
  67. state %lines = map { $_ => 1 } file_get_contents_arr(
  68. File::Spec->catfile($HOME, 'notes', 'pinboard-tag-index')
  69. );
  70. my ($tag) = @_;
  71. return exists $lines{$tag};
  72. }
  73. sub file_get_contents_arr {
  74. my ($file) = @_;
  75. open my $fh, '<', $file
  76. or croak "Couldn't open $file: $!\n";
  77. chomp(my @lines = <$fh>);
  78. close $fh or croak "Couldn't close $file: $!";
  79. return @lines;
  80. }
  81. sub colorize {
  82. my ($color) = @_;
  83. return color(@_) if $use_color;
  84. return '';
  85. }
  86. # Total count of pages that link to this tag:
  87. my $link_count = query_onerow(
  88. 'SELECT COUNT(*) AS COUNT FROM links WHERE target = ?',
  89. $tag
  90. )->{COUNT};
  91. # Count of pages that link to this tag and are log entries:
  92. my $log_count = query_onerow(
  93. 'SELECT COUNT(*) AS COUNT FROM links WHERE target = ? AND page LIKE "log/%"',
  94. $tag
  95. )->{COUNT};
  96. # Head of log for this tag:
  97. my @log_lines;
  98. my $logs_sth = query(
  99. 'SELECT l.page, p.title
  100. FROM links l JOIN pages p ON l.page = p.page
  101. WHERE
  102. l.page LIKE "log/%"
  103. AND l.target = ?
  104. LIMIT 10;',
  105. $tag
  106. );
  107. while (my $row = $logs_sth->fetchrow_hashref()) {
  108. push @log_lines, $row->{title};
  109. }
  110. # List of pages to display for this tag:
  111. my @display_pages;
  112. my $pages_sth = query(
  113. 'SELECT page FROM links
  114. WHERE target = ?
  115. AND page NOT LIKE "log/%"
  116. AND PAGE NOT LIKE "diary/%"',
  117. $tag
  118. );
  119. while (my $row = $pages_sth->fetchrow_hashref()) {
  120. push @display_pages, $row->{page};
  121. }
  122. # XXX: This doesn't quite work, but digikam-extract could be generalized a bit
  123. # with a little work.
  124. # Photos
  125. # my @photos = `digikam-extract --tag "$tag"`;
  126. # Actual display here:
  127. my $bg = colorize('bold green');
  128. my $reset = colorize('reset');
  129. say "$bg$tag$reset: $link_count links ($log_count log entries)";
  130. print "\n";
  131. say "${bg}pages$reset: " . join ' ', @display_pages;
  132. print "\n";
  133. if (on_pinboard($tag)) {
  134. say "${bg}on pinboard${reset}";
  135. print "\n";
  136. }
  137. if (on_p1k3($tag)) {
  138. say "${bg}topic on p1k3${reset}";
  139. print "\n";
  140. }
  141. my $wikifile = File::Spec->catfile($HOME, 'notes', 'vimwiki', "$tag.wiki");
  142. if (-f $wikifile) {
  143. print `head $wikifile`;
  144. } else {
  145. say "No wiki file found for $tag";
  146. }
  147. print "\n";
  148. if ($log_count > 0) {
  149. say "${bg}log:${reset}";
  150. foreach my $line (@log_lines) {
  151. say " - $line";
  152. }
  153. }
  154. # XXX: put photos here
  155. # print "\n";
  156. # if (@photos > 0) {
  157. # say "${bg}photos:${reset}";
  158. # foreach my $photo (@photos) {
  159. # say $photo;
  160. # }
  161. # }