1<?php
2/**
3 * action plugin for simplified XHTML Ruby notation
4 * (full page post processing required due to ruby in headers and ToC)
5 *
6 * @license	GPLv3 (http://www.gnu.org/licenses/gpl.html)
7 * @link	   http://www.dokuwiki.org/plugin:xhtmlruby
8 * @author	 Mike "Pomax" Kamermans <pomax@nihongoresources.com>
9 */
10
11if(!defined('DOKU_INC')) die();
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'action.php');
14
15class action_plugin_xhtmlruby extends DokuWiki_Action_Plugin {
16
17	var $version = '2009-10-26';
18
19	// configurable options
20	var $parse_wiki_text = false;
21	var $parse_toc_text = false;
22
23	// for now, we operate on Japanese only - TODO: bopomofo and hangul rubification
24	var $re_kanji = "[\x{4E00}-\x{9FFF}\x{3005}\x{30F6}]+";
25	var $re_kana = "[\x{3040}-\x{30FF}]*";
26
27	// s/// patterns
28	var $re_search = "";
29	var $re_replace = "";
30
31	function getInfo() {
32	  return array(
33		'author' => 'Mike "Pomax" Kamermans',
34		'email'  => 'pomax@nihongoresources.com',
35		'date'   => $this->version,
36		'name'   => 'xhtmlruby',
37		'desc'   => 'Converts Japanese 漢字(ふり) into xhtml 1.1 <ruby><rb>漢字</rb><rp>(</rp><rt>ふり</rt><rp>)</rp></ruby> markup',
38		'url'	=> 'n/a');
39	}
40
41	/**
42	 * Postprocesses the html that was built from that, to rubify kanji that have associated furigana.
43	 */
44	function register(&$controller)
45	{
46		// initialise variables
47		$this->re_search = "/(".$this->re_kanji.")\((".$this->re_kana.")\)/u";
48		$this->re_replace = "<ruby><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby>";
49
50		// initialise ini variables
51		$inivars = parse_ini_file(DOKU_INC.'lib/plugins/xhtmlruby/conf.ini');
52		if($inivars['parse_toc_text']==true) { $this->parse_toc_text = true; }
53		if($inivars['parse_wiki_text']==true) { $this->parse_wiki_text = true; }
54
55		// uses a custom hook that needs to be added in html.php, see documentation
56		if($this->parse_toc_text===true) {
57			$controller->register_hook('HTML_TOC_ITEM', 'AFTER', $this, '_rubify_tocitem'); }
58
59		if($this->parse_wiki_text===true) {
60			$controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'AFTER', $this, '_rubify'); }
61	}
62
63	/**
64	 * rubify for ToC items
65	 */
66	function _rubify_tocitem(&$event, $param)
67	{
68		$item = &$event->data;
69		$item = preg_replace($this->re_search,$this->re_replace,$item);
70	}
71
72	/**
73	 * rubify for wiki text
74	 */
75	function _rubify(&$event, $param)
76	{
77		// reference to data and associated data type
78		$data = &$event->data[1];
79		$datatype = &$event->data[0];
80
81		// do nothing if the data is not not xhtml (this only generates xhtml ruby markup)
82		if ($datatype != 'xhtml')  { return; }
83
84		// and finally, perform the postprocessing 'en place'
85		$data = preg_replace($this->re_search,$this->re_replace,$data);
86	}
87}
88?>
89