|
|
- #!/usr/bin/env perl
-
- =pod
-
- =head1 NAME
-
- notes-tag-summary - show a summary of a given tag
-
- =head1 SYNOPSIS
-
- Currently this is vimwiki-dependent.
-
- # Show tag info for warelogging
- notes-tag-summary warelogging
-
- =head1 AUTHOR
-
- Brennen Bearnes
-
- =cut
-
- use warnings;
- use strict;
- use 5.10.0;
-
- use Carp;
- use Cwd;
- use Data::Dumper;
- use DBI;
- use File::Basename;
- use File::Spec;
- use Getopt::Long;
- use Pod::Usage;
- use File::HomeDir;
- use Term::ANSIColor;
-
- # Handle options, including help generated from the POD above.
- my $debug = 0;
- my $use_color = 0;
- my $from_filename = 0;
- GetOptions(
- 'color' => \$use_color,
- 'file' => \$from_filename,
- 'debug' => \$debug,
- 'help' => sub { pod2usage(0) },
- ) or pod2usage(2);
-
- my $tag = $ARGV[0];
-
- unless ($tag) {
- pod2usage(2);
- die;
- }
-
- # Pull a tag name out of a path to a wiki page:
- if ($from_filename) {
- my ($name, $path, $suffix) = fileparse($tag, '.wiki');
- $tag = $name;
- }
-
- my $HOME = File::HomeDir->my_home;
- {
- my $dbfile = $HOME . "/notes/metadata.db";
- my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", "", "");
-
- sub query_onerow {
- return query(@_)->fetchrow_hashref();
- }
-
- sub query {
- my ($query, @bind) = @_;
- my $sth = $dbh->prepare($query);
- $sth->execute(@bind);
- return $sth;
- }
- }
-
- sub on_p1k3 {
- state %lines = map { $_ => 1 } file_get_contents_arr(
- File::Spec->catfile($HOME, 'notes', 'p1k3-index')
- );
- my ($tag) = @_;
- return exists $lines{$tag};
- }
-
- sub on_pinboard {
- state %lines = map { $_ => 1 } file_get_contents_arr(
- File::Spec->catfile($HOME, 'notes', 'pinboard-tag-index')
- );
- my ($tag) = @_;
- return exists $lines{$tag};
- }
-
- sub file_get_contents_arr {
- my ($file) = @_;
-
- open my $fh, '<', $file
- or croak "Couldn't open $file: $!\n";
-
- chomp(my @lines = <$fh>);
-
- close $fh or croak "Couldn't close $file: $!";
-
- return @lines;
- }
-
- sub colorize {
- my ($color) = @_;
- return color(@_) if $use_color;
- return '';
- }
-
- # Total count of pages that link to this tag:
- my $link_count = query_onerow(
- 'SELECT COUNT(*) AS COUNT FROM links WHERE target = ?',
- $tag
- )->{COUNT};
-
- # Count of pages that link to this tag and are log entries:
- my $log_count = query_onerow(
- 'SELECT COUNT(*) AS COUNT FROM links WHERE target = ? AND page LIKE "log/%"',
- $tag
- )->{COUNT};
-
- # Head of log for this tag:
- my @log_lines;
- my $logs_sth = query(
- 'SELECT l.page, p.title
- FROM links l JOIN pages p ON l.page = p.page
- WHERE
- l.page LIKE "log/%"
- AND l.target = ?
- LIMIT 10;',
- $tag
- );
- while (my $row = $logs_sth->fetchrow_hashref()) {
- push @log_lines, $row->{title};
- }
-
- # List of pages to display for this tag:
- my @display_pages;
- my $pages_sth = query(
- 'SELECT page FROM links
- WHERE target = ?
- AND page NOT LIKE "log/%"
- AND PAGE NOT LIKE "diary/%"',
- $tag
- );
- while (my $row = $pages_sth->fetchrow_hashref()) {
- push @display_pages, $row->{page};
- }
-
- # XXX: This doesn't quite work, but digikam-extract could be generalized a bit
- # with a little work.
-
- # Photos
- # my @photos = `digikam-extract --tag "$tag"`;
-
- # Actual display here:
-
- my $bg = colorize('bold green');
- my $reset = colorize('reset');
- say "$bg$tag$reset: $link_count links ($log_count log entries)";
- print "\n";
- say "${bg}pages$reset: " . join ' ', @display_pages;
- print "\n";
-
- if (on_pinboard($tag)) {
- say "${bg}on pinboard${reset}";
- print "\n";
- }
-
- if (on_p1k3($tag)) {
- say "${bg}topic on p1k3${reset}";
- print "\n";
- }
-
- my $wikifile = File::Spec->catfile($HOME, 'notes', 'vimwiki', "$tag.wiki");
- if (-f $wikifile) {
- print `head $wikifile`;
- } else {
- say "No wiki file found for $tag";
- }
-
- print "\n";
- if ($log_count > 0) {
- say "${bg}log:${reset}";
- foreach my $line (@log_lines) {
- say " - $line";
- }
- }
-
- # XXX: put photos here
- # print "\n";
- # if (@photos > 0) {
- # say "${bg}photos:${reset}";
- # foreach my $photo (@photos) {
- # say $photo;
- # }
- # }
|