Lexer->addEntryPattern('(?=.*)', $mode, 'plugin_txt2tags');
}
function postConnect() {
$this->Lexer->addExitPattern('', 'plugin_txt2tags');
}
function handle($match, $state, $pos, &$handler) {
switch ($state) {
case DOKU_LEXER_ENTER : return array($state, '');
case DOKU_LEXER_UNMATCHED :
// add a config.t2t file for tweaking your installation
$match = "\n\n\n%!includeconf: lib/plugins/txt2tags/config.t2t \n".$match ;
$x = new T2T($match);
$x->enableinclude = 1;
$x->go();
$html = $x->bodyhtml;
return array($state, $html);
case DOKU_LEXER_EXIT : return array($state, '');
}
return array($state,'');
}
function render($mode, &$renderer, $data) {
//dbg('function render($mode, &$renderer, $data)-->'.' mode = '.$mode.' data = '.$data);
//dbg($data);
if ($mode == 'xhtml') {
list($state,$match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER : break;
case DOKU_LEXER_UNMATCHED :
$match = $this->_toc($renderer, $match);
$renderer->doc .= $match;
break;
case DOKU_LEXER_EXIT : break;
}
return true;
}else if ($mode == 'metadata') {
//dbg('function render($mode, &$renderer, $data)-->'.' mode = '.$mode.' data = '.$data);
//dbg($data);
list($state,$match) = $data;
switch ($state) {
case DOKU_LEXER_ENTER : break;
case DOKU_LEXER_UNMATCHED :
if (!$renderer->meta['title']){
$renderer->meta['title'] = $this->_t2t_header($match);
}
/** line below includes TOC in the main page.
You can tweak this in other places in dokuwiki
(in your template, add where you need it), so we disable it below
**/
// $this->_toc($renderer, $match);
$internallinks = $this->_internallinks($match);
#dbg($internallinks);
if (count($internallinks)>0){
foreach($internallinks as $internallink)
{
$renderer->internallink($internallink);
}
}
break;
case DOKU_LEXER_EXIT : break;
}
return true;
} else {
return false;
}
}
function _t2t_header($text)
{
$doc = new DOMDocument('1.0','UTF-8');
//dbg($doc);
$meta = '
';
$doc->loadHTML($meta.$text);
//dbg($doc->saveHTML());
if ($nodes = $doc->getElementsByTagName('h1')){
return $nodes->item(0)->nodeValue;
}
return false;
}
function _internallinks($text)
{
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($text);
$links = array();
if ($nodes = $doc->getElementsByTagName('a')){
foreach($nodes as $atag)
{
$href = $atag->getAttribute('href');
if (!preg_match('/^(https{0,1}:\/\/|ftp:\/\/|mailto:)/i',$href)){
$links[] = $href;
}
}
}
return $links;
}
function _toc(&$renderer, $text)
{
$doc = new DOMDocument('1.0','UTF-8');
//dbg($doc);
$meta = '';
$doc->loadHTML($meta.$text);
if ($nodes = $doc->getElementsByTagName("*")){
foreach($nodes as $node)
{
if (preg_match('/h([1-7])/',$node->tagName,$match))
{
#dbg($node);
$node->setAttribute('class', 'sectionedit'.$match[1]);
$hid = $renderer->_headerToLink($node->nodeValue,'true');
$node->setAttribute('id',$hid);
$renderer->toc_additem($hid, $node->nodeValue, $match[1]);
}
}
}
//remove outer tags of content
$html = $doc->saveHTML();
$html = str_replace('','',$html);
$html = preg_replace('/.+/', '', $html);
$html = str_replace('','', $html);
$html = preg_replace('/.+/', '', $html);
$html = str_replace('','', $html);
return $html;
}
}