dokufreaks / plugin-pagelist

plugin-pagelist / syntax.php
100644 112 lines (98 sloc) 3.673 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Pagelist Plugin: lists pages
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Esther Brunner <wikidesign@gmail.com>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
 
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
class syntax_plugin_pagelist extends DokuWiki_Syntax_Plugin {
  
    function getInfo() {
        return array(
                'author' => 'Gina Häußge, Michael Klier, Esther Brunner',
                'email' => 'dokuwiki@chimeric.de',
                'date' => @file_get_contents(DOKU_PLUGIN . 'pagelist/VERSION'),
                'name' => 'Pagelist Plugin (syntax component)',
                'desc' => 'lists pages',
                'url' => 'http://dokuwiki.org/plugin:pagelist',
                );
    }
 
    function getType() { return 'substition';}
    function getPType() { return 'block';}
    function getSort() { return 168; }
 
    /**
* Connect pattern to lexer
*/
    function connectTo($mode) {
        $this->Lexer->addSpecialPattern('<pagelist.+?</pagelist>', $mode, 'plugin_pagelist');
    }
 
    /**
* Handle the match
*/
    function handle($match, $state, $pos, &$handler) {
        global $ID;
 
        $match = substr($match, 9, -11); // strip markup
        list($flags, $match) = explode('>', $match, 2);
        $flags = explode('&', substr($flags, 1));
        $items = explode('*', $match);
 
        $pages = array();
        $c = count($items);
        for ($i = 0; $i < $c; $i++) {
            if (!preg_match('/\[\[(.+?)\]\]/', $items[$i], $match)) continue;
            list($id, $title) = explode('|', $match[1], 2);
            list($id, $section) = explode('#', $id, 2);
            if (!$id) $id = $ID;
            resolve_pageid(getNS($ID), $id, $exists);
 
            // page has an image title
            if (($title) && (preg_match('/\{\{(.+?)\}\}/', $title, $match))) {
                list($image, $title) = explode('|', $match[1], 2);
                list($ext, $mime) = mimetype($image);
                if (!substr($mime, 0, 5) == 'image') $image = '';
                $pages[] = array(
                        'id' => $id,
                        'section' => cleanID($section),
                        'title' => trim($title),
                        'image' => trim($image),
                        'exists' => $exists,
                        );
 
            // text title (if any)
            } else {
                $pages[] = array(
                        'id' => $id,
                        'section' => cleanID($section),
                        'title' => trim($title),
                        'exists' => $exists,
                        );
            }
        }
        return array($flags, $pages);
    }
 
    /**
* Create output
*/
    function render($mode, &$renderer, $data) {
        list($flags, $pages) = $data;
 
        // for XHTML output
        if ($mode == 'xhtml') {
            if (!$my =& plugin_load('helper', 'pagelist')) return false;
            $my->setFlags($flags);
            $my->startList();
            foreach($pages as $page) {
                $my->addPage($page);
            }
            $renderer->doc .= $my->finishList();
            return true;
 
        // for metadata renderer
        } elseif ($mode == 'metadata') {
            foreach ($pages as $page) {
                $renderer->meta['relation']['references'][$page['id']] = $page['exists'];
            }
            return true;
        }
        return false;
    }
}
// vim:ts=4:sw=4:et:enc=utf-8: