| 1 | package Catalyst::View::Markdown; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base qw/Catalyst::View/; | ||
| 7 | use Text::Markdown; | ||
| 8 | use File::Find; | ||
| 9 | use MRO::Compat; | ||
| 10 | use Scalar::Util qw/blessed weaken/; | ||
| 11 | |||
| 12 | our $VERSION = '0.01'; | ||
| 13 | $VERSION = eval $VERSION; | ||
| 14 | |||
| 15 | __PACKAGE__->mk_accessors('markdown_filename'); | ||
| 16 | __PACKAGE__->mk_accessors('include_path'); | ||
| 17 | |||
| 18 | *paths = \&include_path; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Catalyst::View::Markdown - Markdown View Class | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | # use the helper to create your View | ||
| 27 | |||
| 28 | myapp_create.pl view MD Markdown | ||
| 29 | |||
| 30 | # add custom configration in View/MD.pm | ||
| 31 | |||
| 32 | __PACKAGE__->config( | ||
| 33 | # any Markdown configuration items go here | ||
| 34 | FILENAME_EXTENSION => '.md', | ||
| 35 | empty_element_suffix => '/>', | ||
| 36 | tab_width => 4, | ||
| 37 | trust_list_start_value => 1, | ||
| 38 | ); | ||
| 39 | |||
| 40 | # add include path configuration in MyApp.pm | ||
| 41 | |||
| 42 | __PACKAGE__->config( | ||
| 43 | 'View::MD' => { | ||
| 44 | INCLUDE_PATH => [ | ||
| 45 | __PACKAGE__->path_to( 'root', 'src' ), | ||
| 46 | __PACKAGE__->path_to( 'root', 'lib' ), | ||
| 47 | ], | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | # render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm | ||
| 52 | |||
| 53 | sub message : Global { | ||
| 54 | my ( $self, $c ) = @_; | ||
| 55 | $c->stash->{markdown_filename} = 'message'; | ||
| 56 | $c->forward( $c->view('MD') ); | ||
| 57 | } | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub _coerce_paths { | ||
| 62 | my ( $paths, $dlim ) = shift; | ||
| 63 | return () if ( !$paths ); | ||
| 64 | return @{$paths} if ( ref $paths eq 'ARRAY' ); | ||
| 65 | |||
| 66 | # tweak delim to ignore C:/ | ||
| 67 | unless ( defined $dlim ) { | ||
| 68 | $dlim = ( $^O eq 'MSWin32' ) ? ':(?!\\/)' : ':'; | ||
| 69 | } | ||
| 70 | return split( /$dlim/, $paths ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub new { | ||
| 74 | my ( $class, $c, $arguments ) = @_; | ||
| 75 | my $config = { | ||
| 76 | FILENAME_EXTENSION => '', | ||
| 77 | CLASS => 'Text::Markdown', | ||
| 78 | %{ $class->config }, | ||
| 79 | %{$arguments}, | ||
| 80 | }; | ||
| 81 | if ( ! (ref $config->{INCLUDE_PATH} eq 'ARRAY') ) { | ||
| 82 | my $delim = $config->{DELIMITER}; | ||
| 83 | my @include_path | ||
| 84 | = _coerce_paths( $config->{INCLUDE_PATH}, $delim ); | ||
| 85 | if ( !@include_path ) { | ||
| 86 | my $root = $c->config->{root}; | ||
| 87 | my $base = Path::Class::dir( $root, 'base' ); | ||
| 88 | @include_path = ( "$root", "$base" ); | ||
| 89 | } | ||
| 90 | $config->{INCLUDE_PATH} = \@include_path; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ( $c->debug && $config->{DUMP_CONFIG} ) { | ||
| 94 | $c->log->debug( "Markdown Config: ", CORE::dump($config) ); | ||
| 95 | } | ||
| 96 | |||
| 97 | my $self = $class->next::method( | ||
| 98 | $c, { %$config }, | ||
| 99 | ); | ||
| 100 | |||
| 101 | # Set base include paths. Local'd in render if needed | ||
| 102 | $self->include_path($config->{INCLUDE_PATH}); | ||
| 103 | |||
| 104 | $self->config($config); | ||
| 105 | |||
| 106 | # Creation of template outside of call to new so that we can pass [ $self ] | ||
| 107 | # as INCLUDE_PATH config item, which then gets ->paths() called to get list | ||
| 108 | # of include paths to search for templates. | ||
| 109 | |||
| 110 | # Use a weakend copy of self so we dont have loops preventing GC from working | ||
| 111 | my $copy = $self; | ||
| 112 | Scalar::Util::weaken($copy); | ||
| 113 | $config->{INCLUDE_PATH} = [ sub { $copy->paths } ]; | ||
| 114 | |||
| 115 | $self->{markdown} = | ||
| 116 | $config->{CLASS}->new($config) || do { | ||
| 117 | my $error = $config->{CLASS}->error(); | ||
| 118 | $c->log->error($error); | ||
| 119 | $c->error($error); | ||
| 120 | return undef; | ||
| 121 | }; | ||
| 122 | |||
| 123 | |||
| 124 | return $self; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub process { | ||
| 128 | my ( $self, $c ) = @_; | ||
| 129 | |||
| 130 | my $mdfile = $c->stash->{markdown_filename} | ||
| 131 | || $c->action . $self->config->{FILENAME_EXTENSION}; | ||
| 132 | |||
| 133 | unless (defined $mdfile) { | ||
| 134 | $c->log->debug('No Markdown file specified for rendering') if $c->debug; | ||
| 135 | return 0; | ||
| 136 | } | ||
| 137 | |||
| 138 | local $@; | ||
| 139 | my $output = eval { $self->render($c, $mdfile) }; | ||
| 140 | |||
| 141 | unless ( $c->response->content_type ) { | ||
| 142 | $c->response->content_type('text/html; charset=utf-8'); | ||
| 143 | } | ||
| 144 | |||
| 145 | $c->response->body($output); | ||
| 146 | |||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub render { | ||
| 151 | my ($self, $c, $mdfile, $args) = @_; | ||
| 152 | |||
| 153 | $c->log->debug(qq/Rendering Markdown file "$mdfile"/) if $c && $c->debug; | ||
| 154 | |||
| 155 | my $mdtext; | ||
| 156 | if ( ref $mdfile eq 'SCALAR' ) { | ||
| 157 | $mdtext = $$mdfile; | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | |||
| 161 | my $filename; | ||
| 162 | # Find the first readable file with the right filename under the include paths | ||
| 163 | find( | ||
| 164 | sub { $filename = $File::Find::name if !$filename && $_ eq $mdfile && -r $File::Find::name}, | ||
| 165 | @{$self->include_path} | ||
| 166 | ); | ||
| 167 | |||
| 168 | # If we found a match... | ||
| 169 | if ($filename) { | ||
| 170 | open my $in, '<', $filename; | ||
| 171 | $mdtext = join '', do { local $/; <$in> }; # slurp! | ||
| 172 | close $in; | ||
| 173 | }; | ||
| 174 | }; | ||
| 175 | |||
| 176 | return $self->{markdown}->markdown($mdtext) if $mdtext; | ||
| 177 | |||
| 178 | my $error = qq/Couldn't find Markdown file "$mdfile"/; | ||
| 179 | $c->log->error($error); | ||
| 180 | $c->error($error); | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |