<?php
/**
 * Hyphenation Action Plugin
 * Uses phpHyphenation from http://yellowgreen.de/hyphenation-in-web/
 *
 * @license    GPLv3 (http://www.gnu.org/licenses/gpl.html)
 * @link       http://www.dokuwiki.org/plugin:hyphenation
 * @author     Markus Birth <markus@birth-online.de>
 */

if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
include_once(dirname(__FILE__).'/phpHyphenation/phpHyphenation.class.php');

class action_plugin_hyphenation extends DokuWiki_Action_Plugin {

    function getInfo(){
        return confToHash(dirname(__FILE__).'/INFO.txt');
    }

    function register(&$controller) {
        $controller->register_hook('RENDERER_CONTENT_POSTPROCESS', 'BEFORE', $this, '_dohyph');
    }

    function _dohyph(&$event, $param) {
        if ($event->data[0] != 'xhtml') return;   // do nothing if not xhtml (we don't want to hyphenate ODT or PDF)
        $phpHyphPath = dirname(__FILE__).'/phpHyphenation';

        $html = &$event->data[1];
        phpHyphenation::setPatternPath($phpHyphPath . '/patterns/');
        $hyph = new phpHyphenation($this->getConf('language'));
        if ($hyph === false) return;
        $hyph->loadUserDictFromArray(explode("\n", $this->getConf('customhyph')));

        $html = $hyph->doHyphenation($html);
    }

}