MEDIUM: Update plugin pagetitle to v2017-09-08

This commit is contained in:
Bertrand Jacquin 2017-10-15 04:52:26 +01:00
parent 838b4b3e06
commit 2dc3353e32
No known key found for this signature in database
GPG Key ID: 5534871F2E2E93DA
13 changed files with 387 additions and 147 deletions

View File

@ -13,16 +13,130 @@ class action_plugin_pagetitle extends DokuWiki_Action_Plugin {
/**
* register the event handlers
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'deleteObsoletedSingleClass');
function register(Doku_Event_Handler $controller) {
//$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'deleteObsoletedSingleClass');
$controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, '_indexer_version');
$controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, '_indexer_pagetitle');
$controller->register_hook('PARSER_METADATA_RENDER', 'AFTER', $this, '_parser_render');
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_prepare_cache');
}
/**
* Delete syntax.php which is obsoleted since multi-components syntax structure
*/
public function deleteObsoletedSingleClass(Doku_Event $event) {
function deleteObsoletedSingleClass(Doku_Event $event) {
$legacyFile = dirname(__FILE__).'/syntax.php';
if (file_exists($legacyFile)) { unlink($legacyFile); }
}
/**
* INDEXER_VERSION_GET
* Set a version string to the metadata index so that
* the index will be re-created when the version increased
*/
function _indexer_version(Doku_Event $event, $param) {
$event->data['plgin_pagetitle'] = '1.'.$this->getConf('usePersistent');
}
/**
* INDEXER_PAGE_ADD
* Add id of the page to metadata index, relevant pages should be found
* in data/index/plugin_pagetitle_w.idx file
*/
function _indexer_pagetitle(Doku_Event $event, $param) {
$id = p_get_metadata($event->data['page'], 'plugin pagetitle');
if ($id) {
$event->data['metadata']['plugin_pagetitle'] = $id;
}
}
/**
* PAESER_METADATA_RENDER
* Use this event to update/reflesh metadata they may have set elseware.
* The page metadata is passed including both the current and persistent arrays.
*/
function _parser_render(Doku_Event $event, $param) {
global $ID;
/*
* The PageTitle plugin will overwrite "title" metadata of the page
* with "pagetitle" specified in page source. The page must be rendered
* first in xhtml mode to get pagetitle and to stote it on metadata
* storage.
* Each metadata storage (.meta file) may be expired or refleshed by
* DokuWiki or any plugins at elsewhere in any stage. For example,
* metadata will be expired when main config modified. DokuWiki will set
* again title metadata through calls p_get_first_heading() depending on
* $conf['useheading"] setting.
*
* Since page text is not changed, instruction and xhtml cache files are
* used to set title metadata, there is no chance to handle/render page
* source to get pagetitle and to overwite title metadata.
* Therfore, the value of "title" metadata will remain wrong with that
* pagetitle plugin intended.
*
* For the purpose to trigger PageTitle plugin's renderer, we tentatively
* set $ID as "title" to tell DokuWiki caching mechanism so that old
* cache need to be purged and metadata must be rebuild again.
*/
$meta =& $event->data['current'];
$persistent =& $event->data['persistent'];
// check metadata index whether pagetitle had used in the wiki page
$pages = idx_get_indexer()->getPages('plugin_pagetitle');
$pageTitled = in_array($ID, $pages);
if (!$pageTitled) return;
// check whether page has rendered by pagetitle plugin
if (!isset($meta['plugin']['pagetitle'])) {
// tentatively assign full id as page title, just to distinguish
// with normal setting noNS($ID) and to purge .meta file later
$meta['title'] = $ID;
}
// unnecessary persistent metadata should be removed in syntax component,
// however it may be possible to remove it here
if (!$this->getConf('usePersistent')) {
unset($persistent['title']);
}
}
/**
* PARSER_CACHE_USE
* prepare the cache object for default _useCache action
*/
function _prepare_cache(Doku_Event $event, $param) {
$cache =& $event->data;
// we're only interested in wiki pages
if (!isset($cache->page)) return;
// check metadata index whether pagetitle had used in the wiki page
$pages = idx_get_indexer()->getPages('plugin_pagetitle');
$pageTitled = in_array($cache->page, $pages);
if (!$pageTitled) return;
// check title metadata whether cache files should be purged
$title = p_get_metadata($cache->page, 'title', METADATA_DONT_RENDER);
switch ($cache->mode) {
case 'metadata': // metadata cache?
$request = ($title == $cache->page) ? true : false;
break;
case 'i': // instruction cache
$request = ($title == $cache->page) ? true : false;
break;
case 'xhtml': // xhtml cache
$request = ($title == $cache->page) ? true : false;
break;
}
// request purge if necessary
$cache->depends['purge'] = $request;
}
}

View File

@ -0,0 +1,7 @@
<?php
/**
* DokuWiki plugin PageTitle
* @author Satoshi Sahara <sahara.satoshi@gmail.com>
*/
$conf['usePersistent'] = 0; // false

View File

@ -0,0 +1,7 @@
<?php
/**
* DokuWiki plugin PageTitle
* @author Satoshi Sahara <sahara.satoshi@gmail.com>
*/
$meta['usePersistent'] = array('onoff');

View File

@ -1,4 +1,4 @@
# deleted.files : Page Title plugin for DokuWiki
# deleted.files : PageTitle plugin for DokuWiki
#
# This file contains a list of files that were present in previous
# plugin releases but were removed later.
@ -6,4 +6,7 @@
# Since 2015-09-17
syntax.php
# Since 2017-07-11
syntax/metaonly.php
# action.php is used again since 2017-08-27

View File

@ -31,32 +31,32 @@ class helper_plugin_pagetitle extends DokuWiki_Plugin {
function html_youarehere($start_depth = 0) {
global $conf, $ID;
if ($ID == $conf['start']) {
$page = '';
} elseif (noNS($ID) == $conf['start']) {
$page = ':'.getNS($ID); // drop tailing start
} else {
$page = ':'.$ID;
}
//prepend virtual root namespace to $ID that is not start page
$nodes = ($ID == $conf['start']) ? array('') : explode(':', ':'.$ID);
$depth = count($nodes);
$items = array();
$parts = explode(':', $page);
$depth = count($parts) -1;
for ($i = $start_depth; $i < $depth; $i++) {
$id = $id0 = implode(':', array_slice($nodes, 0, $i+1));
$out = '';
$ns = '';
for ($i = $start_depth; $i < count($parts); $i++) {
$ns.= $parts[$i];
$id = $ns ?: $conf['start'];
resolve_pageid('', $id, $exists);
if (!$exists) {
$id = $ns.':';
if (empty($id)) { // root start page
$id = $conf['start'];
} elseif ($id == ':'.$conf['start']) { // start namespace
$id = $conf['start'].':'.$conf['start'];
resolve_pageid('', $id, $exists);
$name = $conf['start'];
} else {
resolve_pageid('', $id, $exists);
if (!$exists) {
$id = $id.':';
resolve_pageid('', $id, $exists);
}
$name = p_get_metadata($id, 'shorttitle', METADATA_DONT_RENDER) ?: noNS($id0);
}
$name = p_get_metadata($id, 'shorttitle') ?: $parts[$i];
$out.= '<bdi>'.$this->html_pagelink($id, $name, $exists).'</bdi>';
if ($i < $depth) $out.= ' &#x00A0;'; // separator
$ns.= ':';
$items[] = '<bdi>'.$this->html_pagelink($id, $name, $exists).'</bdi>';
}
// join items with a separator
$out = implode(' &#x00A0;', $items);
return $out;
}

View File

@ -0,0 +1,41 @@
<?php
/**
* DokuWiki plugin PageTitle; Helper component counter
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Satoshi Sahara <sahara.satoshi@gmail.com>
*/
if (!defined('DOKU_INC')) die();
class helper_plugin_pagetitle_counter extends DokuWiki_Plugin {
/**
* tally counter utility - incrementally counts items
*
* @param string|integer $item item that to be counted
* @return closure function
*
* Note: assign this closure function to variables to use counter
* $obj = $this->loadHelper('pagetitle_counter');
* $counter = $obj->create_counter($item);
* echo $counter($something);
*
* if a closure function assigned to class properties, make sure that
* PHP parses "$MyClass->counter" as fetch the property named "counter".
*
* $MyClass->counter = $obj->create_counter($item);
* echo ($MyClass->counter)($something); // PHP >= 7.0
*
* or call it using call_user_func:
* echo call_user_func($MyClass->counter, $something);
*
*/
function create_counter($item='') {
$counter = array();
return function ($item='') use (&$counter) {
return @$counter[$item]++ ?: 0; // restrain notice of Undefined index
};
}
}

View File

@ -0,0 +1,10 @@
<?php
/**
* DokuWiki plugin PageTitle
* English language file for settings
*
* @author Satoshi Sahara <sahara.satoshi@gmail.com>
*/
$lang['usePersistent'] = 'store pagetitle (= title) on persistent metadata storage';

View File

@ -1,2 +0,0 @@
downloadurl=https://github.com/ssahara/dw-plugin-pagetitle/zipball/master
installed=Mon, 05 Sep 2016 13:14:32 +0000

View File

@ -1,7 +1,7 @@
base pagetitle
name Page Title plugin
name PageTitle plugin
author Satoshi Sahara
email sahara.satoshi@gmail.com
date 2016-09-02
date 2017-09-08
desc Define a title of the wiki page by <title> tag. This plugin allows to write decorative title on the page with setting plain title text in metadata storage.
url https://www.dokuwiki.org/plugin:pagetitle

View File

@ -1,6 +1,6 @@
<?php
/**
* DokuWiki plugin PageTitle Breadcrums; Syntax component
* DokuWiki plugin PageTitle Breadcrumb; Syntax component
* Macro to set the short title of the page in metadata
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
@ -11,38 +11,65 @@ if (!defined('DOKU_INC')) die();
class syntax_plugin_pagetitle_breadcrumb extends DokuWiki_Syntax_Plugin {
protected $special_pattern = '~~ShortTitle:.*?~~';
protected $check = array(); // ensure first matched pattern only effective
protected $mode;
protected $pattern = array();
private $counter = null; // helper component "pagetitle_counter"
private $handledOnce = null; // counter used in handle()
function __construct() {
$this->mode = substr(get_class($this), 7); // drop 'syntax_' from class name
//syntax patterns
$this->pattern[5] = '~~ShortTitle:.*?~~';
// assign helper component to dedicated class property
$this->counter = $this->loadHelper('pagetitle_counter', true);
}
public function getType() { return 'substition'; }
public function getPType(){ return 'normal'; }
public function getSort() { return 990; }
function getType() { return 'substition'; }
function getPType(){ return 'normal'; }
function getSort() { return 990; }
public function connectTo($mode) {
$this->Lexer->addSpecialPattern($this->special_pattern, $mode, $this->mode);
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
}
public function handle($match, $state, $pos, Doku_Handler $handler) {
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
global $ID;
if ($this->check[$ID]++) {
return false; // ignore match after once handled
// assign a closure function to the class property
if ($this->handledOnce === null) {
$this->handledOnce = $this->counter->create_counter($item);
}
list($key, $value) = explode(':', substr($match, 2, -2), 2);
$short_title = trim($value);
// ensure first matched pattern only effective
//if (($this->handledOnce)($ID) > 0) return false; // since PHP 7
//if (call_user_func($this->handledOnce, $ID) > 0) return false; // PHP 7 & 5
$counter = $this->handledOnce; // assign class property to a local variable
if ($counter($ID) > 0) return false;
// get short title
$short_title = trim(substr($match, 13, -2));
return array($state, $short_title, $ID);
}
public function render($format, Doku_Renderer $renderer, $data) {
/**
* Create output
*/
function render($format, Doku_Renderer $renderer, $data) {
global $ID;
list($state, $short_title, $id) = $data;
// skip calls that belong to different pages (eg. title of included page)
if (strcmp($id, $ID) !== 0) return false;
switch ($format) {

View File

@ -10,7 +10,7 @@
* itlic, subscript and superscript, but title metadata remains simple
* plain text without any markup.
* example
* wiki source: <title> H<sub>s</sub>O </title>
* wiki source: <title> H<sub>2</sub>O </title>
* page (html): <h1 class="pagetitle">H<sub>2</sub>O</h1>
* title metadata: H2O
*/
@ -19,56 +19,77 @@ if (!defined('DOKU_INC')) die();
class syntax_plugin_pagetitle_decorative extends DokuWiki_Syntax_Plugin {
protected $entry_pattern = '<title\b[^>\r\n]*?>(?=.*?</title>)';
protected $exit_pattern = '</title>';
protected $mode;
protected $pattern = array();
protected $mode, $name;
protected $store, $capture;
private $counter = null; // helper component "pagetitle_counter"
private $renderedOnce = null; // counter used in render()
protected $doc, $capture; // store properties of $renderer
protected $params; // store title tag parameters
protected $check = array(); // ensure first title only effective, used in render()
function __construct() {
$this->mode = substr(get_class($this), 7); // drop 'syntax_' from class name
$this->name = substr(get_class($this), 14);
// syntax patterns
$this->pattern[1] = '<title\b[^>\r\n]*?>(?=.*?</title>)'; // entry
$this->pattern[4] = '</title>'; // exit
$this->pattern[5] = '~~Title:[^\r\n]*?~~'; // special
// assign helper component to dedicated class property
$this->counter = $this->loadHelper('pagetitle_counter', true);
}
function getType() { return 'baseonly';}
function getPType() { return 'block';}
function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
function getSort() { return 49; }
function getAllowedTypes() {
return array('formatting', 'substition', 'disabled');
}
// Connect pattern to lexer
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addEntryPattern($this->entry_pattern, $mode, $this->mode);
$this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
$this->Lexer->addEntryPattern($this->pattern[1], $mode, $this->mode);
}
function postConnect() {
$this->Lexer->addExitPattern($this->exit_pattern, $this->mode);
$this->Lexer->addExitPattern($this->pattern[4], $this->mode);
}
/*
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
global $ID;
if ($this->check[$ID] > 0) {
return false; // ignore match after once handled
}
$plugin = substr(get_class($this), 14);
switch ($state) {
case DOKU_LEXER_SPECIAL : // ~~Title:*~~ macro syntax
$title = trim(substr($match, 8, -2));
return array($state, $ID, $title);
case DOKU_LEXER_ENTER :
$params = strtolower(trim(substr($match, strpos($match,' '), -1)));
$data = array($state, $ID, $params);
$handler->addPluginCall($this->name, $data, $state,$pos,$match);
// store title tag parameters
if (($n = strpos($match, ' ')) !== false) {
$this->params = strtolower(trim(substr($match, $n, -1)));
} else {
$this->params = '';
}
$data = array($state, $ID, '');
$handler->addPluginCall($plugin, $data, $state,$pos,$match);
return false;
case DOKU_LEXER_UNMATCHED :
$handler->_addCall('cdata', array($match), $pos);
return false;
case DOKU_LEXER_EXIT :
$data = array($state, $ID, '');
$handler->addPluginCall($this->name, $data, $state,$pos,$match);
$this->check[$ID]++;
$data = array($state, $ID, $this->params);
$handler->addPluginCall($plugin, $data, $state,$pos,$match);
return false;
}
return false;
@ -80,13 +101,18 @@ class syntax_plugin_pagetitle_decorative extends DokuWiki_Syntax_Plugin {
function render($format, Doku_Renderer $renderer, $data) {
global $ID;
list($state, $id, $params) = $data;
list($state, $id, $param) = $data;
switch ($state) {
case DOKU_LEXER_SPECIAL : // ~~Title:*~~ macro syntax
// $decorative_title = $param;
// convert to curly quote characters depending on $conf['typography']
$decorative_title = $this->render_text($param);
break;
case DOKU_LEXER_ENTER :
// store title tag parameters
$this->params = $params;
// preserve variables
$this->store = $renderer->doc;
$this->doc = $renderer->doc;
$this->capture = $renderer->capture;
// set doc blank to store parsed "UNMATHCED" content
@ -101,44 +127,69 @@ class syntax_plugin_pagetitle_decorative extends DokuWiki_Syntax_Plugin {
$decorative_title = trim($renderer->doc);
// restore variable
$renderer->doc = $this->store;
$renderer->doc = $this->doc;
if ($format == 'metadata') $renderer->capture = $this->capture;
break; // do not return here
default:
return false; // this should never happen
}
// follow up only for DOKU_LEXER_EXIT
// skip calls that belong to different pages (eg. title of included page)
if (strcmp($id, $ID) !== 0) return false;
// assign a closure function to the class property
if ($this->renderedOnce === null) {
$this->renderedOnce = $this->counter->create_counter($item);
}
// ensure first instruction only effective
//if (($this->renderedOnce)($format) > 0) return false; // since PHP 7
//if (call_user_func($this->renderedOnce, $format) > 0) return false; // PHP 7 & 5
$counter = $this->renderedOnce; // assign property to a local variable
if ($counter($format) > 0) return false;
// get plain title
$title = htmlspecialchars_decode(strip_tags($decorative_title), ENT_QUOTES);
$title = trim(htmlspecialchars_decode(strip_tags($decorative_title), ENT_QUOTES));
if (empty($title)) return false;
// output title
$method = '_' . $format . '_render';
if (method_exists($this, $method)) {
return $this->$method($decorative_title, $title, $renderer);
switch ($format) {
case 'xhtml':
if ($state == DOKU_LEXER_SPECIAL) return false;
if (($wrap = $this->loadHelper('wrap')) != NULL) {
$attr = $wrap->buildAttributes($param, 'pagetitle');
} else {
$attr = ' class="pagetitle"';
}
// even title in <h1>, it never shown up in the table of contents (TOC)
$renderer->doc .= '<h1'.$attr.'>';
$renderer->doc .= $decorative_title;
$renderer->doc .= '</h1>'.DOKU_LF;
return true;
case 'text':
$renderer->doc .= $title . DOKU_LF;
return true;
case 'metadata':
// set metadata for metadata indexer
$renderer->meta['plugin']['pagetitle'] = $ID;
if ($this->getConf('usePersistent')) {
// metadata persistence
$renderer->persistent['title'] = $title;
$renderer->meta['title'] = $title;
} else {
// erase persistent title metadata if defined
unset($renderer->persistent['title']);
$renderer->meta['title'] = $title;
}
return true;
}
else return false;
}
/**
* Revised procedures for renderers
*/
protected function _xhtml_render($decorative_title, $title, $renderer) {
if (($wrap = $this->loadHelper('wrap')) != NULL) {
$attr = $wrap->buildAttributes($this->params, 'pagetitle');
} else $attr = ' class="pagetitle"';
// even title in <h1>, it never shown up in the table of contents (TOC)
$renderer->doc .= '<h1'.$attr.'>';
$renderer->doc .= $decorative_title;
$renderer->doc .= '</h1>'.DOKU_LF;
return true;
}
protected function _metadata_render($decorative_title, $title, $renderer) {
$renderer->meta['title'] = $title;
return true;
return false;
}
}

View File

@ -1,38 +0,0 @@
<?php
/**
* DokuWiki plugin PageTitle metaonly; Syntax component
* Macro to set the itle of the page in metadata
* deteriorated decorative syntax component
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Sahara Satoshi <sahara.satoshi@gmail.com>
*
*/
require_once(dirname(__FILE__).'/decorative.php');
class syntax_plugin_pagetitle_metaonly extends syntax_plugin_pagetitle_decorative {
protected $entry_pattern = '~~Title:(?=.*?~~)';
protected $exit_pattern = '~~';
function getType() { return 'baseonly';}
function getPType() { return 'normal';}
function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); }
function getSort() { return 49; }
/**
* Revised procedures for renderers
*/
protected function _xhtml_render($decorative_title, $title, $renderer) {
return true;
}
protected function _metadata_render($decorative_title, $title, $renderer) {
$renderer->meta['title'] = $title;
return true;
}
}

View File

@ -11,34 +11,54 @@ if (!defined('DOKU_INC')) die();
class syntax_plugin_pagetitle_youarehere extends DokuWiki_Syntax_Plugin {
protected $special_pattern = '<!-- ?YOU_ARE_HERE ?-->';
protected $mode;
protected $pattern =array();
function __construct() {
$this->mode = substr(get_class($this), 7); // drop 'syntax_' from class name
//syntax patterns
$this->pattern[5] = '<!-- ?YOU_ARE_HERE ?-->';
}
public function getType() { return 'substition'; }
public function getPType(){ return 'block'; }
public function getSort() { return 990; }
function getType() { return 'substition'; }
function getPType(){ return 'block'; }
function getSort() { return 990; }
public function connectTo($mode) {
$this->Lexer->addSpecialPattern($this->special_pattern, $mode, $this->mode);
/**
* Connect pattern to lexer
*/
function connectTo($mode) {
$this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
}
public function handle($match, $state, $pos, Doku_Handler $handler) {
return array($state, $match);
/**
* Handle the match
*/
function handle($match, $state, $pos, Doku_Handler $handler) {
global $ID;
return array($state, $match, $ID);
}
public function render($format, Doku_Renderer $renderer, $data) {
/**
* Create output
*/
function render($format, Doku_Renderer $renderer, $data) {
global $ID;
list($state, $match) = $data;
$template = plugin_load('helper','pagetitle');
list($state, $match, $id) = $data;
$renderer->doc .= DOKU_LF.$match.DOKU_LF; // html comment
$renderer->doc .= '<div class="youarehere">';
$renderer->doc .= $template->html_youarehere(1); // start_depth = 1
$renderer->doc .= '</div>'.DOKU_LF;
// skip calls that belong to different pages (eg. title of included page)
if (strcmp($id, $ID) !== 0) return false;
$template = $this->loadHelper('pagetitle');
if ($format == 'xhtml') {
$renderer->doc .= DOKU_LF.$match.DOKU_LF; // html comment
$renderer->doc .= '<div class="youarehere">';
$renderer->doc .= $template->html_youarehere(1); // start_depth = 1
$renderer->doc .= '</div>'.DOKU_LF;
}
return true;
}