Skip to content

Commit 688ce76

Browse files
authored
Merge pull request #1 from adhocore/develop
Develop
2 parents 4624d58 + ddcbfd4 commit 688ce76

File tree

17 files changed

+594
-87
lines changed

17 files changed

+594
-87
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
composer.lock
77
coverage.xml
88
clover.xml
9+
test.php
10+
*.ttf.png

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,33 @@
99

1010

1111
## Installation
12+
13+
### As standalone binary
14+
15+
```sh
16+
composer global require adhocore/cli-syntax
17+
18+
# then you will be able to run it as
19+
clish -h
20+
clish -f file.php
21+
echo '<?php date("Ymd");' | clish
22+
cat file.php | clish
23+
24+
# export png
25+
clish -f file.php -o file.png
26+
```
27+
28+
### As project dependency
1229
```bash
1330
composer require adhocore/cli-syntax
1431
```
1532

1633
## Usage
34+
35+
You can either highlight PHP code in terminal output or export to png image.
36+
37+
### Highlight
38+
1739
```php
1840
use Ahc\CliSyntax\Highlighter;
1941

@@ -26,9 +48,25 @@ echo (new Highlighter)->highlight('<?php echo "Hello world!";');
2648
echo Highlighter::for('/path/to/file.php');
2749
```
2850

29-
See [example](./example.php). Here's the screenshot too:
51+
### Export
52+
53+
```php
54+
use Ahc\CliSyntax\Exporter;
55+
56+
// PHP file
57+
Exporter::for('/path/to/file.php')->export('file.png');
58+
```
59+
60+
See [example usage](./example.php). Here's how the export looks like:
61+
62+
![adhocore/cli-syntax](./example.png)
63+
64+
65+
### Customisation
3066

31-
![adhocore/cli-syntax](https://imgur.com/wcNp3qJ.png)
67+
If you would like to change color etc, extend the classes
68+
[`Highlighter`](./src/Highlighter.php) and [`Exporter`](./src/Exporter.php),
69+
then override `visit()` method which recieves [`DOMNode`](https://php.net/DOMNode).
3270

3371
## Contributing
3472

bin/clish

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if (\PHP_VERSION_ID < 70000) {
5+
echo "clish requires PHP7.0 or newer\n";
6+
7+
exit(1);
8+
}
9+
10+
if (\Phar::running() && !\extension_loaded('zlib')) {
11+
echo "clish.phar requires 'zlib' extension\n(OR you can compile phar with `compression: NONE` in box.json)\n";
12+
13+
exit(1);
14+
}
15+
16+
if (\file_exists(__DIR__ . '/../../../autoload.php')) {
17+
require __DIR__ . '/../../../autoload.php';
18+
} elseif (\file_exists(__DIR__ . '/../../autoload.php')) {
19+
require __DIR__ . '/../../autoload.php';
20+
} else {
21+
require __DIR__ . '/../vendor/autoload.php';
22+
}
23+
24+
\chdir(\getcwd());
25+
26+
$logo = "
27+
_ _ _
28+
___ | | (_) ___ | |__
29+
/ __| | | | | / __| ' _ \
30+
| (__ | | | | \___ \ | | | |
31+
\___| |_| |_| |____/ |_| |_|
32+
33+
PHP CLI Syntax Highlight Tool
34+
=============================
35+
";
36+
37+
$app = new Ahc\Cli\Application(
38+
'Clish',
39+
\trim(\file_get_contents(__DIR__ . '/../VERSION'))
40+
);
41+
42+
// Add commands and their aliases
43+
$app->add(new Ahc\CliSyntax\Console\ClishCommand, 'c', true);
44+
45+
$app->logo($logo)->handle($_SERVER['argv'] + ['', 'clish']);

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "adhocore/cli-syntax",
33
"description": "PHP Code Syntax Highlighter for CLI",
44
"type": "library",
5-
"keywords": ["php", "cli-syntax", "php-cli-highlight", "cli-syntax-highlight", "syntax-highlight"],
5+
"keywords": ["php", "cli-syntax", "php-cli-highlight", "cli-syntax-highlight", "syntax-highlight",
6+
"export-php-code", "code-to-image", "code-screenshot"],
67
"license": "MIT",
78
"authors": [
89
{
@@ -24,7 +25,9 @@
2425
},
2526
"require": {
2627
"php": ">=7.0.0",
27-
"ext-dom": "*"
28+
"ext-dom": "*",
29+
"ext-gd": "*",
30+
"adhocore/cli": "^0.6.0"
2831
},
2932
"require-dev": {
3033
"phpunit/phpunit": "^6.5 || ^7.5"
@@ -35,10 +38,9 @@
3538
"*": "dist"
3639
}
3740
},
41+
"bin": ["bin/clish"],
3842
"scripts": {
39-
"post-root-package-install": [
40-
],
4143
"test": "vendor/bin/phpunit",
42-
"test:cov": "vendor/bin/phpunit --coverage-text --coverage-clover coverage.xml"
44+
"test:cov": "vendor/bin/phpunit --coverage-text --coverage-clover coverage.xml --coverage-html vendor/cov"
4345
}
4446
}

example.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99
* Licensed under MIT license.
1010
*/
1111

12+
use Ahc\CliSyntax\Exporter;
1213
use Ahc\CliSyntax\Highlighter;
1314

15+
require_once __DIR__ . '/src/Pretty.php';
16+
require_once __DIR__ . '/src/Exporter.php';
1417
require_once __DIR__ . '/src/Highlighter.php';
1518

1619
// Highlight code
17-
echo (new Highlighter("<?php echo 'Hello world!'; ?>\n"))->highlight();
20+
echo (new Highlighter("<?php echo 'Hello world!'; ?>" . PHP_EOL))
21+
->highlight();
1822

1923
// Highlight file
2024
echo Highlighter::for(__FILE__)->highlight();
2125

26+
// Export highlighted file as image
27+
Exporter::for(__FILE__)->export(__DIR__ . '/example.png');
28+
2229
// Magic string works too:
23-
// echo new Highlighter('<?php echo "Hello world!";');
30+
echo new Highlighter('<?php echo "Hello Again!";' . PHP_EOL);
2431
// echo Highlighter::for(__FILE__);

example.png

18.7 KB
Loading

font/dejavu.ttf

333 KB
Binary file not shown.

font/droidsans.ttf

114 KB
Binary file not shown.

font/ubuntu.ttf

201 KB
Binary file not shown.

src/Console/ClishCommand.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CLI-SYNTAX package.
7+
*
8+
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
9+
* <https://github.com/adhocore>
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')
29+
->option('-e --echo', 'Forces echo to STDOUT when --output is passed')
30+
->option('-f --file', \implode("\n", [
31+
'Input PHP file to highlight and/or export',
32+
'(will read from piped input if file not given)',
33+
]))
34+
->option('-F --font', 'Font to use for export to png')
35+
->usage(
36+
'<bold> $0</end> <comment>--file file.php</end> ## print<eol/>'
37+
. '<bold> cat file.php | $0</end> ## from piped stream<eol/>'
38+
. '<bold> $0</end> <comment>< file.php ## from redirected stdin<eol/>'
39+
. '<bold> $0</end> <comment>--file file.php --output file.png</end> ## export<eol/>'
40+
. '<bold> $0</end> <comment>--file file.php --output file.png --echo</end> ## print + export<eol/>'
41+
. '<bold> $0</end> <comment>-f file.php -o file.png -F dejavu</end> ## export in dejavu font<eol/>'
42+
);
43+
}
44+
45+
public function interact(Interactor $io)
46+
{
47+
if ($this->file) {
48+
return;
49+
}
50+
51+
$code = $io->readPiped(function ($reader) use ($io) {
52+
$io->warn('Type in or paste PHP Code below, Press Ctrl+D when done.', true);
53+
$io->warn('(Opening tag `<?php` will be prepended as required):', true);
54+
55+
return $reader->readAll();
56+
});
57+
58+
if ('' !== $code && \substr($code, 0, 5) != '<?php') {
59+
$eol = \substr_count(\rtrim($code), "\n") ? "\n\n" : '';
60+
$code = '<?php ' . $eol . $code;
61+
}
62+
63+
$this->set('code', $code);
64+
}
65+
66+
public function execute()
67+
{
68+
$code = $this->file ? \file_get_contents($this->file) : $this->code;
69+
70+
if ('' === \trim($code)) {
71+
return;
72+
}
73+
74+
$this->doExport($code);
75+
$this->doHighlight($code);
76+
}
77+
78+
protected function doHighlight(string $code = null)
79+
{
80+
if (!$this->output || $this->echo) {
81+
$this->app()->io()->raw((string) new Highlighter($code));
82+
}
83+
}
84+
85+
protected function doExport(string $code = null)
86+
{
87+
if (!$this->output) {
88+
return;
89+
}
90+
91+
if (!\is_dir(\dirname($this->output))) {
92+
\mkdir(\dirname($this->output), 0755, true);
93+
}
94+
95+
(new Exporter($code))->export($this->output, \array_filter(['font' => $this->font]));
96+
}
97+
}

0 commit comments

Comments
 (0)