Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 124 lines (100 sloc) 3.2 KB
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
use File::Slurp;
use Text::Markdown ();
use Template ();
use Template::Constants qw( :debug :chomp );
use File::Basename;
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;
MAIN: {
my $markdown = Text::Markdown->new;
my %tt_defaults = (
INCLUDE_PATH => [ qw( tt ) ],
OUTPUT_PATH => $buildpath,
DEBUG => DEBUG_UNDEF,
TRIM => CHOMP_ALL,
PRE_CHOMP => 1,
POST_CHOMP => 1,
ENCODING => 'utf8',
);
my $tt = Template->new( \%tt_defaults );
my %default_vars = (
rfc_1766_lang => 'en',
);
my @allpages;
my @sourcefiles = glob( "$sourcepath/*.{md,html}" );
check_for_dupe_sources( @sourcefiles );
for my $sourcefile ( @sourcefiles ) {
my @source = read_file( $sourcefile );
my $desc = $source[0];
my $html;
my $outfile = basename( $sourcefile );
if ( $outfile =~ s/\.md$/.html/ ) {
$desc = $source[0];
$desc =~ s/^# // or die "$sourcefile doesn't start with a # heading";
$html = $markdown->markdown( join( '', @source ) ),
}
else {
$source[0] =~ /<!-- (.+) -->/ or die "Can't find a description comment on the first line of $sourcefile";
$desc = $1;
$html = join( '', @source );
}
push( @allpages, {
desc => $desc,
outfile => $outfile,
} );
# Convert Markdown to HTML and then process it in the template.
my $vars = {
%default_vars,
body => $html,
title => $desc,
base => $local ? $buildpath : 'http://89starts.com',
};
print "$sourcefile -> $outfile\n";
$tt->process( 'page.tt', $vars, $outfile, { binmode => ':encoding(UTF-8)' } )
|| die sprintf( "file: %s\nerror: %s\n", $outfile, $tt->error );
}
my $md_content = build_list_from_allpages( @allpages );
my $vars = {
%default_vars,
body => $markdown->markdown( $md_content ),
title => 'All pages',
base => $local ? $buildpath : 'http://89starts.com',
};
my $outfile = 'all-pages.html';
$tt->process( 'page.tt', $vars, $outfile, { binmode => ':encoding(UTF-8)' } )
|| die sprintf( "file: %s\nerror: %s\n", $outfile, $tt->error );
}
sub build_list_from_allpages {
my @pages = @_;
my @bullets;
for my $page ( @pages ) {
push( @bullets, sprintf( '* [%s](%s)', $page->{desc}, $page->{outfile} ) );
}
return join( "\n", @bullets );
}
sub check_for_dupe_sources {
my @files = @_;
my $has_dupes;
my %dupes;
for my $file ( @files ) {
$file = basename( $file );
$file =~ /^([a-z0-9-]+)\.(\w+)$/ or die "This filename doesn't look right to me: $file\n";
my ($base,$ext) = ($1,$2);
if ( $dupes{$base} ) {
die "$file conflicts with $dupes{$base}";
}
$dupes{$base} = $file;
}
return;
}
Something went wrong with that request. Please try again.