1<?php
2// must be run within DokuWiki
3if(!defined('DOKU_INC')) die();
4
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once DOKU_PLUGIN.'syntax.php';
7
8/**
9 * All DokuWiki plugins to extend the parser/rendering mechanism
10 * need to inherit from this class
11 */
12class syntax_plugin_alphalist extends DokuWiki_Syntax_Plugin {
13
14    function getPType(){
15       return 'block';
16    }
17
18    function getType() { return 'substition'; }
19    function getSort() { return 32; }
20
21
22    function connectTo($mode) {
23	$this->Lexer->addSpecialPattern('\[alphalist.*?\]',$mode,'plugin_alphalist');
24    }
25
26    function handle($match, $state, $pos, Doku_Handler $handler)
27    {
28	global $ID;
29
30	dbglog($match, 'alphalist syntax $match');
31
32	$alphalist =& plugin_load('helper', 'alphalist');
33
34	//remove [alphalist
35	$match = substr($match, 10);
36	//remove ]
37	$match = substr($match, 0, -1);
38
39	$match = trim($match);
40
41	dbglog($match, 'alphalist syntax $match after processing');
42
43	$pages = array();
44
45	// [alphalist] sytax
46	if(empty($match))
47	{
48	    $pages[0][] = '';
49	    $pages[1][] = '';
50	} else
51	{
52
53	    preg_match_all('/([a-zA-Z0-9:_\-]*)(?:\{([^\}]+)\})?/', $match, $matches);
54
55
56	    //remove empty matches
57	    $k=0;
58	    for($i=0;$i<count($matches[0]);$i++)
59	    {
60		if(!empty($matches[0][$i]))
61		{
62		    for($j=1;$j<count($matches);$j++)
63		    {
64			$pages[$j-1][$k] = $matches[$j][$i];
65		    }
66		    $k++;
67		}
68	    }
69	}
70
71	dbglog($pages, 'alphalist syntax $pages');
72
73
74	$list = array();
75	for($i=0;$i<count($pages[0]);$i++)
76	{
77	    if(empty($pages[0][$i]))
78		$page = $ID;
79	    else
80		$page = $pages[0][$i];
81
82	    //Get section
83	    $section = $pages[1][$i];
84
85	    $file = wikiFN($page);
86	    if(file_exists($file))
87	    {
88		$content = file($file);
89		if($section == false)
90		{
91		    foreach($content as $row)
92		    {
93			if(preg_match('/^  (\-|\*)(.*)/', $row, $match))
94			{
95			    $list[$alphalist->plain($match[2])] = $match[2];
96			}
97		    }
98		} else
99		{
100		    //0 - waiting for header 1 - in header
101		    $state = 0;
102		    foreach($content as $row)
103		    {
104			if($state == 0)
105			{
106			    if(strstr($row, $section))
107				$state++;
108			} else
109			{
110			    if(preg_match('/==.*?==/', $row))
111				break;
112
113			    if(preg_match('/^  (\-|\*)(.*)/', $row, $match))
114			    {
115				$list[$alphalist->plain($match[2])] = $match[2];
116			    }
117			}
118		    }
119		}
120	    }
121	}
122	dbglog($pages, 'alphalist syntax $list');
123	return $list;
124    }
125
126    function render($mode, Doku_Renderer $renderer, $data) {
127	setlocale(LC_COLLATE, $this->getConf('locale'));
128        if($mode == 'xhtml') {
129
130	    $alphalist =& plugin_load('helper', 'alphalist');
131
132	    if(count($data) > 0)
133	    {
134		ksort($data, SORT_LOCALE_STRING);
135
136		$list_cont = '';
137
138		$first_letter = '';
139		$letter_change = false;
140
141		foreach($data as $k => $v)
142		{
143		    $f_letter = mb_substr($k, 0, 1);
144		    if($f_letter != $first_letter)
145		    {
146			$letter_change = true;
147			$first_letter = $f_letter;
148		    }
149		    if($letter_change == true)
150		    {
151			$list_cont .= '==='.$first_letter."===\n";
152			$letter_change = false;
153		    }
154		    $list_cont .= '  - '.$v."\n";
155		}
156		$renderer->doc .= $alphalist->parse($list_cont);
157	    }
158
159	    return true;
160        }
161        return false;
162    }
163}
164