--- /dev/null
+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;
use DateTime::Format::MySQL;
use DateTime::Format::ISO8601;
use Data::Dumper;
-
+use Mojo::DOM;
use Newslash::Util::TextFormatter;
=encoding utf8
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"))
}
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;
+}
######################################################################
#
# Wiki contents reader helper
$app->plugin('Newslash::Plugin::WikiContentsReader');
+ # Set Description automatically
+ $app->plugin('Newslash::Plugin::AddDescription');
+
############################################################
#
# Routing Settings
[%- 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>
--- /dev/null
+[%- 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 %]">
END;
END;
END;
-END -%]
-[% title %]
+END %]
+ <title>[% title %]</title>
+ <meta itemprop="name" content="[% title %]">