forked from beyondgrep/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crank
executable file
·114 lines (88 loc) · 2.63 KB
/
crank
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
#!/usr/bin/env perl
use strict;
use warnings;
use Carp::Always;
use Getopt::Long;
use List::MoreUtils qw( apply );
use Template ();
use Template::Constants qw( :debug :chomp );
use URI::file;
my $LATEST = 'v3.7.0';
MAIN: {
my $sourcepath = 's';
my $buildpath = 'build';
my $local;
GetOptions(
'sourcepath:s' => \$sourcepath,
'buildpath:s' => \$buildpath,
'local' => \$local,
) or exit;
-d $buildpath && -w $buildpath or die;
# XXX This should not be hardcoded
my @pages = get_page_names( $sourcepath );
my %tt_settings = (
INCLUDE_PATH => [ qw( tt tt/wrapper ) ],
OUTPUT_PATH => $buildpath,
DEBUG => DEBUG_UNDEF,
TRIM => CHOMP_ALL,
PRE_CHOMP => CHOMP_COLLAPSE,
POST_CHOMP => CHOMP_ALL,
ENCODING => 'utf8',
);
my $tt = Template->new( \%tt_settings );
for my $page ( @pages ) {
my $source = "$page.ttml";
my $target;
if ( $page eq 'index' ) {
$target = 'index.html';
}
else {
mkdir( "$buildpath/$page" );
$target = "$page/index.html";
}
my $vars = {
base => $local ? local_url( $buildpath ) : 'https://beyondgrep.com/',
index => $local ? 'index.html' : '',
};
$tt->process( $source, $vars, $target, { binmode => ':encoding(UTF-8)' } )
or die sprintf( "file: %s\nerror: %s\n", $target, $tt->error );
}
my $in_body;
my $output = <<'END_PREAMBLE';
[% WRAPPER wrapper/page.ttml title='Man Page' base='' %]
<div class='content'>
END_PREAMBLE
open my $pipe, '-|', 'pod2html', "static/ack-$LATEST" or die "Unable to open pipe to pod2html: $!";
while ( <$pipe> ) {
# I know, I know, I know... HTML + regexes = bad.
if ( !$in_body && /<body/ ) {
$in_body = 1;
}
elsif ( $in_body ) {
if ( m{</body} ) {
$in_body = 0;
}
else {
$output .= $_;
}
}
}
close $pipe;
$output .= <<'END_POSTAMBLE';
</div>
[% END %]
END_POSTAMBLE
$tt->process( \$output, { index => 'index.html' }, "documentation/ack-$LATEST-man.html", { binmode => ':encoding(UTF-8)' })
or die "Failed to something something: " . $tt->error;
}
exit 0;
sub get_page_names {
my $sourcepath = shift;
my @files = glob( "$sourcepath/*.ttml" );
my @pages = apply { s{^\Q$sourcepath\E/(.+)\.ttml$}{$1} or die } @files;
return @pages;
}
sub local_url {
my $path = shift;
return URI::file->new_abs( $path )->as_string . '/';
}