+++ /dev/null
-package Newslash::Plugin::AddStructuredData;
-use Mojo::Base 'Mojolicious::Plugin';
-use Data::Dumper;
-
-sub register {
- my ($self, $app, $conf) = @_;
-
- $app->hook(before_render => sub {
- my ($c, $args) = @_;
- my $data = generate_structured_data($c, $args);
- $c->stash(structured_data => $data);
- });
-}
-
-sub generate_structured_data {
- my ($c, $args) = @_;
- my $cfg = $c->app->config->{Site} || {};
- my $page = $args->{page} || $c->stash("page") || {};
-
- my $data = { '@context' => "http://schema.org",
- };
-
- # set content type
- if ($page->{type} eq "single") {
- # single content page
- if ($page->{content_type} eq "story") {
- $data->{'@type'} = "NewsArticle";
- }
- elsif ($page->{content_type} eq "journal") {
- $data->{'@type'} = "BlogPosting";
- }
- }
- elsif ($page->{type} eq "user") {
- if ($page->{content_type} eq "recent") {
- $data->{'@type'} = "Person";
- my $the_user = $args->{the_user} || $c->stash("the_user");
- if ($the_user) {
- $data->{name} = $the_user->{nickname};
- my $info = $the_user->{config}->{info};
- $data->{alternateName} = $info->{realname} if $info->{realname};
- $data->{url} = $info->{url} if $info->{url};
- $data->{description} = $c->strip_html($info->{biography}) if $info->{biography};
- return $data;
- }
- }
- return;
- }
- elsif ($page->{type} eq "timeline") {
- $data->{'@type'} = "WebSite";
- $data->{name} = $cfg->{name};
- $data->{alternateName} = $cfg->{alternate_name} if $cfg->{alternate_name};
- $data->{description} = $cfg->{description};
- $data->{url} = $cfg->{base_url};
- $data->{potentialAction} = { '@type' => "SearchAction",
- target => "$cfg->{base_url}/search?q={search_term_string}",
- "query-input" => "required name=search_term_string",
- };
- $data->{sameAs} = $cfg->{same_as} if $cfg->{same_as};
- }
- else {
- # no generate structured data
- return;
- }
-
- $data->{headline} = $page->{title} if $page->{title};
- $data->{datePublished} = $c->format_timestamp(mysql => $page->{create_time},
- format => 'iso8601') if $page->{create_time};
- $data->{dateModified} = $c->format_timestamp(mysql => $page->{update_time},
- format => 'iso8601') if $page->{update_time};
- $data->{articleBody} = $page->{description} if $page->{description};
- $data->{image} = $page->{thumbnail} if $page->{thumbnail};
-
- $data->{mainEntityOfPage} = { '@type' => "WebPage",
- '@id' => $page->{canonical} };
-
- # add author data
- if ($page->{author}) {
- my $u = $page->{author};
- my $i = $u->{config}->{info};
- $data->{author} = { '@type' => "Person",
- name => $u->{nickname} };
- $data->{author}->{alternateName} = $i->{realname} if $i->{realname};
- $data->{author}->{url} = $i->{homepage} if $i->{homepage};
- }
-
- # add publisher data
- $data->{publisher} = { '@type' => "Organization",
- name => $cfg->{publisher},
- url => $cfg->{publisher_url},
- logo => { '@type' => "ImageObject",
- url => $cfg->{publisher_logo},
- },
- };
-
- return $data;
-}
-
-1;
+++ /dev/null
-package Newslash::Plugin::AddThumbnail;
-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("thumbnail");
- my $cfg = $app->config->{Site} || {};
- my $base_url = $cfg->{base_url};
- my $icon_url = $base_url . $cfg->{topic_icon_base_url} . "/";
-
- my $item = $c->stash("item") || $args->{item};
- if ($item && ref($item->{primary_topic}) eq "HASH") {
- if ($item->{primary_topic}->{image}) {
- $c->stash(thumbnail => $icon_url . $item->{primary_topic}->{image});
- return;
- }
- }
- if ($cfg->{default_thumbnail}) {
- $c->stash(thumbnail => $base_url . $cfg->{default_thumbnail});
- return;
- }
- });
-}
-
-
-
-1;
+++ /dev/null
-package Newslash::Plugin::Canonical;
-use Mojo::Base 'Mojolicious::Plugin';
-use Mojo::URL;
-
-use POSIX qw(strftime locale_h);
-
-use constant CANONICAL_TABLE => {
- qr{^/~([^/]*)/journal/?} => '/~$1/journals',
- };
-
-sub get_canonical_path {
- my ($self, $path) = @_;
- for my $rex (keys %{CANONICAL_TABLE()}) {
- my $to_path = CANONICAL_TABLE->{$rex};
- if ($path =~ $rex) {
- my $rep = $1;
- $path = $to_path;
- $path =~ s/\$1/$rep/;
- }
- }
- if (substr($path, -1, 1) ne "/") {
- $path = "$path/";
- }
- return $path;
-}
-
-sub register {
- my ($self, $app, $conf) = @_;
-
- $app->hook(before_render => sub {
- my ($c, $args) = @_;
- return if $c->stash('canonical');
- my $url = Mojo::URL->new($c->url_for);
- my $path = $self->get_canonical_path($url->path);
-
- $url->host('srad.jp');
- $url->port('');
- $url->scheme('https');
- $url->path($path);
- $c->stash(canonical => $url->to_string);
- });
- #$app->helper(canonical => sub { state $canonical = $self; });
-}
-
-
-
-1;