extract common variables and add bse.categorize_tags()
authorTony Cook <tony@develop-help.com>
Tue, 29 May 2012 06:41:31 +0000 (16:41 +1000)
committerTony Cook <tony@develop-help.com>
Tue, 29 May 2012 06:41:31 +0000 (16:41 +1000)
.gitignore
MANIFEST
site/cgi-bin/modules/BSE/Request/Base.pm
site/cgi-bin/modules/BSE/Variables.pm [new file with mode: 0644]
site/cgi-bin/modules/Generate.pm
site/docs/.gitignore [new file with mode: 0644]
site/docs/makedocs
t/t20gen.t

index 4754966..09c1287 100644 (file)
@@ -4,40 +4,6 @@ INSTALL.html
 INSTALL.txt
 site/cgi-bin/modules/BSE/Modules.pm
 site/cgi-bin/modules/BSE/Version.pm
-site/docs/Article.html
-site/docs/BSE::TB::SiteCommon.html
-site/docs/BSE::TB::TagOwner.html
-site/docs/BSE::UI::Affiliate.html
-site/docs/Generate.html
-site/docs/Generate::Article.html
-site/docs/Generate::Catalog.html
-site/docs/Generate::Product.html
-site/docs/Squirrel::Template.html
-site/docs/TODO.html
-site/docs/access.html
-site/docs/add.html
-site/docs/affiliate.html
-site/docs/bse.html
-site/docs/bugs.html
-site/docs/config.html
-site/docs/dynamic.html
-site/docs/formmail.html
-site/docs/future_plans.html
-site/docs/gen.html
-site/docs/makedocs.html
-site/docs/pod2htmd.tmp
-site/docs/pod2htmi.tmp
-site/docs/search.html
-site/docs/secure.html
-site/docs/shop.html
-site/docs/shopadmin.html
-site/docs/siteusers.html
-site/docs/standard.html
-site/docs/storages.html
-site/docs/templates.html
-site/docs/thumbnails.html
-site/docs/upgrade_mysql.html
-site/docs/userupdate.html
 .#*
 *~
 
index c050bef..700e922 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -278,6 +278,7 @@ site/cgi-bin/modules/BSE/Util/ValidateHTML.pm
 site/cgi-bin/modules/BSE/Util/ValidateHTML/Tidy.pm
 site/cgi-bin/modules/BSE/Util/ValidateHTML/W3C.pm
 site/cgi-bin/modules/BSE/Validate.pm
+site/cgi-bin/modules/BSE/Variables.pm
 site/cgi-bin/modules/BSE/Version.pm
 site/cgi-bin/modules/BSE/WebUtil.pm
 site/cgi-bin/modules/Constants.pm
@@ -367,9 +368,12 @@ site/docs/access.html
 site/docs/access.pod
 site/docs/add.html
 site/docs/affiliate.html
+site/docs/Article.html
 site/docs/bse-unicode.pod
 site/docs/bse.html
 site/docs/bse.pod
+site/docs/BSE::TB::SiteCommon.html
+site/docs/BSE::TB::TagOwner.html
 site/docs/BSE::UI::Affiliate.html
 site/docs/bse_import.pod
 site/docs/bsedbschema.odt
@@ -404,6 +408,12 @@ site/docs/shopadmin.html
 site/docs/siteusers.html
 site/docs/siteusers.pod
 site/docs/Squirrel::Template.html
+site/docs/Squirrel::Template::Expr.html
+site/docs/Squirrel::Template::Expr::WrapArray.html
+site/docs/Squirrel::Template::Expr::WrapClass.html
+site/docs/Squirrel::Template::Expr::WrapCode.html
+site/docs/Squirrel::Template::Expr::WrapHash.html
+site/docs/Squirrel::Template::Expr::WrapScalar.html
 site/docs/standard.html
 site/docs/standard.pod
 site/docs/storages.html
index 82e4d38..3c7de36 100644 (file)
@@ -5,7 +5,7 @@ use BSE::Cfg;
 use BSE::Util::HTML;
 use Carp qw(cluck confess);
 
-our $VERSION = "1.011";
+our $VERSION = "1.012";
 
 sub new {
   my ($class, %opts) = @_;
@@ -459,6 +459,8 @@ sub _set_vars {
   $self->set_variable(cgi => $self->cgi);
   $self->set_variable(cfg => $self->cfg);
   $self->set_variable(assert_dynamic => 1);
+  require BSE::Variables;
+  $self->set_variable(bse => BSE::Variables->variables);
 }
 
 sub dyn_response {
diff --git a/site/cgi-bin/modules/BSE/Variables.pm b/site/cgi-bin/modules/BSE/Variables.pm
new file mode 100644 (file)
index 0000000..b147eba
--- /dev/null
@@ -0,0 +1,112 @@
+package BSE::Variables;
+use strict;
+use Scalar::Util qw(blessed);
+
+our $VERSION = "1.000";
+
+sub variables {
+  my ($self, %opts) = @_;
+  my $site;
+  return
+    {
+     site => BSE::TB::Site->new,
+     url => 
+     ($opts{admin} || $opts{admin_links}
+      ? sub { $_[0]->admin }
+      : sub { $_[0]->link }
+     ),
+     admin => $opts{admin},
+     admin_links => $opts{admin_links},
+     dumper => sub {
+       require Data::Dumper;
+       return escape_html(Data::Dumper::Dumper(shift));
+     },
+     categorize_tags => \&_categorize_tags,
+    };
+}
+
+sub _categorize_tags {
+  my ($tags) = @_;
+
+  $DB::single = 1;
+  require BSE::TB::Tags;
+  my %cats;
+  for my $tag (@$tags) {
+    my $work = blessed $tag ? $tag->json_data : $tag;
+    my $cat = lc $tag->{cat};
+    unless ($cats{$cat}) {
+      $cats{$cat} =
+       {
+        name => $tag->{cat},
+        tags => [],
+       };
+    }
+    push @{$cats{$cat}{tags}}, $tag;
+  }
+
+  for my $cat (values %cats) {
+    @{$cat->{tags}} = sort { lc $a->{val} cmp lc $b->{val} } @{$cat->{tags}};
+  }
+
+  return [ sort { lc $a->{name} cmp $b->{name} } values %cats ];
+}
+
+1;
+
+=head1 NAME
+
+BSE::Variables - commonly set variables
+
+=head1 SYNOPSIS
+
+  # in perl code
+  require BSE::Variables;
+  $foo->set_variable(bse => BSE::Variables->variables(%opts));
+
+  # in templates
+  <:.set level1 = bse.site.children :>
+  <:= url(article) | html :>
+  <:= tagcats = bse.categorize_tags(article.tag_objects) :>
+  <:.if bse.admin:>...
+  <:= dumper(somevar) :> lots of noise
+
+=head1 DESCRIPTION
+
+Common BSE functionality for use from the new template tags.
+
+=over
+
+=item bse.site
+
+a BSE::TB::Site object, behaves like an article in owning files and
+images, and having children.w
+
+=item bse.url(somearticle)
+
+Return the article admin link in admin (or admin_links) mode,
+otherwise the normal article link.
+
+=item bse.admin
+
+Return true in admin mode.
+
+=item bse.admin_links
+
+Return true in admin_links mode
+
+=item dumper(value)
+
+Dump the value in perl syntax using L<Data::Dumper>.
+
+=item categorize_keys(tags)
+
+Returns the given tags as a list of tag categories, each category has
+a name (of the category) and a list of tags in that category.
+
+=back
+
+=head1 AUTHOR
+
+Tony Cook <tony@develop-help.com>
+
+=cut
index 35a5efc..c7a6d80 100644 (file)
@@ -9,10 +9,11 @@ use BSE::Util::Tags qw(tag_article);
 use BSE::CfgInfo qw(custom_class);
 use BSE::Util::Iterate;
 use BSE::TB::Site;
+use BSE::Variables;
 use base 'BSE::ThumbLow';
 use base 'BSE::TagFormats';
 
-our $VERSION = "1.007";
+our $VERSION = "1.008";
 
 my $excerptSize = 300;
 
@@ -28,21 +29,7 @@ sub new {
   $opts{vars} =
     {
      cfg => $opts{cfg},
-     bse =>
-     {
-      site => BSE::TB::Site->new,
-      url => 
-      ($opts{admin} || $opts{admin_links}
-       ? sub { $_[0]->admin }
-       : sub { $_[0]->link }
-      ),
-      admin => $opts{admin},
-      admin_links => $opts{admin_links},
-      dumper => sub {
-       require Data::Dumper;
-       return escape_html(Data::Dumper::Dumper(shift));
-      },
-     },
+     bse => BSE::Variables->variables(%opts),
     };
   my $self = bless \%opts, $class;
   $self->set_variable_class(articles => "Articles");
diff --git a/site/docs/.gitignore b/site/docs/.gitignore
new file mode 100644 (file)
index 0000000..631ebcc
--- /dev/null
@@ -0,0 +1,41 @@
+/Article.html
+/BSE::TB::SiteCommon.html
+/BSE::TB::TagOwner.html
+/BSE::UI::Affiliate.html
+/BSE::Variables.html
+/Generate.html
+/Generate::Article.html
+/Generate::Catalog.html
+/Generate::Product.html
+/Squirrel::Template.html
+/Squirrel::Template::Expr.html
+/Squirrel::Template::Expr::WrapArray.html
+/Squirrel::Template::Expr::WrapHash.html
+/Squirrel::Template::Expr::WrapScalar.html
+/Squirrel::Template::Expr::WrapCode.html
+/Squirrel::Template::Expr::WrapClass.html
+/TODO.html
+/access.html
+/add.html
+/affiliate.html
+/bse.html
+/bugs.html
+/config.html
+/dynamic.html
+/formmail.html
+/future_plans.html
+/gen.html
+/makedocs.html
+/pod2htmd.tmp
+/pod2htmi.tmp
+/search.html
+/secure.html
+/shop.html
+/shopadmin.html
+/siteusers.html
+/standard.html
+/storages.html
+/templates.html
+/thumbnails.html
+/upgrade_mysql.html
+/userupdate.html
index 6ec7f43..421bc68 100644 (file)
@@ -11,6 +11,7 @@ my @targets =
    'bse.html',
    'BSE::TB::SiteCommon.html',
    'BSE::TB::TagOwner.html',
+   'BSE::Variables.html',
    'bugs.html',
    'templates.html',
    'Generate.html',
@@ -18,6 +19,12 @@ my @targets =
    'Generate::Product.html',
    'Generate::Catalog.html',
    'Squirrel::Template.html',
+   'Squirrel::Template::Expr.html',
+   'Squirrel::Template::Expr::WrapArray.html',
+   'Squirrel::Template::Expr::WrapHash.html',
+   'Squirrel::Template::Expr::WrapScalar.html',
+   'Squirrel::Template::Expr::WrapCode.html',
+   'Squirrel::Template::Expr::WrapClass.html',
    'search.html',
    'shop.html',
    'add.html',
index 8526990..d5da3f6 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use BSE::Test ();
-use Test::More tests=>147;
+use Test::More tests=>150;
 use File::Spec;
 use FindBin;
 BEGIN {
@@ -467,6 +467,33 @@ contentB
 <:or Or:><:eif Or:><:endswitch:>
 EXPECTED
 
+# tags
+template_test "bse.categorize_tags", $parent, <<'TEMPLATE', <<'EXPECTED';
+<:.set tags = [
+  { "cat":"One", "val":"A", "name":"One: A" },
+  { "cat":"One", "val":"B", "name":"One: B" },
+  { "cat":"Two", "val":"C", "name":"Two: C" },
+  { "cat":"", "val":"D", "name":"D" }
+  ] -:>
+<:.set tagcats = bse.categorize_tags(tags) -:>
+<:.for tagcat in tagcats -:>
+Category: <:= tagcat.name:>
+<:.for tag in tagcat.tags :>  Tag: <:= tag.val:>
+<:.end for:>
+<:.end for -:>
+TEMPLATE
+Category: 
+  Tag: D
+
+Category: One
+  Tag: A
+  Tag: B
+
+Category: Two
+  Tag: C
+
+EXPECTED
+
 ############################################################
 # dynamic stuff
 require BSE::Dynamic::Article;
@@ -572,11 +599,11 @@ sub template_test($$$$) {
       $content =
        $gen->generate_low($template, $article, 'Articles', 0);
     };
-    ok($content, "$tag: generate content");
+    ok(defined $content, "$tag: generate content");
     diag $@ unless $content;
   }
  SKIP: {
-     skip "$tag: couldn't gen content", 1 unless $content;
+     skip "$tag: couldn't gen content", 1 unless defined $content;
      is($content, $expected, "$tag: comparing");
    }
 }