plugin:relativens
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
plugin:relativens [2009-01-23 15:19] – 92.254.50.195 | plugin:relativens [2018-06-05 22:54] (current) – [Download and Installation] Klap-in | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== relativens plugin ====== | ||
+ | ---- plugin ---- | ||
+ | description: | ||
+ | author | ||
+ | email : pe78 [at] lodju.dyndns dot org | ||
+ | type : syntax | ||
+ | lastupdate : 2008-12-31 | ||
+ | compatible : 2008-05-05 | ||
+ | depends | ||
+ | conflicts | ||
+ | similar | ||
+ | tags : links namespace | ||
+ | |||
+ | downloadurl: | ||
+ | ---- | ||
+ | |||
+ | This plugin is a remake of relativelinks plugin. This does basically the same thing, but is implemented in a more subtle way. It also tries to handle media. | ||
+ | |||
+ | ===== Download and Installation ===== | ||
+ | |||
+ | Search and install the plugin using the [[plugin: | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | ===== Code ===== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Plugin relativens: Links & media that don't start with / or : default | ||
+ | * to being relative to the namespace in which the current page is. | ||
+ | * | ||
+ | * @license | ||
+ | * @author | ||
+ | * @based_on | ||
+ | */ | ||
+ | |||
+ | if(!defined(' | ||
+ | |||
+ | if(!defined(' | ||
+ | require_once(DOKU_PLUGIN.' | ||
+ | |||
+ | class syntax_plugin_relativens extends DokuWiki_Syntax_Plugin { | ||
+ | |||
+ | function getInfo() { | ||
+ | return array(' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | |||
+ | function getType() { | ||
+ | return ' | ||
+ | } | ||
+ | |||
+ | // before built in links & media | ||
+ | function getSort(){ return 299; } | ||
+ | |||
+ | function connectTo($mode) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | function handle($match, | ||
+ | // Following link parsing code is originally copied from Dokuwiki handler.php | ||
+ | |||
+ | // See which one we caught | ||
+ | $isMedia = preg_match('/ | ||
+ | |||
+ | $isMedia = 0; | ||
+ | |||
+ | $originalMatch = $match; | ||
+ | |||
+ | // Strip the opening and closing markup | ||
+ | // At same time detect if this is media or link | ||
+ | // handler.media and handler.internallink do this too, but | ||
+ | // they don't check if they replaced anything. | ||
+ | $match = preg_replace(array('/ | ||
+ | $match = preg_replace(array('/ | ||
+ | |||
+ | // Split title from URL | ||
+ | $linkAndTitle = preg_split('/ | ||
+ | $linkTrimmed = trim($linkAndTitle[0]); | ||
+ | |||
+ | $modifiedMatch = $originalMatch; | ||
+ | |||
+ | // Give special treatment to the first few characters | ||
+ | // of internal links and media links (media links are always " | ||
+ | // Sadly excluding the non internal links is a complicated process | ||
+ | // and we end up doing it twice, but at least the results of these | ||
+ | // handle functions are cached. | ||
+ | if($isMedia || $this-> | ||
+ | // unless it's explicitly absolute, | ||
+ | // Make it look like relative | ||
+ | $modifiedMatch = preg_replace('/ | ||
+ | } | ||
+ | // let the regular handler take care of the rest | ||
+ | if($isMedia) { | ||
+ | $handler-> | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $handler-> | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | function isLinkInternal($linkPart) { | ||
+ | // If conditions copied from Dokuwiki handler.php | ||
+ | if ( preg_match('/ | ||
+ | return false; | ||
+ | }elseif ( preg_match('/ | ||
+ | return false; | ||
+ | }elseif ( preg_match('# | ||
+ | return false; | ||
+ | }elseif ( preg_match('<' | ||
+ | return false; | ||
+ | }elseif ( preg_match(' | ||
+ | return false; | ||
+ | }else{ | ||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function render($mode, | ||
+ | // Should never come here, since we leech the original handler for most processing | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====PHP4===== | ||
+ | |||
+ | This code assumes php5 because of the preg_replace call - it has one argument too much for php4. | ||
+ | To get it working on php4, change | ||
+ | |||
+ | < | ||
+ | $match = preg_replace(array('/ | ||
+ | </ | ||
+ | to | ||
+ | < | ||
+ | $match = preg_replace(array('/ | ||
+ | </ | ||
+ | |||
+ | To let it work more proper with media in php4 nevertheless, | ||
+ | |||
+ | < | ||
+ | // See which one we caught | ||
+ | $isMedia = preg_match('/ | ||
+ | |||
+ | $isMedia = 0; | ||
+ | </ | ||
+ | To | ||
+ | < | ||
+ | // See which one we caught | ||
+ | $isMedia = preg_match('/ | ||
+ | |||
+ | // $isMedia = 0; | ||
+ | </ | ||
+ | I'm not sure what all this implies, but it seems to be working here. | ||
+ | |||
+ | Allthough using this fix, other plugins that use the media link format (like { { mp3play> | ||
+ | |||
+ | --//pike 200901// | ||
+ | |||
+ | I have also noticed some weird behaviour with media links... | ||
+ | |||
+ | I think I can see why that " | ||
+ | * Only " | ||
+ | * Links with special syntax like " | ||
+ | * Except, that all media links are considered " | ||
+ | |||
+ | Then I think it proceeds and mucks up that mp3play> prefix by inserting ./ at the wrong place | ||
+ | |||
+ | The code that mangles the link to be relative should be made more sensible, or the assumption | ||
+ | that media links are always relative should be removed... | ||
+ | |||
+ | I'll try to fix this and the php4 compatibility next time I need to hack our internal wiki... | ||
+ | |||
+ | --//Peter 200907// |