summaryrefslogtreecommitdiff
path: root/search/bin/build-splashes
blob: d5fd5c1d1dc74b924e0e2ad2caf1a920e7ea1b0e (plain)
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
#!/usr/bin/perl

my $TEMPLATE_DIR = "../template";
my $BASE_DIR = "/var/www/vhosts/carbonpictures.com/httpdocs/splash";
my $OUT_FILE = "/var/www/vhosts/carbonpictures.com/httpdocs/splash/index.html";
my @ab = qw[b c d f g h j k l m n p q r s t v w x y z];
unshift @ab, undef;

# disabled for now
# build_site(read_splashes());

sub build_site
  {
  my ($years) = @_;
  carp("Building site!");
  my $t_page = slurp_template("splash_list_page");
  my $t_year = slurp_template("splash_year");
  my $t_month = slurp_template("splash_month");
  my $t_day = slurp_template("splash_day");
  my $yeas = "";
  foreach my $year (sort keys %$years)
    {
    carp($year);
    my $mons = "";
    foreach my $month (sort keys %{ $years->{$year} })
      {
      my $days = "";
      foreach my $day (sort {$a cmp $b} keys %{ $years->{$year}->{$month} })
        {
        my $d = $years->{$year}->{$month}->{$day};
        $days .= templatize($t_day, $d);
        }
      $mons .= templatize($t_month, { month => $month, days => $days });
      }
    $yeas .= templatize($t_year, { year => "20".$year, months => $mons });
    }
  my $page = templatize($t_page, { years => $yeas } );
  vomit($OUT_FILE, $page);
  }
sub templatize
  {
  my ($t, $o) = @_;
  while ($t =~ /\%\%([^\%]+)\%\%/)
    {
    my $val = $o->{lc $1};
    $t =~ s/\%\%$1\%\%/$val/g;
    }
  return $t;
  }
sub read_splashes
  {
  my $years = {};
  foreach my $y (grep /^\d/, slurp_dir($BASE_DIR))
    {
    my $months = {};
    $years->{$y} = $months;
    foreach my $m (grep /^\d/, slurp_dir("$BASE_DIR/$y"))
      {
      my $days = {};
      $months->{$m} = $days;
      foreach my $d (grep /^\d/, slurp_dir("$BASE_DIR/$y/$m"))
        {
        my $abi = 0;
        foreach my $f (sort grep /html$/, slurp_dir("$BASE_DIR/$y/$m/$d"))
          {
          my $is_index = $f eq "index.html";
          if ($is_index)
            { $k = $d; }
          else
            { $k = "$d".$ab[$abi++]; }
          my $d =
            {
            key => $k,
            url => "/splash/$y/$m/$d/" . ($is_index ? undef : $f),
            };
          $days->{$k} = $d;
          }
        }
      }
    }
  return $years;
  }

sub slurp_dir
  {
  my ($d) = @_;
  my @files;
  opendir D, $d;
  while (my $f = readdir(D))
    { push(@files,$f) if $f !~ /^\./; }
  closedir D;
  return @files;
  }
sub slurp_template
  { return slurp(join "/", $TEMPLATE_DIR, @_); }
sub slurp
  {
  my ($f) = @_;
  open F, $f; my @lines = <F>; close F;
  return join "", @lines;
  }
sub vomit 
  {
  my ($f, $t) = @_;
  carp("Writing $f");
  open F, ">", $f || die "couldn't open $f : $!"; print F $t; close F;
  }
sub carp
  {
  my ($m) = @_;
  print STDERR $m . "\n";
  }