plugin:embed
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
plugin:embed [2008-03-12 17:07] – 131.220.151.196 | plugin:embed [2024-02-02 08:05] (current) – Aleksandr | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Embed Plugin ====== | ||
+ | ---- plugin ---- | ||
+ | description: | ||
+ | author | ||
+ | email : bihler@iai.uni-bonn.de | ||
+ | type : syntax | ||
+ | lastupdate : 2007-05-14 | ||
+ | compatible : 2008-05-05, 2006-11-06, 2007-05-24, 2007-06-26b | ||
+ | depends | ||
+ | conflicts | ||
+ | similar | ||
+ | tags : include, embed | ||
+ | |||
+ | downloadurl: | ||
+ | ---- | ||
+ | |||
+ | ===== The idea ===== | ||
+ | |||
+ | Sometimes you are required to include a wikipage while replacing parts of it, e.g. if building up hierarchical menus with the [[: | ||
+ | |||
+ | ===== Requirements ===== | ||
+ | |||
+ | I've developed the plugin under the DokuWiki version of 2006-11-06 and seems to work with version 2007-06-26 as well. | ||
+ | |||
+ | ===== Download / Installation ===== | ||
+ | |||
+ | Download the plugin here (manually or via Plugin Manager): http:// | ||
+ | |||
+ | ===== Shortcomings ===== | ||
+ | |||
+ | To assure proper use of [[: | ||
+ | |||
+ | ===== Syntax ===== | ||
+ | |||
+ | The Syntax is very simple: | ||
+ | < | ||
+ | <embed wikipage/> | ||
+ | </ | ||
+ | |||
+ | With replacement: | ||
+ | < | ||
+ | <embed wikipage> | ||
+ | <label replace_me> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Use Case ===== | ||
+ | |||
+ | A use case for this plugin is inherited creation of menus for [[: | ||
+ | |||
+ | **: | ||
+ | < | ||
+ | ~~NOTOC~~ | ||
+ | |||
+ | * [[:start]] | ||
+ | * <label research> | ||
+ | * <label teaching> | ||
+ | * <label publications> | ||
+ | </ | ||
+ | |||
+ | This will look like: | ||
+ | * [[:start]] | ||
+ | * [[: | ||
+ | * [[: | ||
+ | * [[: | ||
+ | |||
+ | ---- | ||
+ | |||
+ | **: | ||
+ | < | ||
+ | <embed ..: | ||
+ | <label teaching>< | ||
+ | * <label classes> | ||
+ | * <label labs> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | This will look like: | ||
+ | * [[:start]] | ||
+ | * [[: | ||
+ | * **teaching** | ||
+ | * [[.: | ||
+ | * [[.:labs:]] | ||
+ | * [[: | ||
+ | |||
+ | ---- | ||
+ | |||
+ | **: | ||
+ | < | ||
+ | <embed ..: | ||
+ | <label teaching_header> | ||
+ | <label classes> | ||
+ | * [[OOSC]] | ||
+ | * [[AOSC]]</ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | This will look like: | ||
+ | * [[:start]] | ||
+ | * [[: | ||
+ | * [[: | ||
+ | * **classes** | ||
+ | * [[OOSC]] | ||
+ | * [[AOSC]] | ||
+ | * [[.:labs:]] | ||
+ | * [[: | ||
+ | |||
+ | ===== Code ===== | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | /** | ||
+ | * This plugin allows to embed wikipages into the current page | ||
+ | * In addition, parts marked with id's can be replaced. | ||
+ | * | ||
+ | * Inspired (and copy& | ||
+ | * | ||
+ | * | ||
+ | * For wikipage-portion-replacements, | ||
+ | * | ||
+ | * | ||
+ | * @license | ||
+ | * @author | ||
+ | */ | ||
+ | |||
+ | // must be run within DokuWiki | ||
+ | if(!defined(' | ||
+ | |||
+ | if(!defined(' | ||
+ | if(!defined(' | ||
+ | require_once(DOKU_PLUGIN.' | ||
+ | |||
+ | // | ||
+ | |||
+ | class syntax_plugin_embed extends DokuWiki_Syntax_Plugin { | ||
+ | |||
+ | function syntax_plugin_embed() { | ||
+ | global $embedded_pages_by_plugin_embed; | ||
+ | if (! $embedded_pages_by_plugin_embed) { | ||
+ | $embedded_pages_by_plugin_embed = array(); // To avoid recursion; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function getInfo(){ | ||
+ | return array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | |||
+ | function getType(){ | ||
+ | return ' | ||
+ | } | ||
+ | |||
+ | function getSort(){ | ||
+ | return 500; | ||
+ | } | ||
+ | |||
+ | function connectTo($mode) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Handle the match | ||
+ | */ | ||
+ | function handle($match, | ||
+ | switch ($state) { | ||
+ | case DOKU_LEXER_SPECIAL : | ||
+ | if (preg_match('/< | ||
+ | return array($matches[1], | ||
+ | else if (preg_match('/< | ||
+ | return array($matches[1],'' | ||
+ | break; | ||
+ | } | ||
+ | return array(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Create output | ||
+ | */ | ||
+ | function render($mode, | ||
+ | global $ID; | ||
+ | global $embedded_pages_by_plugin_embed; | ||
+ | |||
+ | |||
+ | if($mode == ' | ||
+ | list($id, | ||
+ | |||
+ | $renderer-> | ||
+ | |||
+ | resolve_pageid(getNS($ID), | ||
+ | |||
+ | // | ||
+ | |||
+ | // avoid circular references | ||
+ | if (in_array($id, | ||
+ | return false; | ||
+ | |||
+ | array_push($embedded_pages_by_plugin_embed, | ||
+ | |||
+ | $ins = $this-> | ||
+ | $renderer-> | ||
+ | |||
+ | array_pop($embedded_pages_by_plugin_embed); | ||
+ | |||
+ | return true; | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | function _embed_file($id, | ||
+ | |||
+ | |||
+ | // check permission | ||
+ | $perm = auth_quickaclcheck($id); | ||
+ | if ($perm < AUTH_READ) return false; | ||
+ | |||
+ | |||
+ | //Read embeded page | ||
+ | $page = io_readfile(wikiFN($id)); | ||
+ | |||
+ | // convert relative links | ||
+ | $page = $this-> | ||
+ | |||
+ | // do replacements (on text-base to preserve List indentation and ordering etc.): | ||
+ | $page = $this-> | ||
+ | |||
+ | $ins = p_get_instructions($page); | ||
+ | |||
+ | return $ins; | ||
+ | } | ||
+ | |||
+ | function _do_replacements($page, | ||
+ | //Build up list of replacements (this needs to be done manually to allow several replacements with nesting if < | ||
+ | $r_list = array(); | ||
+ | preg_match_all('/< | ||
+ | preg_match_all('/< | ||
+ | |||
+ | $level = 0; | ||
+ | $element = array_shift($matches_label[1]); | ||
+ | $end_element = array_shift($matches_labelx[0]); | ||
+ | while ($element || $end_element) { | ||
+ | if ($element && $element[1] < $end_element[1]) { // <label ..> before </ | ||
+ | if ($level == 0) { | ||
+ | $section_name = $element[0]; | ||
+ | $section_start = $element[1]+strlen($section_name)+1; | ||
+ | } | ||
+ | $level++; | ||
+ | $element = array_shift($matches_label[1]); | ||
+ | } else { //</ | ||
+ | $level--; | ||
+ | if ($level == 0) { | ||
+ | $section_end = $end_element[1]; | ||
+ | $r_list[$section_name] = substr($r_str, | ||
+ | } | ||
+ | $end_element = array_shift($matches_labelx[0]); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // now do replacements | ||
+ | foreach ($r_list as $section => $rep) { | ||
+ | $page = preg_replace('/< | ||
+ | } | ||
+ | return $page; | ||
+ | } | ||
+ | |||
+ | //Does not support CamelCase Links currently | ||
+ | function _convert_link($link, | ||
+ | |||
+ | //Check if external: | ||
+ | if (preg_match('/ | ||
+ | return $link; | ||
+ | |||
+ | //check if interwiki or email: | ||
+ | if (strpos('>', | ||
+ | return $link; | ||
+ | |||
+ | // convert internal links and media from relative to absolute | ||
+ | |||
+ | // relative subnamespace | ||
+ | if ($link{0} == ' | ||
+ | // parent namespace | ||
+ | if ($link{1} == ' | ||
+ | $link = getNS($inclNS).':' | ||
+ | // current namespace | ||
+ | else | ||
+ | $link = $inclNS.':' | ||
+ | } elseif (strpos($link, | ||
+ | $link = $inclNS.':' | ||
+ | } | ||
+ | |||
+ | return $link; | ||
+ | } | ||
+ | |||
+ | function _convertInstructions($page, | ||
+ | global $ID; | ||
+ | |||
+ | if (! $page) return; | ||
+ | |||
+ | // check if embeded page is in same namespace | ||
+ | $inclNS = getNS($page_id); | ||
+ | if (getNS($ID) == $inclNS) return $page; | ||
+ | |||
+ | // convert links | ||
+ | $page = preg_replace("/ | ||
+ | "' | ||
+ | $page); | ||
+ | |||
+ | //convert embeddings | ||
+ | $page = preg_replace("/< | ||
+ | "'< | ||
+ | $page); | ||
+ | |||
+ | //convert images | ||
+ | $page = preg_replace("/ | ||
+ | "' | ||
+ | $page); | ||
+ | return $page; | ||
+ | |||
+ | } | ||
+ | } | ||
+ | //Setup VIM: ex: et ts=4 enc=utf-8 : | ||
+ | </ | ||
+ | |||
+ | ===== Bugs? ===== | ||
+ | |||
+ | Getting **Fatal error:** Maximum execution time of 35 seconds exceeded in **lib/ | ||
+ | |||
+ | Some problem with the embedding engine leads to no generation of TOC for %%< | ||
+ | > | ||
+ | |||
+ | >> Take a look at the [[plugin: | ||
+ | |||
+ | >>> | ||
+ | |||
+ | Embeds with absolute internal links go wrong; they come out as ns:ns:page instead of just ns:page. | ||
+ | fixed by change line 184 from | ||
+ | } elseif (strpos($link, | ||
+ | to | ||
+ | } elseif (strpos($link, | ||
+ | | ||
+ | ===== Stopped working ===== | ||
+ | |||
+ | I wanted to make a " |
plugin/embed.txt · Last modified: 2024-02-02 08:05 by Aleksandr