public
Description: the Rx schema and validation system
Homepage: http://rjbs.manxome.org/rx
Clone URL: git://github.com/rjbs/rx.git
rx / util / build-www.pl
100755 61 lines (52 sloc) 1.436 kb
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
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use File::Find::Rule;
use File::Path;
use JSON::XS;
use Text::Template;
 
my $template = Text::Template->new(
  TYPE => 'FILE',
  SOURCE => 'www/src/TEMPLATE',
  DELIMITERS => [ qw({{ }}) ],
);
 
rmtree 'www/out';
mkpath 'www/out';
 
for my $file (File::Find::Rule->file->in('www/src')) {
  next if $file =~ /TEMPLATE/;
  my @parts = split m{/}, $file;
  my $leaf = pop @parts;
  shift @parts for (1 .. 2);
  my $path = join '/', @parts;
  mkpath "www/out/$path";
 
  my @coretypes = sort map { s{.+/}{}; s{.html}{}; $_ }
                  File::Find::Rule->file->in('www/src/coretype');
 
  if ($leaf =~ /\.html/) {
    open my $fh, '>', "www/out/$path/$leaf";
    my $content = `cat $file`;
    my %stash = (
      depth => \(scalar @parts),
      root => '../' x @parts,
      coretypes => \@coretypes,
      ct_page => \(scalar $path =~ /coretype$/),
    );
 
    my $filled_content = Text::Template->fill_this_in(
      $content,
      DELIMITERS => [ '{{', '}}' ],
      HASH => \%stash
    );
    die "template error: $Text::Template::ERROR" unless $filled_content;
 
    my $html = $template->fill_in(
      DELIMITERS => [ '{{', '}}' ],
      HASH => {
        content => \$filled_content,
        %stash
      }
    );
    die "template error: $Text::Template::ERROR" unless $html;
 
    print $fh $html;
  } else {
    `cp $file www/out/$path`;
  }
}