Skip to content

Commit

Permalink
change tags to Link objects
Browse files Browse the repository at this point in the history
Link objects are a lot easier to use in templates.
  • Loading branch information
preaction committed Jan 19, 2015
1 parent 9b31948 commit 059d0d6
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 34 deletions.
18 changes: 9 additions & 9 deletions lib/Statocles/App/Blog.pm
Expand Up @@ -213,7 +213,7 @@ sub post_pages {
my @tags;
for my $tag ( @{ $doc->tags } ) {
push @tags, Statocles::Link->new(
title => $tag,
text => $tag,
href => $self->_tag_url( $tag ),
);
}
Expand Down Expand Up @@ -243,12 +243,12 @@ This includes all the relevant L<feed pages|Statocles::Page::Feed>.

my %FEEDS = (
rss => {
title => 'RSS',
text => 'RSS',
type => 'application/rss+xml',
template => 'index.rss',
},
atom => {
title => 'Atom',
text => 'Atom',
type => 'application/atom+xml',
template => 'index.atom',
},
Expand Down Expand Up @@ -295,7 +295,7 @@ sub index {
);
push @feed_pages, $page;
push @feed_links, Statocles::Link->new(
title => $FEEDS{ $feed }{ title },
text => $FEEDS{ $feed }{ text },
href => $page->path->stringify,
type => $page->type,
);
Expand Down Expand Up @@ -349,7 +349,7 @@ sub tag_pages {
);
push @feed_pages, $page;
push @feed_links, Statocles::Link->new(
title => $FEEDS{ $feed }{ title },
text => $FEEDS{ $feed }{ text },
href => $page->path->stringify,
type => $page->type,
);
Expand Down Expand Up @@ -383,18 +383,18 @@ sub pages {

=method tags()
Get a set of hashrefs suitable for creating a list of tag links. The hashrefs
contain the following keys:
Get a set of L<link objects|Statocles::Link> suitable for creating a list of
tag links. The common attributes are:
title => 'The tag text'
text => 'The tag text'
href => 'The URL to the tag page'
=cut

sub tags {
my ( $self ) = @_;
my %tagged_docs = $self->_tag_docs( @{ $self->_post_pages } );
return map {; { title => $_, href => $self->_tag_url( $_ ), } }
return map {; Statocles::Link->new( text => $_, href => $self->_tag_url( $_ ) ) }
sort keys %tagged_docs
}

Expand Down
14 changes: 13 additions & 1 deletion lib/Statocles/Page/Document.pm
Expand Up @@ -28,11 +28,12 @@ most important attributes are:
=cut

has tags => (
has _tags => (
is => 'ro',
isa => LinkArray,
default => sub { [] },
coerce => LinkArray->coercion,
init_arg => 'tags',
);

has '+_links' => (
Expand Down Expand Up @@ -92,6 +93,17 @@ sub last_modified {
return $self->document->last_modified;
}

=method tags()
Get the list of tags for this page.
=cut

sub tags {
my ( $self ) = @_;
return @{ $self->_tags };
}

1;
__END__
Expand Down
4 changes: 2 additions & 2 deletions share/theme/default/blog/index.atom.ep
Expand Up @@ -32,8 +32,8 @@
<p><a href="<%= $site->url( $p->path ) %>#section-2">Continue reading...</a></p>
% }
<p>Tags:
% for my $tag ( @{ $p->tags } ) {
<a href="<%= $tag->{href} %>"><%= $tag->{title} %></a>
% for my $tag ( $p->tags ) {
<a href="<%= $tag->href %>"><%= $tag->text %></a>
% }
</p>
]]></content>
Expand Down
6 changes: 3 additions & 3 deletions share/theme/default/blog/index.html.ep
Expand Up @@ -11,8 +11,8 @@
<h1><a href="<%= $page->path %>"><%= $doc->title %></a></h1>

<p class="tags">Tags:
% for my $tag ( @{ $page->tags } ) {
<a href="<%= $tag->{href} %>" rel="tag"><%= $tag->{title} %></a>
% for my $tag ( $page->tags ) {
<a href="<%= $tag->href %>" rel="tag"><%= $tag->text %></a>
% }
</p>

Expand Down Expand Up @@ -69,7 +69,7 @@
<h1>Tags</h1>
<ul class="list-inline">
% for my $tag ( $app->tags ) {
<li><a href="<%= $tag->{href} %>"><%= $tag->{title} %></a></li>
<li><a href="<%= $tag->href %>"><%= $tag->text %></a></li>
% }
</ul>
% if ( my @links = $self->links( 'feed' ) ) {
Expand Down
4 changes: 2 additions & 2 deletions share/theme/default/blog/index.rss.ep
Expand Up @@ -36,8 +36,8 @@
<p><a href="<%= $site->url( $p->path ) %>#section-2">Continue reading...</a></p>
% }
<p>Tags:
% for my $tag ( @{ $p->tags } ) {
<a href="<%= $tag->{href} %>"><%= $tag->{title} %></a>
% for my $tag ( $p->tags ) {
<a href="<%= $tag->href %>"><%= $tag->text %></a>
% }
</p>
]]></description>
Expand Down
6 changes: 3 additions & 3 deletions share/theme/default/blog/post.html.ep
Expand Up @@ -5,8 +5,8 @@
<header>
<h1><%= $doc->title %></h1>
<p class="tags">Tags:
% for my $tag ( @{ $self->tags } ) {
<a href="<%= $tag->{href} %>" rel="tag"><%= $tag->{title} %></a>
% for my $tag ( $self->tags ) {
<a href="<%= $tag->href %>" rel="tag"><%= $tag->text %></a>
% }
</p>
<aside>
Expand Down Expand Up @@ -40,7 +40,7 @@
<h1>Tags</h1>
<ul class="list-inline">
% for my $tag ( $app->tags ) {
<li><a href="<%= $tag->{href} %>"><%= $tag->{title} %></a></li>
<li><a href="<%= $tag->href %>"><%= $tag->text %></a></li>
% }
</ul>
</nav>
Expand Down
10 changes: 5 additions & 5 deletions t/page/document.t
Expand Up @@ -161,23 +161,23 @@ subtest 'page tags' => sub {
},
],
template => <<'ENDTEMPLATE',
% for my $link ( @{ $self->tags } ) {
<%= $link->{title} %>: <%= $link->{href} %>
% for my $link ( $self->tags ) {
<%= $link->title %>: <%= $link->href %>
% }
ENDTEMPLATE
);

my $output = $page->render;
my $expect = join "\n", ( map { join ": ", $_->{title}, $_->{href} } @{ $page->tags } ), "", "";
my $expect = join "\n", ( map { join ": ", $_->title, $_->href } $page->tags ), "", "";
eq_or_diff $output, $expect;

subtest 'default' => sub {
my $page = Statocles::Page::Document->new(
document => $doc,
path => '/path/to/page.html',
template => <<'ENDTEMPLATE',
% for my $link ( @{ $self->tags } ) {
<%= $link->{title} %>: <%= $link->{href} %>
% for my $link ( $self->tags ) {
<%= $link->title %>: <%= $link->href %>
% }
ENDTEMPLATE
);
Expand Down
4 changes: 2 additions & 2 deletions t/share/theme/blog/index.atom.ep
Expand Up @@ -36,8 +36,8 @@
<p><a href="<%= $site->url( $p->path ) %>#section-2">Continue reading...</a></p>
% }
<p>Tags:
% for my $tag ( @{ $p->tags } ) {
<a href="<%= $tag->{href} %>"><%= $tag->{title} %></a>
% for my $link ( $p->tags ) {
<a href="<%= $link->href %>"><%= $link->text %></a>
% }
</p>
]]></content>
Expand Down
2 changes: 1 addition & 1 deletion t/share/theme/blog/index.html.ep
Expand Up @@ -16,7 +16,7 @@
<h1>Tags</h1>
<ul class="list-inline tags">
% for my $tag ( $app->tags ) {
<li><a href="<%= $tag->{href} %>"><%= $tag->{title} %></a></li>
<li><a href="<%= $tag->href %>"><%= $tag->text %></a></li>
% }
</ul>

Expand Down
4 changes: 2 additions & 2 deletions t/share/theme/blog/index.rss.ep
Expand Up @@ -36,8 +36,8 @@
<p><a href="<%= $site->url( $p->path ) %>#section-2">Continue reading...</a></p>
% }
<p>Tags:
% for my $tag ( @{ $p->tags } ) {
<a href="<%= $tag->{href} %>"><%= $tag->{title} %></a>
% for my $link ( $p->tags ) {
<a href="<%= $link->href %>"><%= $link->text %></a>
% }
</p>
]]></description>
Expand Down
8 changes: 4 additions & 4 deletions t/share/theme/blog/post.html.ep
Expand Up @@ -3,8 +3,8 @@
<h1><%= $doc->title %></h1>

<p class="tags">Tags:
% for my $tag ( @{ $self->tags } ) {
<a href="<%= $tag->{href} %>" rel="tag"><%= $tag->{title} %></a>
% for my $link ( $self->tags ) {
<a href="<%= $link->href %>" rel="tag"><%= $link->text %></a>
% }
</p>

Expand Down Expand Up @@ -42,8 +42,8 @@
<nav id="tags">
<h1>Tags</h1>
<ul class="list-inline">
% for my $tag ( $app->tags ) {
<li><a href="<%= $tag->{href} %>"><%= $tag->{title} %></a></li>
% for my $link ( $app->tags ) {
<li><a href="<%= $link->href %>"><%= $link->text %></a></li>
% }
</ul>
</nav>

0 comments on commit 059d0d6

Please sign in to comment.