Skip to content

Commit

Permalink
Merge pull request #64 from jolicode/phar
Browse files Browse the repository at this point in the history
Add a support for PHAR building
  • Loading branch information
lyrixx authored Aug 31, 2021
2 parents f9fb293 + f3d7ce0 commit ed608d7
Show file tree
Hide file tree
Showing 11 changed files with 3,639 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/setup.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Install project and run tests
name: Run tests

on:
push:
branches: [ main ]
branches: [ master ]
pull_request:

jobs:
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/phar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Compile PHAR

on:
push:
branches: [ master ]
pull_request:

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0']

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php-versions }}

- name: Install dependencies (lib)
run: composer install --prefer-dist --no-progress --no-dev --optimize-autoloader --classmap-authoritative

- name: Install dependencies (phar builder)
run: composer install --prefer-dist --no-progress --optimize-autoloader --classmap-authoritative
working-directory: tools/phar

- name: Compile PHAR
run: vendor/bin/box compile
working-directory: tools/phar

- name: Ensure PHAR is OK
run: build/jolitypo.phar fr_FR README.md
working-directory: tools/phar
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

### 1.3.0 (not released yet) ###

* Add PHAR generation to use the library in CLI context

### 1.2.0 (2021-08-30) ###

* Switch from Travis to GitHub Action
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ $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](https://github.com/jolicode/JoliTypo/releases/latest)

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

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

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

- (Built-in) [Symfony Bundle](src/JoliTypo/Bridge/Symfony)
- (Built-in) [Twig extension](src/JoliTypo/Bridge/Twig)
- (Built-in) [CLI](https://github.com/jolicode/JoliTypo/releases/latest)
- [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
8 changes: 8 additions & 0 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class Fixer
const COPY = '©'; // ©
const ALL_SPACES = "\xE2\x80\xAF|\xC2\xAD|\xC2\xA0|\\s"; // All supported spaces, used in regexps. Better than \s

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'],
];


/**
* @var array HTML Tags to bypass
*/
Expand Down
2 changes: 2 additions & 0 deletions tools/phar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/vendor/
8 changes: 8 additions & 0 deletions tools/phar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# JoliTypo PHAR building

## Usage

```bash
composer install
vendor/bin/box compile
```
97 changes: 97 additions & 0 deletions tools/phar/bin/jolitypo
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env php
<?php

use JoliTypo\Fixer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../../../vendor/autoload.php';

$help = <<<EOTXT
JoliTypo is a tool fixing Microtypography glitches inside your HTML content.
# Usages
## Get the list of all available fixers:
<comment>%command.name% --list-rules foo bar</>
## Run the fixer on one file
<comment>%command.name% fr_FR index.html</>
## Run the fixer on one directory
<comment>%command.name% fr_FR directory/</>
## Specify some rules:
<comment>%command.name% fr_FR index.html --rule=FrenchQuotes --rule=Dash</>
EOTXT;

(new SingleCommandApplication('JoliTypo'))
->addArgument('locale', InputArgument::REQUIRED, 'Locale of the content to fix.')
->addArgument('path', InputArgument::REQUIRED, 'Path of file(s) to fix.')
->addOption('rule', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Rules used to fix the content')
->addOption('list-rules', null, InputOption::VALUE_NONE, 'List available rules')
->setHelp($help)
->setCode(function (InputInterface $input, OutputInterface $output): int {
$io = new SymfonyStyle($input, $output);

if ($input->getOption('list-rules')) {
$list = [];
foreach ((new Finder())->in(__DIR__ . '/../../../src/JoliTypo/Fixer')->sortByName()->files() as $file) {
$list[] = $file->getFilenameWithoutExtension();
}
$io->listing($list);

return 0;
}

$locale = $input->getArgument('locale');
$path = $input->getArgument('path');
$rules = $input->getOption('rule');

if (!$rules) {
if (!array_key_exists($locale, Fixer::RECOMMENDED_RULES_BY_LOCALE)) {
throw new \InvalidArgumentException(sprintf('There is no recommended rules for "%s" locale. Please specify manually the rules to apply.', $locale));
}

$rules = Fixer::RECOMMENDED_RULES_BY_LOCALE[$locale];
}


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

if (is_dir($path)) {
foreach ((new Finder())->in($path)->files() as $file) {
if ($output->isVerbose()) {
$io->comment(sprintf('Fixing <comment>%s</comment>', $file->getRealPath()));
}
$fixedContent = $fixer->fix(file_get_contents($file->getRealPath()));
file_put_contents($file->getRealPath(), $fixedContent);
}

$io->success(sprintf('All files in "%s" has been fixed with success!', $path));
} elseif (is_file($path)) {
$fixedContent = $fixer->fix(file_get_contents($path));
file_put_contents($path, $fixedContent);

$io->success(sprintf('"%s" content has been fixed with success!', $path));
} else {
throw new \InvalidArgumentException(sprintf('The path "%s" does not exist.', $path));
}

return 0;
})
->run()
;
19 changes: 19 additions & 0 deletions tools/phar/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"base-path": "../../",
"finder": [
{
"in": ".",
"name": [
"*.php",
"*.txt",
"*.properties",
"*.ini",
"*.dic"
],
"exclude": ["tests", "Tests", "phpunit"]
}
],
"git-version": "package_version",
"main": "tools/phar/bin/jolitypo",
"output": "tools/phar/build/jolitypo.phar"
}
29 changes: 29 additions & 0 deletions tools/phar/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "jolicode/jolitypo-phar",
"description": "Tooling for building JoliTypo PHAR",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Grégoire Pineau",
"email": "[email protected]"
},
{
"name": "Mathieu Santostefano",
"email": "[email protected]"
}
],
"require": {
"symfony/console": "^5.3",
"symfony/finder": "^5.3"
},
"require-dev": {
"humbug/box": "^3.13",
"humbug/php-scoper": "dev-master#d3c7b5cde60ce6e269a202d96ba0549ac7528319 as 0.14.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
}
}
Loading

0 comments on commit ed608d7

Please sign in to comment.