Skip to content

Commit

Permalink
Build a .phar version
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Aug 30, 2021
1 parent f9fb293 commit 0351c5a
Show file tree
Hide file tree
Showing 7 changed files with 3,040 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.phpunit.result.cache
/composer.lock
/vendor/
/vendor-bin/box/vendor/
/build/
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ $fixer = new Fixer(array("Trademark", "SmartQuotes"));
$fixedContent = $fixer->fixString('Here is a "protip(c)"!'); // Here is a “protip©”!
```

CLI usage
=========

You can run a standalone version of JoliTypo by downloading [the PHAR version](#phar)

Run `jolitypo.phar --help` to know how to configure the Fixer.

```
Fix Microtypography glitches inside your HTML content.
Usage: ./jolitypo.phar [--rules="Ellipsis,Dimension,Unit,Dash,SmartQuotes,NoSpaceBeforeComma,CurlyQuote,Hyphen,Trademark"] --locale locale --file file [--quiet] [--help]
Required Arguments:
--locale locale
Locale of the content to fix.
--file file
File to fix.
Optional Arguments:
--rules="Ellipsis,Dimension,Unit,Dash,SmartQuotes,NoSpaceBeforeComma,CurlyQuote,Hyphen,Trademark"
Rules used to fix the content, comma separated.
--quiet
Do not output anything.
--help
Show this help.
```

Installation
============

Expand All @@ -71,6 +98,7 @@ Integrations

- (Built-in) [Symfony Bundle](src/JoliTypo/Bridge/Symfony)
- (Built-in) [Twig extension](src/JoliTypo/Bridge/Twig)
- (Built-in) [CLI](jolitypo)
- [Wordpress plugin](http://wordpress.org/plugins/typofr/)
- [Drupal module](https://github.com/Anaethelion/JoliTypo-for-Drupal)
- [Joomla plugin](https://github.com/YGomiero/typographe)
Expand Down Expand Up @@ -247,6 +275,13 @@ Add your own Fixer / Contribute a Fixer
- Implement `JoliTypo\FixerInterface`;
- Send your Pull request.

Build the PHAR version
======================

Install [Box](https://github.com/box-project/box) and its dependencies with `composer bin box require --dev humbug/box`

Then, run `composer run compile`

### Contribution guidelines

- You MUST write code in english;
Expand Down
45 changes: 45 additions & 0 deletions box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"chmod": "0755",
"directories": [
"src"
],
"files": [
"LICENSE",
"vendor/autoload.php"
],
"finder": [
{
"name": "*.php",
"in": "vendor/composer"
},
{
"name": "*.php",
"exclude": ["tests"],
"in": "vendor/org_heigl"
},
{
"name": "*.properties",
"exclude": ["tests"],
"in": "vendor/org_heigl"
},
{
"name": "*.ini",
"exclude": ["tests"],
"in": "vendor/org_heigl"
},
{
"name": "*.dic",
"exclude": ["tests"],
"in": "vendor/org_heigl"
},
{
"name": "*.txt",
"exclude": ["tests"],
"in": "vendor/org_heigl"
}
],
"git-version": "package_version",
"main": "jolitypo",
"output": "build/jolitypo.phar",
"stub": true
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
"symfony/phpunit-bridge": "^5.0",
"symfony/framework-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/twig-bundle": "^3.4.26|^4.1.12|^5.0",
"symfony/yaml": "^3.4.26|^4.1.12|^5.0"
"symfony/yaml": "^3.4.26|^4.1.12|^5.0",
"bamarni/composer-bin-plugin": "^1.4"
},
"minimum-stability": "dev",
"prefer-stable": true,
"conflict": {
"ext-apc": "3.1.11"
},
Expand All @@ -36,6 +39,7 @@
},
"scripts": {
"test": "vendor/bin/simple-phpunit -c phpunit.xml.dist",
"cs": "vendor/bin/php-cs-fixer fix"
"cs": "vendor/bin/php-cs-fixer fix",
"compile": "vendor/bin/box compile"
}
}
193 changes: 193 additions & 0 deletions jolitypo
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env php
<?php

use JoliTypo\Fixer;

if (file_exists(__DIR__ . '/vendor/autoload.php')) {
$loader = require(__DIR__ . '/vendor/autoload.php');
} elseif (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
$loader = require(__DIR__ . '/../../../vendor/autoload.php');
} else {
throw new \RuntimeException('Unable to load autoloader.');
}

final class Cli
{
const DESCRIPTION = 'Fix Microtypography glitches inside your HTML content.';
const RECOMMENDED_RULES_BY_LOCALE = [
'en_GB' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'fr_FR' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'fr_CA' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
'de_DE' => ['Ellipsis', 'Dimension', 'Unit', 'Dash', 'SmartQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'],
];
const ARGUMENTS = [
'rules' => [
'name' => 'rules',
'info' => 'Rules used to fix the content, comma separated.',
'required' => false,
'example' => "Ellipsis,Dimension,Unit,Dash,SmartQuotes,NoSpaceBeforeComma,CurlyQuote,Hyphen,Trademark",
],
'locale' => [
'name' => 'locale',
'info' => 'Locale of the content to fix.',
'required' => true,
],
'file' => [
'name' => 'file',
'info' => 'File to fix.',
'required' => true,
],
'quiet' => [
'name' => 'quiet',
'info' => 'Do not output anything.',
'required' => false,
],
'help' => [
'name' => 'help',
'info' => 'Show this help.',
'required' => false,
'flag' => true,
],
];

private $arguments = [];
private $command;

public function __construct()
{
$this->command = $_SERVER['argv'][0];
}

public function parse()
{
$options = '';
$longOptions = array_map(function ($rule) {
$flag = $rule['required'] ? ':' : '::';

return $rule['name'] . $flag;
}, self::ARGUMENTS);

$this->arguments = getopt($options, $longOptions) ?: [];
}

public function getOption(string $name)
{
return $this->arguments[$name] ?? false;
}

public function hasOption(string $name): bool
{
return isset($this->arguments[$name]);
}

public function validate(): bool
{
$valid = true;

foreach (self::ARGUMENTS as $arg) {
if ($arg['required'] && !$this->hasOption($arg['name'])) {
$this->log("Please specify fixer {$arg['name']} with the option --{$arg['name']}");
$valid = false;
}
}

return $valid;
}

public function showUsage()
{
$required = [];
$optional = [];
$usage = $this->command;

foreach (self::ARGUMENTS as $name => $arg) {
$prefix = $postfix = '';
if ($arg['required']) {
$required[$name] = $arg;
} else {
$optional[$name] = $arg;
$prefix = '[';
$postfix = ']';
}
$usage .= ' ' . $prefix . $this->formatUsage($name, $arg) . $postfix;
}

$this->log(self::DESCRIPTION);
$this->log(PHP_EOL . 'Usage: ' . trim($usage));

$this->log(PHP_EOL . 'Required Arguments:');
foreach ($required as $name => $info) {
$value = $this->formatUsage($name, $info);
$this->log("\t$value");
$this->log("\t\t{$info['info']}");
}

$this->log(PHP_EOL . 'Optional Arguments:');
foreach ($optional as $name => $info) {
$value = $this->formatUsage($name, $info);
$this->log("\t$value");
$this->log("\t\t{$info['info']}");
}
}

public function log(string $message)
{
if (!$this->hasOption('quiet')) {
echo $message . PHP_EOL;
}
}

private function formatUsage($name, $rule): string
{
$example = $rule['example'] ?? $name;
$value = $rule['required'] ? " $name" : "=\"$example\"";
$value = isset($rule['flag']) && $rule['flag'] ? '' : $value;

return '--' . $name . $value;
}
}

$cli = new Cli();

try {
$cli->parse();
} catch (Exception $e) {
$cli->log($e->getMessage());
exit(1);
}

if ($cli->hasOption('help')) {
$cli->showUsage();
exit(0);
}

if (!$cli->validate()) {
exit(1);
}

$locale = $cli->getOption('locale');
$file = $cli->getOption('file');
$rules = $cli->getOption('rules');

if ($rules) {
$rules = explode(',', $rules);
} elseif (array_key_exists($locale, Cli::RECOMMENDED_RULES_BY_LOCALE)) {
$rules = Cli::RECOMMENDED_RULES_BY_LOCALE[$locale];
} else {
$cli->log(sprintf('There is no recommended rules for "%s" locale. Please specify manually the rules to apply.', $locale));
exit(1);
}

if (!file_exists($file)) {
$cli->log(sprintf('The file "%s" does not exist.', $file));
exit(1);
}

$fixer = new Fixer($rules);
$fixer->setLocale($locale);

$fixedContent = $fixer->fix(file_get_contents($file));
file_put_contents($file, $fixedContent);

$cli->log(sprintf('"%s" content has been fixed with success!', $file));

7 changes: 7 additions & 0 deletions vendor-bin/box/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"minimum-stability": "dev",
"prefer-stable": true,
"require-dev": {
"humbug/box": "^3.13"
}
}
Loading

0 comments on commit 0351c5a

Please sign in to comment.