1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class syntax_plugin_codify extends DokuWiki_Syntax_Plugin {
6
7  public function getType() {
8    return 'substition';
9  }
10
11  public function getSort() {
12    return 158;
13  }
14
15  public function connectTo($mode) {
16    $this->Lexer->addEntryPattern('<codify.*?>(?=.*?</codify>)', $mode, 'plugin_codify');
17  }
18
19  public function postConnect() {
20    $this->Lexer->addExitPattern('</codify>', 'plugin_codify');
21  }
22
23  public function handle($match, $state, $pos, Doku_Handler $handler) {
24    if ($state == DOKU_LEXER_ENTER) {
25      $attributes = trim(substr($match, 7, -1));
26      $chunk = preg_split("/\s+/", $attributes);
27      $match = $chunk[0];
28    }
29
30    return array($state, $match);
31  }
32
33  public function render($mode, Doku_Renderer $renderer, $data) {
34    if ($mode != 'xhtml') return false;
35
36    list($state, $match) = $data;
37
38    switch ($state) {
39      case DOKU_LEXER_ENTER:
40        $lang = (empty($match)) ? '' : "language-{$match}";
41
42        $renderer->doc .= "<pre class=\"dokuwiki-plugin-codify line-numbers\">";
43        $renderer->doc .= "<code class=\"{$lang}\">";
44        break;
45
46      case DOKU_LEXER_UNMATCHED:
47        $match = ltrim($match);
48        $renderer->doc .= $renderer->_xmlEntities($match);
49        break;
50
51      case DOKU_LEXER_EXIT:
52        $renderer->doc .= "</code>";
53        $renderer->doc .= "</pre>";
54        break;
55    }
56
57    return false;
58  }
59
60}
61