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_publistx 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_publistx'); 33 } 34 35 function handle($match, $state, $pos, &$handler){ 36 $data = array(); 37 38 // Partition properly 39 $matches = array(); 40 $pattern = '/\[publist(?:\|(page|file|url):(.+?))(?:\|(wiki|html):(page|file|url):(.+?))(?:\|(.+?(?:\|.+?)*))?\]/'; 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 50 // Set default language. Get current lang from translation plugin 51 // if installed & enabled or fall back to default lang in conf. 52 if (!plugin_isdisabled('translation')) { 53 $trans =& plugin_load('helper', 'translation'); 54 global $ID; 55 $mylang = $trans->getLangPart($ID); 56 } else { 57 global $conf; 58 $mylang = $conf['lang']; 59 } 60 $data['options']['lang'] = $mylang; 61 62 if ( !empty($matches[6]) ) { 63 $matches = explode('|', $matches[6]); 64 foreach ( $matches as $opt ) { 65 $optparts = array(); 66 if ( preg_match('/(.+?):(.+)/', $opt, $optparts) ) { 67 $optparts[2] = explode(';', $optparts[2]); 68 $option = array(); 69 foreach ( $optparts[2] as $single ) { 70 $single = explode('=', $single); 71 if (count($single) == 1 && count($optparts[2]) == 1) { 72 $option = $single[0]; 73 } 74 else { 75 $option[$single[0]] = str_replace(',', '|', $single[1]); 76 } 77 } 78 $data['options'][$optparts[1]] = $option; 79 } 80 } 81 } 82 83 if ($data['options']['authors']) 84 { 85 $tmp = explode(':', $data['options']['authors'],2); 86 $data['authors'] = array('type' => $tmp[0], 'ref' => $tmp[1]); 87 } 88 } 89 return $data; 90 } 91 92 function render($mode, &$renderer, $data) { 93 if($mode != 'xhtml') return false; 94 95 if ( empty($data['error']) ) { 96 // Retrieve BibTeX source 97 $bibtex = $this->_load($data, 'bibtex'); 98 if ( empty($bibtex) ) { 99 $data['error'] .= $data['bibtex']['type'].' '.$data['bibtex']['ref'].' does not exist<br />'; 100 } 101 102 // Retrieve Template source 103 $template = $this->_load($data, 'template'); 104 if ( empty($template) ) { 105 $data['error'] .= $data['template']['type'].' '.$data['template']['ref'].' does not exist<br />'; 106 } 107 108 $authors = null; 109 if ($data['authors']) { 110 // Retrieve Authors source 111 $authors = $this->_load($data, 'authors'); 112 if ( empty($authors) ) { 113 $data['error'] .= $data['authors']['type'].' '.$data['authors']['ref'].' does not exist<br />'; 114 } 115 } 116 117 118 if ( !empty($bibtex) && !empty($template) ) { 119 require_once(dirname(__FILE__).'/bib2tpl/bibtex_converter.php'); 120 if ( is_readable(dirname(__FILE__).'/sanitiser.php')) { 121 include(dirname(__FILE__).'/sanitiser.php'); 122 } 123 if ( empty($sanitiser) ) { 124 $sanitiser = create_function('$i', 'return $i;'); 125 } 126 $parser = new BibtexConverter($data['options'],$sanitiser,$authors); 127 $code = $parser->convert($bibtex, $template); 128 129 if ( $data['template']['target'] == 'wiki' ) { 130 $code = p_render($mode, p_get_instructions($code), $info); 131 } 132 133 $renderer->doc .= $code; 134 } 135 } 136 137 if ( !empty($data['error']) ) { 138 $renderer->doc .= $data['error']; 139 } 140 141 $renderer->info['cache'] = false; 142 143 return true; 144 } 145 146 function _load($data, $kind) { 147 global $INFO; 148 149 if ( $data[$kind]['type'] == 'url' ) { 150 return file_get_contents($data[$kind]['ref']); 151 } 152 if ( $data[$kind]['type'] == 'file' ) { 153 return file_get_contents(dirname(__FILE__).'/'.$kind.'/'.$data[$kind]['ref']); 154 } 155 else if ( $data[$kind]['type'] == 'page' ) { 156 $exists = false; 157 $id = $data[$kind]['ref']; 158 resolve_pageid($INFO['namespace'], $id, $exists); 159 if ( $exists ) { 160 return rawWiki($id); 161 } 162 } 163 164 return null; 165 } 166} 167 168// vim:ts=4:sw=4:et:enc=utf-8: 169