Browse code

press: add press/press.pl to ease updates

traumschule authored on11/09/2018 13:11:21
Showing1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,59 @@
1
+#!/usr/bin/env perl
2
+# Returns html rows based on csv lines
3
+# Usage: /path/to/script /path/to/csv/file > file
4
+use strict;
5
+use warnings;
6
+#use HTML::Escape qw/escape_html/;
7
+
8
+my $str; # append all strings here
9
+
10
+sub print_rows {
11
+  my $string = shift;
12
+
13
+  # define where to output to:
14
+  #  1) STDOUT
15
+  print "$string";
16
+
17
+  #  2) press.html
18
+  #open my $out, '>>', 'press.html';
19
+  #print $out $string;
20
+  #  3) escaped htmlfile (install module above: cpan -i HTML::Escape
21
+  #print $out escape_html($string);
22
+  #close $out;
23
+}
24
+sub add_row {
25
+  $str .= shift;
26
+}
27
+sub parse_line {
28
+  my $str = shift;
29
+  if ($str =~ /(\d+\/\d+\/\d+),([^,]+),(.+),(.+)/) { # magic regex :)
30
+      chomp(my $date = qx/LANG=en_US.UTF-8 date -d "$1" "+%Y %B %d"/);
31
+      chomp(my $source = $2);
32
+      my $name = $3;
33
+      my $url = $4;
34
+      my $string = "
35
+<tr>
36
+<td>$date</td>
37
+<td>$source</td>
38
+<td><a href=\"$url\">$name</a></td>
39
+</tr>
40
+";
41
+    add_row $string;
42
+  }
43
+}
44
+
45
+foreach my $arg (@ARGV) {
46
+  chomp($arg);
47
+  if (-f $arg) {
48
+    open my $fh, '<', $arg
49
+      or warn "Can't open '$arg': $!\n" and next;
50
+    foreach (<$fh>) {
51
+      parse_line $_;
52
+    }
53
+    close $fh;
54
+  } else {
55
+    parse_line $arg;
56
+  }
57
+}
58
+if ($str) { print_rows $str; }
59
+else { print "Nothing found.\n"; exit 1; }