Add modified opengraph plugin.
authorFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Sun, 27 Nov 2022 14:35:49 +0000 (22:35 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Sun, 27 Nov 2022 14:35:49 +0000 (22:35 +0800)
IkiWiki/Plugin/opengraph2.pm [new file with mode: 0644]

diff --git a/IkiWiki/Plugin/opengraph2.pm b/IkiWiki/Plugin/opengraph2.pm
new file mode 100644 (file)
index 0000000..4148a24
--- /dev/null
@@ -0,0 +1,156 @@
+#!/usr/bin/perl
+
+package IkiWiki::Plugin::opengraph2;
+
+use warnings;
+use strict;
+use IkiWiki 3.00;
+
+our $VERSION = '0.2.0';
+
+sub import {
+    hook(type => "preprocess", id => "opengraph2", call => \&preprocess);
+    hook(type => "pagetemplate", id => "opengraph2", call => \&opengraph_tags);
+}
+
+sub scrub ($$$) {
+       if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
+               return IkiWiki::Plugin::htmlscrubber::sanitize(
+                       content => shift, page => shift, destpage => shift);
+       }
+       else {
+               return shift;
+       }
+}
+
+sub preprocess (@) {
+        return "" unless @_;
+        my %params=@_;
+        my $key=shift;
+        my $value=$params{$key};
+        delete $params{$key};
+        my $page=$params{page};
+        delete $params{page};
+       my $destpage=$params{destpage};
+        delete $params{destpage};
+        delete $params{preview};
+
+        eval q{use HTML::Entities};
+        $value=decode_entities($value);
+
+       if ($key eq 'ogtitle') {
+           $pagestate{$page}{opengraph2}{ogtitle}=$value;
+       } elsif ($key eq 'ogimage') {
+           $pagestate{$page}{opengraph2}{ogimage}=urlto($value, 'index', '1');
+       } elsif ($key eq 'ogdesc') {
+           $pagestate{$page}{opengraph2}{ogdesc}=$value;
+       } elsif ($key eq 'ogtype') {
+           $pagestate{$page}{opengraph2}{ogtype}=$value;
+       }
+       return "";
+}
+
+sub opengraph_tags (@) {
+        my %params=@_;
+        my $page=$params{page};
+        my $destpage=$params{destpage};
+        my $template=$params{template};
+       
+       my ${opengraph_title} = $pagestate{$page}{opengraph2}{ogtitle} || pagetitle(${params}{destpage}) || ${config}{'opengraph_title'};
+       my ${opengraph_description} = $pagestate{$page}{opengraph2}{ogdesc} || ${config}{'opengraph_description'};
+       my ${opengraph_type} = $pagestate{$page}{opengraph2}{ogtype} || ${config}{'opengraph_type'};
+       my ${opengraph_image} = $pagestate{$page}{opengraph2}{ogimage} || ${config}{'opengraph_image'};
+       my ${opengraph_url} = urlto(${params}{destpage}, 'index', '1') || ${config}{'opengraph_url'};
+
+       my ${opengraph_tag_title} = '<meta property="og:title" content="'.${opengraph_title}.'" />';
+       my ${opengraph_tag_description} = '<meta property="og:description" content="'.${opengraph_description}.'" />';
+       my ${opengraph_tag_type} = '<meta property="og:type" content="'.${opengraph_type}.'" />';
+       my ${opengraph_tag_image} = '<meta property="og:image" content="'.${opengraph_image}.'" />';
+       my ${opengraph_tag_url} = '<meta property="og:url" content="'.${opengraph_url}.'" />';
+
+       if (exists $pagestate{$page}{opengraph2} && $template->query(name => "meta")) {
+           my %seen;
+           $template->param(meta => join("\n",grep { (! $seen{$_}) && ($seen{$_}=1) } ${opengraph_tag_title},
+                                         ,"\n",${opengraph_tag_description},
+                                         ,"\n",${opengraph_tag_type},
+                                         ,"\n",${opengraph_tag_image},
+                                         ,"\n",${opengraph_tag_url}));
+       }
+           
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+IkiWiki::Plugin::opengraph2 - Adds Open Graph tags on the html head
+
+=head1 DESCRIPTION
+
+This plugin implements the Open Graph tags in the head of the hmtl for pages that have an opengraph2 directive. For more information on what is Open Graph, visit
+<https://en.wikipedia.org/wiki/Open_Graph>. To test your site against the
+Open Graph rules, use the tool available on
+<https://developers.facebook.com/tools/debug/og/object/>.
+
+=head1 INSTALLATION
+
+Put F<opengraph2.pm> in F<$HOME/.ikiwiki/IkiWiki/Plugin/> or elsewhere in
+your C<@INC> path. Or read <http://ikiwiki.info/plugins/install/>.
+
+=head1 CONFIGURATION
+
+Add to the configuration in your F<blog.setup> file.
+
+       ## opengraph2 plugin
+       # For more information, see
+       # <https://en.wikipedia.org/wiki/Open_Graph#Open_Graph_protocol>.
+       # Default values for <http://ikiwiki.info>
+       # obtained from <https://developers.facebook.com/tools/debug/og/object/>
+       # meta property="og:title"
+       opengraph_title: "ikiwiki"
+       # meta property="og:type"
+       opengraph_type: "website"
+       # meta property="og:url"
+       opengraph_url: "http://ikiwiki.info/"
+       # meta property="og:image"
+       opengraph_image: "http://ikiwiki.info/logo/ikiwiki.png"
+       # meta property="og:description"
+       opengraph_description: "Ikiwiki is a wiki compiler."
+
+Add C<meta> and C<opengraph2> to the list of plugins:
+
+       add_plugins => [qw{goodstuff opengraph2}],
+
+Use opengraph2 directives ogdesc, ogtype, ogtitle or ogimage to set a page as needing Open Graph headers. Values from the config will be used for any omitted directives.
+
+=head1 TEMPLATES
+
+Meta tags will show where the meta plugin has been configured to place them.
+
+=head1 BUGS AND LIMITATIONS
+
+=head1 LICENSE AND COPYRIGHT
+
+Copyleft (.) 2022 Frederik Vanrenterghem
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+=head1 SEE ALSO
+
+=over 4
+
+=item https://ikiwiki.info/plugins/contrib/opengraph/
+
+=item https://en.wikipedia.org/wiki/Open_Graph
+
+=back