Skip to content

Internal Refactoring v1.0 #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ $phiki = new Phiki($environment);
$phiki->codeToHtml('...', 'my-language', 'my-theme');
```

If you don't have a JSON file containing the grammar file and would rather use a PHP array, you can instead create a `ParsedGrammar` instance from an array.

```php
$environment
->getGrammarRepository()
->register('my-language', ParsedGrammar::fromArray([
'scopeName' => 'source.language',
'patterns' => [
// ...
]
]));
```

### Terminal Output

Phiki has support for generating output designed for use in the terminal. This is available through the `codeToTerminal()` method which accepts the same parameters as the `codeToHtml()` method.
Expand Down Expand Up @@ -234,6 +247,10 @@ preg_match(): Compilation failed: length of lookbehind assertion is not limited
> [!NOTE]
> If you're running Phiki on PHP 8.2 or PHP 8.3, then you're still going to run into warnings or errors with these patched grammars since those versions of PHP do not use the latest version of PCRE2.

## Contributing

All contributions are welcome. Please consult the [CONTRIBUTING](./CONTRIBUTING.md) guide.

## Credits

* [Ryan Chandler](https://github.com/ryangjchandler)
Expand Down
104 changes: 6 additions & 98 deletions bin/phiki
Original file line number Diff line number Diff line change
Expand Up @@ -3,105 +3,13 @@

namespace Phiki\Console;

use Phiki\Phiki;
use Symfony\Component\Console\Application;

$vendorPath = dirname(__DIR__, 4) . '/vendor/autoload.php';
$localPath = dirname(__DIR__) . '/vendor/autoload.php';
require_once file_exists(__DIR__ . '/../vendor/autoload.php') ? __DIR__ . '/../vendor/autoload.php' : __DIR__ . '/../../autoload.php';

if (file_exists($vendorPath)) {
require_once $vendorPath;
} else {
require_once $localPath;
}
$application = new Application('Phiki');

function main(int $argc, array $argv): void {
if ($argc === 0) {
help();
exit(1);
}
$application->add(new HighlightCommand());
$application->setDefaultCommand('highlight', true);
$application->run();

$args = parse_args($argv);

if (! file_exists($args['file'])) {
echo "File not found: {$args['file']}\n";
exit(1);
}

$phiki = new Phiki();

echo $phiki->codeToTerminal(
file_get_contents($args['file']),
$args['grammar'],
$args['theme']
);
}

function parse_args(array $argv): array {
$args = [];
$processed = [];

foreach ($argv as $i => $arg) {
if (in_array($arg, ['-h', '--help'])) {
help();
exit(0);
}

if (in_array($arg, ['-g', '--grammar'])) {
$processed[] = $i;

if (! isset($argv[$i + 1]) || str_starts_with($argv[$i + 1], '-')) {
echo "Please specify a grammar using the -g or --grammar option.\n";
exit(1);
}

$args['grammar'] = $argv[$i + 1];
$processed[] = $i + 1;
}

if (in_array($arg, ['-t', '--theme'])) {
$processed[] = $i;

if (! isset($argv[$i + 1]) || str_starts_with($argv[$i + 1], '-')) {
echo "Please specify a theme using the -t or --theme option.\n";
exit(1);
}

$args['theme'] = $argv[$i + 1];
$processed[] = $i + 1;
}

if (isset($args['grammar']) && isset($args['theme'])) {
break;
}
}

$remaining = array_values(array_filter($argv, fn (int $index) => ! in_array($index, $processed), ARRAY_FILTER_USE_KEY));

if ($remaining === []) {
echo "Please specify a file to highlight.\n";
exit(1);
}

if (count($remaining) > 1) {
echo "Please specify only one file to highlight.\n";
exit(1);
}

$args['file'] = $remaining[0];

return $args;
}

function help(): void {
echo <<<'TXT'
Usage: phiki [options] <file>

Options:
-h, --help Show this help message
-g, --grammar Specify which grammar to use (e.g. php, javascript, ...)
-t, --theme Specify which theme to use (e.g. github-dark, ayu-dark, ...)

TXT;
}

main($argc - 1, array_slice($argv, 1));
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"bin": "bin/phiki",
"require": {
"php": "^8.2",
"league/commonmark": "^2.5.3"
"league/commonmark": "^2.5.3",
"symfony/console": "^7.2"
},
"autoload": {
"psr-4": {
Expand All @@ -33,7 +34,8 @@
"laravel/pint": "^1.18.1",
"phpstan/phpstan": "^2.0",
"phpstan/extension-installer": "^1.4.3",
"illuminate/support": "^11.30"
"illuminate/support": "^11.30",
"symfony/var-exporter": "^7.2"
},
"config": {
"allow-plugins": {
Expand All @@ -44,17 +46,18 @@
"scripts": {
"sample": [
"Composer\\Config::disableProcessTimeout",
"@php -S 127.0.0.1:8080 ./meta/sample.php"
"@php -S 127.0.0.1:8080 ./samples/serve.php"
],
"sample:debug": [
"Composer\\Config::disableProcessTimeout",
"herd debug -S 127.0.0.1:8080 ./meta/sample.php"
"herd debug -S 127.0.0.1:8080 ./samples/serve.php"
],
"update-sources": [
"Composer\\Config::disableProcessTimeout",
"@php ./meta/update-sources.php"
"@php ./scripts/update-sources.php"
],
"test": "@php -dmemory_limit=-1 vendor/bin/pest --enforce-time-limit --default-time-limit=1",
"lint": "vendor/bin/phpstan"
"lint": "vendor/bin/phpstan --memory-limit=-1",
"fmt": "vendor/bin/pint"
}
}
71 changes: 0 additions & 71 deletions meta/vscode-textmate.js

This file was deleted.

12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading