Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
initial version in Github V2.1
  • Loading branch information
heikoS2C committed Apr 24, 2017
1 parent fe5acbc commit 7b0f6a6
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README
@@ -0,0 +1,27 @@
htmlmetatags Plugin for DokuWiki

Set html meta tags.

All documentation for this plugin can be found at
http://www.soft2c.de

If you install this plugin manually, make sure it is installed in
lib/plugins/htmlmetatags/ - if the folder is called different it
will not work!

Please refer to http://www.dokuwiki.org/plugins for additional info
on how to install plugins in DokuWiki.

----
Copyright (C) Heiko Heinz <heiko.heinz@soft2c.de>

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; version 2 of the License

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.

See the COPYING file in your DokuWiki folder for details
90 changes: 90 additions & 0 deletions action.php
@@ -0,0 +1,90 @@
<?php
/**
* DokuWiki Plugin htmlmetatags (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Heiko Heinz <heiko.heinz@soft2c.de>
* @author Eric Maeker <eric@maeker.fr>
*/

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');

class action_plugin_htmlmetatags extends DokuWiki_Action_Plugin {

/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {

// $controller->register_hook('htmlmetatags', 'FIXME', $this, 'handle_htmlmetatags');
$controller->register_hook('TPL_METAHEADER_OUTPUT','BEFORE',$this,'handle_htmlmetatags',array());

}
/*
public function searchname($namematch, $meta){
for ($i=0;$i<sizeof($meta);$i++) {
$a = $meta[$i];
if($namematch == $a['name']){
return $a;
}
}
return null;
}
*/

function replaceMeta(&$pageArray, $name, $value) {
// Override dokuwiki default meta tags
$found = False;
foreach($pageArray['meta'] as $k => $v) {
if ($v["name"] == $name) {
$v["content"] = $value;
$pageArray['meta'][$k] = $v;
$found = True;
}
}
// If meta not set, add it as name or property
if (!$found) {
if (strpos($name, ':') !== false)
$pageArray['meta'][] = array("property" => $name, "content" => $value);
else
$pageArray['meta'][] = array("name" => $name, "content" => $value);
}
}


/**
* [Custom event handler which performs action]
* Prints keywords to the meta header
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_htmlmetatags(Doku_Event &$event, $param) {

global $ID;
global $conf;
if (empty($event->data)) return; // nothing to do for us

$metadata = p_get_metadata($ID);

$a = $metadata["htmlmetatags"];
if(empty($a)) return;

foreach(array_keys($a) as $cur_key) {
$this->replaceMeta($event->data, $cur_key, $a[$cur_key]);
}
}

}

// vim:ts=4:sw=4:et:
100 changes: 100 additions & 0 deletions descr.txt
@@ -0,0 +1,100 @@
====== HtmlMetaTags Plugin ======

---- plugin ----
description: Adds some (open graph) meta tags to the html header (e.g. keywords, description or any others)
author : Soft2C.de
email : info@soft2c.de
type : Action, Syntax
lastupdate : 2017-04-24
compatible : 2016-06-26 "Elenor Of Tsort",2012-10-13 "Adora Belle",2013-05-10a "Weatherwax",Release 2013-12-08 "Binky",Release 2014-05-05e "Ponder Stibbons",Release 2014-09-29d "Hrun", 2015-08-10 "Detritus"
tags : html meta search seo info open graph

downloadurl: http://www.soft2c.de/pub/htmlmetatagsV2.2.zip
donationurl: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GN27JNRYKUYTG
----

===== Description =====

This plugin enriches the html page with all meta tags what you wont, e.g. keywords, description and any others.
Some meta information are very usefull for web crawlers for indexing and categorizing the page.

Extends with V2.0:
This version allow you to add :
* open graph metatags like //og:description//, //og:image//...
* link to media file. tags must start with //media-//
* //media-og:image=(:wiki:image.jpg)//
* multiple calls to htmlmetatags all values will be added

**Note:** The Initial version of this plugIn is not compatible to the new V2.xy. The difference of attribute to value splitter is changed from ':' to '='. \\

<code>
{{htmlmetatags>metatag-keywords=(values) metatag-og:description=(open graph description.)}}
</code>

**Note:** Linebreaks can add inside the <nowiki>{{ }}</nowiki> without any trouble.
<code>
{{htmlmetatags>metatag-keywords=(values)
metatag-og:description=(open graph description.)
}}
</code>

===== Syntax =====

With the string ''%%{{htmlmetatags>...}}%%'' on the page level you activates the plugin. The leading string "metatag-" marks a tag for the web site. After this leading string followed the key from tag. In ''%%=(...)%%'' write the value of the tag.

Also, the plugin is on page level.

Example to activate the plugin on your page write:

<code>
{{htmlmetatags>metatag-mytag=(some usefull text)}}
</code>

The meta tag: //mytag// is any word. This word is the name of the new meta tag, e.g. keyword. Followed on this name is to write ''='' with ''%%('...')%%''. This brackets contains the text required for the value of the new meta tag.

You can write more than once meta tag on the page separate by space.

<code>
{{htmlmetatags>metatag-robots=()
metatag-keywords=(apfel,bananne,birne)
metatag-description=(Allgemeiner Obstbauer)
metatag-media-og:image=(:wiki:image.jpg)
metatag-og:description=(anything else)
metatag-og:any=(anything else 2)
}}

</code>

The **result** on web page is:

<code html>
<meta name="robots" content="noindex,nofollow"/>
<meta name="keywords" content="apfel,bananne,birne"/>
<meta name="description" content="Allgemeiner Obstbauer"/>
<meta property="og:image" content="http://localhost/lib/exe/fetch.php?media=wiki:image.jpg"/>
<meta property="og:description" content="anything else"/>
<meta property="og:any" content="anything else 2"/>
</code>

===== Installation =====

Download the plugin at [[http://www.soft2c.de/pub/htmlmetatagsV2.2.zip|htmlmetatagsV2.2.zip]] and extract the zip file to the plugin folder on your dokuwiki.

===== Further Informtions =====
This section explains some usefully tips for using the plugin. Check your generated html code for the right meta tag and optimize it with this plugin. Crawlers (like google) collect their data by reading the the meta tag and then by the content. Of curse every crawler uses own algorithm for indexing the web pages.

* [[https://wiki.selfhtml.org/wiki/HTML/Kopfdaten/meta| baselines for html meta tags]]
* [[http://ogp.me| description for The Open Graph protocol]]
* [[http://www.suchradar.de/magazin/archiv/2013/4-2013/html-verbesserungen.php|tips for optimization your web page]]

===== Change Log =====

* **2015-05-27** Initial release
* **2016-04-22** Change the date for "last Update"
* **2016-07-12** V2.0 add handling for open graph metatags (Eric Maeker <eric@maeker.fr>)
* **2017-02-08** V2.1 when no metadata is available then set/add this on page (with the given keys via this plugin)
* **2017-04-24** V2.2 make it compatible wit PHP 7

===== FAQ =====

===== Discussion =====
7 changes: 7 additions & 0 deletions plugin.info.txt
@@ -0,0 +1,7 @@
base htmlmetatags
author Heiko Heinz / Eric Maeker
email heiko.heinz@soft2c.de / eric@maeker.fr
date 2017-04-24
name htmlmetatags plugin
desc Set html and open graph meta tags.
url http://www.soft2c.de
126 changes: 126 additions & 0 deletions syntax/syntax.php
@@ -0,0 +1,126 @@
<?php
/**
* DokuWiki Plugin htmlmetatags (Syntax Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Heiko Heinz <heiko.heinz@soft2c.de>
* @author Eric Maeker <eric@maeker.fr>
*/

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_htmlmetatags_syntax extends DokuWiki_Syntax_Plugin {
/**
* @return string Syntax mode type
*/
public function getType() {
return 'substition';
}
/**
* @return string Paragraph type
*/
public function getPType() {
return 'block';
}
/**
* @return int Sort order - Low numbers go before high numbers
*/
public function getSort() {
return 110;
}

/**
* Connect lookup pattern to lexer.
*
* @param string $mode Parser mode
*/
public function connectTo($mode) {
if ($mode == 'base'){
$this->Lexer->addSpecialPattern('{{htmlmetatags>.+?}}',$mode,'plugin_htmlmetatags_syntax');
}
// $this->Lexer->addEntryPattern('<FIXME>',$mode,'plugin_htmlmetatags_syntax');
}

// public function postConnect() {
// $this->Lexer->addExitPattern('</FIXME>','plugin_htmlmetatags_syntax');
// }

/**
* Handle matches of the htmlmetatags syntax
*
* @param string $match The match of the syntax
* @param int $state The state of the handler
* @param int $pos The position in the document
* @param Doku_Handler $handler The handler
* @return array Data for the renderer
*/
public function handle($match, $state, $pos, Doku_Handler $handler){
// Remove all linefeeds before parsing attributes
$match = str_replace(PHP_EOL,"",$match);

// remove the plugin-activator string
$match = str_replace("{{htmlmetatags>","",$match);
$match = str_replace("}}","",$match);

// Explode match into attributes array using 'metatag-' as mask
return explode("metatag-", $match);
}

/**
* Render xhtml output or metadata
*
* usage: {{htmlmetatags>metatag-keywords:(apfel,bananne,birne) metatag-description:(Allgemeiner Obstbauer)}}
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $renderer, $data) {

global $ID;

switch ($mode) {
case 'metadata' :
/*
* e.g.
* data[0]="keywords=(apfel, bananne, birne) "
* data[1]="og:description=Allgemeiner Obstbauer"
*/
for ($i=0;$i<sizeof($data);$i++) {
$mt = explode("=", $data[$i]);
$size = sizeof($mt);

// If attributes as value
if(sizeof($mt)==2){
$name = trim($mt[0]);
$content = trim(preg_replace("/\((.*?)\)\s*$/","\\1",$mt[1]));
// Test if attribute name is a media files and get media file absolute URL
if (substr($name, 0, 6) === 'media-') {
$name = substr($name, 6);
$content = ml($content, '', true, '&amp;', true);
}
// Send result to renderer
if (!empty($content)) {
if ($name == "keywords") {
if (!empty($renderer->meta['htmlmetatags'][$name]))
$renderer->meta["htmlmetatags"][$name] .= ', '.$content;
else
$renderer->meta["htmlmetatags"][$name] .= $content;
}
else
$renderer->meta["htmlmetatags"][$name] .= $content;
}
}
}
return true;
}

return false;
}
}

// vim:ts=4:sw=4:et:

0 comments on commit 7b0f6a6

Please sign in to comment.