├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── VERSION ├── bin └── clish ├── box.json ├── composer.json ├── docs ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── example.php ├── example.png ├── font ├── dejavu.ttf ├── droidsans.ttf └── ubuntu.ttf ├── phpunit.xml.dist ├── src ├── Console │ └── ClishCommand.php ├── Exporter.php ├── Highlighter.php └── Pretty.php └── tests ├── Console └── ClishCommandTest.php ├── ExporterTest.php ├── HighlighterTest.php ├── bootstrap.php └── example.phps /.editorconfig: -------------------------------------------------------------------------------- 1 | ; http://editorconfig.org 2 | ; 3 | ; Sublime: https://github.com/sindresorhus/editorconfig-sublime 4 | ; Phpstorm: https://plugins.jetbrains.com/plugin/7294-editorconfig 5 | 6 | root = true 7 | 8 | [*] 9 | indent_style = space 10 | indent_size = 4 11 | end_of_line = lf 12 | charset = utf-8 13 | trim_trailing_whitespace = true 14 | insert_final_newline = true 15 | 16 | [{*.js,*.css,*.scss,*.html}] 17 | indent_size = 2 18 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: adhocore 2 | custom: ['https://paypal.me/ji10'] 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # standards 2 | /.cache/ 3 | /.env 4 | /.idea/ 5 | /vendor/ 6 | composer.lock 7 | coverage.xml 8 | clover.xml 9 | test.php 10 | *.ttf.png 11 | *.phar 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | cache: 2 | directories: 3 | - $HOME/.composer/cache/files 4 | 5 | language: php 6 | 7 | php: 8 | - 7.0 9 | - 7.1 10 | - 7.2 11 | - 7.3 12 | - 7.4 13 | 14 | install: 15 | - composer install --prefer-dist 16 | 17 | before_script: 18 | - for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done 19 | 20 | script: 21 | - composer test:cov 22 | 23 | after_success: 24 | - bash <(curl -s https://codecov.io/bash) 25 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.4.0](https://github.com/adhocore/php-cli-syntax/releases/tag/0.4.0) (2020-01-08) 2 | 3 | ### Features 4 | - **Clish.cmd**: Add --with-line-no option (Jitendra Adhikari) [_664c263_](https://github.com/adhocore/php-cli-syntax/commit/664c263) 5 | - **Highlighter**: Support lineno (Jitendra Adhikari) [_5ad19fa_](https://github.com/adhocore/php-cli-syntax/commit/5ad19fa) 6 | - **Exporter**: Support lineno (Jitendra Adhikari) [_dffe047_](https://github.com/adhocore/php-cli-syntax/commit/dffe047) 7 | - **Pretty**: Support line no, refactor reset() (Jitendra Adhikari) [_513095a_](https://github.com/adhocore/php-cli-syntax/commit/513095a) 8 | 9 | ### Miscellaneous 10 | - **Composer**: Bump adhocore/cli version (Jitendra Adhikari) [_26f4b38_](https://github.com/adhocore/php-cli-syntax/commit/26f4b38) 11 | - Cleanup (Jitendra Adhikari) [_90ca8c9_](https://github.com/adhocore/php-cli-syntax/commit/90ca8c9) 12 | 13 | ### Documentations 14 | - Update programatic usage options (Jitendra Adhikari) [_2655e21_](https://github.com/adhocore/php-cli-syntax/commit/2655e21) 15 | - Update options, example and screenshot (Jitendra Adhikari) [_dca4f92_](https://github.com/adhocore/php-cli-syntax/commit/dca4f92) 16 | 17 | 18 | ## [0.3.1](https://github.com/adhocore/php-cli-syntax/releases/tag/0.3.1) (2019-12-31) 19 | 20 | ### Miscellaneous 21 | - **Travis**: Script (Jitendra Adhikari) [_d0ef38d_](https://github.com/adhocore/php-cli-syntax/commit/d0ef38d) 22 | - **Composer**: Tweak script.test (Jitendra Adhikari) [_8df0473_](https://github.com/adhocore/php-cli-syntax/commit/8df0473) 23 | 24 | ### Documentations 25 | - Add clish ascii art (Jitendra Adhikari) [_5b7c713_](https://github.com/adhocore/php-cli-syntax/commit/5b7c713) 26 | - Improve (Jitendra Adhikari) [_08b2b32_](https://github.com/adhocore/php-cli-syntax/commit/08b2b32) 27 | 28 | 29 | ## [0.3.0](https://github.com/adhocore/php-cli-syntax/releases/tag/0.3.0) (2019-12-30) 30 | 31 | ### Bug Fixes 32 | - **Clish.cmd**: Usage msg, font path in phar (Jitendra Adhikari) [_cef8caa_](https://github.com/adhocore/php-cli-syntax/commit/cef8caa) 33 | 34 | ### Internal Refactors 35 | - Invert font check logic (Jitendra Adhikari) [_1b8cda0_](https://github.com/adhocore/php-cli-syntax/commit/1b8cda0) 36 | 37 | ### Miscellaneous 38 | - Bump cli (Jitendra Adhikari) [_a684003_](https://github.com/adhocore/php-cli-syntax/commit/a684003) 39 | - **Clish**: Check gd extension (Jitendra Adhikari) [_16d7689_](https://github.com/adhocore/php-cli-syntax/commit/16d7689) 40 | - **Phar**: Add box.json (Jitendra Adhikari) [_048eb60_](https://github.com/adhocore/php-cli-syntax/commit/048eb60) 41 | 42 | 43 | ## [0.2.1](https://github.com/adhocore/php-cli-syntax/releases/tag/0.2.1) (2019-12-28) 44 | 45 | ### Bug Fixes 46 | - **Clish.cmd**: Ensure file is str (Jitendra Adhikari) [_5509140_](https://github.com/adhocore/php-cli-syntax/commit/5509140) 47 | 48 | 49 | ## [0.2.0](https://github.com/adhocore/php-cli-syntax/releases/tag/0.2.0) (2019-12-28) 50 | 51 | ### Features 52 | - **Bin**: Add clish entrypoint (Jitendra Adhikari) [_b6dfe0f_](https://github.com/adhocore/php-cli-syntax/commit/b6dfe0f) 53 | - **Console**: Add clish command (Jitendra Adhikari) [_1a36015_](https://github.com/adhocore/php-cli-syntax/commit/1a36015) 54 | - **Pretty**: Use domxpath, add reset() and codeToHtml() (Jitendra Adhikari) [_aa65e14_](https://github.com/adhocore/php-cli-syntax/commit/aa65e14) 55 | - Add png Exporter class (Jitendra Adhikari) [_494c4a0_](https://github.com/adhocore/php-cli-syntax/commit/494c4a0) 56 | - Add abstract base Pretty class (Jitendra Adhikari) [_09feb82_](https://github.com/adhocore/php-cli-syntax/commit/09feb82) 57 | 58 | ### Bug Fixes 59 | - **Clish**: Phar name (Jitendra Adhikari) [_6499722_](https://github.com/adhocore/php-cli-syntax/commit/6499722) 60 | - **Exporter**: Validate output dir (Jitendra Adhikari) [_bd8e3e7_](https://github.com/adhocore/php-cli-syntax/commit/bd8e3e7) 61 | - **Clish**: Phar name (Jitendra Adhikari) [_c82c585_](https://github.com/adhocore/php-cli-syntax/commit/c82c585) 62 | 63 | ### Internal Refactors 64 | - **Clish.cmd**: Dont trim input, use writer instead of echo (Jitendra Adhikari) [_e30bd02_](https://github.com/adhocore/php-cli-syntax/commit/e30bd02) 65 | - **Exporter**: No static, add setOptions, reset, refactor visit (Jitendra Adhikari) [_6a38fd4_](https://github.com/adhocore/php-cli-syntax/commit/6a38fd4) 66 | - **Highlighter**: Implement reset(), refactor visit() (Jitendra Adhikari) [_e6d6b4e_](https://github.com/adhocore/php-cli-syntax/commit/e6d6b4e) 67 | - Extend Pretty, cleanup redundant (Jitendra Adhikari) [_772c3d7_](https://github.com/adhocore/php-cli-syntax/commit/772c3d7) 68 | 69 | ### Miscellaneous 70 | - Update test:cov (Jitendra Adhikari) [_285182f_](https://github.com/adhocore/php-cli-syntax/commit/285182f) 71 | - Update example output (Jitendra Adhikari) [_4867ec3_](https://github.com/adhocore/php-cli-syntax/commit/4867ec3) 72 | - **Composer**: Add keywords, use adhocore/cli, register clish (Jitendra Adhikari) [_3652e5f_](https://github.com/adhocore/php-cli-syntax/commit/3652e5f) 73 | - Chmod +x clish (Jitendra Adhikari) [_3982889_](https://github.com/adhocore/php-cli-syntax/commit/3982889) 74 | - Update gitignore (Jitendra Adhikari) [_c0f09e1_](https://github.com/adhocore/php-cli-syntax/commit/c0f09e1) 75 | - Add more fonts (Jitendra Adhikari) [_0184d40_](https://github.com/adhocore/php-cli-syntax/commit/0184d40) 76 | - Add export example image output (Jitendra Adhikari) [_eb2ef0f_](https://github.com/adhocore/php-cli-syntax/commit/eb2ef0f) 77 | - Add export example (Jitendra Adhikari) [_8f88fa9_](https://github.com/adhocore/php-cli-syntax/commit/8f88fa9) 78 | - Require gd ext (Jitendra Adhikari) [_c41b11c_](https://github.com/adhocore/php-cli-syntax/commit/c41b11c) 79 | - Add dejavu font (Jitendra Adhikari) [_0927f75_](https://github.com/adhocore/php-cli-syntax/commit/0927f75) 80 | 81 | ### Documentations 82 | - Add global installation docs (Jitendra Adhikari) [_ddcbfd4_](https://github.com/adhocore/php-cli-syntax/commit/ddcbfd4) 83 | - About extension (Jitendra Adhikari) [_32aa562_](https://github.com/adhocore/php-cli-syntax/commit/32aa562) 84 | - Add customisation info (Jitendra Adhikari) [_9565097_](https://github.com/adhocore/php-cli-syntax/commit/9565097) 85 | - Add export usage (Jitendra Adhikari) [_d91624f_](https://github.com/adhocore/php-cli-syntax/commit/d91624f) 86 | 87 | 88 | ## [0.1.0](https://github.com/adhocore/php-cli-syntax/releases/tag/0.1.0) (2019-12-21) 89 | 90 | ### Features 91 | - Add highlighter (Jitendra Adhikari) [_a7a0ca2_](https://github.com/adhocore/php-cli-syntax/commit/a7a0ca2) 92 | 93 | ### Internal Refactors 94 | - Make code param optional (Jitendra Adhikari) [_d41b677_](https://github.com/adhocore/php-cli-syntax/commit/d41b677) 95 | 96 | ### Miscellaneous 97 | - Fix example.phps (Jitendra Adhikari) [_ef633bb_](https://github.com/adhocore/php-cli-syntax/commit/ef633bb) 98 | - **Composer**: Fix package name (Jitendra Adhikari) [_7f9912e_](https://github.com/adhocore/php-cli-syntax/commit/7f9912e) 99 | - **Composer**: Add test:cov, require ext-dom (Jitendra Adhikari) [_6087842_](https://github.com/adhocore/php-cli-syntax/commit/6087842) 100 | - Add example script (Jitendra Adhikari) [_c2c8808_](https://github.com/adhocore/php-cli-syntax/commit/c2c8808) 101 | - Add github meta files (Jitendra Adhikari) [_e14ede6_](https://github.com/adhocore/php-cli-syntax/commit/e14ede6) 102 | - **Composer**: Add composer json (Jitendra Adhikari) [_5c38863_](https://github.com/adhocore/php-cli-syntax/commit/5c38863) 103 | - Add license, changelog, contributing (Jitendra Adhikari) [_bc73517_](https://github.com/adhocore/php-cli-syntax/commit/bc73517) 104 | - Init (Jitendra Adhikari) [_722a725_](https://github.com/adhocore/php-cli-syntax/commit/722a725) 105 | 106 | ### Documentations 107 | - Styleci (Jitendra Adhikari) [_cfb88ef_](https://github.com/adhocore/php-cli-syntax/commit/cfb88ef) 108 | - Update usage, add screenshot (Jitendra Adhikari) [_11526f7_](https://github.com/adhocore/php-cli-syntax/commit/11526f7) 109 | - Add readme (Jitendra Adhikari) [_21b5b8a_](https://github.com/adhocore/php-cli-syntax/commit/21b5b8a) 110 | 111 | ### Builds 112 | - **Travis**: Add travis yml (Jitendra Adhikari) [_e75bee9_](https://github.com/adhocore/php-cli-syntax/commit/e75bee9) 113 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## How to contribute 2 | 3 | ### Before you start 4 | 5 | 6 | 7 | 8 | ### Setting up 9 | 10 | You may need to fork this project in [GitHub](https://github.com/adhocore/php-cli-highlight). 11 | 12 | ```sh 13 | git clone git@github.com:adhocore/php-cli-highlight.git 14 | 15 | # OR if you have a fork 16 | git clone git@github.com:/php-cli-highlight.git 17 | 18 | # You may also add upstream 19 | git remote add upstream https://github.com/adhocore/php-cli-highlight.git 20 | 21 | cd php-cli-highlight 22 | 23 | # Create a new branch 24 | git checkout -b $branch_name 25 | 26 | # Install deps 27 | composer install -o 28 | ``` 29 | 30 | ### Moving forward 31 | 32 | ```sh 33 | # Open php-cli-highlight in IDE 34 | subl php-cli-highlight 35 | 36 | # ... and do the needful 37 | 38 | # Optionally run the lint 39 | for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done 40 | 41 | # ... and phpcs fixer or stuffs like that! 42 | 43 | # Run tests 44 | vendor/bin/phpunit --coverage-text 45 | 46 | 47 | # If your feature takes long your dev branch might be out of sync, you may want to 48 | git checkout $branch_name 49 | git pull upstream master # branch could be something else than master 50 | ``` 51 | 52 | ### Finalizing 53 | 54 | Everything looking good? 55 | 56 | ```sh 57 | # Commit your stuffs 58 | git add $file ...$files 59 | git commit -m "..." 60 | 61 | # Push 'em 62 | git push origin HEAD 63 | ``` 64 | 65 | Now goto [GitHub](https://github.com/adhocore/php-cli-highlight/compare?expand=1), select your branch and create PR. 66 | 67 | ### Getting PR merged 68 | 69 | You have to wait. You have to address change requests. Be patient. 70 | 71 | Thank you for contribution! 72 | 73 | **Lastly** Please be informed that your works will be licensed same as the project [license](./LICENSE) 74 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jitendra Adhikari 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## adhocore/cli-syntax 2 | 3 | [![Latest Version](https://img.shields.io/github/release/adhocore/php-cli-syntax.svg?style=flat-square)](https://github.com/adhocore/php-cli-syntax/releases) 4 | [![Travis Build](https://img.shields.io/travis/com/adhocore/php-cli-syntax.svg?branch=master&style=flat-square)](https://travis-ci.com/adhocore/php-cli-syntax?branch=master) 5 | [![Scrutinizer CI](https://img.shields.io/scrutinizer/g/adhocore/php-cli-syntax.svg?style=flat-square)](https://scrutinizer-ci.com/g/adhocore/php-cli-syntax/?branch=master) 6 | [![Codecov branch](https://img.shields.io/codecov/c/github/adhocore/php-cli-syntax/master.svg?style=flat-square)](https://codecov.io/gh/adhocore/php-cli-syntax) 7 | [![StyleCI](https://styleci.io/repos/229348504/shield)](https://styleci.io/repos/229348504) 8 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](./LICENSE) 9 | 10 | 11 | ``` 12 | _ _ _ 13 | ___ | | (_) ___ | |__ 14 | / __| | | | | / __| ' _ \ 15 | | (__ | | | | \___ \ | | | | 16 | \___| |_| |_| |____/ |_| |_| 17 | 18 | PHP CLI Syntax Highlight Tool 19 | ============================= 20 | ``` 21 | 22 | ## Installation 23 | 24 | ### As phar binary 25 | 26 | ```sh 27 | curl -SsLo ~/clish.phar https://github.com/adhocore/php-cli-syntax/releases/latest/download/clish.phar 28 | 29 | chmod +x ~/clish.phar && sudo ln -s ~/clish.phar /usr/local/bin/clish 30 | ``` 31 | > Follow same steps to upgrade. 32 | 33 | ### As standalone binary 34 | 35 | ```sh 36 | composer global require adhocore/cli-syntax 37 | ``` 38 | > Follow same steps to upgrade. 39 | 40 | ### As project dependency 41 | ```bash 42 | composer require adhocore/cli-syntax 43 | ``` 44 | 45 | ## Usage 46 | 47 | ### Shell command 48 | 49 | If you installed as binary following any of the above methods, then: 50 | 51 | ```sh 52 | # you will be able to run it as 53 | clish -h 54 | clish -f file.php 55 | echo ' `clish` stands for CLI syntax highlight. 63 | 64 | #### Options 65 | 66 | Parameter options: 67 | 68 | ``` 69 | [-e|--echo] Forces echo to STDOUT when --output is passed 70 | [-f|--file] Input PHP file to highlight and/or export 71 | (will read from piped input if file not given) 72 | [-F|--font] Font to use for export to png 73 | [-l|--with-line-no] Highlight with line number 74 | [-o|--output] Output filepath where PNG image is exported 75 | ``` 76 | 77 | > Run `clish -h` to show help. 78 | 79 | ##### Examples 80 | 81 | ```sh 82 | bin/clish --file file.php # print 83 | cat file.php | bin/clish # from piped stream 84 | bin/clish < file.php # from redirected stdin 85 | bin/clish --file file.php --output file.png # export 86 | bin/clish --file file.php --output file.png --echo # print + export 87 | bin/clish --file file.php --with-line-no # print with lineno 88 | bin/clish -f file.php -o file.png -F dejavu # export in dejavu font 89 | ``` 90 | 91 | ### Programatically 92 | 93 | You can either highlight PHP code in terminal output or export to png image. 94 | 95 | #### Highlight 96 | 97 | ```php 98 | use Ahc\CliSyntax\Highlighter; 99 | 100 | // PHP code 101 | echo new Highlighter('highlight(' true, // bool 111 | ]; 112 | ``` 113 | 114 | #### Export 115 | 116 | ```php 117 | use Ahc\CliSyntax\Exporter; 118 | 119 | // PHP file 120 | Exporter::for('/path/to/file.php')->export('file.png', $options); 121 | 122 | // $options array is optional and can contain: 123 | [ 124 | 'lineNo' => true, // bool 125 | 'font' => 'full/path/of/font.ttf', // str 126 | 'size' => 'font size', // int 127 | ]; 128 | ``` 129 | 130 | See [example usage](./example.php). Here's how the export looks like: 131 | 132 | ![adhocore/cli-syntax](./example.png) 133 | 134 | --- 135 | And with line numbers: 136 | 137 | ![Example with line numbers](https://imgur.com/Jqiydf8.png) 138 | 139 | ## Customisation 140 | 141 | If you would like to change color etc, extend the classes 142 | [`Highlighter`](./src/Highlighter.php) and [`Exporter`](./src/Exporter.php), 143 | then override `visit()` method which recieves [`DOMNode`](https://php.net/DOMNode). 144 | 145 | ## Contributing 146 | 147 | Please check [the guide](./CONTRIBUTING.md). 148 | 149 | ## LICENSE 150 | 151 | > © [MIT](./LICENSE) | 2019, Jitendra Adhikari 152 | 153 | ## Credits 154 | 155 | This project is bootstrapped by [phint](https://github.com/adhocore/phint) 156 | and releases managed by [please](https://github.com/adhocore/please). 157 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 0.4.0 2 | -------------------------------------------------------------------------------- /bin/clish: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | add(new Ahc\CliSyntax\Console\ClishCommand, 'c', true); 50 | 51 | $app->logo($logo)->handle($_SERVER['argv'] + ['', 'clish']); 52 | -------------------------------------------------------------------------------- /box.json: -------------------------------------------------------------------------------- 1 | { 2 | "chmod": "0755", 3 | 4 | "check-requirements": false, 5 | 6 | "banner": [ 7 | "Clish is a PHP code highlight and/or export tool." 8 | ], 9 | 10 | "files": [ 11 | "VERSION" 12 | ], 13 | 14 | "directories-bin": [ 15 | "font" 16 | ], 17 | 18 | "compression": "GZ", 19 | "datetime": "release-date" 20 | } 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "adhocore/cli-syntax", 3 | "description": "PHP Code Syntax Highlighter for CLI", 4 | "type": "library", 5 | "keywords": ["php", "cli-syntax", "php-cli-highlight", "cli-syntax-highlight", "syntax-highlight", 6 | "export-php-code", "code-to-image", "code-screenshot"], 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Jitendra Adhikari", 11 | "email": "jiten.adhikary@gmail.com", 12 | "role": "Lead Developer" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4": { 17 | "Ahc\\CliSyntax\\": "src/" 18 | }, 19 | "files": [] 20 | }, 21 | "autoload-dev": { 22 | "psr-4": { 23 | "Ahc\\CliSyntax\\Test\\": "tests/" 24 | } 25 | }, 26 | "require": { 27 | "php": ">=7.0.0", 28 | "ext-dom": "*", 29 | "ext-gd": "*", 30 | "adhocore/cli": "^0.8.3" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": "^6.5 || ^7.5" 34 | }, 35 | "config": { 36 | "optimize-autoloader": true, 37 | "preferred-install": { 38 | "*": "dist" 39 | } 40 | }, 41 | "bin": ["bin/clish"], 42 | "scripts": { 43 | "test": "phpunit", 44 | "test:cov": "phpunit --coverage-text --coverage-clover coverage.xml --coverage-html vendor/cov" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /docs/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | **This is a** 4 | 5 | 6 | - [ ] Bug/Issue 7 | - [ ] Feature request 8 | - [ ] General stuff 9 | 10 | 37 | 38 | 43 | 44 | 50 | -------------------------------------------------------------------------------- /docs/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ## Introduction 2 | 3 | **This is a** 4 | 5 | 6 | - [ ] New feature 7 | - [ ] Fix for ... 8 | - [ ] General improvement 9 | - [ ] Backward incompatible change 10 | 11 | It closes # 12 | 13 | ## Describe the change 14 | 15 | ... 16 | 17 | ## How to use it 18 | 19 | ... 20 | 21 | #### Sample code 22 | 23 | ```php 24 | 25 | ``` 26 | -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * 9 | * Licensed under MIT license. 10 | */ 11 | 12 | use Ahc\CliSyntax\Exporter; 13 | use Ahc\CliSyntax\Highlighter; 14 | 15 | require_once __DIR__ . '/src/Pretty.php'; 16 | require_once __DIR__ . '/src/Exporter.php'; 17 | require_once __DIR__ . '/src/Highlighter.php'; 18 | 19 | // Highlight code 20 | echo (new Highlighter("" . PHP_EOL)) 21 | ->highlight(); 22 | 23 | // Highlight file 24 | echo Highlighter::for(__FILE__)->highlight(); 25 | 26 | // Export highlighted file as image 27 | Exporter::for(__FILE__)->export(__DIR__ . '/example.png'); 28 | 29 | // Magic string works too: 30 | echo new Highlighter(' 2 | 3 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | 20 | ./src 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/Console/ClishCommand.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * 11 | * Licensed under MIT license. 12 | */ 13 | 14 | namespace Ahc\CliSyntax\Console; 15 | 16 | use Ahc\Cli\Input\Command; 17 | use Ahc\Cli\IO\Interactor; 18 | use Ahc\CliSyntax\Exporter; 19 | use Ahc\CliSyntax\Highlighter; 20 | 21 | class ClishCommand extends Command 22 | { 23 | public function __construct() 24 | { 25 | parent::__construct('clish', 'PHP CLI syntax highlight and/or export.'); 26 | 27 | $this 28 | ->option('-o --output', 'Output filepath where PNG image is exported', null, '') 29 | ->option('-e --echo', 'Forces echo to STDOUT when --output is passed', null, '') 30 | ->option('-l --with-line-no', 'Highlight with line number') 31 | ->option('-f --file', \implode("\n", [ 32 | 'Input PHP file to highlight and/or export', 33 | '(will read from piped input if file not given)', 34 | ]), null, '') 35 | ->option('-F --font', 'Font to use for export to png', null, '') 36 | ->usage( 37 | ' $0 --file file.php ## print' 38 | . ' cat file.php | $0 ## from piped stream' 39 | . ' $0 < file.php ## from redirected stdin' 40 | . ' $0 --file file.php --output file.png ## export' 41 | . ' $0 --file file.php --output file.png --echo ## print + export' 42 | . ' $0 --file file.php --with-line-no ## print with lineno' 43 | . ' $0 -f file.php -o file.png -F dejavu ## export in dejavu font' 44 | ); 45 | } 46 | 47 | public function interact(Interactor $io) 48 | { 49 | if ($this->file) { 50 | return; 51 | } 52 | 53 | $code = $io->readPiped(function ($reader) use ($io) { 54 | $io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true); 55 | $io->warn('(Opening tag `readAll(); 58 | }); 59 | 60 | if ('' !== $code && \substr($code, 0, 5) != 'set('code', $code); 66 | } 67 | 68 | public function execute() 69 | { 70 | $code = $this->file ? \file_get_contents($this->file) : $this->code; 71 | 72 | if ('' === \trim($code)) { 73 | return; 74 | } 75 | 76 | $this->doExport($code); 77 | $this->doHighlight($code); 78 | } 79 | 80 | protected function doHighlight(string $code = null) 81 | { 82 | if (!$this->output || $this->echo) { 83 | $this->app()->io()->raw((new Highlighter)->highlight($code, ['lineNo' => $this->lineNo])); 84 | } 85 | } 86 | 87 | protected function doExport(string $code = null) 88 | { 89 | if (!$this->output) { 90 | return; 91 | } 92 | 93 | if (!\is_dir(\dirname($this->output))) { 94 | \mkdir(\dirname($this->output), 0755, true); 95 | } 96 | 97 | $options = ['font' => $this->fixFont($this->font ?: 'ubuntu'), 'lineNo' => $this->lineNo]; 98 | 99 | (new Exporter($code))->export($this->output, $options); 100 | } 101 | 102 | /** @codeCoverageIgnore */ 103 | protected function fixFont(string $font): string 104 | { 105 | if (\Phar::running()) { 106 | $basename = \basename($font, '.ttf'); 107 | $pharfont = __DIR__ . "/../../font/$basename.ttf"; 108 | 109 | if (!\is_file($font) && \is_file($pharfont)) { 110 | \copy($pharfont, $font = sys_get_temp_dir() . '/clish.ttf'); 111 | } 112 | } 113 | 114 | return $font; 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/Exporter.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * 11 | * Licensed under MIT license. 12 | */ 13 | 14 | namespace Ahc\CliSyntax; 15 | 16 | class Exporter extends Pretty 17 | { 18 | /** @var int Font size */ 19 | protected $size = 16; 20 | 21 | /** @var string Font path */ 22 | protected $font = __DIR__ . '/../font/ubuntu.ttf'; 23 | 24 | /** @var resource The image */ 25 | protected $image; 26 | 27 | /** @var array */ 28 | protected $imgSize = []; 29 | 30 | /** @var array Colors cached for each types. */ 31 | protected $colors = []; 32 | 33 | public function __destruct() 34 | { 35 | if (\is_resource($this->image)) { 36 | \imagedestroy($this->image); 37 | } 38 | } 39 | 40 | public function export(string $output, array $options = []) 41 | { 42 | if (!\is_dir(\dirname($output))) { 43 | throw new \InvalidArgumentException('The output path doesnot exist.'); 44 | } 45 | 46 | $this->setOptions($options); 47 | 48 | $this->imgSize = $this->estimateSize($this->code); 49 | $this->lineCount = \substr_count($this->code, "\n") ?: 1; 50 | 51 | $padLineNo = $this->withLineNo ? $this->estimateSize($this->formatLineNo())['x'] : 0; 52 | $this->image = \imagecreate($this->imgSize['x'] + $padLineNo + 50, $this->imgSize['y'] + 25); 53 | 54 | \imagecolorallocate($this->image, 0, 0, 0); 55 | 56 | $this->parse(); 57 | 58 | \imagepng($this->image, $output); 59 | } 60 | 61 | protected function setOptions(array $options) 62 | { 63 | parent::setOptions($options); 64 | 65 | if (isset($options['size'])) { 66 | $this->size = $options['size']; 67 | } 68 | 69 | if (!isset($options['font'])) { 70 | return; 71 | } 72 | 73 | if (\is_file($options['font'])) { 74 | $this->font = $options['font']; 75 | 76 | return; 77 | } 78 | 79 | $base = \basename($options['font'], '.ttf'); 80 | if (!\is_file($font = __DIR__ . "/../font/$base.ttf")) { 81 | throw new \InvalidArgumentException('The given font doesnot exist.'); 82 | } 83 | 84 | $this->font = $font; 85 | } 86 | 87 | protected function estimateSize(string $for): array 88 | { 89 | $eol = \substr_count($for, "\n") ?: 1; 90 | $box = \imagettfbbox($this->size, 0, $this->font, $for); 91 | 92 | return ['x' => $box[2], 'y' => $box[1], 'y1' => \intval($box[1] / $eol)]; 93 | } 94 | 95 | protected function doReset() 96 | { 97 | $this->colors = []; 98 | } 99 | 100 | protected function visit(\DOMNode $el) 101 | { 102 | $type = $el instanceof \DOMElement ? $el->getAttribute('data-type') : 'raw'; 103 | $color = $this->colorCode($type); 104 | $ncolor = $this->colorCode('lineno'); 105 | $text = \str_replace([' ', '<', '>'], [' ', '<', '>'], $el->textContent); 106 | 107 | foreach (\explode("\n", $text) as $line) { 108 | $xlen = $this->lengths[$this->lineNo] ?? 0; 109 | $ypos = 12 + $this->imgSize['y1'] * $this->lineNo; 110 | 111 | if ('' !== $lineNo = $this->formatLineNo()) { 112 | $xlen += $this->estimateSize($lineNo)['x']; 113 | \imagefttext($this->image, $this->size, 0, 12, $ypos, $ncolor, $this->font, $lineNo); 114 | } 115 | 116 | \imagefttext($this->image, $this->size, 0, 12 + $xlen, $ypos, $color, $this->font, $line); 117 | 118 | $this->lengths[$this->lineNo] = $xlen + $this->estimateSize($line)['x']; 119 | 120 | $this->lineNo++; 121 | } 122 | } 123 | 124 | protected function colorCode(string $type): int 125 | { 126 | if (isset($this->colors[$type])) { 127 | return $this->colors[$type]; 128 | } 129 | 130 | $palette = [ 131 | 'comment' => [0, 96, 192], 132 | 'default' => [0, 192, 0], 133 | 'keyword' => [192, 0, 0], 134 | 'string' => [192, 192, 0], 135 | 'raw' => [128, 128, 128], 136 | 'lineno' => [128, 224, 224], 137 | ]; 138 | 139 | return $this->colors[$type] = \imagecolorallocate($this->image, ...$palette[$type]); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/Highlighter.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * 11 | * Licensed under MIT license. 12 | */ 13 | 14 | namespace Ahc\CliSyntax; 15 | 16 | class Highlighter extends Pretty 17 | { 18 | /** @var string Output string */ 19 | protected $out = ''; 20 | 21 | public function __toString(): string 22 | { 23 | return $this->highlight(); 24 | } 25 | 26 | public function highlight(string $code = null, array $options = []): string 27 | { 28 | $this->setOptions($options); 29 | 30 | $this->parse($code ?? $this->code); 31 | 32 | return \trim($this->out, "\n") . "\n"; 33 | } 34 | 35 | protected function doReset() 36 | { 37 | $this->out = ''; 38 | } 39 | 40 | protected function visit(\DOMNode $el) 41 | { 42 | $type = $el instanceof \DOMElement ? $el->getAttribute('data-type') : 'raw'; 43 | $text = \str_replace([' ', '<', '>'], [' ', '<', '>'], $el->textContent); 44 | 45 | $lastLine = 0; 46 | $lines = \explode("\n", $text); 47 | foreach ($lines as $i => $line) { 48 | $this->out .= $this->formatLine($type, $line); 49 | 50 | if (isset($lines[$i + 1])) { 51 | $this->out .= "\n"; 52 | } 53 | 54 | $this->lengths[$this->lineNo++] = \strlen($line); 55 | } 56 | } 57 | 58 | protected function formatLine(string $type, string $line) 59 | { 60 | static $formats = [ 61 | 'comment' => "\033[0;34;40m%s\033[0m", 62 | 'default' => "\033[0;32;40m%s\033[0m", 63 | 'keyword' => "\033[0;31;40m%s\033[0m", 64 | 'string' => "\033[0;33;40m%s\033[0m", 65 | 'lineno' => "\033[2;36;40m%s\033[0m", 66 | 'raw' => '%s', 67 | ]; 68 | 69 | if ('' !== $lineNo = $this->formatLineNo()) { 70 | $lineNo = \sprintf($formats['lineno'], $lineNo); 71 | } 72 | 73 | return $lineNo . \sprintf($formats[$type], $line); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/Pretty.php: -------------------------------------------------------------------------------- 1 | 9 | * 10 | * 11 | * Licensed under MIT license. 12 | */ 13 | 14 | namespace Ahc\CliSyntax; 15 | 16 | abstract class Pretty 17 | { 18 | /** @var string The PHP code. */ 19 | protected $code; 20 | 21 | /** @var int The current line number. */ 22 | protected $lineNo = 0; 23 | 24 | /** @var int The total lines count. */ 25 | protected $lineCount = 0; 26 | 27 | /** @var bool Show line numbers. */ 28 | protected $withLineNo = false; 29 | 30 | /** @var array Lengths of each line */ 31 | protected $lengths = []; 32 | 33 | /** @var bool Indicates if it has been already configured. */ 34 | protected static $configured; 35 | 36 | public function __construct(string $code = null) 37 | { 38 | $this->code = $code ?? ''; 39 | } 40 | 41 | public static function for(string $file): self 42 | { 43 | if (!\is_file($file)) { 44 | throw new \InvalidArgumentException('The given file doesnot exist or is unreadable.'); 45 | } 46 | 47 | return new static(\file_get_contents($file)); 48 | } 49 | 50 | public static function configure() 51 | { 52 | if (static::$configured) { 53 | return; 54 | } 55 | 56 | foreach (['comment', 'default', 'html', 'keyword', 'string'] as $type) { 57 | \ini_set("highlight.$type", \ini_get("highlight.$type") . \sprintf('" data-type="%s', $type)); 58 | } 59 | 60 | static::$configured = true; 61 | } 62 | 63 | protected function setOptions(array $options) 64 | { 65 | if ($options['lineNo'] ?? false) { 66 | $this->withLineNo = true; 67 | } 68 | } 69 | 70 | protected function parse(string $code = null) 71 | { 72 | $this->reset(); 73 | 74 | $dom = new \DOMDocument; 75 | $dom->loadHTML($this->codeToHtml($code ?? $this->code)); 76 | 77 | $adjust = -1; 78 | foreach ((new \DOMXPath($dom))->query('/html/body/code/span/*') as $el) { 79 | $this->lineNo = $el->getLineNo() + $adjust; 80 | $this->visit($el); 81 | } 82 | } 83 | 84 | protected function codeToHtml(string $code): string 85 | { 86 | static::configure(); 87 | 88 | $this->lineCount = \substr_count($code, "\n") ?: 1; 89 | 90 | $html = \highlight_string($code, true); 91 | 92 | return \str_replace(['
'], ["\n"], $html); 93 | } 94 | 95 | protected function formatLineNo(): string 96 | { 97 | if ($this->withLineNo && $this->lineNo <= $this->lineCount && !isset($this->lengths[$this->lineNo])) { 98 | return \str_pad("$this->lineNo", \strlen("$this->lineCount"), ' ', \STR_PAD_LEFT) . '. '; 99 | } 100 | 101 | return ''; 102 | } 103 | 104 | protected function reset() 105 | { 106 | $this->doReset(); 107 | 108 | $this->lengths = []; 109 | $this->lineNo = $this->lineCount = 0; 110 | } 111 | 112 | abstract protected function doReset(); 113 | 114 | abstract protected function visit(\DOMNode $el); 115 | } 116 | -------------------------------------------------------------------------------- /tests/Console/ClishCommandTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * 9 | * Licensed under MIT license. 10 | */ 11 | 12 | namespace Ahc\CliSyntax\Test\Console; 13 | 14 | use Ahc\Cli\Application; 15 | use Ahc\Cli\IO\Interactor; 16 | use Ahc\Cli\Test\CliTestCase; 17 | use Ahc\CliSyntax\Console\ClishCommand; 18 | 19 | require_once __DIR__ . '/../../vendor/adhocore/cli/tests/CliTestCase.php'; 20 | 21 | class ClishCommandTest extends CliTestCase 22 | { 23 | protected $app; 24 | 25 | protected static $in = __DIR__ . '/stdin'; 26 | protected static $ou = __DIR__ . '/stdout'; 27 | protected static $ex = __DIR__ . '/output/export.png'; 28 | 29 | public function setUp() 30 | { 31 | parent::setUp(); 32 | \touch(static::$in); 33 | 34 | $this->app = (new Application('clish', 'test', function () { 35 | }))->add(new ClishCommand, 'c', true); 36 | $this->app->io(new Interactor(static::$in, static::$ou)); 37 | } 38 | 39 | public static function tearDownAfterClass() 40 | { 41 | parent::tearDownAfterClass(); 42 | 43 | if (\is_file(static::$ex)) { 44 | \unlink(static::$ex); 45 | } 46 | if (\is_file(static::$in)) { 47 | \unlink(static::$in); 48 | } 49 | 50 | if (\is_dir(\dirname(static::$ex))) { 51 | \rmdir(\dirname(static::$ex)); 52 | } 53 | } 54 | 55 | public function testExecute() 56 | { 57 | $this->app->handle(['clish', 'c', '--file=' . __DIR__ . '/../../example.php']); 58 | 59 | $this->assertBufferContains(""); 60 | $this->assertBufferContains('This file is part of the CLI-SYNTAX package'); 61 | } 62 | 63 | public function testExecuteExport() 64 | { 65 | $this->app->handle(['clish', 'c', '--file=' . __DIR__ . '/../../example.php', '--output=' . static::$ex]); 66 | 67 | $this->assertFileExists(static::$ex, 'Should export to a file'); 68 | $this->assertEmpty($this->buffer(), 'Shouldnt write to stdout when --output is given'); 69 | } 70 | 71 | public function testInteractEmptyInput() 72 | { 73 | \file_put_contents(static::$in, ''); 74 | 75 | $this->app->handle(['clish', 'c']); 76 | 77 | $this->assertBufferContains('Type in or paste PHP Code below, Press Ctrl+D when done'); 78 | } 79 | 80 | public function testInteractNonEmpty() 81 | { 82 | \file_put_contents(static::$in, 'echo 1234;'); 83 | 84 | $this->app->handle(['clish', 'c']); 85 | 86 | $this->assertBufferContains('assertBufferContains('echo'); 88 | $this->assertBufferContains('1234'); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /tests/ExporterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * 9 | * Licensed under MIT license. 10 | */ 11 | 12 | namespace Ahc\CliSyntax\Test; 13 | 14 | use Ahc\CliSyntax\Exporter; 15 | use PHPUnit\Framework\TestCase; 16 | 17 | class ExporterTest extends TestCase 18 | { 19 | protected $out = __DIR__ . '/export.png'; 20 | protected $ref = __DIR__ . '/../example.png'; 21 | 22 | public function setUp() 23 | { 24 | if (\is_file($this->out)) { 25 | @unlink($this->out); 26 | } 27 | } 28 | 29 | public function testExport() 30 | { 31 | Exporter::for(__DIR__ . '/../example.php')->export($this->out); 32 | 33 | $this->assertFileExists($this->out, 'It should export to given output path'); 34 | // $this->assertSame(\file_get_contents($this->ref), \file_get_contents($this->out)); 35 | } 36 | 37 | /** 38 | * @expectedException \InvalidArgumentException 39 | * 40 | * @expectedExceptionMessage The output path doesnot exist. 41 | */ 42 | public function testExportThrows() 43 | { 44 | Exporter::for(__DIR__ . '/../example.php')->export(__DIR__ . '/no/dir/file.png'); 45 | } 46 | 47 | public function testSetOptions() 48 | { 49 | Exporter::for(__DIR__ . '/../example.php')->export($this->out, ['font' => 'ubuntu', 'size' => 18]); 50 | 51 | $this->assertFileExists($this->out, 'It should export with given options'); 52 | // $this->assertSame(\file_get_contents($this->ref), \file_get_contents($this->out)); 53 | } 54 | 55 | public function testSetOptionsFont() 56 | { 57 | Exporter::for(__DIR__ . '/../example.php')->export( 58 | $this->out, 59 | ['font' => __DIR__ . '/../font/dejavu.ttf', 'lineNo' => true] 60 | ); 61 | 62 | $this->assertFileExists($this->out, 'It should export with given font'); 63 | // $this->assertSame(\file_get_contents($this->ref), \file_get_contents($this->out)); 64 | } 65 | 66 | /** 67 | * @expectedException \InvalidArgumentException 68 | * 69 | * @expectedExceptionMessage The given font doesnot exist. 70 | */ 71 | public function testSetOptionsThrows() 72 | { 73 | Exporter::for(__DIR__ . '/../example.php')->export($this->out, ['font' => 'invalid']); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/HighlighterTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * 9 | * Licensed under MIT license. 10 | */ 11 | 12 | namespace Ahc\CliSyntax\Test; 13 | 14 | use Ahc\CliSyntax\Highlighter; 15 | use PHPUnit\Framework\TestCase; 16 | 17 | class HighlighterTest extends TestCase 18 | { 19 | public function testHighlightCode() 20 | { 21 | $code = (new Highlighter)->highlight('assertContains( 24 | 'highlight(' true]); 32 | 33 | $this->assertContains( 34 | '1. assertSame(\file_get_contents(__DIR__ . '/example.phps'), $code); 44 | } 45 | 46 | /** 47 | * @expectedException \InvalidArgumentException 48 | * 49 | * @expectedExceptionMessage The given file doesnot exist or is unreadable. 50 | */ 51 | public function testHighlightFileThrows() 52 | { 53 | Highlighter::for(__DIR__ . '/' . rand()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * 9 | * Licensed under MIT license. 10 | */ 11 | 12 | require_once __DIR__ . '/../vendor/autoload.php'; 13 | -------------------------------------------------------------------------------- /tests/example.phps: -------------------------------------------------------------------------------- 1 |  7 |  *      8 |  * 9 |  * Licensed under MIT license. 10 |  */ 11 |  12 | use Ahc\CliSyntax\Exporter; 13 | use Ahc\CliSyntax\Highlighter; 14 |  15 | require_once __DIR__ . '/src/Pretty.php'; 16 | require_once __DIR__ . '/src/Exporter.php'; 17 | require_once __DIR__ . '/src/Highlighter.php'; 18 |  19 | // Highlight code 20 | echo (new Highlighter("" . PHP_EOL)) 21 |     ->highlight(); 22 |  23 | // Highlight file 24 | echo Highlighter::for(__FILE__)->highlight(); 25 |  26 | // Export highlighted file as image 27 | Exporter::for(__FILE__)->export(__DIR__ . '/example.png'); 28 |  29 | // Magic string works too: 30 | echo new Highlighter('