Skip to content
Browse files

added drafts: preview articles before publishing

  • Loading branch information...
1 parent 3cb96b2 commit f37ff3811f0c435ac6124b800b05bf95852a2b3b @memowe memowe committed
View
11 README.md
@@ -14,11 +14,12 @@ And here's how it works:
Look at
-1. the `articles` directory to see examples
-2. the `pages` directory to see examples
-3. the `bootylite.conf` file to change the Bootylite parameters.
-4. the templates (`bootylite.pl inflate`) to customize Bootylite
-5. the code.
+1. the `articles` directory to see examples.
+2. the `drafts` directory to see how to preview articles before publishing.
+3. the `pages` directory to see examples for content pages.
+4. the `bootylite.conf` file to change the Bootylite parameters.
+5. the templates (`bootylite.pl inflate`) to customize Bootylite
+6. the code.
The renderer for articles and pages is determined by the file name extension.
These renderers ship with Bootylite:
View
3 bootylite.conf
@@ -7,6 +7,9 @@
encoding => 'utf-8',
+ drafts_dir => app->home->rel_dir('drafts'),
+ drafts_url => '/drafts',
+
refresh_url => '/refresh',
name => 'Bootylite',
View
21 bootylite.pl
@@ -21,6 +21,7 @@
my $booty = Bootylite->new(
articles_dir => $config->{articles_dir},
pages_dir => $config->{pages_dir},
+ drafts_dir => $config->{drafts_dir},
encoding => $config->{file_encoding},
);
app->helper(booty => sub { $booty });
@@ -227,6 +228,21 @@
$plugins->call_tag_feed($self);
} => 'tag_feed';
+# render articles before publishing
+get $config->{drafts_url} . '/:draft_url' => sub {
+ my $self = shift;
+
+ # get draft
+ my $url = $self->param('draft_url');
+ my $draft = $self->booty->get_draft($url);
+ $self->render_not_found and return unless $draft;
+
+ # store
+ $self->stash(draft => $draft);
+
+ $plugins->call_draft($self);
+} => 'draft';
+
# refresh the bootylite
get $config->{refresh_url} => sub {
my $self = shift;
@@ -332,6 +348,11 @@
<div id="content"><%== content2html $page %></div>
</div>
+@@ draft.html.ep
+% layout 'bootyblack';
+% title config('name') . ' - ' . $draft->meta->{title};
+%= include 'show_article', article => $draft, single => 1
+
@@ feed.xml.ep
<?xml version="1.0" encoding="<%= config 'encoding' %>"?>
<feed xmlns="http://www.w3.org/2005/Atom">
View
4 drafts/2011-04-05-23-42_draft1.md
@@ -0,0 +1,4 @@
+Title: Article draft
+Tags: foo, bar
+
+This is a *draft*.
View
36 lib/Bootylite.pm
@@ -1,14 +1,17 @@
package Bootylite;
use Mojo::Base -base;
+use Bootylite::Page;
use Bootylite::Article;
use Mojo::Loader;
has articles_dir => sub { die 'no articles directory given' };
has pages_dir => sub { die 'no pages directory given' };
+has drafts_dir => sub { die 'no drafts directory given' };
has encoding => 'utf-8';
has articles => sub { shift->_build_articles }; # aref of Articles
has pages => sub { shift->_build_pages }; # aref of Pages
+has drafts => sub { shift->_build_drafts }; # aref of Articles
has renderers => sub { shift->_build_renderers }; # href: ext => renderer
sub _build_articles {
@@ -58,6 +61,25 @@ sub _build_pages {
return \@pages;
}
+sub _build_drafts {
+ my $self = shift;
+
+ # glob draft files
+ my @drafts;
+ my @draft_files = sort glob $self->drafts_dir . '/*';
+ @draft_files = grep { ! /\.bak$/ } @draft_files;
+
+ # scan drafts
+ foreach my $filename (@draft_files) {
+ push @drafts, Bootylite::Article->new(
+ filename => $filename,
+ encoding => $self->encoding,
+ );
+ }
+
+ return \@drafts;
+}
+
sub _build_renderers {
my $self = shift;
@@ -114,6 +136,20 @@ sub get_page {
return;
}
+sub get_draft {
+ my ($self, $url) = @_;
+
+ # scan drafts
+ foreach my $draft (@{$self->drafts}) {
+
+ # found!
+ return $draft if $draft->url eq lc $url;
+ }
+
+ # not found!
+ return;
+}
+
sub refresh {
my $self = shift;
View
1 lib/Bootylite/Plugins.pm
@@ -31,6 +31,7 @@ sub call_tags { $_->tags(@_) for @{shift->plugins} };
sub call_page { $_->page(@_) for @{shift->plugins} };
sub call_feed { $_->feed(@_) for @{shift->plugins} };
sub call_tag_feed { $_->tag_feed(@_) for @{shift->plugins} };
+sub call_draft { $_->draft(@_) for @{shift->plugins} };
sub call_refresh { $_->refresh(@_) for @{shift->plugins} };
!! 42;
View
40 t/06-drafts.t
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use utf8;
+
+use Test::More tests => 13;
+
+use FindBin '$Bin';
+use lib "$Bin/../lib";
+use Bootylite;
+
+# build object
+my $dir = "$Bin/drafts";
+my $b = Bootylite->new(drafts_dir => $dir);
+
+# build drafts
+my @fns = sort glob("$dir/*");
+my @drafts = @{$b->drafts};
+is(scalar(@drafts), 2, 'found 2 drafts');
+my $draft1 = shift @drafts;
+isa_ok($draft1, 'Bootylite::Article', 'first draft');
+is($draft1->filename, $fns[0], 'right filename');
+my $draft2 = shift @drafts;
+isa_ok($draft2, 'Bootylite::Article', 'second draft');
+is($draft2->filename, $fns[1], 'right filename');
+
+# get drafts by url
+my $d1 = $b->get_draft('draft1');
+ok(defined($d1), 'draft1 found by url');
+isa_ok($d1, 'Bootylite::Article', 'found draft1');
+is($d1->url, 'draft1', 'found the right draft');
+is($d1->first, "first draft\n", 'right content');
+my $d2 = $b->get_draft('draft2');
+ok(defined($d2), 'draft2 found by url');
+isa_ok($d2, 'Bootylite::Article', 'found draft2');
+is($d2->url, 'draft2', 'found the right draft');
+is($d2->first, "second draft\n", 'right content');
+
+__END__
View
3 t/07-plugins.t
@@ -4,7 +4,7 @@ use strict;
use warnings;
use utf8;
-use Test::More tests => 30;
+use Test::More tests => 33;
use Test::Mojo;
use FindBin '$Bin';
@@ -28,6 +28,7 @@ $t->get_ok('/tags') ->status_is(200)->content_is('test tags');
$t->get_ok('/pages/foo_bar_baz')->status_is(200)->content_is('test page');
$t->get_ok('/feed.xml') ->status_is(200)->content_is('test feed');
$t->get_ok('/feed/foo.xml') ->status_is(200)->content_is('test tag feed');
+$t->get_ok('/drafts/draft1') ->status_is(200)->content_is('test draft');
$t->get_ok('/refresh') ->status_is(200)->content_is('test refresh');
__END__
View
8 t/10-web.t
@@ -4,7 +4,7 @@ use strict;
use warnings;
use utf8;
-use Test::More tests => 94;
+use Test::More tests => 99;
use Test::Mojo;
use FindBin '$Bin';
@@ -96,6 +96,12 @@ $t->get_ok('/pages/foo_bar_baz')->status_is(200);
$t->text_is('title', 'Bootylite - Test that shit, yo!');
$t->text_is('h1', 'Test that shit, yo!')->text_is('#page em', 'page');
+# second draft
+$t->get_ok('/drafts/draft2')->status_is(200);
+$t->text_is('title', 'Bootylite - Test Draft Two');
+$t->text_is('h1', 'Test Draft Two');
+$t->text_is('#content p', 'second draft');
+
# atom feed
@articles = @{$t->app->booty->articles};
$t->get_ok('/feed.xml')->status_is(200)->content_type_like(qr/xml/);
View
3 t/bootylite.conf
@@ -7,6 +7,9 @@
encoding => 'utf-8',
+ drafts_dir => app->home->rel_dir('t/drafts'),
+ drafts_url => '/drafts',
+
refresh_url => '/refresh',
name => 'Bootylite',
View
3 t/bootylite_plugins.conf
@@ -7,6 +7,9 @@
encoding => 'utf-8',
+ drafts_dir => app->home->rel_dir('t/drafts'),
+ drafts_url => '/drafts',
+
refresh_url => '/refresh',
name => 'Bootylite',
View
4 t/drafts/2011-04-05-13-37_draft1.md
@@ -0,0 +1,4 @@
+Title: Test Draft One
+Tags: foo, bar
+
+first draft
View
4 t/drafts/2011-04-05-13-38_draft2.md
@@ -0,0 +1,4 @@
+Title: Test Draft Two
+Tags: bar, baz
+
+second draft
View
1 t/lib/Bootylite/Plugin/TestPlugin.pm
@@ -15,6 +15,7 @@ sub tags { $_[1]->render_text('test tags') }
sub page { $_[1]->render_text('test page') }
sub feed { $_[1]->render_text('test feed') }
sub tag_feed { $_[1]->render_text('test tag feed') }
+sub draft { $_[1]->render_text('test draft') }
sub refresh { $_[1]->render_text('test refresh') }
!! 42;

0 comments on commit f37ff38

Please sign in to comment.
Something went wrong with that request. Please try again.