DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:cli

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:cli [2013-01-25 14:11] – old revision restored (2011/09/29 11:32) 141.65.129.192plugin:cli [2023-04-15 21:12] (current) Aleksandr
Line 1: Line 1:
-====== Command Line Interface (CLI) Plugin  ======+====== CLI: a Command Line Interface formatter Plugin  ======
  
 ---- plugin ---- ---- plugin ----
 description: Format command line transcripts description: Format command line transcripts
-author     : Chris P. Jobling +author     : Schplurtz le Déboulonné 
-email      : C.P.Jobling@Swansea.ac.uk+email      : Schplurtz-AT-Laposte•net
 type       : syntax type       : syntax
-lastupdate : 2010-08-07 +lastupdate : 2020-08-18 
-compatible : +compatible : elenor of tsort, detritus, frusterick manners, greebo, hogfather, igor, jack jackrum
 depends    :  depends    : 
 conflicts  conflicts 
-similar    : code +similar    : konsole 
-tags       : code, cli, syntax, highlight +tags       : code, cli, syntax, highlighting 
- +downloadurl: https://github.com/schplurtz/dokuwiki-plugin-cli/zipball/master 
-downloadurl: http://github.com/downloads/cpjobling/plugin-cli/plugin-cli.zip+bugtracker : https://github.com/schplurtz/dokuwiki-plugin-cli/issues 
 +sourcerepo : https://github.com/schplurtz/dokuwiki-plugin-cli 
 +donationurl:  
 +screenshot_img : 
 ---- ----
  
-This [[wiki:plugins|plugin]] brings a Command Line Interface formatter to [[wiki:Dokuwiki]] allowing you to format the output of a transcript say for a user manual or on-line tutorial. It is designed to work with the output of a standard Unix Bash Shell, but should be suitable to document other kinds of CLI interaction, e.g. Windows Command Window, Python and Ruby, Matlab, etc. +===== Description =====
  
-The assumptions made are  +This plugin brings a Command Line Interface formatter to DokuWiki 
-  - all user commands start with a prompt +allowing you to format the output of a transcript say for a user 
 +manual or on-line tutorial. It is designed to work with the output 
 +of a standard Unix Bash Shell, but is suitable to document 
 +other kinds of CLI interaction, e.g. Windows Command Window, Python 
 +and Ruby, Matlab, etc. 
 + 
 +The assumptions made are 
 +  - all user commands start with a prompt
   - the CLI prompt will end in a recognizable character (typically '$' or '>')   - the CLI prompt will end in a recognizable character (typically '$' or '>')
   - user commands will follow the CLI prompt on the same line   - user commands will follow the CLI prompt on the same line
-  - lines that do not start with a prompt are outputs from the CLI((this turns out to be a problem for the Ruby interactive shell //irb// which seems to prefix every line with a prompt!))+  - user commands may contain a comment introduced by an easily recognizable character (usually '#'
 +  - lines that do not start with a prompt are outputs from the CLI.
  
-A key design feature is that it should be possible to display a CLI transcript (e.g. output from a history file or copy of text from a console window) for display with the mimumum of additional markup.  +A key design feature is that it should be possible to display a CLI transcript (e.g. output from a history file or copy of text from a console window) for display with the mimumum of additional markup.
  
-It is possible to adjust the style of the display using a style sheet. For details refer to [[#style.css]].+Very complicated prompts can be recognized with regexp; see [[#Configuration and Settings]] for the details. It is possible to adjust the style of the display using a style sheet. For details refer to [[#style]].
  
-==== Notes ====+===== Installation =====
  
-  * My version of this page -- which could be more recently updated -- can be found [[http://www.cpjobling.org.uk/dokuwiki/plugins:cli|here]]+Install the plugin using the [[extension|Extension Manager Plugin]] and the download URL above, which points to latest version of the plugin. Refer to [[:Plugins]] on how to install plugins manually.
  
-  * Since 7 August 2010 the source for this plugin code has been hosted at GitHub at http://github.com/cpjobling/plugin-cli. To install the plugin when you have ''git'' installed on your DokuWiki server, you just need to:+===== Examples/Usage =====
  
-  cd ../dokuwiki/plugins +A simple Bash Shell interaction:
-  git clone git://github.com/cpjobling/plugin-cli.git cli +
  
 +  <cli>
 +  user@host:~/somedir $ ls # List current directory
 +  conf      lang         README        screen.gif  ui
 +  info.txt  manager.dat  renderer.php  syntax.php
 +  user@host:~/somedir $ 
 +  </cli>
  
 +{{ https://imagizer.imageshack.com/img924/3346/k5o8ZG.png }}
  
 +Next example shows that ''<cli>'' can nest, and shows the shortcut ''<cli type=dos>'' in action.
 +  <cli>
 +  user@host:~/somedir $ dosemu
 +  <cli type=dos>
 +  (C) Copyright 1995-2006 Pasquale J. Villani and The FreeDOS Project.
 +  D: = LINUX\FS/HOME/USER attrib = READ/WRITE
 +  E: = LINUX\FS/MEDIA/CDROM CDROM:1 attrib = READ ONLY
 +  "Welcome to dosemu 1.4.0.8!"
 +  C:\>dir
 +   Volume in drive C is mu/drives/c
 +   Directory of C:\
 +  
 +  TMP                  <DIR>  12-28-17  6:29a
 +  CONFIG   SYS           346  06-22-15  1:17a
 +  AUTOEXEC BAT           556  06-22-15  1:17a
 +           2 file(s)            902 bytes
 +           1 dir(s)   2,147,450,880 bytes free
 +  C:\>echo pouet
 +  pouet
 +  C:\>rem This is a remark and does nothing
 +  C:\>echo But this rem is not a remark.
 +  But this rem is not a remark.
 +  C:\>exit
 +  </cli>
 +  user@host:~/somedir $ echo Back to normal again
 +  Back to normal again
 +  user@host:~/somedir $
 +  </cli>
  
-=====Acknowledgements=====+{{ https://imagizer.imageshack.com/img922/9683/0hAkoB.png }}
  
-A similar feature is to be found in [[http://moinmoin.wikiwikiweb.de/|MoinMoin Wiki]] and [[http://www.tiddlywiki.com/|TiddlyWiki]]. The styles are based on those developed for the [[http://www.ee.surrey.ac.uk/Teaching/Unix/|UNIX Tutorial for Beginners]] by Michael Stonebank. The plugin and its documentation are based on lessons learned from the [[http://wiki.splitbrain.org/plugin:tutorial|Plugin Tutorial]] and Christopher Smith's implementation and documentation of the [[http://wiki.splitbrain.org/plugin:boxes|boxes plugin]].+Custom prompts, when there are no satisfying defaults 
 +  <cli prompt="SQL> " comment="--" continue="/^\s*[0-9]{1,4}  /"> 
 +  SQL> select 
 +      * --comment 
 +     3  from 
 +      dual; 
 +   
 +  D 
 +  - 
 +  X 
 +   
 +  SQL> 
 +  </cli>
  
-Several improvements suggested by Stephane Chazelas and Andy Webber.+{{ https://imagizer.imageshack.com/img924/3301/IAyO54.png }}
  
 +===== Syntax =====
  
 +The full syntax:
  
 +  <cli prompt='some prompt' continue='continue prompt' comment='comment' type=shortcut class class...>
 +  session transcript
 +  </cli>
  
 +  * //prompt// --- [optional] the prompt used by the CLI. See configuration below for the description of the syntax. If omitted, the default value is used.
 +  * //continue// --- [optional]. The continue prompt used by the CLI. See configuration below for the description of the syntax. If ommitted, the default value is used. A continue prompt is an alternate prompt that can only happen **immediately after** a line containing a prompt, of any kind.\\ **NB**\\ //continue// can be abbreviated //cont//.
 +  * //comment// --- [optional] the comment used by the CLI. See configuration below for the description of the syntax. If omitted, the default value is used.
 +  * //shortcut// --- [optional] This is the name of a shortcut, i.e. a predefined set of //prompt//, //continue// and //comment//. It is not an error to use an undefined shortcut. It simply has no effect. Shortcuts are also useful to customize the visual aspect of the session since the shortcut name is added as classname in the html block.. See [[#Style]] below. \\ **NB**\\ one may write ''type=shortcut'', or ''t=shortcut'', ''language=shortcut'', ''lang=shortcut'', ''lng=shortcut'', ''l=shortcut''.
 +  * //class// --- [optional] Class to add to the html block. This allows to create all sorts of custom [[#style]].
  
 +Each of the values may be enclosed in single (''%%'%%'') or double (''%%"%%'') quote if they have spaces or tabs in them.
  
 +Within single quotes, «\\» stands for «\» and «\'%%%%» stands for «%%'%%».
  
 +Within double quotes, «\\» stands for «\» and «%%\"%%» stands for «%%"%%».
  
 +In unquoted strings, the following characters must be \-escaped to be part of the string: «''%%"%%''», «''%%'%%''», «'' ''» (space), «''=''», «''\''» and «''>''».
  
-===== Syntax =====+There may be spaces around the equal sign. 
 +The following examples are all equivalent: 
 +  <cli comment=\"\ > 
 +  <cli comment  "\" "> 
 +  <cli 'comment'='" '>
  
-A simple Bash Shell interaction:+The opening ''**<cli ... >**'' must all appear on one line. The transcript contents can appear over as many lines as are needed.
  
-  <cli> +Values are always set in this orderdefault, shortcut, individual settingsI.eif you write something like 
-  user@host:~/somedir $ ls # List current directory +''<cli continue="%%===>%%" t=irb>'', the continue value is first set from defaults, then overridden by the shortcut and finally overridden by the value of the continue attribute. So in the end, in this example, continue="%%===>%%".
-  conf      lang         README        screen.gif  ui +
-  info.txt  manager.dat  renderer.php  syntax.php +
-  user@host:~/somedir $  +
-  </cli>+
  
-Should be rendered:+Default values match most unix shells use.
  
-<html> 
-<pre class="cli"> 
-<span class="cli_prompt">user@host:~/somedir $</span> <span class="cli_command">ls</span> <span class="cli_comment"># List current directory</span> 
-<span class="cli_output">conf      lang         README        screen.gif  ui</span> 
-<span class="cli_output">info.txt  manager.dat  renderer.php  syntax.php</span> 
-<span class="cli_prompt">user@host:~/somedir $</span> 
-</pre> 
-</html> 
  
-The full syntax:  +===== Configuration and Settings =====
  
-  <cli prompt='prompt ' comment='comment'> +The plugin has 9 configuration settings. The first 3 are the default prompts and comment marker to use.
-  transcript +
-  </cli>+
  
-  * //prompt// --- [optional] the prompt used by the CLI. ((In practice only the final character is needed as everything on the line up to the final character will be taken to be part of the prompt and everything after will be regarded as a command.)) If no prompt is given, '(note the space) is assumed (('' is the standard Bash shell prompt))+  * //prompt// is a either a regexp((enclosed in <<''/''>>, <<''|''>>, <<''=''>>, <<'',''>>, <<'';''>>, <<''%''>>, <<''@''>>, <<''=''>>, or <<''#''>>)) that describe the prompt your CLI uses, or a string that should be equal the last char or chars of the prompt. When a regexp is used, it must match from the start of line to the last char considered part of the prompt. I.e. ''%%/^.*?> /%%'' is correctbut <del>''%%/> /%%''</del> is not. Note that ''"> "'' is correct as it is a string and not a regexp
-  * //comment// --- [optional] the comment string used by the CLIIf ommitted, '#' is assumed (('#' is the standard Bash shell comment character))+  * //continue// is a description of the continue promptIt uses the same syntax as //prompt//
-  * //continue// -- [optional] the prompt used for continuation markers: regex '/^> /' is the default. +  * //comment// is a description of the comment marker. Also a string or regexp. It is matched only to the part of each transcript lines **after** the prompt. For this regexp, the start of line is the first non-prompt char of a command line.
-  *   The defaults above match Bourne shell ${PS1} and ${PS2} prompts and comments+
  
-The opening ''**<cli ... >**'' must all appear on one line.  The transcript contents can appear over as many lines as are needed.+If any of these is empty, then the cli plugin uses a harcoded internal value that corresponds to the defaults shown below.
  
-See the plugin in action [[http://www.cpjobling.org.uk/dokuwiki/test:cli|here]]The sample page shows examples from number of CLI interfaces, including various shells, dynamic language interpreters and Matlab.+Default values: 
 +  * //prompt// ''/^.{0,30}?[$%>#](?:$|\s)/''\\ That is the shortest string of at most 30 chars that ends in ''$'', ''%'', ''>'' or ''#'' followed by a space or end of line. 
 +  * //continue// ''/^.{0,30}?>(?:$|\s)/''\\ That is the shortest string of at most 30 char that ends in ''>'' followed by a space or end of line. 
 +  * //comment// ''/(?:^#)|\s#/''\\ That is either sharp sign at start of line (right after the prompt) or a space followed by a sharp sign.
  
-===== Configuration =====+The next three settings (namedprompt, namedcontinue, namedcomment) are used to create named shortcuts. Although default values will work for most of the unix CLI, there are some situations when those defaults won't work. For example, the irb [[http://www.ruby-lang.org/|ruby]] shell needs special values for //prompt//, //continue// and //comment//. Instead of typing each time ''<cli prompt='/^(>|irb.*?:\d+:\d+|(?:ruby-?)?\d.*?:\d+\s)>\s/' continue='@^(\?>|irb.*?:\d+:\d+[]\'"/`*>]|(?:ruby-?)?\d.*?:\d+[]\'"/`?]>)\s@' comment='/#(?!{)/' >'', which is tedious and error-prone, you may use the previously created irb shortcut: ''<cli t=irb>''. Shortcuts define some replacements for //prompt//, //continue// and //comment//. The syntax to define shortcut is this:
  
-The plugin has no configuration settings, although you may want to review the default colour scheme in [[#style.css]] to ensure it is appropriate for your wiki.+  name:regexp-or-string
  
 +It is not necessary that values are given for the three of them. The defaults below define shortcuts for interactive Ruby (irb), Cisco-like CLI (nospace), MS-DOS and powershell (dos), and Python (python).
  
 +  * //namedprompt//<code>
 +irb:/(?x) ^ ( > | irb.*?:\d+:\d+ | (?: ruby-?)? \d.*?:\d+\s )>\s /
 +nospace:/^.{0,30}?[$%>#]/
 +dos:/^[A-Z]:.{0,28}?>/
 +</code>
 +  * //namedcontinue//<code>
 +irb:@(?x) ^ ( \?> | irb.*?:\d+:\d+[]'"/`*>] | (?: ruby-?)? \d.*?:\d+[]'"/`?]> )\s @
 +R:/^\+ /
 +python:...
 +nospace:undef continue
 +</code>
 +  * //namedcomment//<code>
 +irb:/#(?!{)/
 +dos:/^\s*rem(\s+|$)/
 +</code>
  
-===== Installation ===== +As you can see, for python, you only have to define a //continue// replacement, as the standard prompt definition will also catch the python prompt. irb needs to replace all the three definitions. The continue prompt ''undef continue'' is not special at all; this definition replaces the standard definition and will (most probably) never happen in real life. So it actually disables the continue prompt.
-==== Sources: ====+
  
-  * Current: [[http://github.com/downloads/cpjobling/plugin-cli/plugin-cli.zip|zip format (5k)]], [[http://github.com/downloads/cpjobling/plugin-cli/plugin-cli.zip|tar.gz format (4k)]]+The last set of configuration options, //odtbackground////odtborderwidth// and //odtbordercolor// are used when exporting to odt with the help of the [[odt]] plugin.
  
-If your wiki uses the [[plugin:plugin|plugin manager]] you can use them with the links above to install the plugin.  +==== odt support ====
  
-To install the plugin manuallydownload the source to your plugin folder''lib/plugins'' and extract its contents That will create new plugin folder''lib/plugins/cli'', and install the plugin.+When exporting a page to odt with the [[odt]] plugin, font colours are taken from the CSS **default** style. Computer output is blueprompt is green, user input red and comments orangeYou may change the default colours for both html and odt by [[#style|creating custom style]]The background, and background border are controlled by the 3 configuration options //odtbackground//, //odtborderwidth// and //odtbordercolor//.
  
-The folder will contain+  * //odtbackground// is the Background colour. By default, the same colour as the one used for //<code>// or //<file>// is used''#f7f9fa''. 
-  style.css                              all the cli styles +  * //odtborderwidth// is the border widthby default, the same as //<code>// or //<file>//: ''0.06pt'' 
-  syntax.php                             plugin script+  * //odtbordercolor// is the border colourby default, the same as //<code>// or //<file>//: ''#8cacbb''
  
-The plugin is now installed.+==== style ====
  
 +You may customize the colours of the text and also the border, background, padding, margin, font etc... well, pretty much anything customizable with CSS. Where to create your CSS files is documented in [[devel:css#user_styles|user styles]]. This example should work with most dokuwiki [[:template|templates]].
  
 +<code css userstyle.css>
 +#dokuwiki__content pre.cli {
 +  /* This changes the default style */
 +  background-color: #000;
 +  span.cli_prompt  { color: red }
 +  span.cli_command { color: white }
 +  span.cli_comment { color: grey }
 +  span.cli_output  { color: yellow }
 +  /* retro green and retro green with prompt in reverse video */ 
 +  &.retro-green, div.retro-green {
 +    background-color: #1e2e15;
 +    span.cli_prompt  { color: #0bc81b }
 +    span.cli_command { color: #8fe35f }
 +    span.cli_comment { color: #69a845 }
 +    span.cli_output  { color: #93eb60 }
 +    &.reverse {
 +      span.cli_prompt  { color: #1e2e15; background-color: #93eb60; font-weight: bold }
 +    }
 +  }
 +  /* retro ochre and retro ochre with prompt in reverse video */ 
 +  &.retro-ochre, div.retro-ochre {
 +    background-color: #2b2514;
 +    span.cli_prompt  { color: #ff8a11 }
 +    span.cli_command { color: #ffbf0e }
 +    span.cli_comment { color: #a87f0d }
 +    span.cli_output  { color: #de7407 }
 +    &.reverse {
 +      span.cli_prompt  { color: #2b2514; background-color: #de7407; font-weight: bold }
 +    }
 +  }
 +  /* Just for fun, but should be useless */
 +  &.uppercase, div.uppercase {
 +    text-transform: uppercase;
 +  }
 +}
 +</code>
  
 +Here are some examples
 +  <cli>
 +  me@mycomputer (0) $ ls # list files
 +  README i prompt-plugin.txt
 +  basic-usage.png lang style.css
 +  conf plugin.info.txt syntax.php
 +  dos-example.png prompt-examples.txt
 +  </cli>
  
 +{{ https://imagizer.imageshack.com/img921/2705/8iB6AN.png }}
  
 +  <cli green uppercase>
 +  me@mycomputer (0) $ ls -adtr * # list files with weid args
 +  README i prompt-plugin.txt
 +  basic-usage.png lang style.css
 +  conf plugin.info.txt syntax.php
 +  dos-example.png prompt-examples.txt
 +  </cli>
  
-===== Details =====+{{ https://imagizer.imageshack.com/img922/3950/HWLFjg.png }}
  
-==== syntax.php ====+  <cli type=dos retro-ochre reverse> 
 +  C:\>dir 
 +   Volume in drive C is mu/drives/
 +   Directory of C:\ 
 +  TMP                  <DIR>  12-28-17  6:29a 
 +  CONFIG   SYS           346  06-22-15  1:17a 
 +  AUTOEXEC BAT           556  06-22-15  1:17a 
 +           2 file(s)            902 bytes 
 +           1 dir(s)   2,147,450,880 bytes free 
 +  C:\>echo pouet 
 +  pouet 
 +  C:\>rem This is a remark and does nothing 
 +  </cli>
  
-<code php> +{{ https://imagizer.imageshack.com/img924/6294/Jqebtx.png }}
-<?php +
-/** +
- * Command Line Interface (CLI) Plugin +
-     Typeset transcripts of interactive sessions with mimumum effort. +
- * Syntax: +
-     <cli prompt="$ " continue="> " comment="#"> +
-     user@host:~/somedir $ ls \ +
-     > # List directory +
-     file1 file2 +
-     </cli> +
-   prompt --- [optional] prompt character used. '$ ' is default - note the space. +
-   comment --- [optional] comment character used. '#' is default - note no space. +
-   continue --- [optional] regex of shell continuation '/^> /' is the default. +
-   The defaults above match Bourne shell ${PS1} and ${PS2} prompts and comment +
- * +
- * Acknowledgements: +
-  Borrows heavily from the boxes plugin! +
-  Support for continuation added by Andy Webber +
-  Improved parsing added by Stephane Chazelas +
- *  +
- * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html) +
- * @author     Chris P. Jobling <C.P.Jobling@Swansea.ac.uk> +
- */ +
-  +
-if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); +
-if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); +
-require_once(DOKU_PLUGIN.'syntax.php'); +
-  +
-/** +
- * All DokuWiki plugins to extend the parser/rendering mechanism +
- * need to inherit from this class +
- */ +
-class syntax_plugin_cli extends DokuWiki_Syntax_Plugin { +
-     +
-    var $prompt_str = '$ '; +
-    var $prompt_cont = '/^> /'; // this is a regex +
-    var $prompt_continues = false; +
-    var $comment_str = '#'; +
-     +
-    /** +
-     * return some info +
-     */ +
-    function getInfo(){ +
-        return array( +
-            'author' => 'Chris P. Jobling; Stephan Chazelas; Andy Webber', +
-            'email'  => 'C.P.Jobling@Swansea.ac.uk', +
-            'date'   => '2008-13-02', +
-            'name'   => 'Command Line Interface (CLI) Plugin', +
-            'desc'   => 'Renders transcripts of command line interactions, e.g. for shell and dynamic language interpretor tutorials', +
-            'url'    => 'http://eehope.swan.ac.uk/dokuwiki/plugins:cli', +
-        ); +
-    } +
-  +
-    /** +
-     * What kind of syntax are we? +
-     */ +
-    function getType(){ +
-        return 'protected'; +
-    } +
-  +
-    /** +
-     * What kind of syntax do we allow (optional) +
-     */ +
-//    function getAllowedTypes() { +
-//        return array(); +
-//    } +
-  +
-    // override default accepts() method to allow nesting +
-    // - ie, to get the plugin accepts its own entry syntax +
-    function accepts($mode) { +
-        if ($mode == substr(get_class($this), 7)) return true; +
-        return parent::accepts($mode); +
-    } +
-     +
-    /** +
-     * What about paragraphs? (optional) +
-     */ +
-    function getPType(){ +
-        return 'block'; +
-    } +
-  +
-    /** +
-     * Where to sort in? +
-     */  +
-    function getSort(){ +
-        return 601; +
-    } +
-  +
-  +
-    /** +
-     * Connect pattern to lexer +
-     */ +
-    function connectTo($mode) { +
-         $this->Lexer->addEntryPattern('<cli(?:[)]?'+
-             '"(?:\\\\.|[^\\\\"])*"' .     /* double-quoted string */ +
-             '|\'(?:\\\\.|[^\'\\\\])*\'' . /* single-quoted string */ +
-             '|\\\\.' .                    /* escaped character */ +
-             '|[^\'"\\\\>]|[(?:])*>\r?\n?(?=.*?</cli>)',$mode,'plugin_cli'); +
-         /* +
-          * The [)]? and |[(?:] is to work around a bug in lexer.php +
-          * wrt nested (...) +
-         */ +
-    } +
-  +
-    function postConnect() { +
-       $this->Lexer->addExitPattern('\r?\n?</cli>','plugin_cli'); +
-    } +
-  +
-  +
-    /** +
-     * Handle the match +
-     */ +
-    function handle($match, $state, $pos, &$handler){ +
-        switch ($state) { +
-          case DOKU_LEXER_ENTER : +
-            $args = substr($match, 4, -1); +
-            return array($state, $args); +
-          case DOKU_LEXER_MATCHED : +
-            break; +
-          case DOKU_LEXER_UNMATCHED : +
-            return array($state, $match); +
-          case DOKU_LEXER_EXIT : +
-            return array($state, ''); +
-          case DOKU_LEXER_SPECIAL : +
-            break; +
-        } +
-        return array(); +
-    } +
-  +
-    /** +
-     * Create output +
-     */ +
-    function render($mode, &$renderer, $data) { +
-        if($mode == 'xhtml'){ +
-             list($state, $match) = $data; +
-             switch ($state) { +
-             case DOKU_LEXER_ENTER : +
-                 $args = $match; +
-                 $this->_process_args($args); +
-                 $renderer->doc .= '<pre class="cli">'; +
-                 break; +
-             case DOKU_LEXER_UNMATCHED :  +
-                 $this->_render_conversation($match, $renderer); +
-                 break; +
-             case DOKU_LEXER_EXIT : +
-                 $renderer->doc .= "</pre>"; +
-                 break; +
-             } +
-             return true; +
-        } +
-        return false; +
-    } +
-  +
-     function _extract($args, $param) { +
-         /* +
-          * extracts value from $args for $param +
-          * xxx = "foo\"bar"  -> foo"bar +
-          * xxx = a\ b        -> a b +
-          * xxx = 'a\' b'     -> a' b +
-          * +
-          * returns null if value is empty. +
-          */ +
-         if (preg_match("/$param" . '\s*=\s*('+
-             '"(?:\\\\.|[^\\\\"])*"' .     /* double-quoted string */ +
-             '|\'(?:\\\\.|[^\'\\\\])*\'' . /* single-quoted string */ +
-             '|(?:\\\\.|[^\\\\\s])*' .     /* escaped characters */ +
-             ')/', $args, $matches)) { +
-             switch (substr($matches[1], 0, 1)) { +
-             case "'": +
-                 $result = substr($matches[1], 1, -1); +
-                 $result = preg_replace('/\\\\([\'\\\\])/', '$1', $result); +
-                 break; +
-             case '"': +
-                 $result = substr($matches[1], 1, -1); +
-                 $result = preg_replace('/\\\\(["\\\\])/', '$1', $result); +
-                 break; +
-             default: +
-                 $result = preg_replace('/\\\\(.)/', '$1', $matches[1]); +
-             } +
-             if ($result != ""+
-                 return $result; +
-         } +
-     } +
-  +
-     function _process_args($args) { +
-         // process args to CLI tag: sets $comment_str and $prompt_str +
-         if (!is_null($prompt = $this->_extract($args, 'prompt'))) +
-             $this->prompt_str = $prompt; +
-         if (!is_null($comment = $this->_extract($args, 'comment'))) +
-             $this->comment_str = $comment; +
-     } +
-     +
-    function _render_conversation($match, &$renderer) { +
-        $prompt_continues = false; +
-        $lines = preg_split('/\n\r|\n|\r/',$match); +
-        if ( trim($lines[0]) == "" ) unset( $lines[0] ); +
-        if ( trim($lines[count($lines)]) == "" ) unset( $lines[count($lines)] ); +
-        foreach ($lines as $line) { +
-            $index = strpos($line, $this->prompt_str); +
-            if ($index === false) {    +
-                if ($this->prompt_continues) { +
-                  if (preg_match($this->prompt_cont, $line, $promptc) === 0) $this->prompt_continues = false; +
-                } +
-                if ($this->prompt_continues) { +
-                    // format prompt +
-                    $renderer->doc .= '<span class="cli_prompt">' . $renderer->_xmlEntities($promptc[0]) . "</span>"; +
-                    // Split line into command + optional comment (only end-of-line comments supported) +
-                    $command =  preg_split($this->prompt_cont, $line); +
-                    $commands = explode($this->comment_str, $command[1]); +
-                    // Render command +
-                    $renderer->doc .= '<span class="cli_command">' . $renderer->_xmlEntities($commands[0]) . "</span>"; +
-                    // Render comment if there is one +
-                    if ($commands[1]) { +
-                        $renderer->doc .= '<span class="cli_comment">'+
-                            $renderer->_xmlEntities($this->comment_str . $commands[1]) . "</span>"; +
-                  } +
-                  $renderer->doc .= DOKU_LF; +
-                } else { +
-                  // render as output +
-                  $renderer->doc .= '<span class="cli_output">' . $renderer->_xmlEntities($line) . "</span>" . DOKU_LF; +
-                  $this->prompt_continues=false; +
-                } +
-            } else { +
-                $this->prompt_continues = true; +
-                // format prompt +
-                $prompt = substr($line, 0, $index) . $this->prompt_str; +
-                $renderer->doc .= '<span class="cli_prompt">' . $renderer->_xmlEntities($prompt) . "</span>"; +
-                // Split line into command + optional comment (only end-of-line comments supported) +
-                $commands = explode($this->comment_str, substr($line, $index + strlen($this->prompt_str))); +
-                // Render command +
-                 $renderer->doc .= '<span class="cli_command">' . $renderer->_xmlEntities($commands[0]) . "</span>"; +
-                // Render comment if there is one +
-                if ($commands[1]) { +
-                     $renderer->doc .= '<span class="cli_comment">'+
-                        $renderer->_xmlEntities($this->comment_str . $commands[1]) . "</span>"; +
-                } +
-                 $renderer->doc .= DOKU_LF; +
-            } +
-        } +
-    } +
-+
-//Setup VIM: ex: et ts=4 enc=utf-8 sw=4 : +
-?>+
  
-</code>+----
  
 +A few technical details.
  
-==== style.css ====+''<cli>'' are rendered as html ''<pre>'' tags with ''cli'' class. Classe from the ''type'' attribute is added as well as optional classes.
  
-These may be modified to suit your own requirements.+  <cli type=dos retro-ochre reverse> 
 +is rendered as 
 +<code html> 
 +<pre class="cli dos  retro-ochre reverse"> 
 +<!--        ^       additional classes 
 +            |   | 
 +            |   +---- type=dosadded as a class 
 +            | 
 +            +-------- class cli is always present 
 +--> 
 +</code>
  
-<code css> +A full example:
-/* plugin:cli */+
  
-.cli_output { +<code xml> 
- colorblue; +<cli retro-ochre> 
-}+me@mycomputer (0) $ dosemu # let's go back in the 80s 
 +<cli type=dos> 
 +C:>rem This is a remark and does nothing 
 +C:>echo pouet 
 +pouet 
 +C:>exit 
 +</cli> 
 +me@mycomputer (0) $  
 +</cli> 
 +</code>
  
-.cli_comment { +Will produce this html code
- color: brown; +
-}+
  
-.cli_prompt { +<code html> 
- colorgreen+<pre class="cli   retro-ochre"> 
-}+<span class="cli_prompt">me@mycomputer (0) $ </span><span class="cli_command">dosemu</span><span class="cli_comment"> # let's go back in the 80s</span> 
 +<div class="dos "> 
 +<span class="cli_prompt">C:&gt;</span><span class="cli_comment">rem This is a remark and does nothing</span> 
 +<span class="cli_prompt">C:&gt;</span><span class="cli_command">echo pouet</span> 
 +<span class="cli_output">pouet</span> 
 +<span class="cli_prompt">C:&gt;</span><span class="cli_command">exit</span> 
 +</div> 
 +<span class="cli_prompt">me@mycomputer (0) $ </span><span class="cli_command">exit</span> 
 +</pre> 
 +</code>
  
-.cli_command { 
- color: red; 
-} 
  
-// nested CLI +===== Development =====
-pre.cli pre.cli { +
- background-color: #F8F8F8; +
-}+
  
-/* end plugin:cli */ 
-</code> 
  
 +=== Change Log ===
  
-===== Revision History =====+{{rss>https://github.com/schplurtz/dokuwiki-plugin-cli/commits/master.atom date}}
  
-  * 2007-09-10 --- First release 
-  * 2008-13-02 --- Several improvements to lexing and white space handling as suggested by Stephane Chazelas; continuation prompt support and whitespace handling improvements added by Any Webber. 
-  * 2010-07-08 --- Hosting moved to GitHub. Note, hosting of examples at http://www.cpjobling.org.uk may not be stable. 
  
-===== To Do ===== +=== ToDo/Wish List ===
-  +
-  * It would be nice to support end of line markers for long command strings +
-  * Doesn't work for transcripts that have no prompt: e.g. executable scripts. Better to use the ''<code>'' tag for these.+
  
-===== Bugs =====+Please, create an issue on [[https://github.com/schplurtz/dokuwiki-plugin-cli/issues|GitHub]].
  
-  * Care needed for CLI's that use > to end a prompt -- done. +===== FAQ =====
-  * Doesn't handle continuation prompts in shells that allow multi-line input. -- done (thanks to Andy Webber) +
-  * Ruby's multiple output line decorators are likely to cause problems. +
-  * Bound to be others ... this is my first DokuWiki plugin (actually it's my first attempt at PHP coding ^_^) +
-  * following codes don't display right +
-<code> +
-<cli prompt="#"> +
-# rpm -ivh darcs-1.0.9-3.fc6.i386.rpm +
-Preparing...                ########################################### [100%] +
-   1:darcs                  ########################################### [100%] +
-</cli> +
-</code>+
  
-===== Comments Please ===== 
  
-I modified the **_render_conversation** function to //remove// first and last blank lines, otherwise it produces two //blank excess output spans//. +===== Discussion =====
-<code php> +
-if ( trim($lines[0]) == "" ) unset( $lines[0] ); +
-if ( trim($lines[count($lines)]) == "" ) unset( $lines[count($lines)] ); +
-</code> +
-This is just a quick hack and needs more afterthought, of course.+
  
-I also did not like the "empty lines got removed" effectthus the **preg_split** line was also modified:+Pleasediscuss on the [[https://forum.dokuwiki.org/|forum]].
  
-<code php> 
-$lines = preg_split('/\n\r|\n|\r/',$match); 
-</code> 
  
-The previous comment still applies.+----
  
-Added to new release ... CPJ 
  
-I would like to be able to have multiple prompts for routers for example where the prompt changes depending +===== Acknowledgements =====
-on configuration mode:+
  
-<code> +This plugin was originally written in 2007 by [[https://github.com/cpjobling|Chris P. Jobling]]. He was inspired by a similar feature found in [[http://moinmo.in/|MoinMoin Wiki]] and [[http://tiddlywiki.com/|TiddlyWiki]]. The styles were based on those developed for the [[http://www.ee.surrey.ac.uk/Teaching/Unix/|UNIX Tutorial for Beginners]] by Michael Stonebank. The plugin and its documentation were based on lessons Chris learned from the [[tutorial|Plugin Tutorial]] and Christopher Smith's implementation and documentation of the [[box|boxes plugin]].
-router>enable +
-router#conf t +
-router(conf)# +
-</code>+
  
-It should be noted that after install by plugin manager plugin folder should be renamed from "plugin-cli" to "cli"at least for me (Version of dokuwiki Busy  Wednesday). +Several improvements suggested by Stephane Chazelas and Andy Webber.
- +
-One more : to make background color effective, change line in style.css +
-<code css> +
-pre.cli pre.cli { +
-</code> +
-to +
-<code css> +
-div.dokuwiki pre.cli { +
-</code>+
  
 +More than 12 years after its creation, this plugin was modernized by Schplurtz le Déboulonné who added a few features.
  
  
plugin/cli.1359119476.txt.gz · Last modified: 2013-01-25 14:11 by 141.65.129.192

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