1<?php
2/**
3 * Abstract plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  peterfromearth <coder@peterfromearth.de>
7 */
8
9class syntax_plugin_abstract extends DokuWiki_Syntax_Plugin {
10
11    function getType() { return 'container'; }
12    function getPType() { return 'normal'; }
13    function getSort() { return 200; }
14	function getAllowedTypes() {
15      return array('container', 'formatting', 'substition', 'disabled','protected');
16   }
17
18
19	function connectTo($mode) {
20        $this->Lexer->addEntryPattern('<abstract[^>]*>(?=.*</abstract>)',$mode,'plugin_abstract');
21    }
22
23    function postConnect() {
24        $this->Lexer->addExitPattern('</abstract>','plugin_abstract');
25    }
26
27
28    /**
29     * Handle the match
30     */
31    function handle($match, $state, $pos, Doku_Handler $handler){
32        switch ($state) {
33            case DOKU_LEXER_ENTER :
34				$flags = substr($match, 9, -1);
35				$flags = $this->parseFlags($flags);
36				return [$state,$flags];
37				break;
38            case DOKU_LEXER_UNMATCHED :
39                return [$state, $match];
40				break;
41            case DOKU_LEXER_EXIT :
42                return [$state, ''];
43				break;
44        }
45        return array();
46    }
47
48    function render($mode, Doku_Renderer $renderer, $data) {
49        if ($mode == 'metadata') {
50            list($state, $match) = $data;
51            switch ($state) {
52                case DOKU_LEXER_ENTER :
53					//reset the output
54					$renderer->doc = '';
55					break;
56                case DOKU_LEXER_UNMATCHED :
57                    $renderer->doc .= hsc($match);
58                    break;
59                case DOKU_LEXER_EXIT :
60					// cut off too long abstracts
61					$renderer->doc = trim($renderer->doc);
62					if (strlen($renderer->doc) > 500)
63						$renderer->doc = utf8_substr($renderer->doc, 0, 500).'…';
64					$renderer->meta['description']['abstract'] = $renderer->doc;
65
66					//$renderer->doc .= '#####ABSTRACEND#####';
67                    break;
68            }
69			return true;
70        } else if($mode == 'xhtml'){
71           list($state, $match) = $data;
72		   switch ($state) {
73                case DOKU_LEXER_ENTER :
74					if($match['hide'])
75						$renderer->doc .= '<span style="display:none">';
76					else
77						$renderer->doc .= '<span>';
78					break;
79                case DOKU_LEXER_UNMATCHED :
80                    $renderer->doc .= hsc($match);
81                    break;
82                case DOKU_LEXER_EXIT :
83					$renderer->doc .= '</span>';
84                    break;
85            }
86
87        }
88        return false;
89    }
90
91	/*
92	 * parseFlags checks for tagfilter flags and returns them as true/false
93	 * @param $flags array
94	 * @return array tagfilter flags
95	 */
96	function parseFlags($flags){
97		$flags = explode('&',trim($flags));
98		$conf = [
99			'hide' => false,
100		];
101
102		foreach($flags as $k=>$flag) {
103			list($flag,$value) = explode('=',$flag,2);
104			switch($flag) {
105				case 'hide':
106					$conf['hide'] = true;
107					break;
108			}
109		}
110
111		return $conf;
112	}
113
114}
115