Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit fe63afc722ff24e504cbe9af872b12bd3cbd6a4a Benjamin Trott committed Dec 8, 2009
Showing with 223 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 .shipit
  3. +4 −0 Changes
  4. +22 −0 MANIFEST
  5. +15 −0 MANIFEST.SKIP
  6. +11 −0 Makefile.PL
  7. +24 −0 README
  8. +122 −0 lib/WWW/Tumblr/ThemeParser.pm
  9. +4 −0 t/00_compile.t
  10. +5 −0 xt/perlcritic.t
  11. +4 −0 xt/pod.t
  12. +4 −0 xt/synopsis.t
6 .gitignore
@@ -0,0 +1,6 @@
+META.yml
+Makefile
+inc/
+pm_to_blib
+blib/
+*~
2 .shipit
@@ -0,0 +1,2 @@
+steps = FindVersion, ChangeVersion, CheckChangeLog, DistTest, Commit, Tag, MakeDist, UploadCPAN
+git.push_to = origin
4 Changes
@@ -0,0 +1,4 @@
+Revision history for Perl extension WWW::Tumblr::ThemeParser
+
+0.01 Tue Dec 8 10:11:31 2009
+ - original version
22 MANIFEST
@@ -0,0 +1,22 @@
+Changes
+inc/Module/Install.pm
+inc/Module/Install/AuthorTests.pm
+inc/Module/Install/Base.pm
+inc/Module/Install/Can.pm
+inc/Module/Install/Fetch.pm
+inc/Module/Install/Include.pm
+inc/Module/Install/Makefile.pm
+inc/Module/Install/Metadata.pm
+inc/Module/Install/ReadmeFromPod.pm
+inc/Module/Install/Repository.pm
+inc/Module/Install/Win32.pm
+inc/Module/Install/WriteAll.pm
+lib/WWW/Tumblr/ThemeParser.pm
+Makefile.PL
+MANIFEST This list of files
+META.yml
+README
+t/00_compile.t
+xt/perlcritic.t
+xt/pod.t
+xt/synopsis.t
15 MANIFEST.SKIP
@@ -0,0 +1,15 @@
+\bRCS\b
+\bCVS\b
+\.svn/
+\.git/
+^MANIFEST\.
+^Makefile$
+~$
+\.old$
+^blib/
+^pm_to_blib
+^MakeMaker-\d
+\.gz$
+\.cvsignore
+\.gitignore
+\.shipit
11 Makefile.PL
@@ -0,0 +1,11 @@
+use inc::Module::Install;
+name 'WWW-Tumblr-ThemeParser';
+all_from 'lib/WWW/Tumblr/ThemeParser.pm';
+readme_from 'lib/WWW/Tumblr/ThemeParser.pm';
+requires 'HTML::TokeParser';
+build_requires 'Test::More';
+use_test_base;
+auto_include_deps;
+author_tests('xt');
+auto_set_repository;
+WriteAll;
24 README
@@ -0,0 +1,24 @@
+NAME
+ WWW::Tumblr::ThemeParser - Parse Tumblr custom themes
+
+SYNOPSIS
+ use WWW::Tumblr::ThemeParser;
+ my $html;
+ my $p = WWW::Tumblr::ThemeParser->new( \$html );
+ while ( my $t = $p->get_token ) {
+ # ...
+ }
+
+DESCRIPTION
+ *WWW::Tumblr::ThemeParser* is a token-based parser for Tumblr's custom
+ theme tag format. Parsing a Tumblr theme constructs a list of
+ tokens--very similar to the list of tokens provided by
+ *HTML::TokeParser*--that can be processed by the caller.
+
+AUTHOR
+ Benjamin Trott <ben@sixapart.com>
+
+LICENSE
+ This library is free software; you can redistribute it and/or modify it
+ under the same terms as Perl itself.
+
122 lib/WWW/Tumblr/ThemeParser.pm
@@ -0,0 +1,122 @@
+package WWW::Tumblr::ThemeParser;
+use strict;
+use base qw( Class::Accessor::Fast );
+
+use 5.008_001;
+use HTML::TokeParser;
+
+our $VERSION = '0.01';
+
+__PACKAGE__->mk_accessors( qw( tokens index settings ) );
+
+my $Tags = qr/(\{(\/?[\w\s\-:]+)\})/;
+
+sub new {
+ my $class = shift;
+ my( $data ) = @_;
+ my $parser = $class->SUPER::new( {
+ tokens => [],
+ settings => {},
+ index => 0,
+ } );
+ if ( defined $data ) {
+ $parser->_tokenize( $data );
+ $parser->_extract_settings( $data );
+ }
+ return $parser;
+}
+
+sub _tokenize {
+ my $parser = shift;
+ my( $text_ref ) = @_;
+
+ my @tokens;
+ my $pos = 0;
+
+ while ( $$text_ref =~ /$Tags/gs ) {
+ my( $match, $tag ) = ( $1, $2 );
+ my $tag_start = pos( $$text_ref ) - length $match;
+
+ # Is there any text between the last tag and this one? If so,
+ # grab it and create a Text token.
+ if ( $pos < $tag_start ) {
+ push @tokens, [ 'TEXT', substr( $$text_ref, $pos, $tag_start - $pos ) ];
+ }
+
+ if ( $tag =~ /^(\/?)block:/ ) {
+ my $type = $1 ? 'EBLOCK' : 'SBLOCK';
+ push @tokens, [ $type, $tag ];
+ } elsif ( $tag =~ /^(\w+):([\w\s]+)$/ ) {
+ push @tokens, [ 'SETTING', $1, $2 ];
+ } else {
+ push @tokens, [ 'VAR', $tag ];
+ }
+
+ $pos = pos( $$text_ref );
+ }
+
+ if ( $pos < length $$text_ref ) {
+ push @tokens, [ 'TEXT', substr( $$text_ref, $pos ) ];
+ }
+
+ $parser->tokens( \@tokens );
+}
+
+sub _extract_settings {
+ my $parser = shift;
+ my( $text_ref ) = @_;
+
+ my $settings = $parser->settings;
+
+ my $hp = HTML::TokeParser->new( $text_ref );
+ while ( my $tag = $hp->get_tag( 'meta' ) ) {
+ if ( $tag->[1]{name} && $tag->[1]{name} =~ /^(\w+):([\w\s]+)$/ ) {
+ $settings->{ $1 }{ $2 } = $tag->[1]{content};
+ }
+ }
+}
+
+sub get_token {
+ my $parser = shift;
+ my $token = $parser->tokens->[ $parser->index || 0 ];
+ $parser->index( $parser->index + 1 );
+ return $token;
+}
+
+1;
+__END__
+
+=encoding utf-8
+
+=for stopwords
+
+=head1 NAME
+
+WWW::Tumblr::ThemeParser - Parse Tumblr custom themes
+
+=head1 SYNOPSIS
+
+ use WWW::Tumblr::ThemeParser;
+ my $html;
+ my $p = WWW::Tumblr::ThemeParser->new( \$html );
+ while ( my $t = $p->get_token ) {
+ # ...
+ }
+
+=head1 DESCRIPTION
+
+I<WWW::Tumblr::ThemeParser> is a token-based parser for Tumblr's custom theme
+tag format. Parsing a Tumblr theme constructs a list of tokens--very similar
+to the list of tokens provided by I<HTML::TokeParser>--that can be processed
+by the caller.
+
+=head1 AUTHOR
+
+Benjamin Trott E<lt>ben@sixapart.comE<gt>
+
+=head1 LICENSE
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
4 t/00_compile.t
@@ -0,0 +1,4 @@
+use strict;
+use Test::More tests => 1;
+
+BEGIN { use_ok 'WWW::Tumblr::ThemeParser' }
5 xt/perlcritic.t
@@ -0,0 +1,5 @@
+use strict;
+use Test::More;
+eval q{ use Test::Perl::Critic };
+plan skip_all => "Test::Perl::Critic is not installed." if $@;
+all_critic_ok("lib");
4 xt/pod.t
@@ -0,0 +1,4 @@
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok();
4 xt/synopsis.t
@@ -0,0 +1,4 @@
+use Test::More;
+eval "use Test::Synopsis";
+plan skip_all => "Test::Synopsis required" if $@;
+all_synopsis_ok();

0 comments on commit fe63afc

Please sign in to comment.