Skip to content

Commit

Permalink
move store attribute to site object
Browse files Browse the repository at this point in the history
Giving each application its own directory makes everything more
complicated: The site configuration is more complicated, explaining how
the system works is more complicated, and extending the site with new
code is more complicated.

Mostly people just want to say "My site is in this directory, change
Markdown files into HTML and copy it to my server". Having a single path
for the entire site makes that work better.

This changes a lot of things:

* Statocles::App objects are now given a list of files/documents and can
  return additional pages to add.
    * Since Statocles::App objects do not have stores,
      Statocles::App::Role::Store is now gone.
* The basic task of changing a markdown document into an HTML page is
  handled directly by the site, making Statocles::App::Basic obsolete.
    * Statocles::App::Basic is now deprecated and will be removed in
      v1.000.

This change also comes with some additional things:

* I deleted a lot of tests that weren't testing anything useful
* I changed most of the other tests to stop rendering pages into HTML.
  This greatly improves the performance of the test suite.
  Nearly every test in this project was an integration test, rendering
  HTML and then parsing it to look for the right HTML. This is slow and
  useless. Now, the t/page tests test rendering to HTML, and everything
  else assumes that pages already know how to render HTML.

There is still some refactoring to do before I release v1.000 upon the
world.
  • Loading branch information
preaction committed Sep 23, 2018
1 parent 141b382 commit f7c8d5b
Show file tree
Hide file tree
Showing 59 changed files with 582 additions and 1,624 deletions.
23 changes: 10 additions & 13 deletions lib/Statocles/App/Basic.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ our $VERSION = '0.094';
use Statocles::Base 'Class';
use Statocles::Document;
use Statocles::Util qw( run_editor read_stdin );
with 'Statocles::App::Role::Store';
with 'Statocles::App';

=attr store
The L<store path|Statocles::Store> containing this app's documents and files.
Required.
=cut
sub pages { }

=method command
Expand Down Expand Up @@ -53,19 +48,19 @@ sub command {
if ( my $content = read_stdin() ) {
my $doc = Statocles::Document->parse_content(
path => $path.'',
store => $self->store,
store => $self->site->store,
content => $content,
);
$self->store->write_file( $path => $doc );
$self->site->store->write_file( $path => $doc );
}
elsif ( !$self->store->has_file( $path ) ) {
elsif ( !$self->site->store->has_file( $path ) ) {
my $doc = Statocles::Document->new(
content => "Markdown content goes here.\n",
);
$self->store->write_file( $path => $doc );
$self->site->store->write_file( $path => $doc );
}

my $full_path = $self->store->path->child( $path );
my $full_path = $self->site->store->path->child( $path );
if ( my $content = run_editor( $full_path ) ) {
$full_path->spew_utf8( $content );
}
Expand All @@ -90,12 +85,14 @@ __END__
my $app = Statocles::App::Basic->new(
url_root => '/',
store => 'share/root',
);
my @pages = $app->pages;
=head1 DESCRIPTION
B<DEPRECATED>: The functionality of this app is now built-in to the site
itself. There's no reason to use this anymore.
This application builds basic pages based on L<Markdown documents|Statocles::Document> and
other files. Use this to have basic informational pages like "About Us" and "Contact Us".
57 changes: 23 additions & 34 deletions lib/Statocles/App/Blog.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,7 @@ use Statocles::Document;
use Statocles::Page::Document;
use Statocles::Page::List;
use Statocles::Util qw( run_editor read_stdin );

with 'Statocles::App::Role::Store';

=attr store
# site.yml
blog:
class: Statocles::App::Blog
args:
store: _posts
The L<store directory path|Statocles::Store> to read for blog posts. Required.
The Blog directory is organized in a tree by date, with a directory for the
year, month, day, and post. Each blog post is its own directory to allow for
additional files for the post, like images or additional pages.
=cut
with 'Statocles::App';

=attr tag_text
Expand Down Expand Up @@ -228,27 +211,29 @@ ENDHELP
);

my $slug = $self->make_slug( $doc->title || "new post" );
my @partsdir = (@date_parts, $slug);
my $url_root = $self->url_root;
$url_root =~ s{^/}{}g;
my @partsdir = ($url_root, @date_parts, $slug);
my @partsfile = (@partsdir, "index.markdown");
my $path = Mojo::Path->new->parts(\@partsfile);
$self->store->write_file( $path => $doc );
my $full_path = $self->store->path->child( @partsfile );
$self->site->store->write_file( $path => $doc );
my $full_path = $self->site->store->path->child( @partsfile );

if ( my $content = run_editor( $full_path ) ) {
my $old_title = $doc->title;
my $doc = Statocles::Document->parse_content(
path => $path.'',
store => $self->store,
store => $self->site->store,
content => $content,
);
if ( $doc->title ne $old_title ) {
$self->store->path->child( @partsdir )->remove_tree;
$self->site->store->path->child( @partsdir )->remove_tree;
$slug = $self->make_slug( $doc->title || "new post" );
@partsdir = (@date_parts, $slug);
@partsdir = ($url_root, @date_parts, $slug);
@partsfile = (@partsdir, "index.markdown");
$path = Mojo::Path->new->parts(\@partsfile);
$self->store->write_file( $path => $doc );
$full_path = $self->store->path->child( @partsfile );
$self->site->store->write_file( $path => $doc );
$full_path = $self->site->store->path->child( @partsfile );
}
}

Expand Down Expand Up @@ -343,6 +328,7 @@ sub index {
for my $feed ( sort keys %FEEDS ) {
my $page = Statocles::Page::List->new(
app => $self,
site => $self->site,
pages => $index->pages,
path => $self->url( 'index.' . $feed ),
template => $self->template( $FEEDS{$feed}{template} ),
Expand Down Expand Up @@ -411,6 +397,7 @@ sub tag_pages {

my $page = Statocles::Page::List->new(
app => $self,
site => $self->site,
pages => $index->pages,
path => $self->url( join( "/", 'tag', $tag_file ) ),
template => $self->template( $FEEDS{$feed}{template} ),
Expand Down Expand Up @@ -462,13 +449,13 @@ Defaults to the current date.
=cut

# sub pages
around pages => sub {
my ( $orig, $self, %opt ) = @_;
sub pages {
my ( $self, $pages, %opt ) = @_;
$opt{date} ||= DateTime::Moonpig->now( time_zone => 'local' )->ymd;
my $root = $self->url_root;
my $is_dated_path = qr{^$root/?(\d{4})/(\d{2})/(\d{2})/};
my @parent_pages = $self->$orig( %opt );
$root =~ s{^/}{}g;
my $is_dated_path = qr{^/?$root/?(\d{4})/(\d{2})/(\d{2})/};
my @parent_pages = @$pages;
my @pages =
map { $_->[0] }
# Only pages today or before
Expand All @@ -477,14 +464,16 @@ around pages => sub {
map { [ $_, join "-", $_->path =~ $is_dated_path ] }
# Only dated pages
grep { $_->path =~ $is_dated_path }
#$self->$orig( %opt );
@parent_pages;
@pages = _sort_page_list( @pages );

my @post_pages;
my %tag_pages;

#; say "Got pages: " . join ", ", map { $_->path } @pages;

for my $page ( @pages ) {
$page->app( $self );

if ( $page->isa( 'Statocles::Page::Document' ) ) {

Expand Down Expand Up @@ -524,9 +513,9 @@ around pages => sub {
# XXX: This needs to be handled more intelligently with proper dependencies
$self->_post_pages( \@post_pages );

my @all_pages = ( $self->index( \@post_pages ), $self->tag_pages( \%tag_pages ), @pages );
my @all_pages = ( $self->index( \@post_pages ), $self->tag_pages( \%tag_pages ) );
return @all_pages;
};
}

=method tags
Expand Down
95 changes: 0 additions & 95 deletions lib/Statocles/App/Role/Store.pm

This file was deleted.

47 changes: 19 additions & 28 deletions lib/Statocles/Command/create.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ sub create_site {
};
}
elsif ( $answer{flavor} == 2 ) {
$vars{site}{index} = "/page";
$vars{site}{index} = "/";
$vars{site}{nav}{main}[0] = {
href => "/blog",
text => "Blog",
Expand Down Expand Up @@ -174,33 +174,24 @@ sub create_site {
### Copy initial site content
# Blog
if ( my $ref = $site->{site}{apps}{blog} ) {
my $path = $site->{ $ref->{ '$ref' } }{store};
if ( $path ) {
my ( undef, undef, undef, $day, $mon, $year ) = localtime;
$year += 1900;
$mon += 1;

my @date_parts = (
sprintf( '%04i', $year ),
sprintf( '%02i', $mon ),
sprintf( '%02i', $day ),
);

my $post_path = $root->child( $path, @date_parts, 'first-post', 'index.markdown' );
$post_path->parent->mkpath;
$create_dir->child( 'blog', 'post.markdown' )->copy( $post_path );
}
}

# Page
if ( my $ref = $site->{site}{apps}{page} ) {
my $path = $site->{ $ref->{ '$ref' } }{store};
if ( $path ) {
my $page_path = $root->child( $path, 'index.markdown' );
$page_path->parent->mkpath;
$create_dir->child( 'page', 'index.markdown' )->copy( $page_path );
};
}
my $path = $site->{blog_app}{url_root};
my ( undef, undef, undef, $day, $mon, $year ) = localtime;
$year += 1900;
$mon += 1;

my @date_parts = (
sprintf( '%04i', $year ),
sprintf( '%02i', $mon ),
sprintf( '%02i', $day ),
);

my $post_path = $root->child( $path, @date_parts, 'first-post', 'index.markdown' );
$post_path->parent->mkpath;
$create_dir->child( 'blog', 'post.markdown' )->copy( $post_path );
}
my $page_path = $root->child( 'index.markdown' );
$page_path->parent->mkpath;
$create_dir->child( 'page', 'index.markdown' )->copy( $page_path );

### DONE!
print "\n", "\n", $question->{finish}, "\n", "\n";
Expand Down
4 changes: 2 additions & 2 deletions lib/Statocles/Page.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The site this page is part of.
=cut

has site => (
is => 'ro',
is => 'rw',
isa => InstanceOf['Statocles::Site'],
lazy => 1,
default => sub { $Statocles::SITE },
Expand All @@ -27,7 +27,7 @@ The application this page came from, so we can give it to the templates.
=cut

has app => (
is => 'ro',
is => 'rw',
isa => ConsumerOf['Statocles::App'],
);

Expand Down
Loading

0 comments on commit f7c8d5b

Please sign in to comment.