package MT::Plugin::JSONFeed; # $Id$ # # Yoshiki Kurihara # use strict; our $VERSION = '0.01'; use MT; use MT::Template::Context; use MT::Entry; use JSON; my $mt_major_version = $MT::VERSION; $mt_major_version =~ s/^(\d).+/$1/; if ($mt_major_version > 2) { require MT::Plugin; my $plugin = MT::Plugin->new({ name => "JSON Feed v$VERSION", description => "Output feeds using json", }); MT->add_plugin($plugin); } MT::Template::Context->add_tag(JSONFeed => \&mk_jsonfeed); sub mk_jsonfeed { my($ctx, $args) = @_; my $obj_name = $args->{obj_name} || 'JSONFeed'; my $n = $args->{num} || 10; my $blog = $ctx->stash('blog'); my %args; $args{'sort'} = 'created_on'; $args{direction} = 'descend'; my $iter = MT::Entry->load_iter({ blog_id => $blog->id, status => MT::Entry::RELEASE() }, \%args); my $i = 0; my @entries; while (my $entry = $iter->()) { push @entries, { title => $entry->title, permalink => $entry->permalink, author => $entry->author->nickname || $entry->author->name || undef, excerpt => $entry->excerpt, }; last if $n && $i++ >= $n-1; } require Jcode; my $from = $args->{charset_from} || 'utf8'; my $to = $args->{charset_to} || 'utf8'; my $json = new JSON; my $js = Jcode->new($json->objToJson(\@entries, { pretty => 1, indent => 2 }), $from)->$to(); return "if(typeof($obj_name) == 'undefined') $obj_name = {}; $obj_name.posts = $js;"; } 1; __END__ =head1 NAME mt-jsonfeed.pl - Output feeds using JSON =head1 SYNOPSIS # template <$MTJSONFeed charset_from="euc" charset_to="utf8" obj_name="JSONFeed" num="10"$> =head1 TAGS =over =item MTJSONFeed This Tag output feeds which is JSON style. =over =item charset_from Specify charset code feeds charset. =item charset_to Specify output charset code for feeds. =item obj_name Specify object name is used in json. =item num Specify number of entries to output. =back =back =head1 AUTHOR Yoshiki Kurihara Ekurihara@cpan.orgE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L, L, L =cut