plugins:cli
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
plugins:cli [2007/09/19 22:25] – eechris | plugins:cli [2011/01/14 12:47] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Command Line Interface (CLI) Plugin | ||
+ | ---- plugin ---- | ||
+ | description: | ||
+ | author | ||
+ | email : C.P.Jobling@Swansea.ac.uk | ||
+ | type : syntax | ||
+ | lastupdate : 2010-08-07 | ||
+ | compatible : | ||
+ | depends | ||
+ | conflicts | ||
+ | similar | ||
+ | tags : code, cli, syntax, highlighting | ||
+ | ---- | ||
+ | |||
+ | This [[wiki: | ||
+ | |||
+ | The assumptions made are | ||
+ | - all user commands start with a prompt | ||
+ | - the CLI prompt will end in a recognizable character (typically ' | ||
+ | - 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!)) | ||
+ | |||
+ | 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 [[# | ||
+ | |||
+ | ==== Notes ==== | ||
+ | |||
+ | * My version of this page -- which could be more recently updated -- can be found [[http:// | ||
+ | |||
+ | * Since 7 August 2010 the source for this plugin code has been hosted at GitHub at http:// | ||
+ | |||
+ | cd ../ | ||
+ | git clone git:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | =====Acknowledgements===== | ||
+ | |||
+ | A similar feature is to be found in [[http:// | ||
+ | |||
+ | Several improvements suggested by Stephane Chazelas and Andy Webber. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Syntax ===== | ||
+ | |||
+ | A simple Bash Shell interaction: | ||
+ | |||
+ | <cli> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | |||
+ | Should be rendered: | ||
+ | |||
+ | < | ||
+ | <pre class=" | ||
+ | <span class=" | ||
+ | <span class=" | ||
+ | <span class=" | ||
+ | <span class=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | The full syntax: | ||
+ | |||
+ | <cli prompt=' | ||
+ | transcript | ||
+ | </ | ||
+ | |||
+ | * //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)). | ||
+ | * //comment// --- [optional] the comment string used by the CLI. If ommitted, '#' | ||
+ | * // | ||
+ | * The defaults above match Bourne shell ${PS1} and ${PS2} prompts and comments | ||
+ | |||
+ | The opening '' | ||
+ | |||
+ | See the plugin in action [[http:// | ||
+ | |||
+ | ===== Configuration ===== | ||
+ | |||
+ | The plugin has no configuration settings, although you may want to review the default colour scheme in [[# | ||
+ | |||
+ | |||
+ | |||
+ | ===== Installation ===== | ||
+ | ==== Sources: ==== | ||
+ | |||
+ | * Current: [[http:// | ||
+ | |||
+ | If your wiki uses the [[plugin: | ||
+ | |||
+ | To install the plugin manually, download the source to your plugin folder, '' | ||
+ | |||
+ | The folder will contain: | ||
+ | style.css | ||
+ | syntax.php | ||
+ | |||
+ | The plugin is now installed. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== Details ===== | ||
+ | |||
+ | ==== syntax.php ==== | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Command Line Interface (CLI) Plugin | ||
+ | | ||
+ | * Syntax: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | * Acknowledgements: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * @license | ||
+ | * @author | ||
+ | */ | ||
+ | |||
+ | if(!defined(' | ||
+ | if(!defined(' | ||
+ | require_once(DOKU_PLUGIN.' | ||
+ | |||
+ | /** | ||
+ | * All DokuWiki plugins to extend the parser/ | ||
+ | * need to inherit from this class | ||
+ | */ | ||
+ | class syntax_plugin_cli extends DokuWiki_Syntax_Plugin { | ||
+ | | ||
+ | var $prompt_str = '$ '; | ||
+ | var $prompt_cont = '/ | ||
+ | var $prompt_continues = false; | ||
+ | var $comment_str = '#'; | ||
+ | | ||
+ | /** | ||
+ | * return some info | ||
+ | */ | ||
+ | function getInfo(){ | ||
+ | return array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * What kind of syntax are we? | ||
+ | */ | ||
+ | function getType(){ | ||
+ | return ' | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 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), | ||
+ | return parent:: | ||
+ | } | ||
+ | | ||
+ | /** | ||
+ | * What about paragraphs? (optional) | ||
+ | */ | ||
+ | function getPType(){ | ||
+ | return ' | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Where to sort in? | ||
+ | | ||
+ | function getSort(){ | ||
+ | return 601; | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Connect pattern to lexer | ||
+ | */ | ||
+ | function connectTo($mode) { | ||
+ | | ||
+ | '" | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | /* | ||
+ | * The [)]? and |[(?:] is to work around a bug in lexer.php | ||
+ | * wrt nested (...) | ||
+ | */ | ||
+ | } | ||
+ | |||
+ | function postConnect() { | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * Handle the match | ||
+ | */ | ||
+ | function handle($match, | ||
+ | switch ($state) { | ||
+ | case DOKU_LEXER_ENTER : | ||
+ | $args = substr($match, | ||
+ | return array($state, | ||
+ | case DOKU_LEXER_MATCHED : | ||
+ | break; | ||
+ | case DOKU_LEXER_UNMATCHED : | ||
+ | return array($state, | ||
+ | case DOKU_LEXER_EXIT : | ||
+ | return array($state, | ||
+ | case DOKU_LEXER_SPECIAL : | ||
+ | break; | ||
+ | } | ||
+ | return array(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Create output | ||
+ | */ | ||
+ | function render($mode, | ||
+ | if($mode == ' | ||
+ | | ||
+ | | ||
+ | case DOKU_LEXER_ENTER : | ||
+ | $args = $match; | ||
+ | | ||
+ | | ||
+ | | ||
+ | case DOKU_LEXER_UNMATCHED : | ||
+ | | ||
+ | | ||
+ | case DOKU_LEXER_EXIT : | ||
+ | | ||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | | ||
+ | /* | ||
+ | * extracts value from $args for $param | ||
+ | * xxx = " | ||
+ | * xxx = a\ b -> a b | ||
+ | * xxx = ' | ||
+ | * | ||
+ | * returns null if value is empty. | ||
+ | */ | ||
+ | if (preg_match("/ | ||
+ | '" | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | | ||
+ | case "'": | ||
+ | | ||
+ | | ||
+ | | ||
+ | case '"': | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | if ($result != "" | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | // process args to CLI tag: sets $comment_str and $prompt_str | ||
+ | if (!is_null($prompt = $this-> | ||
+ | | ||
+ | if (!is_null($comment = $this-> | ||
+ | | ||
+ | } | ||
+ | | ||
+ | function _render_conversation($match, | ||
+ | $prompt_continues = false; | ||
+ | $lines = preg_split('/ | ||
+ | if ( trim($lines[0]) == "" | ||
+ | if ( trim($lines[count($lines)]) == "" | ||
+ | foreach ($lines as $line) { | ||
+ | $index = strpos($line, | ||
+ | if ($index === false) { | ||
+ | if ($this-> | ||
+ | if (preg_match($this-> | ||
+ | } | ||
+ | if ($this-> | ||
+ | // format prompt | ||
+ | $renderer-> | ||
+ | // Split line into command + optional comment (only end-of-line comments supported) | ||
+ | $command = preg_split($this-> | ||
+ | $commands = explode($this-> | ||
+ | // Render command | ||
+ | $renderer-> | ||
+ | // Render comment if there is one | ||
+ | if ($commands[1]) { | ||
+ | $renderer-> | ||
+ | $renderer-> | ||
+ | } | ||
+ | $renderer-> | ||
+ | } else { | ||
+ | // render as output | ||
+ | $renderer-> | ||
+ | $this-> | ||
+ | } | ||
+ | } else { | ||
+ | $this-> | ||
+ | // format prompt | ||
+ | $prompt = substr($line, | ||
+ | $renderer-> | ||
+ | // Split line into command + optional comment (only end-of-line comments supported) | ||
+ | $commands = explode($this-> | ||
+ | // Render command | ||
+ | | ||
+ | // Render comment if there is one | ||
+ | if ($commands[1]) { | ||
+ | | ||
+ | $renderer-> | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | //Setup VIM: ex: et ts=4 enc=utf-8 sw=4 : | ||
+ | ?> | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== style.css ==== | ||
+ | |||
+ | These may be modified to suit your own requirements. | ||
+ | |||
+ | <code css> | ||
+ | /* plugin:cli */ | ||
+ | |||
+ | .cli_output { | ||
+ | color: blue; | ||
+ | } | ||
+ | |||
+ | .cli_comment { | ||
+ | color: brown; | ||
+ | } | ||
+ | |||
+ | .cli_prompt { | ||
+ | color: green; | ||
+ | } | ||
+ | |||
+ | .cli_command { | ||
+ | color: red; | ||
+ | } | ||
+ | |||
+ | // nested CLI | ||
+ | pre.cli pre.cli { | ||
+ | background-color: | ||
+ | } | ||
+ | |||
+ | /* end plugin:cli */ | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Revision History ===== | ||
+ | |||
+ | * 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:// | ||
+ | |||
+ | ===== To Do ===== | ||
+ | |||
+ | * It would be nice to support end of line markers for long command strings | ||
+ | * Doesn' | ||
+ | |||
+ | ===== Bugs ===== | ||
+ | |||
+ | * Care needed for CLI's that use > to end a prompt -- done. | ||
+ | * Doesn' | ||
+ | * 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 | ||
+ | < | ||
+ | <cli prompt="#"> | ||
+ | # rpm -ivh darcs-1.0.9-3.fc6.i386.rpm | ||
+ | Preparing... | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Comments Please ===== | ||
+ | |||
+ | I modified the **_render_conversation** function to //remove// first and last blank lines, otherwise it produces two //blank excess output spans//. | ||
+ | <code php> | ||
+ | if ( trim($lines[0]) == "" | ||
+ | if ( trim($lines[count($lines)]) == "" | ||
+ | </ | ||
+ | This is just a quick hack and needs more afterthought, | ||
+ | |||
+ | I also did not like the "empty lines got removed" | ||
+ | |||
+ | <code php> | ||
+ | $lines = preg_split('/ | ||
+ | </ | ||
+ | |||
+ | The previous comment still applies. | ||
+ | |||
+ | Added to new release ... CPJ |
plugins/cli.txt · Last modified: 2011/01/14 12:47 by 127.0.0.1