aboutsummaryrefslogtreecommitdiff
path: root/stest.pl
blob: 5480f8f1372c5f12caebac246bfed7fb609d3840 (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
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
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/perl 
use strict;
use warnings;
use Slay::Maker;
use File::Next;
use Path::Class;
use Template;
use File::Cache::Parsed;
use Cwd 'abs_path';
use Text::Restructured;
use Text::Restructured::Writer::LibXML;
 
my $stash={};
my $template=Template->new();
my $rest=Text::Restructured->new({},'WebCoso');
 
my $fc=File::Cache::Parsed->new();
$fc->add_parser(qr{\.rest\.tt2?$} =>
                    sub {
                        my ($name,$content)=@_;
                        my $real_name=abs_path($name);
                        my $output;
                        $template->process($real_name,
                                           {%$stash,name=>$name},
                                           \$output);
                        return $output;
                    });
$fc->add_parser(qr{\.rest\.txt$} =>
                    sub {
                        my ($name,$content)=@_;
                        my $dudom=$rest->Parse($content,$name);
                        return Text::Restructured::Writer::LibXML
                            ->new->ProcessDOM($dudom);
                    });
 
sub expandTT {
    my ($maker,$target,$deps,$matches)=@_;
 
    print "TT: $target <- @$deps@$matches\n";
    open my $fh,'>',$target;
}
 
sub parseRST {
    my ($maker,$target,$deps,$matches)=@_;
 
    print "RST: $target <- @$deps@$matches\n";
    open my $fh,'>',$target;
}
 
sub du2html {
    my ($maker,$target,$deps,$matches)=@_;
 
    print "HTML: $target <- @$deps@$matches\n";
    open my $fh,'>',$target;
}
 
sub getTags {
    my ($maker,$target,$deps,$matches)=@_;
 
    print "tags: $target <- @$deps@$matches\n";
    open my $fh,'>',$target;
}
 
 
sub getChanges {
    my ($maker,$target,$deps,$matches)=@_;
 
    print "changes: $target <- @$deps@$matches\n";
    open my $fh,'>',$target;
}
 
sub ifExists {
    my ($src)=@_;
    return sub {
        my ($maker,$target,$matches)=@_;
        my $dep=Slay::MakerRule::var_expand_dep($src,$target,$matches);
        return if -e $target and ! -e $dep;
        return $dep;
    }
}
 
sub fromTo {
    my ($base,$opts)=@_;
    my $iter=File::Next::files(
        {
            file_filter=>$opts->{files},
            descend_filter=>$opts->{dirs},
        },
        $base);
    my (@ret,$file);
    if (defined $opts->{transform}) {
        push @ret,$opts->{transform}->($filewhile $file=$iter->();
    }
    else {
        push @ret,$file while $file=$iter->();
    }
    return @ret;
}
 
{
my %order=(
    'document.rest.tt'=>0,
    'document.rest.txt'=>1,
    'document.du.xml'=>2,
);
sub earliest {
    my ($a,$b)=@_;
    return $a unless $b;
    return $order{$a} < $order{$b}
        $a
        $b;
}
}
 
sub keepEarliest {
    my %dirs;
    for my $f (@_) {
        my $c=file($f);
        $dirs{$c->parent}=earliest($c->basename,$dirs{$c->parent});
    }
    my @ret;
    while (my ($d,$f)=each %dirs) {
        push @ret,file($d,$f)->stringify;
    }
    return @ret;
}
 
my %files=(files=>sub{m{^document\.}});
 
my $maker=Slay::Maker->new({
    rules => [
        ['src/(**)/document.rest.txt',':',ifExists('src/$1/document.rest.tt'),'=',\&expandTT],
        ['src/(**)/document.du.xml',':',ifExists('src/$1/document.rest.txt'),'=',\&parseRST],
        ['deps/tags.xml',':',fromTo('src/',{%files,transform=>sub{my $s=shift;$s=~s{\..*$}{.du.xml};$s}}),'=',\&getTags],
        ['deps/changes.xml',':',keepEarliest(fromTo('src/',{%files})),'=',\&getChanges],
        ['dst/(**)/document.xhtml',':','deps/tags.xml','deps/changes.xml',ifExists('src/$1/document.du.xml'),'=',\&du2html],
    ],
    options => {
        auto_create_dirs => 1,
        #detect_no_diffs => 1, 
        #detect_no_size_change => 1, 
        #debug => 1, 
    },
});
 
my @targets=fromTo('src/',
                   {
                       %files,
                       transform=>sub{
                           my $src=shift;
                           $src=~s{\..*$}{.xhtml};
                           $src=~s{^src/}{dst/};
                           return $src;
                       },
                   });
 
print "targets: @targets\n";
$maker->make(@targets);