Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédente | |||
logiciel:internet:dokuwiki:plugins:cli:start [2022/08/25 20:46] – [CLI : plugin Command Line Interface pour Dokuwiki] admin | logiciel:internet:dokuwiki:plugins:cli:start [2022/10/01 23:26] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | {{tag> | ||
+ | ====== cli : plugin Command Line Interface pour Dokuwiki ====== | ||
+ | |||
+ | Ce plugin formate la sortie d'une ligne de commande, par exemple pour un manuel d' | ||
+ | Il est conçu pour fonctionner avec la sortie d'un shell Unix standard, mais devrait convenir pour documenter d' | ||
+ | |||
+ | Sa conception permet d' | ||
+ | |||
+ | On peut ajuster le style de l' | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | ===== Pré-requis ===== | ||
+ | |||
+ | On suppose que : | ||
+ | * toutes les commandes utilisateur commencent par une invite | ||
+ | * l' | ||
+ | * les commandes utilisateur suivent l' | ||
+ | * les lignes qui ne commencent pas par une invite sont des sorties de CLI ((c' | ||
+ | |||
+ | <WRAP center round important 60%> | ||
+ | **Avec PHP 7** | ||
+ | |||
+ | Pour les plugins qui donnent des erreurs, | ||
+ | éditez le fichier **lib/ | ||
+ | * remplacez tous les **& | ||
+ | * remplacez tous les **& | ||
+ | |||
+ | comme indiqué dans les messages d' | ||
+ | </ | ||
+ | |||
+ | ===== Installation ===== | ||
+ | |||
+ | Le code source de ce plugin est hébergé chez GitHub à [[https:// | ||
+ | |||
+ | Pour installer le plugin lorsque **git** est installé sur votre serveur DokuWiki, il vous suffit de lancer depuis votre serveur : | ||
+ | * < | ||
+ | $ git clone git:// | ||
+ | |||
+ | Le dossier contiendra : | ||
+ | ; style.css | ||
+ | : tous les styles pour cli | ||
+ | ; syntax.php | ||
+ | : plugin script | ||
+ | |||
+ | Le plugin est maintenant installé. | ||
+ | |||
+ | Details | ||
+ | |||
+ | <code php syntax.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 style.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 */ | ||
+ | </ | ||
+ | |||
+ | ===== Configuration ===== | ||
+ | |||
+ | Le plugin n'a pas de paramètres de configuration, | ||
+ | |||
+ | ===== Utilisation ===== | ||
+ | |||
+ | ==== Syntaxe ==== | ||
+ | |||
+ | Une interaction Bash simple : | ||
+ | <code - > | ||
+ | <cli> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | s' | ||
+ | <cli> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | |||
+ | Syntaxe complète : | ||
+ | |||
+ | <code - > | ||
+ | <cli prompt=' | ||
+ | transcript | ||
+ | </ | ||
+ | </ | ||
+ | s' | ||
+ | <cli prompt=' | ||
+ | transcript | ||
+ | </ | ||
+ | ; prompt — [facultatif] | ||
+ | : invite utilisée par CLI. ((En pratique, seul le caractère final est nécessaire, | ||
+ | ; comment — [facultatif] | ||
+ | : chaîne de commentaire utilisée par CLI. Si omis, '#' | ||
+ | ; continue – [facultatif] | ||
+ | : invite utilisée pour les marqueurs de continuation: | ||
+ | * Les valeurs par défaut ci-dessus correspondent aux invites et commentaires $ {PS1} et $ {PS2} de Bourne shell | ||
+ | |||
+ | Le %%<cli … >%% du début doit apparaître sur une seule ligne. Le contenu de la transcription peut apparaître sur autant de lignes que nécessaire. | ||
+ | |||
+ | ==== Exemples ==== | ||
+ | |||
+ | Cette page fournit un ensemble de tests pour cli et sert également d' | ||
+ | |||
+ | === Script shell de base === | ||
+ | |||
+ | Texte : | ||
+ | <code - > | ||
+ | <cli> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | 55 108 1032 info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | <cli> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | 55 108 1032 info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | |||
+ | === Script shell avec commentaires === | ||
+ | |||
+ | Texte : | ||
+ | <code - > | ||
+ | <cli comment="#"> | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === Script shell root avec commentaires === | ||
+ | |||
+ | Texte : (caractère de commentaire shell par défaut): | ||
+ | <code - > | ||
+ | <cli prompt="#"> | ||
+ | root@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | root@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | <cli prompt="#"> | ||
+ | root@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | root@host: | ||
+ | </ | ||
+ | |||
+ | Ceci est également valable : | ||
+ | <code - > | ||
+ | <cli prompt="# | ||
+ | root@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | root@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <cli prompt="# | ||
+ | root@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | root@host: | ||
+ | </ | ||
+ | |||
+ | Exemple avec une invite de continuation: | ||
+ | |||
+ | <code - > | ||
+ | <cli prompt=" | ||
+ | user@host: | ||
+ | > # List directory | ||
+ | file1 file2 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <cli prompt=" | ||
+ | user@host: | ||
+ | > # List directory | ||
+ | file1 file2 | ||
+ | </ | ||
+ | |||
+ | === Script shell avec commentaires === | ||
+ | |||
+ | <code - > | ||
+ | <cli prompt=" | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | user@host: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === Fenêtre de commande Windows === | ||
+ | |||
+ | Texte : | ||
+ | <code - > | ||
+ | <cli prompt=">" | ||
+ | C: | ||
+ | C: | ||
+ | 'hello world!' | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | <cli prompt=">" | ||
+ | C: | ||
+ | C: | ||
+ | 'hello world!' | ||
+ | </ | ||
+ | |||
+ | === Ruby irb === | ||
+ | |||
+ | Une implémentation simple ne fonctionnera pas pour les résultats car la fin de l' | ||
+ | |||
+ | <code - > | ||
+ | <cli prompt=">"> | ||
+ | irb(main): | ||
+ | => 4 | ||
+ | irb(main): | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === Python === | ||
+ | |||
+ | <code - > | ||
+ | <cli prompt=">>>"> | ||
+ | ActivePython 2.5.1.1 (ActiveState Software Inc.) based on | ||
+ | Python 2.5.1 (r251: | ||
+ | win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | 4 | ||
+ | >>> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === Python + Windows Shell (Nested CLIs) === | ||
+ | |||
+ | Texte : | ||
+ | <code - > | ||
+ | <cli prompt=">"> | ||
+ | C: | ||
+ | <cli prompt=">>>"> | ||
+ | ActivePython 2.5.1.1 (ActiveState Software Inc.) based on | ||
+ | Python 2.5.1 (r251: | ||
+ | win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | 4 | ||
+ | >>> | ||
+ | |||
+ | </ | ||
+ | C: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Résultat : | ||
+ | <cli prompt=">"> | ||
+ | C: | ||
+ | <cli prompt=">"> | ||
+ | ActivePython 2.5.1.1 (ActiveState Software Inc.) based on | ||
+ | Python 2.5.1 (r251: | ||
+ | win32 | ||
+ | Type " | ||
+ | >>> | ||
+ | 4 | ||
+ | >>> | ||
+ | |||
+ | </ | ||
+ | C: | ||
+ | </ | ||
+ | |||
+ | === Cas particuliers === | ||
+ | |||
+ | * Les codes suivants ne s' | ||
+ | < | ||
+ | <cli prompt="#"> | ||
+ | # rpm -ivh darcs-1.0.9-3.fc6.i386.rpm | ||
+ | Preparing... | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <cli prompt="#"> | ||
+ | # rpm -ivh darcs-1.0.9-3.fc6.i386.rpm | ||
+ | Preparing... | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Pas sûr de pouvoir résoudre ce problème car le marqueur de progression de téléchargement utilise le même caractère que l' | ||
+ | |||
+ | * CLI ne doit pas supprimer silencieusement des lignes blanches intentionnelles: | ||
+ | |||
+ | <cli prompt=" | ||
+ | user@host: | ||
+ | conf lang | ||
+ | info.txt | ||
+ | |||
+ | |||
+ | user@host: | ||
+ | </ | ||
+ | |||
+ | ===== Désinstallation ===== | ||
+ | |||
+ | ===== Voir aussi ===== | ||
+ | |||
+ | * **(fr)** [[https:// | ||
+ | * **(en)** [[http:// | ||
+ | * **(en)** [[https:// | ||
+ | * **(en)** [[http:// | ||
+ | * **(fr)** [[https:// | ||
+ | |||
+ | ---- | ||
+ | //Basé sur << [[http:// |