1<?php
2/**
3 * DokuWiki Plugin publist (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Raphael Reitzig <code@verrech.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'syntax.php';
17
18class syntax_plugin_publist extends DokuWiki_Syntax_Plugin {
19    function getType() {
20        return 'substition';
21    }
22
23    function getPType() {
24        return 'block';
25    }
26
27    function getSort() {
28        return 105;
29    }
30
31    function connectTo($mode) {
32        $this->Lexer->addSpecialPattern('\[publist\|.+?\]',$mode,'plugin_publist');
33    }
34
35    function handle($match, $state, $pos, Doku_Handler $handler){
36        $data = array();
37
38        // Partition properly
39        $matches = array();
40        $pattern = '/\[publist(?:\|(page|file):(.+?))(?:\|(wiki|html):(page|file):(.+?))(?:\|(.+?(?:\|.+?)*))?\]/';
41        if ( 0 === preg_match($pattern, $match, $matches) ) {
42            $data['error'] = 'Not valid publist syntax: '.$match;
43        }
44        else {
45            $data['bibtex'] = array('type' => $matches[1], 'ref' => $matches[2]);
46            $data['template'] = array('target' => $matches[3], 'type' => $matches[4], 'ref' => $matches[5]);
47            $data['options'] = array();
48
49            // Set default language. Get current lang from translation plugin
50            // if installed & enabled or fall back to default lang in conf.
51            if (!plugin_isdisabled('translation')) {
52                $trans =& plugin_load('helper', 'translation');
53                global $ID;
54                $mylang = $trans->getLangPart($ID);
55            } else {
56                global $conf;
57                $mylang = $conf['lang'];
58            }
59            $data['options']['lang'] = $mylang;
60
61            if ( !empty($matches[6]) ) {
62               $matches = explode('|', $matches[6]);
63               foreach ( $matches as $opt ) {
64                 $optparts = array();
65                 if ( preg_match('/(.+?):(.+)/', $opt, $optparts) ) {
66                    $optparts[2] = explode(';', $optparts[2]);
67                    $option = array();
68                    foreach ( $optparts[2] as $single ) {
69                        $single = explode('=', $single);
70                        if (count($single) == 1 && count($optparts[2]) == 1) {
71                            $option = $single[0];
72                        }
73                        else {
74                            $option[$single[0]] = str_replace(',', '|', $single[1]);
75                        }
76                    }
77                    $data['options'][$optparts[1]] = $option;
78                 }
79               }
80            }
81        }
82        return $data;
83    }
84
85    function render($mode, Doku_Renderer $renderer, $data) {
86        if($mode != 'xhtml') return false;
87
88        if ( empty($data['error']) ) {
89            // Retrieve BibTeX source
90            $bibtex = $this->_load($data, 'bibtex');
91            if ( empty($bibtex) ) {
92                $data['error'] .= $data['bibtex']['type'].' '.$data['bibtex']['ref'].' does not exist<br />';
93            }
94
95            // Retrieve Template source
96            $template = $this->_load($data, 'template');
97            if ( empty($template) ) {
98                $data['error'] .= $data['template']['type'].' '.$data['template']['ref'].' does not exist<br />';
99            }
100
101            if ( !empty($bibtex) && !empty($template) ) {
102                require_once(dirname(__FILE__).'/bib2tpl/bibtex_converter.php');
103                if ( is_readable(dirname(__FILE__).'/sanitiser.php')) {
104                    include(dirname(__FILE__).'/sanitiser.php');
105                }
106                if ( empty($sanitiser) ) {
107                   $sanitiser = create_function('$i', 'return $i;');
108                }
109                $parser = new BibtexConverter($data['options'],$sanitiser);
110                $code = $parser->convert($bibtex, $template);
111
112                if ( $data['template']['target'] == 'wiki' ) {
113                    $code = p_render($mode, p_get_instructions($code), $info);
114                }
115
116                $renderer->doc .= $code;
117            }
118        }
119
120        if ( !empty($data['error']) ) {
121            $renderer->doc .= $data['error'];
122        }
123
124        $renderer->info['cache'] = false;
125
126        return true;
127    }
128
129    function _load($data, $kind) {
130        global $INFO;
131
132        if ( $data[$kind]['type'] == 'file' ) {
133            return file_get_contents(dirname(__FILE__).'/'.$kind.'/'.$data[$kind]['ref']);
134        }
135        else if ( $data[$kind]['type'] == 'page' ) {
136            $exists = false;
137            $id = $data[$kind]['ref'];
138            resolve_pageid($INFO['namespace'], $id, $exists);
139            if ( $exists ) {
140                return rawWiki($id);
141            }
142        }
143
144        return null;
145    }
146}
147
148// vim:ts=4:sw=4:et:enc=utf-8:
149