Skip to content

Instantly share code, notes, and snippets.

@skx
Created October 3, 2014 14:19
Show Gist options
  • Save skx/ae316bf8edf674019ee2 to your computer and use it in GitHub Desktop.
Save skx/ae316bf8edf674019ee2 to your computer and use it in GitHub Desktop.

Revisions

  1. skx created this gist Oct 3, 2014.
    118 changes: 118 additions & 0 deletions StaticPages.pm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,118 @@
    package Chronicle::Plugin::Generate::StaticPages;


    use strict;
    use warnings;


    =begin doc
    Create a table for the static-pages.
    =end doc
    =cut

    sub on_db_create
    {
    my ( $self, %args ) = (@_);

    #
    # Create the "pages" table
    #
    my $dbh = $args{'dbh'};

    $dbh->do( "CREATE TABLE pages (id INTEGER PRIMARY KEY, filename, title, content) " );


    }


    =begin doc
    Don't generate a blog/tag/archive entry if we have a "page: 1" header,
    instead insert the post into the static-page table.
    =end doc
    =cut

    sub on_insert
    {
    my ( $self, %args ) = (@_);

    #
    # The post data and DB-handle.
    #
    my $data = $args{ 'data' };
    my $dbh = $args{ 'dbh' };

    #
    # Is this a page?
    #
    my $page = $data->{ 'page' };
    if ( $page )
    {
    print "Treating page as static $data->{'file'}\n";

    #
    # Insert into the static-pages
    #
    my $sql = $dbh->prepare( "INSERT INTO pages (title,filename,content) VALUES( ?, ? , ? )" ) or die "Failed to prepare";

    $sql->execute( $data->{'title'},
    $data->{'output'},
    $data->{'body'} )
    or die "Failed to insert";
    $sql->finish();

    #
    # Don't allow this to be treated as normal.
    #
    return undef;
    }
    else
    {
    #
    # Allow proceeding as normal
    #
    return ($data);
    }
    }



    sub on_generate
    {
    my ( $self, %args ) = (@_);

    my $dbh = $args{ 'dbh' };
    my $config = $args{ 'config' };


    my $pages = $dbh->prepare("SELECT filename,content,title FROM pages" )
    or die "Failed to find static-pages";
    $pages->execute() or die "Failed to execute query";


    my( $filename,$content,$title);

    $pages->bind_columns( undef, \$filename, \$content, \$title );

    while( $pages->fetch() )
    {
    #
    # TODO: Load a template and populate it.
    #

    #
    # TODO: Write to disk.
    #
    print "Writing Title:$title -> $config->{'output'}/$filename\n";
    }

    $pages->finish();
    }


    1;
    7 changes: 7 additions & 0 deletions about.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Subject: About Steve
    Output: about.html
    Page: 1
    Date: 7th March 2012


    This is my static /about.html page.