OSDN Git Service

add <meta type="description"> tag
authorhylom <hylom@users.sourceforge.jp>
Thu, 18 Apr 2019 12:18:42 +0000 (21:18 +0900)
committerhylom <hylom@users.sourceforge.jp>
Thu, 18 Apr 2019 12:18:42 +0000 (21:18 +0900)
src/newslash_web/lib/Newslash/Plugin/AddDescription.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Plugin/NewslashHelpers.pm
src/newslash_web/lib/Newslash/Web.pm
src/newslash_web/templates/common/header.html.tt2
src/newslash_web/templates/common/meta_description.html.tt2 [new file with mode: 0644]
src/newslash_web/templates/common/title.html.tt2

diff --git a/src/newslash_web/lib/Newslash/Plugin/AddDescription.pm b/src/newslash_web/lib/Newslash/Plugin/AddDescription.pm
new file mode 100644 (file)
index 0000000..949c7eb
--- /dev/null
@@ -0,0 +1,22 @@
+package Newslash::Plugin::AddDescription;
+use Mojo::Base 'Mojolicious::Plugin';
+use Data::Dumper;
+sub register {
+    my ($self, $app, $conf) = @_;
+
+    $app->hook(before_render => sub {
+                   my ($c, $args) = @_;
+                   return if $c->stash("description");
+
+                   my $item = $c->stash("item") || $args->{item};
+                   if ($item) {
+                       if ($item->{intro_text}) {
+                           $c->stash(description => $c->extract_description($item->{intro_text}));
+                       }
+                   }
+               });
+}
+
+
+
+1;
index 918bfab..d01df43 100644 (file)
@@ -7,7 +7,7 @@ use DateTime;
 use DateTime::Format::MySQL;
 use DateTime::Format::ISO8601;
 use Data::Dumper;
-
+use Mojo::DOM;
 use Newslash::Util::TextFormatter;
 
 =encoding utf8
@@ -46,6 +46,7 @@ sub register {
                   get_timestamp_by_id
                   tidy_html clean_html escape_html format_htmltext escape_plaintext strip_by_mode
                   get_authors
+                  extract_description
                 ]) {
         $app->helper($k => $self->can("_$k"))
     }
@@ -179,6 +180,68 @@ sub _get_authors {
     return $authors;
 }
 
+=head2 extract_description($text, $min_length, $max_length)
+
+  extract description from text string
+
+=cut
+
+sub _extract_description {
+    my ($c, $str, $min_length, $max_length) = @_;
+    my $cnf = ($c->config->{Site} || {})->{meta_description} || {};
+    $min_length ||= $cnf->{min_length} || 50;
+    $max_length ||= $cnf->{max_length} || 180;
+
+    # remove HTML tags
+    my $dom = Mojo::DOM->new($str);
+    my $text;
+    if (!$dom) {
+        $c->app->log->error("NewslashHelpers::extract_description: html parse failed: $str");
+        $text = $str;
+    }
+    else {
+        $text = $dom->all_text;
+    }
+
+    # remove parentheses
+    while ($text =~ m/[\(\(][^\(\(]*?[\)\)]/) {
+        $text =~ s/[\(\(][^\(\(]*?[\)\)]//g;
+    }
+
+    # remove CR
+    $text =~ s/\n//g;
+
+    # remove preface
+    $text =~ s/^\s*[^曰]*曰く、\s*//;
+
+    # split to paragraphs
+    my @paragraphs = split(/([。.])/, $text);
+    my $result;
+    my $length = 0;
+
+    while (@paragraphs) {
+        my $t = shift @paragraphs;
+        if (($length + length($t)) > $max_length) {
+            unshift @paragraphs, $t;
+            last;
+        }
+        $length += length($t);
+        $result .= $t;
+    }
+
+    if ($length <= $min_length) {
+        my $t = shift @paragraphs;
+        $result .= $t;
+        $length += length($t);
+
+        if ($length > $max_length) {
+            $result = substr($result, 0, $max_length - 3);
+            $result .= "...";
+        }
+    }
+
+    return $result;
+}
 
 ######################################################################
 # 
index 4e28fb8..7bcfaac 100644 (file)
@@ -233,6 +233,9 @@ sub startup {
     # Wiki contents reader helper
     $app->plugin('Newslash::Plugin::WikiContentsReader');
 
+    # Set Description automatically
+    $app->plugin('Newslash::Plugin::AddDescription');
+
     ############################################################
     #
     # Routing Settings
index 3652f3a..be3332a 100644 (file)
@@ -5,18 +5,19 @@
     [%- ELSE %]
     <!-- running under [% helpers.app.mode %] mode. -->
     [%- END %]
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-    <meta name="viewport" content="width=device-width" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
+    <meta name="viewport" content="width=device-width" >
 
     [%- # IF noindex -%]
-    <meta name="robots" content="noindex" />
+    <meta name="robots" content="noindex" >
     [%- #END -%]
 
     [%- IF canonical %]
-    <link rel="caonical" href="[% canonical %]" />
+    <link rel="caonical" href="[% canonical %]" >
     [%- END -%]
 
-    <title>[% INCLUDE common/title %]</title>
+    [%- INCLUDE common/title %]
+    [%- INCLUDE common/meta_description %]
 
     <link rel="stylesheet" type="text/css" media="screen, projection" href="[% helpers.preprocessor.get_md5_path('/css/newslash.css'); %]" />
     <script src="[% Site.js_path %]/jquery/jquery-3.1.1.min.js"></script>
diff --git a/src/newslash_web/templates/common/meta_description.html.tt2 b/src/newslash_web/templates/common/meta_description.html.tt2
new file mode 100644 (file)
index 0000000..3156ab8
--- /dev/null
@@ -0,0 +1,10 @@
+[%- IF !description;
+  IF item && item.content_type == "poll";
+    description = "「" _ item.title _ "」投票結果";
+  ELSE;
+    description = config.Site.description;
+  END;
+END -%]
+    <meta name="description" content="[% description %]">
+    <meta property="og:description" content="[% description %]">
+    <meta itemprop="description" content="[% description %]">
index 51b50cb..fdd03a6 100644 (file)
@@ -104,5 +104,6 @@ ELSE;
       END;
     END;
   END;
-END -%]
-[% title %]
+END %]
+    <title>[% title %]</title>
+    <meta itemprop="name" content="[% title %]">