DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:code

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
plugin:code [2017-12-07 17:31] – [Code Replacement Plugin] 80.92.226.33plugin:code [2024-03-04 21:59] (current) Aleksandr
Line 1: Line 1:
-sdsd====== Code Replacement Plugin ======+====== Code Replacement Plugin ======
  
 ---- plugin ---- ---- plugin ----
 description: Enhancements for the normal <code> syntax description: Enhancements for the normal <code> syntax
-author     : Christopher Smith +author     : Christopher Smith, LGnap 
-email      : chris@jalakai.co.uk+email      : chris@jalakai.co.uk, N/A
 type       : syntax type       : syntax
-lastupdate : 2008-08-13 +lastupdate : 2020-07-28 
-compatible : 2005-09-22 and later+compatible : Hogfather
 depends    :  depends    : 
 conflicts  conflicts 
Line 13: Line 13:
 tags       : code, syntaxhighlight tags       : code, syntaxhighlight
  
-downloadurl: http://dokuwiki.jalakai.co.uk/plugin-code.tar.gz+downloadurl: https://github.com/lgnap/dokuwiki-plugin-code/archive/master.zip
 ---- ----
  
-This [[plugin]] replaces the current DokuWiki handlers for ''%%<code> </code>%%'' & ''%%<file> </file>%%'' markup.  It adds ability to recognize and render a title above the code box.+This plugin replaces the current DokuWiki handlers for ''%%<code> </code>%%'' & ''%%<file> </file>%%'' markup.  It adds ability to recognize and render a title above the code box.
  
 This plugin was inspired by [[bug>477]] and my own curiosity to see if it was possible to override DokuWiki's native syntax handling. This plugin was inspired by [[bug>477]] and my own curiosity to see if it was possible to override DokuWiki's native syntax handling.
Line 24: Line 24:
 ---- ----
  
-See the plugin in action [[http://wiki.jalakai.co.uk/dokuwiki/doku.php/tutorials/codeplugin|here]] (or below)+See the plugin in action [[http://wiki.jalakai.co.uk/dokuwiki/doku.php/tutorials/codeplugin|here (maybe outdated)]] (or below)
 ===== Syntax ===== ===== Syntax =====
  
Line 71: Line 71:
  
 ===== Installation ===== ===== Installation =====
 +Search and install the plugin using the [[plugin:extension|Extension Manager]]. Refer to [[:Plugins]] on how to install plugins manually.
  
-Plugin sources: [[http://dokuwiki.jalakai.co.uk/plugin-code.zip|zip format (4k)]], [[http://dokuwiki.jalakai.co.uk/plugin-code.tar.gz|tar.gz format (2k)]] | [[http://wiki.jalakai.co.uk/repo/dokuwiki/plugins/code|darcs]] (requires [[plugin:darcs|darcs plugin]])+Plugin sources: [[https://github.com/lgnap/dokuwiki-plugin-code/archive/master.zip|zip format (4k)]]
  
-Download the source to your plugin folder, ''lib/plugins'' and extract its contents.  That will create a new plugin folder, ''lib/plugins/code'', and install the plugin. 
- 
-The folder will contain: 
-  style.css                              styles for the new boxes and titles 
-  syntax/                                syntax plugin folder 
-  syntax/code.php                        plugin script for <code> 
-  syntax/file.php                        plugin script for <file> 
- 
-The plugin is now installed. 
  
  
Line 88: Line 80:
  
 The plugin consists of three files, two (almost identical) syntax plugins files, [[#syntaxcode.php|syntax/code.php]] & [[#syntaxfile.php|syntax/file.php]] and some CSS styles, [[#style.css|style.css]].sd The plugin consists of three files, two (almost identical) syntax plugins files, [[#syntaxcode.php|syntax/code.php]] & [[#syntaxfile.php|syntax/file.php]] and some CSS styles, [[#style.css|style.css]].sd
-==== syntax/code.php ==== 
  
-<code php code.php> 
-<?php 
-/** 
- * Code Plugin: replaces DokuWiki's own code syntax 
- * 
- * Syntax:     <code lang |title> 
-   lang      (optional) programming language name, is passed to geshi for code highlighting 
-             if not provided, the plugin will attempt to derive a value from the file name 
-             (refer $extensions in render() method) 
-   title     (optional) all text after '|' will be rendered above the main code text with a 
-             different style. 
- * 
- * if no title is provided will render as native DokuWiki code syntax mode, e.g. 
-   <pre class='code {lang}'> ... </pre> 
- * 
- * if title is provide will render as follows 
-   <div class='source'> 
-     <p>{title}</p> 
-     <pre class='code {lang}'> ... </pre> 
-   </div> 
- * 
- * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html) 
- * @author     Christopher Smith <chris@jalakai.co.uk> 
- */ 
- 
-if(!defined('DOKU_INC')) die(); 
- 
-/** 
- * All DokuWiki plugins to extend the parser/rendering mechanism 
- * need to inherit from this class 
- */ 
-class syntax_plugin_code_code extends DokuWiki_Syntax_Plugin { 
- 
-    var $syntax = ""; 
- 
-    /** 
-     * return some info 
-     */ 
-    function getInfo(){ 
-      return array( 
-        'author' => 'Christopher Smith', 
-        'email'  => 'chris@jalakai.co.uk', 
-        'date'   => '2008-08-13', 
-        'name'   => 'Code Replacement Plugin', 
-        'desc'   => 'Replacement for DokuWiki\'s own <code> handler, adds a title to the box. 
-                     Syntax: <code lang|title>, lang and title are optional. title does not support any DokuWiki markup.', 
-        'url'    => 'http://www.dokuwiki.org/plugin:code', 
-      ); 
-    } 
- 
-    function getType(){ return 'protected';} 
-    function getPType(){ return 'block';} 
- 
-    // must return a number lower than returned by native 'code' mode (200) 
-    function getSort(){ return 195; } 
- 
-    /** 
-     * Connect pattern to lexer 
-     */ 
-    function connectTo($mode) { 
-      $this->Lexer->addEntryPattern('<code(?=[^\r\n]*?>.*?</ code>)',$mode,'plugin_code_code'); 
-    } 
- 
-    function postConnect() { 
-      $this->Lexer->addExitPattern('</ code>', 'plugin_code_code'); 
-    } 
- 
-    /** 
-     * Handle the match 
-     */ 
-    function handle($match, $state, $pos, &$handler){ 
- 
-        switch ($state) { 
-          case DOKU_LEXER_ENTER: 
-            $this->syntax = substr($match, 1); 
-            return false; 
- 
-          case DOKU_LEXER_UNMATCHED: 
-             // will include everything from <code ... to ... </code > 
-             // e.g. ... [lang] [|title] > [content] 
-             list($attr, $content) = preg_split('/>/u',$match,2); 
-             list($lang, $title) = preg_split('/\|/u',$attr,2); 
- 
-             if ($this->syntax == 'code') { 
-               $lang = trim($lang); 
-               if ($lang == 'html') $lang = 'html4strict'; 
-               if (!$lang) $lang = NULL; 
-             } else { 
-               $lang = NULL; 
-             } 
- 
-             return array($this->syntax, $lang, trim($title), $content); 
-        } 
-        return false; 
-    } 
- 
-    /** 
-     * Create output 
-     */ 
-    function render($mode, &$renderer, $data) { 
- 
-        if (count($data) == 4) { 
-          list($syntax, $lang, $title, $content) = $data; 
- 
-          if($mode == 'xhtml'){ 
-            if ($title) $renderer->doc .= "<div class='$syntax'><p>".$renderer->_xmlEntities($title)."</p>"; 
-            if ($syntax == 'code') $renderer->code($content, $lang); else $renderer->file($content); 
-            if ($title) $renderer->doc .= "</div>"; 
-        } else { 
-            if ($syntax == 'code') $renderer->code($content, $lang); else $renderer->file($content); 
-        } 
- 
-        return true; 
-      } 
-      return false; 
-    } 
-} 
- 
-//Setup VIM: ex: et ts=4 enc=utf-8 : 
-</code> 
- 
- 
- 
-<code php file.php> 
-<?php 
-/** 
- * File Plugin: replaces DokuWiki's own file syntax 
- * 
- * Syntax:     <file |title> 
-   title     (optional) all text after '|' will be rendered above the main code text with a 
-             different style. 
- * 
- * if no title is provided will render as native DokuWiki code syntax mode, e.g. 
-   <pre class='file'> ... </pre> 
- * 
- * if title is provide will render as follows 
-   <div class='file'> 
-     <p>{title}</p> 
-     <pre class='file'> ... </pre> 
-   </div> 
- * 
- * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html) 
- * @author     Christopher Smith <chris@jalakai.co.uk> 
- */ 
- 
-if(!defined('DOKU_INC')) die(); 
- 
-/** 
- * All DokuWiki plugins to extend the parser/rendering mechanism 
- * need to inherit from this class 
- */ 
-class syntax_plugin_code_file extends DokuWiki_Syntax_Plugin { 
- 
-    var $syntax = ""; 
- 
-    /** 
-     * return some info 
-     */ 
-    function getInfo(){ 
-      return array( 
-        'author' => 'Christopher Smith', 
-        'email'  => 'chris@jalakai.co.uk', 
-        'date'   => '2008-08-13', 
-        'name'   => '<file> replacement plugin', 
-        'desc'   => 'Replacement for DokuWiki\'s own <file> handler, adds a title to the box. 
-                     Syntax: <file|title>, title is optional and does not support any DokuWiki markup.', 
-        'url'    => 'http://www.dokuwiki.org/plugin:code', 
-      ); 
-    } 
- 
-    function getType(){ return 'protected';} 
-    function getPType(){ return 'block'; } 
- 
-    // must return a number lower than returned by native 'file' mode (210) 
-    function getSort(){ return 194; } 
- 
-    /** 
-     * Connect pattern to lexer 
-     */ 
-    function connectTo($mode) { 
-      $this->Lexer->addEntryPattern('<file(?=[^\r\n]*?>.*?</file>)',$mode,'plugin_code_file'); 
-    } 
- 
-    function postConnect() { 
-    $this->Lexer->addExitPattern('</file>', 'plugin_code_file'); 
-    } 
- 
-    /** 
-     * Handle the match 
-     */ 
-    function handle($match, $state, $pos, &$handler){ 
- 
-        switch ($state) { 
-            case DOKU_LEXER_ENTER: 
-                $this->syntax = substr($match, 1); 
-                return false; 
- 
-            case DOKU_LEXER_UNMATCHED: 
-                // will include everything from <code ... to ... </code > 
-                // e.g. ... [lang] [|title] > [content] 
-                list($attr, $content) = preg_split('/>/u',$match,2); 
-                list($lang, $title) = preg_split('/\|/u',$attr,2); 
- 
-                if ($this->syntax == 'code') { 
-                    $lang = trim($lang); 
-                    if ($lang == 'html') $lang = 'html4strict'; 
-                    if (!$lang) $lang = NULL; 
-                } else { 
-                    $lang = NULL; 
-                } 
- 
-                return array($this->syntax, $lang, trim($title), $content); 
-        } 
-        return false; 
-    } 
- 
-    /** 
-     * Create output 
-     */ 
-    function render($mode, &$renderer, $data) { 
- 
-      if (count($data) == 4) { 
-        list($syntax, $lang, $title, $content) = $data; 
- 
-        if($mode == 'xhtml'){ 
-            if ($title) $renderer->doc .= "<div class='$syntax'><p>".$renderer->_xmlEntities($title)."</p>"; 
-            if ($syntax == 'code') $renderer->code($content, $lang); else $renderer->file($content); 
-            if ($title) $renderer->doc .= "</div>"; 
-        } else { 
-            if ($syntax == 'code') $renderer->code($content, $lang); else $renderer->file($content); 
-        } 
- 
-        return true; 
-      } 
-      return false; 
-    } 
-} 
- 
-//Setup VIM: ex: et ts=4 enc=utf-8 : 
-</code> 
- 
- 
-==== style.css ==== 
- 
-These may be modified to suit your own requirements. 
- 
-<code css style.css> 
-/* 
- * code plugin extension - style additions 
- * 
- * @author  Christopher Smith  chris@jalakai.co.uk 
- * @link    http://wiki.jalakai.co.uk/dokuwiki/doku.php/tutorials/codeplugin 
- */ 
- 
-/* layout */ 
-div.code, div.file { 
-  width: 92%; 
-  margin: 1em auto; 
-  border: 1px solid; 
-  padding: 4px; 
-} 
- 
-div.code p, div.file p { 
-  font-size: 90%; 
-  margin: 0; 
-  padding: 2px; 
-} 
- 
-div.code pre.code, div.file pre.file { 
-  margin: 4px 0 0 0; 
-} 
- 
-/* colours */ 
-div.code { 
-  border-color:  #bbd; 
-  background: #e4ecf8; 
-} 
- 
-div.code p { 
-  background: #cad0ee; 
-} 
- 
-div.file { 
-  border-color: #dbb; 
-  background: #f8ece4; 
-} 
- 
-div.file p { 
-  background: #eed0ca; 
-} 
- 
-div.file pre.file { 
-  background: #fdf4ec; 
-  border-color: #dbb; 
-} 
- 
-/* end code plugin style additions */ 
-</code> 
  
 ===== Revision History ===== ===== Revision History =====
  
 +  * 2020-07-29 --- Fix fatal issue about function signatures, and put code on github 
   * 2008-08-13 --- Update plugin URL   * 2008-08-13 --- Update plugin URL
     * --- Add support for unknown render formats (e.g. ODT plugin)     * --- Add support for unknown render formats (e.g. ODT plugin)
Line 405: Line 99:
  
 ===== Discussion ===== ===== Discussion =====
 +
 +doesn't work with version 2024-02-06a "Kaos" !!!
 +
 +--- //[[|MaddoX]] 2024-03-4 (updated 2024-03-4)//
 +----
  
 What would be nice is if it automatically generates a title based on the language attribute set if no title is explicitly set.  --- //[[webmaster@lajzar.co.uk|ta' lajzar]] 2005-07-30 05:06// What would be nice is if it automatically generates a title based on the language attribute set if no title is explicitly set.  --- //[[webmaster@lajzar.co.uk|ta' lajzar]] 2005-07-30 05:06//
plugin/code.1512664279.txt.gz · Last modified: 2017-12-07 17:31 by 80.92.226.33

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki