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.
 
 
 
 
 
 

196 lines
3.6 KiB

#!/usr/bin/env perl
=pod
=head1 NAME
notes-pages - work with pages within my notes
=head1 SYNOPSIS
Currently this is vimwiki-dependent.
# List pages
notes pages
# All pages with a date of July 18th, 2022:
notes pages --date 2022-07-18
# Set output format:
notes pages --format [vimwiki, tsv, location, full, name]
# All pages with a date:
notes pages --with-date
# All pages without a date:
notes pages --no-date
# Print this help:
notes pages --help
# Debug queries
notes pages --debug
=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;
# Display formats:
my %FORMATS = (
vimwiki => sub {
my ($data) = @_;
my $timestamp = '';
if (defined $data->{timestamp}) {
$timestamp = $data->{timestamp} . " ";
}
return ' - '
. $timestamp
. '[[/' . $data->{page} . '|'
. $data->{title}
. "]]\n";
},
tsv => sub {
my ($data) = @_;
my $timestamp = '(null)';
if (defined $data->{timestamp}) {
$timestamp = $data->{timestamp};
}
return (join "\t", ($timestamp, $data->{page}, $data->{title})) . "\n";
},
# vim location list
location => sub {
my ($data) = @_;
my $result = "$ENV{HOME}/notes/vimwiki/$data->{page}.wiki";
if ($data->{title}) {
$result .= ":1:$data->{title}";
} else {
$result .= print ":1:$data->{page}"
}
return $result . "\n";
},
# Bare names of wiki pages (no extension):
name => sub {
my ($data) = @_;
return $data->{page} . "\n";
},
# Fulltext of vimwiki pages:
full => sub {
my ($data) = @_;
my $result = '';
my $pagepath = "$ENV{HOME}/notes/vimwiki/$data->{page}.wiki";
$result .= "%% $pagepath {{{\n\n";
$result .= file_get_contents($pagepath);
$result .= "\n%% }}}\n\n";
},
);
# Handle options, including help generated from the POD above.
my $debug = 0;
my $with_date = 0;
my $date;
my $file;
my $no_date;
my $format = 'location';
GetOptions(
'debug' => \$debug,
'date=s' => \$date,
'format=s' => \$format,
'with-date' => \$with_date,
'no-date' => \$no_date,
'help' => sub { pod2usage(0) },
) or pod2usage(2);
my %where = ();
# Get only links from pages that have a datetime:
if ($with_date) {
# NOT NULL:
$where{'pages.datetime'} = { '!=', undef };
}
# Get only links from pages that lack a datetime:
if ($no_date) {
# IS NULL:
$where{'pages.datetime'} = undef;
}
# Get only links from pages that have a given specific datetime:
if ($date) {
$where{'date'} = $date;
}
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(
'pages',
# timestamp will be null if datetime is
"*,
DATETIME(datetime, 'localtime') AS timestamp,
DATE(datetime, 'localtime') AS date",
\%where,
{-asc => [ 'date', 'page' ] }
);
if ($debug) {
say STDERR $query;
say STDERR Dumper(%where);
}
my $sth = $dbh->prepare($query);
$sth->execute(@bind);
while (my $data = $sth->fetchrow_hashref())
{
if ($debug) {
print STDERR Dumper($data);
}
print $FORMATS{$format}->($data);
}
$sth->execute();
# PHP-style file-content grabbing:
sub file_get_contents {
my ($file) = @_;
open my $fh, '<', $file
or die "Couldn't open $file: $!\n";
my $contents;
{
# line separator:
local $/ = undef;
$contents = <$fh>;
}
close $fh or die "Couldn't close $file: $!";
return $contents;
}
1;