]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/opengraph2.pm
Add modified opengraph plugin.
[git.ikiwiki.info.git] / IkiWiki / Plugin / opengraph2.pm
1 #!/usr/bin/perl
3 package IkiWiki::Plugin::opengraph2;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 our $VERSION = '0.2.0';
11 sub import {
12     hook(type => "preprocess", id => "opengraph2", call => \&preprocess);
13     hook(type => "pagetemplate", id => "opengraph2", call => \&opengraph_tags);
14 }
16 sub scrub ($$$) {
17         if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
18                 return IkiWiki::Plugin::htmlscrubber::sanitize(
19                         content => shift, page => shift, destpage => shift);
20         }
21         else {
22                 return shift;
23         }
24 }
26 sub preprocess (@) {
27         return "" unless @_;
28         my %params=@_;
29         my $key=shift;
30         my $value=$params{$key};
31         delete $params{$key};
32         my $page=$params{page};
33         delete $params{page};
34         my $destpage=$params{destpage};
35         delete $params{destpage};
36         delete $params{preview};
38         eval q{use HTML::Entities};
39         $value=decode_entities($value);
41         if ($key eq 'ogtitle') {
42             $pagestate{$page}{opengraph2}{ogtitle}=$value;
43         } elsif ($key eq 'ogimage') {
44             $pagestate{$page}{opengraph2}{ogimage}=urlto($value, 'index', '1');
45         } elsif ($key eq 'ogdesc') {
46             $pagestate{$page}{opengraph2}{ogdesc}=$value;
47         } elsif ($key eq 'ogtype') {
48             $pagestate{$page}{opengraph2}{ogtype}=$value;
49         }
50         return "";
51 }
53 sub opengraph_tags (@) {
54         my %params=@_;
55         my $page=$params{page};
56         my $destpage=$params{destpage};
57         my $template=$params{template};
58         
59         my ${opengraph_title} = $pagestate{$page}{opengraph2}{ogtitle} || pagetitle(${params}{destpage}) || ${config}{'opengraph_title'};
60         my ${opengraph_description} = $pagestate{$page}{opengraph2}{ogdesc} || ${config}{'opengraph_description'};
61         my ${opengraph_type} = $pagestate{$page}{opengraph2}{ogtype} || ${config}{'opengraph_type'};
62         my ${opengraph_image} = $pagestate{$page}{opengraph2}{ogimage} || ${config}{'opengraph_image'};
63         my ${opengraph_url} = urlto(${params}{destpage}, 'index', '1') || ${config}{'opengraph_url'};
65         my ${opengraph_tag_title} = '<meta property="og:title" content="'.${opengraph_title}.'" />';
66         my ${opengraph_tag_description} = '<meta property="og:description" content="'.${opengraph_description}.'" />';
67         my ${opengraph_tag_type} = '<meta property="og:type" content="'.${opengraph_type}.'" />';
68         my ${opengraph_tag_image} = '<meta property="og:image" content="'.${opengraph_image}.'" />';
69         my ${opengraph_tag_url} = '<meta property="og:url" content="'.${opengraph_url}.'" />';
71         if (exists $pagestate{$page}{opengraph2} && $template->query(name => "meta")) {
72             my %seen;
73             $template->param(meta => join("\n",grep { (! $seen{$_}) && ($seen{$_}=1) } ${opengraph_tag_title},
74                                           ,"\n",${opengraph_tag_description},
75                                           ,"\n",${opengraph_tag_type},
76                                           ,"\n",${opengraph_tag_image},
77                                           ,"\n",${opengraph_tag_url}));
78         }
79             
80 }
82 1;
84 __END__
86 =head1 NAME
88 IkiWiki::Plugin::opengraph2 - Adds Open Graph tags on the html head
90 =head1 DESCRIPTION
92 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
93 <https://en.wikipedia.org/wiki/Open_Graph>. To test your site against the
94 Open Graph rules, use the tool available on
95 <https://developers.facebook.com/tools/debug/og/object/>.
97 =head1 INSTALLATION
99 Put F<opengraph2.pm> in F<$HOME/.ikiwiki/IkiWiki/Plugin/> or elsewhere in
100 your C<@INC> path. Or read <http://ikiwiki.info/plugins/install/>.
102 =head1 CONFIGURATION
104 Add to the configuration in your F<blog.setup> file.
106         ## opengraph2 plugin
107         # For more information, see
108         # <https://en.wikipedia.org/wiki/Open_Graph#Open_Graph_protocol>.
109         # Default values for <http://ikiwiki.info>
110         # obtained from <https://developers.facebook.com/tools/debug/og/object/>
111         # meta property="og:title"
112         opengraph_title: "ikiwiki"
113         # meta property="og:type"
114         opengraph_type: "website"
115         # meta property="og:url"
116         opengraph_url: "http://ikiwiki.info/"
117         # meta property="og:image"
118         opengraph_image: "http://ikiwiki.info/logo/ikiwiki.png"
119         # meta property="og:description"
120         opengraph_description: "Ikiwiki is a wiki compiler."
122 Add C<meta> and C<opengraph2> to the list of plugins:
124         add_plugins => [qw{goodstuff opengraph2}],
126 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.
128 =head1 TEMPLATES
130 Meta tags will show where the meta plugin has been configured to place them.
132 =head1 BUGS AND LIMITATIONS
134 =head1 LICENSE AND COPYRIGHT
136 Copyleft (.) 2022 Frederik Vanrenterghem
137 This program is free software: you can redistribute it and/or modify
138 it under the terms of the GNU General Public License as published by
139 the Free Software Foundation, either version 3 of the License, or
140 (at your option) any later version.
141 This program is distributed in the hope that it will be useful,
142 but WITHOUT ANY WARRANTY; without even the implied warranty of
143 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
144 GNU General Public License for more details.
145 You should have received a copy of the GNU General Public License
146 along with this program. If not, see <http://www.gnu.org/licenses/>.
148 =head1 SEE ALSO
150 =over 4
152 =item https://ikiwiki.info/plugins/contrib/opengraph/
154 =item https://en.wikipedia.org/wiki/Open_Graph
156 =back