-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate.raku
executable file
·150 lines (141 loc) · 5.5 KB
/
annotate.raku
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env raku
my $p = $*PROGRAM.absolute;
my $path-obj = $p.IO;
my $progdir = $path-obj.dirname;
if $*CWD ne $progdir {
die "Error: script must be run from its own directory: $progdir";
}
my $dir = prompt("Enter a directory path: ");
my $filecount = index( :directory($dir) );
sub index( :$directory, :$subdir = 0 ) {
my $output-file = "$directory/index.html";
my $annotations-file = "$directory/Annotations.txt";
if $directory.IO.d {
if ! $annotations-file.IO.f {
say "The file 'Annotations.txt' does not exist in directory '$directory'.";
return;
}
}
else {
say "The path you entered is not a directory or does not exist.";
say "Path: {$directory.IO}";
return;
}
my %notes;
my $content = ''; my $title = '';
# load titles and notes
my $count = 0;
for $annotations-file.IO.lines -> $line {
next if $line ~~ /^\s*$/; # ignore blank lines
$count++;
if ( $count == 1 ) {
$title = $line;
next;
}
if $line ~~ / ^ (.*?) \: (.+) $ / {
my $filename = $0;
my $annotation = $1;
%notes{$filename} = $annotation;
if ! $filename || ! $annotation {
say "Invalid capture: $filename, $annotation";
}
}
else {
say "Invalid format: $line";
}
}
#say %notes;
# build index from all files in directory
my $filecount = 0; my $totalsubfiles = 0;
my $series = 0;
my $previous_name = ''; my $subdirs;
for $directory.IO.dir.sort -> $file {
# skip Annotations.txt or .Annotations.txt.swp
next if $file.basename ~~ / ^ \.?Annotations\.txt.* $ /;
next if $file.basename eq '.DS_Store';
next if $file.basename eq 'index.html';
# say "File object: {$file.^name}";
if $file.IO.d { # subdirectory recursion
my $count = index( :directory( "$directory/{$file.basename}" ), :subdir(1) );
$totalsubfiles = $totalsubfiles + $count;
$subdirs ~= "<li><a href='{$file.basename}/index.html'> 📁 {$file.basename}</a></li>\n";
}
elsif $file.f { # normal file processing
$filecount++;
my $num; my $name;
# look for files in series like: Fire_01.jpeg
if $file.basename ~~ /(.+?)(\d+)?\..+$/ {
$name = $/[0] // 'None';
$name = $name.subst("_", " ", :g);
if $/[1] {
$series++;
$num = $/[1];
# reset series for first file so name displays
# or reset if we have a new file from new series
if $filecount == 1 || ($previous_name && $previous_name ne $name) {
$series = 0; # reset
}
}
else {
$series = 0; # reset, as it's not a series file
}
}
else {
say "ERROR: file should match regex";
}
if $series > 0 {
$content ~= " - <a href='#' onClick=\"showImg('{$file.basename}');\">{$num}</a> ";
}
else {
if %notes{$file.basename}:exists {
# escape single quotes for JavaScript
# and convert any double into escaped singles
my $notes = %notes{$file.basename}.subst("'", "\\'", :g).subst('"', "\\'", :g);
$content ~= "<li><a href='#' onClick=\"showImg('{$file.basename}', '$notes');\">{$name}</a>: { %notes{ $file.basename } }";
}
else {
$content ~= "<li><a href='#' onClick=\"showImg('{$file.basename}');\">{$name}</a>";
}
}
$content ~= "</li>\n" if $series == 0;
$previous_name = $name;
}
else {
say "This is neither a directory nor a normal file: {$file.basename}";
}
}
# read the template and replace the placeholder
my $template-file = 'index.tmpl';
my $template = $template-file.IO.slurp;
# if we *have* subdirectories
$template ~~ s/'<!-- SUBDIRS -->'/$subdirs/ if $subdirs;
# if we *are* a subdirectory
my $linkup = "<h3><a href='../index.html'>../</a></h3>";
$template ~~ s/'<!-- SUBDIR -->'/$linkup/ if $subdir;
$template ~~ s/'<!-- CONTENT -->'/$content/;
$template ~~ s:g/'<!-- TITLE -->'/$title/;
my $now = DateTime.now;
$now = sprintf '%04d-%02d-%02d %02d:%02d',
$now.year,
$now.month,
$now.day,
$now.hour,
$now.minute;
$template ~~ s:g/'<!-- DATETIME -->'/$now/;
$template ~~ s:g/'<!-- COUNT -->'/$filecount/;
my $total = $totalsubfiles + $filecount;
$template ~~ s:g/'<!-- TOTAL -->'/($total total)/ if $total != $filecount;
# pick a random image to display for spice
my @images = $directory.IO.dir
.grep(*.IO.f) # only files
.grep({ $_.extension ne 'txt' }) # exclude .txt file
.map(*.basename); # get the filenames
my $randomimg = @images.pick;
$template ~~ s:g/'<!-- RANDOM_IMAGE -->'/$randomimg/;
my $caption = "$randomimg: { %notes{$randomimg} // '' }";
$template ~~ s:g/'<!-- RANDOM_IMAGE_CAPTION -->'/$caption/;
# write the output to index.html
$output-file.IO.spurt($template);
say "Generated 'index.html' successfully at: $output-file";
return $filecount;
}