Skip to content

Commit

Permalink
Merge branch 'import-org-to-bbd'
Browse files Browse the repository at this point in the history
  • Loading branch information
eserte committed Aug 14, 2024
2 parents 56259ec + 512e862 commit 91d858a
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions miscsrc/org2bbd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env perl
# -*- perl -*-

#
# Author: Slaven Rezic
#
# Copyright (C) 2024 Slaven Rezic. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://www.rezic.de/eserte/
#

use strict;
use warnings;
use POSIX qw(strftime);

my $headline_start_rx = qr{^\*+\s+(TODO|DONE|WAITING|WONTFIX|LATER)\s+};

my $file = shift or die "org mode file?";

my @locations;

open my $fh, '<:encoding(utf-8)', $file
or die "Can't open $file: $!";
my $active_headline;
my $active_headline_is_TODO;
while(<$fh>) {
if (/$headline_start_rx/) {
$active_headline = $_;
$active_headline_is_TODO = $1 eq 'TODO';
}
if ($active_headline_is_TODO && m{\[\[geo:([-+]?\d+(?:\.\d+)?),([-+]?\d+(?:\.\d+)?)(?:\?z=(\d+(?:\.\d+)?))?\](?:\[(.*?)\])?\]}) {
my($lat, $lon, $zoom, $link_title) = ($1,$2,$3,$4); # $zoom not yet used
if (!defined $active_headline) {
warn "Found geo URI for $lat/$lon, but without an associated org-mode headline, skipping...\n";
next;
}
push @locations, {
link_title => (defined $link_title ? $link_title : extract_headline($active_headline)),
lon => $lon,
lat => $lat,
};
}
}

if (!@locations) {
die "No locations found in $file.\n";
}

binmode STDOUT, ':encoding(utf-8)';
print <<EOF;
#: encoding: utf-8
#: map: polar
#:
# Converted from $file at @{[ strftime("%Y-%m-%d %H:%M:%S", localtime) ]}
#
EOF
for my $location (@locations) {
my $link_title = $location->{link_title};
$link_title =~ s{[\t\n]}{ }g;
print "$link_title\tX $location->{lon},$location->{lat}\n";
}

sub extract_headline {
my $headline = shift;
$headline =~ s{$headline_start_rx}{}; # TODO/DONE
$headline =~ s{\[#A-F\]\s*}{}; # priority
$headline =~ s{\s+:.*?:\s*$}{}; # tags
$headline;
}

__END__
=head1 NAME
org2bbd - find org-mode TODO items with geolocations and convert to bbd
=head1 SYNOPSIS
org2bbd TODO.org > TODO.bbd
=head1 TODO
* maybe make it public by moving to bbbike?
* document pipe together with bbd2gpx
* probably a better fallback for the location name would be a string on the same line like the geo link
=cut

0 comments on commit 91d858a

Please sign in to comment.