├── composer.json ├── LICENSE ├── README.md └── indent /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cebe/indent", 3 | "description": "A small tool to convert text file indentation.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Carsten Brandt", 8 | "email": "mail@cebe.cc" 9 | } 10 | ], 11 | "require": {}, 12 | "bin": ["indent"] 13 | } 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Carsten Brandt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | indent 2 | ====== 3 | 4 | A small tool to convert (or enforce) text file indentation. Tabs to spaces or spaces to tabs. 5 | 6 | Installation 7 | ------------ 8 | 9 | Install via [composer](https://packagist.org/packages/cebe/indent) or just clone the repo. 10 | 11 | ### Global installation 12 | 13 | Install it globally using: 14 | 15 | composer global require cebe/indent 16 | 17 | Make sure your composer directory (`$HOME/.composer/vendor/bin`) is in your `PATH`. 18 | 19 | Usage 20 | ----- 21 | 22 | indent [--tabs|--spaces] [-r [--pattern=...]] [files or directories...] 23 | 24 | --tabs convert all indentation to tabs. Assuming 4 spaces tab length. 25 | --spaces convert all indentation to spaces. 26 | 27 | -r recursively go over all directories given as argument and convert 28 | files that match --pattern. 29 | 30 | --pattern the pattern to match files for when using -r. Defaults to '*.php'. 31 | 32 | --tabstop=N define number of spaces N to replace a tab with. Defaults to 4. 33 | 34 | --help shows this usage information. 35 | 36 | If no file is specified input will be read from STDIN. 37 | 38 | Examples 39 | -------- 40 | 41 | Convert `myfile.php` to tabs: 42 | 43 | indent --tabs myfile.php 44 | 45 | Convert all `.php`-files and the `README.md` in current dir to spaces: 46 | 47 | indent --spaces *.php README.md 48 | 49 | Convert all `.php`-files in `dir` to tabs (recursively): 50 | 51 | indent --tabs -r dir 52 | 53 | Convert all `.md`-files in dir to spaces (recursively): 54 | 55 | indent --spaces --pattern=*.md -r dir 56 | 57 | Convert STDIN, which is the content of `myfile.php` to spaces and print the result to STDOUT: 58 | 59 | cat myfile.php | ./indent --spaces 60 | -------------------------------------------------------------------------------- /indent: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | $arg) { 18 | if ($k == 0) { 19 | continue; 20 | } 21 | if ($arg[0] == '-') { 22 | $arg = explode('=', $arg); 23 | switch($arg[0]) { 24 | case '-r': 25 | $recursive = true; 26 | break; 27 | case '--pattern': 28 | $pattern = $arg[1]; 29 | break; 30 | case '--spaces': 31 | $targetIndent = 'spaces'; 32 | break; 33 | case '--tabs': 34 | $targetIndent = 'tabs'; 35 | break; 36 | case '--tabstop': 37 | if (!is_numeric($arg[1]) || $arg[1] < 1) { 38 | error('Invalid value for ' . $arg[0], 'usage'); 39 | } 40 | $tabstop = (int)$arg[1]; 41 | break; 42 | case '-h': 43 | case '--help': 44 | echo "Indentation converter\n"; 45 | echo "---------------------\n\n"; 46 | echo "by Carsten Brandt \n\n"; 47 | usage(); 48 | break; 49 | default: 50 | error('Unknown argument ' . $arg[0], 'usage'); 51 | } 52 | } else { 53 | $src[] = $arg; 54 | } 55 | } 56 | 57 | if ($targetIndent === false) { 58 | error('indentation flag --tabs or --spaces is missing.', 'usage'); 59 | } 60 | 61 | if (empty($src)) { 62 | echo convertIndent(file_get_contents("php://stdin"), $targetIndent, $tabstop); 63 | } elseif ($recursive) { 64 | $pattern = '/^' . implode('(.*)', array_map('preg_quote', explode('*', $pattern))) . '$/i'; 65 | foreach ($src as $dir) { 66 | if (is_file($dir)) { 67 | convertIndentFile($dir, $targetIndent, $tabstop); 68 | } elseif (is_dir($dir)) { 69 | convertIndentDir($dir, $pattern, $targetIndent, $tabstop); 70 | } else { 71 | error("File or directory does not exist: $dir"); 72 | } 73 | } 74 | } elseif (count($src) == 1) { 75 | $file = reset($src); 76 | if (!file_exists($file)) { 77 | error("File does not exist: $file"); 78 | } elseif (!is_file($file)) { 79 | error("Argument is not a file: $file\nDid you forget the -r option?"); 80 | } 81 | convertIndentFile($file, $targetIndent, $tabstop); 82 | } else { 83 | foreach($src as $file) { 84 | convertIndentFile($file, $targetIndent, $tabstop); 85 | } 86 | } 87 | 88 | // functions 89 | 90 | /** 91 | * Display usage information 92 | */ 93 | function usage() { 94 | global $argv; 95 | $cmd = $argv[0]; 96 | echo << $line) { 184 | if (preg_match('/^\s+/', $line, $matches)) { 185 | switch($target) { 186 | case 'spaces': 187 | $line = str_replace("\t", $spaces, $matches[0]) . substr($line, strlen($matches[0])); 188 | break; 189 | case 'tabs': 190 | $line = str_replace($spaces, "\t", $matches[0]) . substr($line, strlen($matches[0])); 191 | break; 192 | } 193 | $lines[$i] = $line; 194 | } 195 | } 196 | return implode(PHP_EOL, $lines); 197 | } 198 | --------------------------------------------------------------------------------