From: Frederik Vanrenterghem Date: Sun, 27 Nov 2022 14:35:49 +0000 (+0800) Subject: Add modified opengraph plugin. X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/commitdiff_plain/6c546c8f3182668c6d21d578b789674894f18c39 Add modified opengraph plugin. --- diff --git a/IkiWiki/Plugin/opengraph2.pm b/IkiWiki/Plugin/opengraph2.pm new file mode 100644 index 000000000..4148a2438 --- /dev/null +++ b/IkiWiki/Plugin/opengraph2.pm @@ -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} = ''; + my ${opengraph_tag_description} = ''; + my ${opengraph_tag_type} = ''; + my ${opengraph_tag_image} = ''; + my ${opengraph_tag_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 +. To test your site against the +Open Graph rules, use the tool available on +. + +=head1 INSTALLATION + +Put F in F<$HOME/.ikiwiki/IkiWiki/Plugin/> or elsewhere in +your C<@INC> path. Or read . + +=head1 CONFIGURATION + +Add to the configuration in your F file. + + ## opengraph2 plugin + # For more information, see + # . + # Default values for + # obtained from + # 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 and C 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 . + +=head1 SEE ALSO + +=over 4 + +=item https://ikiwiki.info/plugins/contrib/opengraph/ + +=item https://en.wikipedia.org/wiki/Open_Graph + +=back