|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) |
| 4 | + * @author Andreas Gohr <[email protected]> |
| 5 | + */ |
| 6 | +// must be run within Dokuwiki |
| 7 | +if(!defined('DOKU_INC')) die(); |
| 8 | + |
| 9 | +if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); |
| 10 | +require_once(DOKU_PLUGIN.'syntax.php'); |
| 11 | + |
| 12 | +class syntax_plugin_xref extends DokuWiki_Syntax_Plugin { |
| 13 | + |
| 14 | + var $dir = ''; |
| 15 | + var $web = ''; |
| 16 | + |
| 17 | + function syntax_plugin_xref(){ |
| 18 | + $this->dir = rtrim($this->getConf('dir'),'/'); |
| 19 | + $this->web = rtrim($this->getConf('web'),'/'); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * return some info |
| 24 | + */ |
| 25 | + function getInfo(){ |
| 26 | + return array( |
| 27 | + 'author' => 'Andreas Gohr', |
| 28 | + |
| 29 | + 'date' => '2008-10-03', |
| 30 | + 'name' => 'PHPXref Plugin', |
| 31 | + 'desc' => 'Makes linking to a PHPXref generated API doc easy.', |
| 32 | + 'url' => 'http://dokuwiki.org/plugin:xref', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * What kind of syntax are we? |
| 38 | + */ |
| 39 | + function getType(){ |
| 40 | + return 'substition'; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * What about paragraphs? |
| 45 | + */ |
| 46 | + function getPType(){ |
| 47 | + return 'normal'; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Where to sort in? |
| 52 | + */ |
| 53 | + function getSort(){ |
| 54 | + return 150; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Connect pattern to lexer |
| 60 | + */ |
| 61 | + function connectTo($mode) { |
| 62 | + $this->Lexer->addSpecialPattern('\[\[xref>.+?\]\]',$mode,'plugin_xref'); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Handle the match |
| 68 | + */ |
| 69 | + function handle($match, $state, $pos, &$handler){ |
| 70 | + $match = trim(substr($match,7,-2)); |
| 71 | + |
| 72 | + list($link,$name) = explode('|',$match,2); |
| 73 | + if(!$name) $name = $link; |
| 74 | + |
| 75 | + $first = 0; |
| 76 | + if($link[0] == '$') $first = 4; |
| 77 | + $found = $this->_find($link,$first); |
| 78 | + |
| 79 | + return array($link,$found,$name); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Create output |
| 84 | + */ |
| 85 | + function render($format, &$R, $data) { |
| 86 | + global $conf; |
| 87 | + if($format != 'xhtml') return false; |
| 88 | + |
| 89 | + //prepare for formating |
| 90 | + $link['target'] = $conf['target']['extern']; |
| 91 | + $link['style'] = ''; |
| 92 | + $link['pre'] = ''; |
| 93 | + $link['suf'] = ''; |
| 94 | + $link['more'] = ''; |
| 95 | + $link['class'] = 'xref_plugin'; |
| 96 | + $link['name'] = hsc($data[2]); |
| 97 | + |
| 98 | + if(!$data[1]){ |
| 99 | + $link['url'] = $this->web; |
| 100 | + $link['title'] = $this->getLang('unknown'); |
| 101 | + $link['class'] .= ' xref_plugin_err'; |
| 102 | + }else{ |
| 103 | + $link['url'] = $this->web.'/'.$data[1]; |
| 104 | + $link['title'] = sprintf($this->getLang('view'),hsc($data[0])); |
| 105 | + } |
| 106 | + |
| 107 | + $R->doc .= $R->_formatLink($link); |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Try to find the given name in the xref directory |
| 113 | + * |
| 114 | + * @param int $first - defines which type should be searched first for the name |
| 115 | + */ |
| 116 | + function _find($name,$first=0){ |
| 117 | + $paths = array( |
| 118 | + 0 => '_functions', |
| 119 | + 1 => '_classes', |
| 120 | + 2 => '_constants', |
| 121 | + 3 => '_tables', |
| 122 | + 4 => '_variables' |
| 123 | + ); |
| 124 | + |
| 125 | + $clean = preg_replace('/[^\w\-_]+/','',$name); |
| 126 | + $small = strtolower($clean); |
| 127 | + |
| 128 | + $path = $paths[$first]; |
| 129 | + unset($paths[$first]); |
| 130 | + do{ |
| 131 | + $check = $path.'/'.$clean.'.html'; |
| 132 | + if(@file_exists($this->dir.'/'.$check)) return $check; |
| 133 | + $check = $path.'/'.$small.'.html'; |
| 134 | + if(@file_exists($this->dir.'/'.$check)) return $check; |
| 135 | + $path = array_shift($paths); |
| 136 | + }while($path); |
| 137 | + |
| 138 | + // still here? might be a file reference |
| 139 | + $clean = preg_replace('/\.\.+/','.',$name); |
| 140 | + if(@file_exists($this->dir.'/'.$clean.'.html')){ |
| 141 | + return $clean.'.html'; |
| 142 | + } |
| 143 | + |
| 144 | + return ''; |
| 145 | + } |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +//Setup VIM: ex: et ts=4 enc=utf-8 : |
0 commit comments