1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Time::Piece;
6 use File::Find;
7
8 my $date = localtime->strftime('%Y-%m-%d');
9
10 # First, every operation starts with going to the ratf dir
11 chdir "/home/dave/wiki/ratf";
12
13 my $cmd = ($ARGV[0] or '');
14
15 if ($cmd eq 'edit') { rat_edit(); }
16 elsif ($cmd eq 'feed') { rat_feed(); }
17 elsif ($cmd eq 'new') { rat_new(); }
18 elsif ($cmd eq 'dirs') { rat_dirs(); }
19 elsif ($cmd eq 'grep') { rat_grep(); }
20 elsif ($cmd eq 'lsd') { rat_lsd(); }
21 elsif ($cmd eq 'pub') { rat_pub(); }
22 elsif ($cmd eq 'card') { rat_card(); }
23 elsif ($cmd eq 'book') { rat_book(); }
24 elsif ($cmd eq 'exif') { rat_exif(); }
25 else {
26 print q{Usage:
27 rat edit <page_name> (lists partial matches)
28 rat feed [<page_name> (lists partial matches)] opens atom.xml
29 rat new <page_name>
30 rat dirs (list all dirs under ratf/src/)
31 rat grep (searches all .adoc files)
32 rat lsd (list all drafts)
33 rat pub
34 rat book (open b/ (books) index)
35 rat exif (strip exif data from images)
36 };
37 }
38
39 # PUB
40 ###########################################################################
41 sub rat_pub {
42 system qw( ./make.rb );
43 system qw( rsync -av --exclude '*~' --exclude '.*' src/ ratf:www/ );
44 }
45
46 # EDIT
47 ###########################################################################
48 sub rat_edit {
49 my $page = $ARGV[1] or die "Edit requires page name!\n";
50
51 my $fname = '';
52
53 if ($page eq 'index') {
54 # exactly 'index' should always open THE index
55 $fname = './src/index.adoc';
56 }
57 else {
58 $fname = pagepicker($page);
59 }
60 print "Cool, opening $fname...\n";
61
62 system ('vim', $fname);
63 }
64
65 # CARD
66 ###########################################################################
67 sub rat_card {
68 my $fname = 'src/cards/index.adoc';
69 print "Cool, opening $fname...\n";
70
71 system ('vim', $fname);
72 }
73
74 # BOOKS
75 ###########################################################################
76 sub rat_book {
77 my $fname = 'src/b/index.adoc';
78 print "Cool, opening $fname...\n";
79
80 system ('vim', $fname);
81 }
82
83 # FEED
84 ###########################################################################
85 sub rat_feed {
86 my $page = $ARGV[1];
87 my $atom = 'src/atom.xml';
88
89 if ($page) {
90 my $fname = pagepicker($page);
91 print "Cool, adding $fname...\n";
92 add_to_feed($fname, $atom);
93 }
94
95 system ('vim', $atom);
96 }
97
98 # EXIF
99 ###########################################################################
100 sub rat_exif {
101 my $output = `git ls-files -o -m --exclude-standard`;
102 my @files = split(/\n/, $output);
103 my $fcount = @files;
104 print "Site has $fcount changed files...\n";
105 foreach (@files) {
106 if($_ =~ /\.(jpg|png)$/){
107 print "Found: $_\n";
108 print `exiftool -all= -overwrite_original_in_place "$_"`
109 }
110 }
111 print "Done.\n";
112 }
113
114 # NEW
115 ###########################################################################
116 sub rat_new {
117 my $page = $ARGV[1] or die "New requires page name!\n";
118
119 my $newfname = "src/$page.adoc";
120
121 die "$newfname already exists!\n(Edit with 'rat edit $page' intead.)\n" if (-e $newfname);
122
123 open(my $fh, '>', $newfname) or die "Could not open '$newfname': $!\n";
124 print $fh qq{:title: $page
125 :subtitle: Stuff
126 :created: $date
127 :updated: $date
128 :updated_reason:
129 :draft: true
130
131 };
132 close $fh;
133
134 print "Editing new $newfname...\n";
135 system ('vim', $newfname);
136 }
137
138 # DIRS
139 ###########################################################################
140 sub rat_dirs {
141 foreach(glob('src/*')){
142 my $withoutsrc = s/src\///r; # 'r' modifier keeps original intact
143 print "\t$withoutsrc\n" if -d;
144 }
145 }
146
147 # GREP
148 ###########################################################################
149 sub rat_grep {
150 my $search = $ARGV[1] or die "Search for what?\n";
151 #find( sub { push @matches, $File::Find::name if ( $File::Find::name =~ /$page.*\.adoc$/ ) }, '.' );
152 system ('ag', '-G', '.adoc$', $search );
153 }
154
155 # LSD (List Drafts)
156 ###########################################################################
157 sub rat_lsd {
158 print "TODO!\n";
159 #find( sub { push @matches, $File::Find::name if ( $File::Find::name =~ /$page.*\.adoc$/ ) }, '.' );
160 }
161
162
163 ###########################################################################
164 ###########################################################################
165
166 sub pagepicker {
167 my $page = shift;
168
169 my @matches = ();
170 find( sub { push @matches, $File::Find::name if ( $File::Find::name =~ /$page.*\.(adoc|html)$/ ) }, '.' );
171
172 my $match_count = scalar @matches;
173
174 if ($match_count > 1) {
175 my $num = 1;
176 print "Which match?\n";
177 print "-------------------\n";
178 print "0. CANCEL\n";
179 for (@matches) {
180 print "$num. $_\n";
181 $num++;
182 }
183 print "-------------------\n";
184 print "> ";
185 my $selection = <STDIN>;
186 chomp $selection;
187 if ($selection == 0) {
188 die "Okay, bye!\n";
189 }
190 $selection--;
191 die "Invalid selection!\n" unless (defined $matches[$selection]);
192
193 return $matches[$selection];
194 }
195 elsif ($match_count == 1)
196 {
197 return $matches[0];
198 }
199
200 die "Found no pages matching '$page'. :-(\n";
201 }
202
203 sub add_to_feed {
204 my $fname = shift;
205 my $atom = shift;
206 my $title = "";
207 my $subtitle = "";
208
209 my $page_path = substr $fname, 6, -5; # chop './src/' off of front, .adoc off end
210
211 open(my $fh, '<', $fname) or die "Could not open '$fname': $!\n";
212 while( my $line = <$fh>) {
213 $title = $1 if($line =~ /:title: (.*)$/);
214 $subtitle = $1 if($line =~ /:subtitle: (.*)$/);
215 last if $line =~ /^\s*$/; # empty line is end of page metadata
216 }
217 close $fh;
218
219 open(my $fh2, '>>', $atom) or die "Could not open '$atom': $!\n";
220 print $fh2 qq{
221 TODO: delete </feed> above!!!!
222 <entry>
223 <title>$title</title>
224 <link href="http://ratfactor.com/$page_path" />
225 <id>http://ratfactor.com/$page_path</id>
226 <updated>${date}T00:00:00Z</updated>
227 <author><name>Dave Gauer</name></author>
228 <summary>
229 $subtitle ...
230 </summary>
231 </entry>
232
233 </feed>};
234 close $fh2;
235 }