Skip to content

Commit

Permalink
use formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Feb 22, 2010
1 parent 6fb218f commit ee8ef8d
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 63 deletions.
2 changes: 2 additions & 0 deletions lib/RavLog/Form/Article.pm
Expand Up @@ -17,6 +17,8 @@ has_field 'tags' => (
multiple => 1,
);

with 'RavLog::Form::Formats';

has_field 'body' => (
required => 1,
type => 'TextArea',
Expand Down
2 changes: 2 additions & 0 deletions lib/RavLog/Form/Comment.pm
Expand Up @@ -20,6 +20,8 @@ has_field 'url' => (
label => 'Website',
size => 25 );

with 'RavLog::Form::Formats';

has_field 'comment' => (
type => 'TextArea',
required => 1,
Expand Down
18 changes: 18 additions & 0 deletions lib/RavLog/Form/Formats.pm
@@ -0,0 +1,18 @@
package RavLog::Form::Formats;
use HTML::FormHandler::Moose::Role;

use RavLog::Format;

has_field 'format' => (
type => 'Select', required => 1,
);
sub options_format {
my $self = shift;
my @types = RavLog::Format::types;
my @options;
foreach my $type (@types) {
push @options, { value => $type->{type}, label => $type->{description} };
}
return \@options;
}
1;
2 changes: 2 additions & 0 deletions lib/RavLog/Form/Page.pm
Expand Up @@ -27,6 +27,8 @@ has_field 'display_in_drawer' => (
options => [ { value => 1, label => 'Yes'}, { value => 0, label => 'No'}],
);

with 'RavLog::Form::Formats';

has_field 'body' => (
required => 1,
label => 'Page Content',
Expand Down
36 changes: 35 additions & 1 deletion lib/RavLog/Format.pm
Expand Up @@ -2,6 +2,10 @@ package RavLog::Format;
use strict;
use warnings;
use Carp;
use namespace::autoclean;

# this formatting code is from Angerwhale, and is copyright
# Jonathan Rockway and Florian Ragwitz

use Module::Pluggable (
search_path => ['RavLog::Format'],
Expand Down Expand Up @@ -50,9 +54,39 @@ __END__
=head1 NAME
RavLog::Format - Dispatches formatting of posts/comments to sub-modules
RavLog::Format - Dispatches formatting of articles/comments
=head1 SYNOPSIS
my @types = (
{
'description' => 'Text::WikiFormat formatted text',
'type' => 'wiki',
},
{
'description' => 'Perl POD (Plain Old Documentation)',
'type' => 'pod',
},
{
'description' => 'Markdown formatted text',
'type' => 'markdown',
},
{
'description' => 'HTML',
'type' => 'html',
},
{
'description' => 'Textile formatted text',
'type' => 'textile',
},
{
'description' => 'Plain text',
'type' => 'text',
},
{
'description' => 'Simple Blog Code',
'type' => 'sbc',
}
);
=cut
31 changes: 7 additions & 24 deletions lib/RavLog/Schema/Result/Article.pm
Expand Up @@ -3,7 +3,9 @@ package RavLog::Schema::Result::Article;
use strict;
use warnings;
use base 'DBIx::Class';
use Text::Textile 'textile';
#use Text::Textile 'textile';
use Ravlog::Format;
use namespace::autoclean;

__PACKAGE__->load_components( 'TimeStamp', 'InflateColumn::DateTime', 'Core' );
__PACKAGE__->table('articles');
Expand Down Expand Up @@ -78,30 +80,11 @@ __PACKAGE__->has_many(
);
__PACKAGE__->many_to_many( 'tags' => 'tags_articles', 'tag' );

sub textilize
{
my $self = shift;
my $what = shift;

my $temp = $self->$what;

return $temp if ( $self->format && $self->format eq 'html' );
# TODO: support different formats!!!
$temp =~ s/<textarea/==<textarea/g;
$temp =~ s/<\/textarea>/<\/textarea>==/g;
# $temp =~ s/\[code (.*?)\]/==\[code $1\]/g;
# $temp =~ s/\[\/code\]/\[\/code\]==/g;
return textile($temp);
}

sub insert
{
my $self = shift;
$self->created_at( DateTime->now() );
$self->next::method(@_);
sub formatted_body {
my $self = shift;
my $format = $self->format || 'text';
return RavLog::Format::format_html( $self->body, $format );
}



1;

35 changes: 15 additions & 20 deletions lib/RavLog/Schema/Result/Comment.pm
Expand Up @@ -4,6 +4,8 @@ use strict;
use warnings;

use base 'DBIx::Class';
use RavLog::Format;
use namespace::autoclean;

__PACKAGE__->load_components( "InflateColumn::DateTime", "Core" );
__PACKAGE__->table("comments");
Expand Down Expand Up @@ -43,6 +45,13 @@ __PACKAGE__->add_columns(
is_nullable => 1,
size => undef,
},
"format",
{
data_type => "varchar",
default_value => undef,
is_nullable => 1,
size => 12,
},
"remote_ip",
{
data_type => "character varying",
Expand All @@ -61,30 +70,16 @@ __PACKAGE__->add_columns(
user_id => { data_type => "integer", default_value => undef, is_nullable => 1, size => 4 },
);

use Text::Textile qw(textile);

sub textilize
{
my $self = shift;
my $what = shift;

my $temp = $self->$what;
$temp =~ s/\[code (.*?)\]/==<pre>\[code $1\]/g;
$temp =~ s/\[\/code\]/\[\/code\]<\/pre>==/g;
return textile($temp);
}

sub insert
{
my $self = shift;
$self->created_at( DateTime->now() );
$self->next::method(@_);
}

__PACKAGE__->set_primary_key("comment_id");

__PACKAGE__->belongs_to( 'article', 'RavLog::Schema::Result::Article', 'article_id' );
__PACKAGE__->belongs_to( 'user', 'RavLog::Schema::Result::User', 'user_id' );

sub formatted_body {
my $self = shift;
my $format = $self->format || 'text';
return RavLog::Format::format_html( $self->body, $format );
}

1;

17 changes: 9 additions & 8 deletions lib/RavLog/Schema/Result/Page.pm
Expand Up @@ -29,6 +29,12 @@ __PACKAGE__->add_columns(
default_value => undef,
is_nullable => 1,
},
"format",
{
data_type => 'varchar',
is_nullable => 1,
size => 12,
},
"display_sidebar",
{
data_type => 'smallint',
Expand All @@ -44,15 +50,10 @@ __PACKAGE__->add_columns(
);
__PACKAGE__->set_primary_key("page_id");

sub textilize {
sub formatted_body {
my $self = shift;
my $what = shift;

my $temp = $self->$what;
$temp =~ s/\[code (.*?)\]/==<pre>\[code $1\]/g;
$temp =~ s/\[\/code\]/\[\/code\]<\/pre>==/g;
return textile($temp);
my $format = $self->format || 'text';
return RavLog::Format::format_html( $self->body, $format );
}

1;

2 changes: 1 addition & 1 deletion root/templates/admin/article/view.tt
Expand Up @@ -3,4 +3,4 @@

<h2>[% article.subject %]</h2>

<p>[% article.body %]</p>
<p>[% article.formatted_body %]</p>
4 changes: 2 additions & 2 deletions root/templates/chaoticsoul/index.tt
Expand Up @@ -21,7 +21,7 @@
</div>

<div class="post-content">
[% article.textilize('body') %]
[% article.formatted_body %]
</div>
<div class="post-footer">&nbsp;</div>
</div>
Expand All @@ -39,7 +39,7 @@
[% END %]
<h3><a title="[% comment.url %]" href="[% comment.url %]">[% comment.name %]</a></h3>
<small class="commentmetadata">Posted [% c.render_ravlog_date(comment.created_at) %]</small>
<p>[% comment.textilize('comment') %]</p>
<p>[% comment.formatted_body %]</p>
</div>
</li>
[% END %]
Expand Down
4 changes: 2 additions & 2 deletions root/templates/connections/index.tt
Expand Up @@ -23,7 +23,7 @@
</div>

<div class="post-content">
[% article.textilize('body') %]
[% article.formatted_body %]
</div>
<div class="post-footer">&nbsp;</div>
</div>
Expand All @@ -41,7 +41,7 @@
[% END %]
<h3><a title="[% comment.url %]" href="[% comment.url %]">[% comment.name %]</a></h3>
<small class="commentmetadata">Posted [% c.render_ravlog_date(comment.created_at) %]</small>
<p>[% comment.textilize('comment') %]</p>
<p>[% comment.formatted_body %]</p>
</div>
</li>
[% END %]
Expand Down
4 changes: 2 additions & 2 deletions root/templates/default/blog_index.tt
Expand Up @@ -18,7 +18,7 @@
</div>

<div class="post-content">
[% article.textilize('body') %]
[% article.formatted_body %]
</div>
<div class="post-footer">&nbsp;
[% IF article.tags %]
Expand Down Expand Up @@ -50,7 +50,7 @@
[% END %]
<h3><a title="[% url %]" href="[% url %]">[% name %]</a></h3>
<small class="commentmetadata">Posted [% c.render_ravlog_date(comment.created_at) %]</small>
<p>[% comment.textilize('body') %]</p>
<p>[% comment.formatted_body %]</p>
</div>
</li>
[% END %]
Expand Down
4 changes: 2 additions & 2 deletions root/templates/default/static/stylesheets/master.css
Expand Up @@ -123,7 +123,7 @@ h1,h5,h4 {

.post h3 {
color: #396ea4;
font-size: 25px;
font-size: 2.0em;
display: inline;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ h1,h5,h4 {

.post h4 {
color: #699d80;
font-size: 16px;
font-size: 1.6em;
display: inline;
}

Expand Down
2 changes: 1 addition & 1 deletion root/templates/shared/page.tt
@@ -1 +1 @@
[% page.textilize('body') %]
[% page.formatted_body %]
1 change: 1 addition & 0 deletions t/db.t
Expand Up @@ -25,6 +25,7 @@ foreach my $article (@articles)
{
print $article->subject, "\n";
print $article->user->username, "\n";
print $article->formatted_body, "\n";
}

my $article = $schema->resultset('Article')->new_result({});
Expand Down

0 comments on commit ee8ef8d

Please sign in to comment.