forked from petdance/bobby-tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrank.pl
executable file
·129 lines (111 loc) · 3.92 KB
/
crank.pl
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
#!/usr/bin/perl -w
use strict;
use warnings;
eval 'use Carp::Always'; # Not everyone has it
use Getopt::Long;
use File::Slurp;
use Encode qw(decode encode);
use Locale::Messages qw(bind_textdomain_codeset);
use Locale::TextDomain qw(com.bobby-tables share/locale);
use POSIX qw(setlocale LC_ALL);
setlocale(LC_ALL, q());
bind_textdomain_codeset 'com.bobby-tables' => 'UTF-8';
use Text::Markdown ();
use Template ();
use Template::Constants qw( :debug :chomp );
use URI qw();
my $sourcepath = 's';
my $buildpath = 'build';
GetOptions(
'sourcepath:s' => \$sourcepath,
'buildpath:s' => \$buildpath,
) or exit;
-d $buildpath && -w $buildpath or die;
my $home = decode 'UTF-8', __('Home');
my $translations = decode 'UTF-8', __('Translations');
my $about = decode 'UTF-8', __('About');
my $pages = [
index => $home,
about => $about,
asp => 'ASP',
coldfusion => 'ColdFusion',
csharp => 'C#',
delphi => 'Delphi',
dotnet => '.NET',
java => 'Java',
perl => 'Perl',
php => 'PHP',
plsql => 'PL/SQL',
postgresql => 'PostgreSQL',
python => 'Python',
ruby => 'Ruby',
scheme => 'Scheme',
translations => $translations,
];
MAIN: {
my $m = Text::Markdown->new;
my @sidelinks;
my %tt_first_pass_defaults = ( FILTERS => { 'loc' => [
sub {
my ($context, $arg) = @_;
return sub {
my ($key) = @_;
return '' if $key eq '';
my $value = __x(encode('UTF-8', $key), currlang => $arg);
return decode 'UTF-8', $value;
};
}, 1 # dynamic filter
] } );
my %tt_defaults = (
INCLUDE_PATH => [ qw( tt ) ],
OUTPUT_PATH => $buildpath,
DEBUG => DEBUG_UNDEF,
TRIM => CHOMP_ALL,
PRE_CHOMP => 1,
POST_CHOMP => 1,
ENCODING => 'utf8',
%tt_first_pass_defaults,
);
my $tt = Template->new( \%tt_defaults );
my $tt_first_pass = Template->new( \%tt_first_pass_defaults );
my @pages = @{$pages};
while ( @pages ) {
my ($section,$desc) = splice( @pages, 0, 2 );
my $path = ($section eq 'index') ? './' : "./$section.html";
push( @sidelinks, {
path => $path,
text => $desc,
} );
}
my $vars = {
sidelinks => \@sidelinks,
};
$vars->{rel_static} = ( 'C' eq $ENV{LANG} ) ? q(./) : q(../); # path prefix to static assets
$vars->{rfc_1766_lang} = ( 'C' eq $ENV{LANG} ) ? 'en' : [map {tr/_/-/;$_} $ENV{LANG}]->[0];
@pages = @{$pages};
while ( @pages ) {
my ($section,$desc) = splice( @pages, 0, 2 );
my $source = read_file( "$sourcepath/$section.md.tt2", { binmode => ':encoding(UTF-8)' } );
my $first_pass;
$tt_first_pass->process( \$source, undef, \$first_pass, { binmode => ':encoding(UTF-8)' } )
|| die sprintf("file: %s\nerror: %s\n", "$sourcepath/$section.md.tt2", $tt->error);
my $html = $m->markdown($first_pass);
$html =~ s{<code>\n}{<code>}smxg;
$vars->{body} = $html;
$vars->{section} = ($section eq 'index') ? '.' : "$section.html";
$vars->{currlang} = ( $desc eq $home or $desc eq $about or $desc eq $translations ) ? '' : $desc;
{
local $ENV{LANG} = 'C';
open my $git, '-|', qw(git log --pretty=format:commit-hash=%h;ref-names=%d;committer-date-relative=%cr);
if ($git) {
chomp(my $log = <$git>);
my $u = URI->new;
$u->scheme('tag');
$u->opaque("bobby-tables.com,2012:$log");
$vars->{git} = $u->as_string;
}
}
$tt->process( 'page.tt', $vars, "$section.html", { binmode => ':encoding(UTF-8)' } )
|| die sprintf("file: %s\nerror: %s\n", "$section.html", $tt->error);
}
}