OSDN Git Service

Plugin: rename 'QuasiStaticConent' to 'Preprocessor' and add some helpers
authorhylom <hylom@users.sourceforge.jp>
Fri, 22 Jun 2018 10:44:24 +0000 (19:44 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 22 Jun 2018 10:44:24 +0000 (19:44 +0900)
src/newslash_web/lib/Newslash/Plugin/Preprocessor.pm [moved from src/newslash_web/lib/Newslash/Plugin/QuasiStaticContent.pm with 92% similarity]
src/newslash_web/lib/Newslash/Web.pm
src/newslash_web/templates/common/header.html.tt2

@@ -1,4 +1,4 @@
-package Newslash::Plugin::QuasiStaticContent;
+package Newslash::Plugin::Preprocessor;
 use Mojo::Base 'Mojolicious::Plugin';
 
 use File::Spec;
@@ -238,23 +238,6 @@ sub register {
     #my $contents = $self->{contents};
     #$app->log->debug(dumper($contents));
 
-    if ($app->config->{TT2Renderer}) {
-        my $tt2r = $app->config->{TT2Renderer}->{self};
-        my $get_content = sub {
-            my ($c, $name) = @_;
-            my $content = $self->get_cache($name);
-            if ($content) {
-                return $content->{path};
-            }
-            else {
-                $self->{app}->log->debug("QSC: no cache found for '$name'");
-            }
-            return '';
-        };
-        $tt2r->add_NS_function('static_content', $get_content);
-        $tt2r->add_NS_function('generated_content', $get_content);
-    }
-
     my $r = $app->routes;
     $r->any('/static/*content_path' => sub {
                 my $c = shift;
@@ -271,7 +254,19 @@ sub register {
                 $c->rendered(404);
             });
 
-    $app->helper(static_content => sub { state $static_content = $self; });
+    $app->helper(preprocessor => sub { state $preprocessor = $self; });
+}
+
+sub get_path {
+    my ($self, $name) = @_;
+    my $content = $self->get_cache($name);
+    if ($content) {
+        return $content->{path};
+    }
+    else {
+        $self->{app}->log->debug("QSC: no cache found for '$name'");
+    }
+    return;
 }
 
 sub _execute_cmd {
index d8d803e..7675c59 100644 (file)
@@ -130,8 +130,8 @@ sub startup {
         $app->plugin('Newslash::Plugin::AntiCsrf');
     }
 
-    # quasi-static content
-    $app->plugin('Newslash::Plugin::QuasiStaticContent');
+    # contents preprocessor
+    $app->plugin('Newslash::Plugin::Preprocessor');
 
     # javascript loader
     $app->plugin('Newslash::Plugin::JavaScriptLoader');
@@ -197,7 +197,7 @@ sub startup {
                 acl2_types => \@acl2_types,
                };
     my $siteconfig = $app->tt2renderer->render($templ_name, $vars);
-    $app->static_content->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
+    $app->preprocessor->add_content("js/siteconfig.js", $siteconfig, "text/javascript; charset=utf-8");
 
     ############################################################
     #
index 759ba46..ba68393 100644 (file)
@@ -19,8 +19,8 @@
 
   <!-- Latest compiled and minified CSS -->
   <script src="/jquery/jquery-3.1.1.min.js"></script>
-  <link rel="stylesheet" type="text/css" href="[% NS.static_content('css/bootstrap.css'); %]" />
-  <link rel="stylesheet" type="text/css" media="screen, projection" href="[% NS.static_content('css/newslash.css'); %]" />
+  <link rel="stylesheet" type="text/css" href="[% helpers.preprocessor.get_path('css/bootstrap.css'); %]" />
+  <link rel="stylesheet" type="text/css" media="screen, projection" href="[% helpers.preprocessor.get_path('css/newslash.css'); %]" />
   <script src="/bootstrap/js/bootstrap.min.js" ></script>
   <script src="/js/newslash.js"></script>