Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 123 lines (100 sloc) 3.273 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 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
#!/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.