Added ~~Recipe~~ macro (thanks to @coastgnu on #1 issue) and several improvements

This commit is contained in:
Giuseppe Di Terlizzi 2015-10-30 16:25:39 +01:00
parent 95f02e1bff
commit 77f2311ffe
4 changed files with 206 additions and 199 deletions

View file

@ -3,7 +3,7 @@
* Semantic Action Plugin * Semantic Action Plugin
* *
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html) * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Giuseppe Di Terlizzi <giuseppe.diterlizzi> * @author Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
* @copyright (C) 2015, Giuseppe Di Terlizzi * @copyright (C) 2015, Giuseppe Di Terlizzi
*/ */
@ -17,201 +17,201 @@ if(!defined('DOKU_INC')) die();
*/ */
class action_plugin_semantic extends DokuWiki_Action_Plugin { class action_plugin_semantic extends DokuWiki_Action_Plugin {
/** /**
* Register events * Register events
* *
* @param Doku_Event_Handler $controller * @param Doku_Event_Handler $controller
*/ */
public function register(Doku_Event_Handler $controller) { public function register(Doku_Event_Handler $controller) {
if ($this->getConf('useJSONLD')) { if ($this->getConf('useJSONLD')) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'json_ld'); $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'json_ld');
}
if ($this->getConf('useMetaDescription')) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
}
if ($this->getConf('useMetaAuthor')) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
}
}
/**
* JSON-LD Event handler
*
* @param Doku_Event &$event
*/
public function json_ld(Doku_Event &$event, $param) {
global $INFO;
global $ID;
if ((bool) preg_match('/'.$this->getConf('excludedPages').'/', $ID)) {
return false;
}
if (! $INFO['perm']) {
return false;
}
global $license;
global $auth;
global $conf;
$meta = $INFO['meta'];
if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) {
return false;
}
if (isset($meta['date']) && $meta['date'] !== '') {
$type = ((isset($meta['semantic']['schema.org']['type']))
? $meta['semantic']['schema.org']['type']
: $this->getConf('defaultSchemaOrgType'));
$user_data = $auth->getUserData($meta['user']);
$license_url = @$license[$conf['license']]['url'];
$page_url = wl($ID, '', true);
$image_url = (($meta['relation']['firstimage']) ? ml($meta['relation']['firstimage'], '', true, '&amp;', true) : null);
$description = trim(ltrim($meta['description']['abstract'], @$meta['title']));
$created = date(DATE_W3C, $meta['date']['created']);
$modified = date(DATE_W3C, $meta['date']['modified']);
$json_ld = array(
'@context' => 'http://schema.org',
'@type' => $type,
'headline' => @$meta['title'],
'name' => @$meta['title'],
'image' => array($image_url),
'datePublished' => $created,
'dateCreated' => $created,
'dateModified' => $modified,
'description' => $description,
'license' => $license_url,
'url' => $page_url,
);
if (isset($meta['creator']) && $meta['creator'] !== '') {
$json_ld['creator'] = array(
'@context' => 'http://schema.org',
'@type' => 'Person',
'name' => $meta['creator'],
'email' => $user_data['mail']
);
if (isset($meta['contributor'])) {
foreach ($meta['contributor'] as $uid => $fullname) {
$contributor_data = $auth->getUserData($uid);
$json_ld['contributor'][] = array(
'@context' => 'http://schema.org',
'@type' => 'Person',
'name' => $fullname,
'email' => $contributor_data['mail']
);
}
} }
if ($this->getConf('useMetaDescription')) { }
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_description');
if (isset($meta['relation']['references'])) {
$json_ld_webpage = array(
'@context' => 'http://schema.org',
'@type' => 'WebPage'
);
foreach ($meta['relation']['references'] as $page => $status) {
if ($status) {
$json_ld_webpage['relatedLink'][] = wl($page, '', true);
}
} }
if ($this->getConf('useMetaAuthor')) { if (isset($json_ld_webpage['relatedLink'])) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'meta_author');
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($json_ld_webpage),
);
} }
}
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($json_ld),
);
} }
/** }
* JSON-LD Event handler
*
* @param Doku_Event &$event
*/
public function json_ld(Doku_Event &$event, $param) {
global $INFO;
global $ID;
if ((bool) preg_match_all('/'.$this->getConf('excludedPages').'/', $ID)) { public function meta_description(Doku_Event &$event, $params) {
return false;
}
if ($INFO['perm'] > 0) { global $INFO;
global $ID;
global $license; if ((bool) preg_match_all('/'.$this->getConf('excludedPages').'/', $ID)) {
global $auth; return false;
global $conf; }
$meta = $INFO['meta']; if (! $INFO['perm']) {
return false;
}
if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) { $meta = $INFO['meta'];
return false;
}
if (isset($meta['date']) && $meta['date'] !== '') { if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) {
return false;
}
$type = ((isset($meta['semantic']['schema.org']['type'])) if (isset($meta['date']) && $meta['date'] !== '') {
? $meta['semantic']['schema.org']['type']
: $this->getConf('defaultSchemaOrgType'));
$user_data = $auth->getUserData($meta['user']);
$license_url = $license[$conf['license']]['url'];
$page_url = wl($ID, '', true);
$image_url = (($meta['relation']['firstimage']) ? ml($meta['relation']['firstimage'], '', true, '&amp;', true) : null);
$description = trim(ltrim($meta['description']['abstract'], $meta['title']));
$created = date(DATE_W3C, $meta['date']['created']);
$modified = date(DATE_W3C, $meta['date']['modified']);
$json_ld = array( $description = str_replace("\n", ' ', trim(ltrim($meta['description']['abstract'], @$meta['title'])));
'@context' => 'http://schema.org',
'@type' => $type,
'headline' => $meta['title'],
'name' => $meta['title'],
'image' => array($image_url),
'datePublished' => $created,
'dateCreated' => $created,
'dateModified' => $modified,
'description' => $description,
'license' => $license_url,
'url' => $page_url,
);
if (isset($meta['creator']) && $meta['creator'] !== '') { $event->data['meta'][] = array(
$json_ld['creator'] = array( 'name' => 'description',
'@context' => 'http://schema.org', 'content' => $description,
'@type' => 'Person', );
'name' => $meta['creator'],
'email' => $user_data['mail']
);
if (isset($meta['contributor'])) {
foreach ($meta['contributor'] as $uid => $fullname) {
$contributor_data = $auth->getUserData($uid);
$json_ld['contributor'][] = array(
'@context' => 'http://schema.org',
'@type' => 'Person',
'name' => $fullname,
'email' => $contributor_data['mail']
);
}
}
}
if (isset($meta['relation']['references'])) {
$json_ld_webpage = array(
'@context' => 'http://schema.org',
'@type' => 'WebPage'
);
foreach ($meta['relation']['references'] as $page => $status) {
if ($status) {
$json_ld_webpage['relatedLink'][] = wl($page, '', true);
}
}
if (isset($json_ld_webpage['relatedLink'])) {
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($json_ld_webpage),
);
}
}
$event->data["script"][] = array (
"type" => "application/ld+json",
"_data" => json_encode($json_ld),
);
}
}
} }
}
public function meta_description(Doku_Event &$event, $params) {
global $INFO; public function meta_author(Doku_Event &$event, $params) {
global $ID;
if ((bool) preg_match_all('/'.$this->getConf('excludedPages').'/', $ID)) { global $INFO;
return false; global $ID;
}
if ($INFO['perm'] > 0) {
$meta = $INFO['meta'];
if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) {
return false;
}
if (isset($meta['date']) && $meta['date'] !== '') {
$description = str_replace("\n", ' ', trim(ltrim($meta['description']['abstract'], $meta['title'])));
$event->data['meta'][] = array(
'name' => 'description',
'content' => $description,
);
}
}
if ((bool) preg_match_all('/'.$this->getConf('excludedPages').'/', $ID)) {
return false;
} }
if (! $INFO['perm']) {
public function meta_author(Doku_Event &$event, $params) { return false;
global $INFO;
global $ID;
if ((bool) preg_match_all('/'.$this->getConf('excludedPages').'/', $ID)) {
return false;
}
if ($INFO['perm'] > 0) {
$meta = $INFO['meta'];
if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) {
return false;
}
if ((isset($meta['date']) && $meta['date'] !== '')) {
$meta = $INFO['meta'];
$event->data['meta'][] = array(
'name' => 'author',
'content' => $meta['creator'],
);
}
}
} }
$meta = $INFO['meta'];
if (isset($meta['semantic']['enabled']) && ! $meta['semantic']['enabled']) {
return false;
}
if ((isset($meta['date']) && $meta['date'] !== '')) {
$meta = $INFO['meta'];
$event->data['meta'][] = array(
'name' => 'author',
'content' => $meta['creator'],
);
}
}
} }

View file

@ -8,6 +8,5 @@
$meta['useMetaDescription'] = array('onoff'); $meta['useMetaDescription'] = array('onoff');
$meta['useMetaAuthor'] = array('onoff'); $meta['useMetaAuthor'] = array('onoff');
$meta['useJSONLD'] = array('onoff'); $meta['useJSONLD'] = array('onoff');
$meta['defaultSchemaOrgType'] = array('multichoice','_choices' => array('Article', 'NewsArticle', 'TechArticle', 'BlogPosting')); $meta['defaultSchemaOrgType'] = array('multichoice','_choices' => array('Article', 'NewsArticle', 'TechArticle', 'BlogPosting', 'Recipe'));
$meta['excludedPages'] = array('regex'); $meta['excludedPages'] = array('regex');

View file

@ -1,7 +1,7 @@
base semantic base semantic
author Giuseppe Di Terlizzi author Giuseppe Di Terlizzi
email giuseppe.diterlizzi@gmail.com email giuseppe.diterlizzi@gmail.com
date 2015-03-30 date 2015-10-30
name Semantic Plugin name Semantic Plugin
desc Add Semantic Data in Dokuwiki desc Add Semantic Data in DokuWiki
url http://www.dokuwiki.org/plugin:semantic url http://www.dokuwiki.org/plugin:semantic

View file

@ -12,37 +12,45 @@ if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin { class syntax_plugin_semantic extends DokuWiki_Syntax_Plugin {
function getType() { return 'substition'; } private $macros = array(
function getSort() { return 99; } '~~NewsArticle~~', '~~Article~~', '~~TechArticle~~',
'~~BlogPosting~~', '~~Recipe~~', '~~NOSEMANTIC~~'
);
function connectTo($mode) { function getType() { return 'substition'; }
$this->Lexer->addSpecialPattern('~~NewsArticle~~', $mode, 'plugin_semantic'); function getSort() { return 99; }
$this->Lexer->addSpecialPattern('~~Article~~', $mode, 'plugin_semantic');
$this->Lexer->addSpecialPattern('~~TechArticle~~', $mode, 'plugin_semantic'); function connectTo($mode) {
$this->Lexer->addSpecialPattern('~~BlogPosting~~', $mode, 'plugin_semantic');
$this->Lexer->addSpecialPattern('~~NOSEMANTIC~~', $mode, 'plugin_semantic'); foreach ($this->macros as $macro) {
$this->Lexer->addSpecialPattern($macro, $mode, 'plugin_semantic');
} }
function handle($match, $state, $pos, Doku_Handler $handler) { }
return array($match, $state, $pos);
function handle($match, $state, $pos, Doku_Handler $handler) {
return array($match, $state, $pos);
}
function render($mode, Doku_Renderer $renderer, $data) {
if ($mode == 'xthml') {
return true; // don't output anything
} elseif ($mode == 'metadata') {
list($match, $state, $pos) = $data;
if ($match == '~~NOSEMANTIC~~') {
$renderer->meta['semantic']['enabled'] = false;
} else {
$renderer->meta['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~');
$renderer->meta['semantic']['enabled'] = true;
}
} }
function render($mode, Doku_Renderer $renderer, $data) { }
if ($mode == 'xthml') {
return true; // don't output anything
} elseif ($mode == 'metadata') {
list($match, $state, $pos) = $data;
if ($match == '~~NOSEMANTIC~~') {
$renderer->meta['semantic']['enabled'] = false;
} else {
$renderer->meta['semantic']['schema.org']['type'] = trim(str_replace('Schema.org/', '', $match), '~~');
$renderer->meta['semantic']['enabled'] = true;
}
}
}
} }