-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.pl
executable file
·91 lines (72 loc) · 1.72 KB
/
graph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env perl
=head1 graph.pl
Generate the input for Graphviz C<dot(1)> to generate the graph of notes
in the current directory and the links between them.
=head2 USAGE
cd ~/.deft
.../graph.pl | dot -Tx11
or
.../graph.pl | dot -Tpdf -o graph.pdf
=cut
use warnings;
use strict;
use 5.010;
use autodie;
use Getopt::Long;
my $tags = 1;
GetOptions(
'tags!' => \$tags,
) or die "Bad command line arguments\n";
my %titles;
sub title (_) {
my $filename = shift;
if (exists($titles{$filename})) {
$_ = $titles{$filename};
return $_;
}
my $title;
open(my $f, '<', $filename);
while (<$f>) {
$title = $1 and last if /^#\+TITLE:\s*(.*)/
}
close $f;
$titles{$filename} = $title // $filename =~ s/\.org$//r;
$_ = $titles{$filename};
return $_;
}
sub all_tags {
my @files = @_;
my %tags;
for ("@files" =~ /_([^_.]+)/g) {
$tags{$_}++;
}
return keys %tags;
}
my @files = sort <*.org>;
my @tags = all_tags @files;
say '#!/usr/bin/env -S dot -Tx11';
say 'digraph org {';
say ' packmode = "node"';
# say ' rankdir = LR';
print "\n";
if ($tags) {
for my $tag (@tags) {
my @tagged_files = grep /_$tag[_.]/, @files;
say qq( "#$tag" [shape=box]);
print qq( "#$tag" -> {);
print join " ", map {title; qq("$_")} @tagged_files;
say '}';
print "\n";
}
}
for my $file (@files) {
my $file_title = title $file;
my $file_id = $file =~ s/^( \d{8} T? \d{6} ) -- .*/$1/xr;
my @links = `grep -l -F ":$file_id" *.org`;
print "\n";
print ' {';
print join " ", map {chomp; title; qq("$_")} @links;
say qq(} -> "$file_title");
say qq( "$file_title" [URL="$file"]);
}
say '}';