Skip to content

Commit

Permalink
Add NumericFixer to add Non Breaking Space between numeric and unit
Browse files Browse the repository at this point in the history
fix #15.
  • Loading branch information
damienalexandre committed Dec 13, 2015
1 parent 2ce5ce2 commit 351221f
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CHANGELOG

### ??? ###

* add Numeric fixer, adding NoBreakSpace between numeric and their units, fix #15

### 0.2.0 (2015-07-13) ###

* new NoSpaceBeforeComma fixer, cleaning badly placed spaces close to commas
Expand Down
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ $fixed_content = $fixer->fix('<p>"Tell me Mr. Anderson... what good is a phone c
It's designed to be:

- language agnostic (you can fix `fr_FR`, `fr_CA`, `en_US`... You tell JoliTypo what to fix);
- fully tested;
- easy to integrate into modern PHP project (composer and autoload);
- robust (make use of `\DOMDocument` instead of parsing HTML with dummy regexp);
- smart enough to avoid Javascript, Code, CSS processing... (configurable protected tags list);
- fully tested;
- fully open and usable in any project (MIT License).

[![Build Status](https://travis-ci.org/jolicode/JoliTypo.png?branch=master)](https://travis-ci.org/jolicode/JoliTypo)
Expand Down Expand Up @@ -60,7 +60,7 @@ composer require jolicode/jolitypo "~0.2.0"

*Usage outside composer is also possible, just add the `src/` directory to any PSR-0 compatible autoloader.*

Integration
Integrations
===========

- (Official) [Symfony2 Bundle and twig extension](https://github.com/jolicode/JoliTypoBundle)
Expand Down Expand Up @@ -139,6 +139,11 @@ Trademark
Handle trade­mark symbol ``, a reg­is­tered trade­mark symbol `®`, and a copy­right symbol `©`. This fixer replace
commonly used approximations: `(r)`, `(c)` and `(TM)`. A non-breaking space is put between numbers and copyright symbol too.

Numeric
---------

Add a non-breaking space between a numeric and it's unit. Like this: `12_h`, `42_฿` or `88_%`.

**It is really easy to make your own Fixers, feel free to extend the provided ones if they do not fit your typographic rules.**

Fixer recommendations by locale
Expand All @@ -148,7 +153,7 @@ en_GB
-----

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Dash', 'EnglishQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'EnglishQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('en_GB'); // Needed by the Hyphen Fixer
```

Expand All @@ -158,7 +163,7 @@ fr_FR
Those rules apply most of the recommendations of "Abrégé du code typographique à l'usage de la presse", ISBN: 9782351130667.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Dash', 'FrenchQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'FrenchQuotes', 'FrenchNoBreakSpace', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_FR'); // Needed by the Hyphen Fixer
```

Expand All @@ -168,7 +173,7 @@ fr_CA
Mostly the same as fr_FR, but the space before punctuation points is not mandatory.

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Dash', 'FrenchQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'FrenchQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('fr_CA'); // Needed by the Hyphen Fixer
```

Expand All @@ -178,7 +183,7 @@ de_DE
Mostly the same as en_GB, according to [Typefacts](http://typefacts.com/) and [Wikipedia](http://de.wikipedia.org/wiki/Typografie_f%C3%BCr_digitale_Texte).

```php
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Dash', 'GermanQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer = new Fixer(array('Ellipsis', 'Dimension', 'Numeric', 'Dash', 'GermanQuotes', 'NoSpaceBeforeComma', 'CurlyQuote', 'Hyphen', 'Trademark'));
$fixer->setLocale('de_DE'); // Needed by the Hyphen Fixer
```

Expand Down Expand Up @@ -233,10 +238,10 @@ $fixed_content = $fixer->fix("<p>Fixed...</p> <pre>Not fixed...</pre> <p>Fixed.
Add your own Fixer / Contribute a Fixer
=======================================

- Write test
- A Fixer is run on a piece of text, no HTML to deal with
- Implement `JoliTypo\FixerInterface`
- Pull request
- Write test;
- A Fixer is run on a piece of text, no HTML to deal with;
- Implement `JoliTypo\FixerInterface`;
- Pull request;
- PROFIT!!!

Compatibility & OS support restrictions
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Global
- [x] Improve the EnglishTest;
- [ ] Add more pre-configured locale in the README, with the appropriate tests;
- [ ] Add a http://cldr.unicode.org/ Fixer for number formatting (thx @g_marty for the tip!);
- [ ] A website in with you copy some HTML and get a fixed HTML outputted;
- [x] A website in with you copy some HTML and get a fixed HTML outputted;

This comment has been minimized.

Copy link
@pyrech

pyrech Dec 13, 2015

Member

in which?

- [ ] Allow to apply thoses rules: http://en.wikipedia.org/wiki/Non-English_usage_of_quotation_marks#Hungarian and document them

Locale to improve
Expand Down
1 change: 1 addition & 0 deletions src/JoliTypo/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Fixer
const TRADE = ""; // &trade;
const REG = "®"; // &reg;
const COPY = "©"; // &copy;
const ALL_SPACES = '\xE2\x80\xAF|\xC2\xAD|\xC2\xA0|\s'; // All supported spaces, used in regexps. Better than \s

/**
* @var array HTML Tags to bypass
Expand Down
2 changes: 1 addition & 1 deletion src/JoliTypo/Fixer/Dimension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Dimension implements FixerInterface
{
public function fix($content, StateBag $state_bag = null)
{
$content = preg_replace('@(\d+["\']?)( ?)x\\2(?=\d)@', '$1$2'.Fixer::TIMES.'$2', $content);
$content = preg_replace('@(\d+["\']?)('.Fixer::ALL_SPACES.')?x('.Fixer::ALL_SPACES.')?(?=\d)@', '$1$2'.Fixer::TIMES.'$2', $content);

return $content;
}
Expand Down
21 changes: 21 additions & 0 deletions src/JoliTypo/Fixer/Numeric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace JoliTypo\Fixer;

use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\StateBag;

/**
* Add nbsp between numeric and units.
*/
class Numeric implements FixerInterface
{
public function fix($content, StateBag $state_bag = null)
{
// Support a wide range of currencies
$content = preg_replace('@([\dº])( +)([º°%฿₵¢₡$₫֏€ƒ₲₴₭£₤₺₦₨₱៛₹$₪৳₸₮₩¥\w]{1})@', '$1'.Fixer::NO_BREAK_SPACE.'$3', $content);

return $content;
}
}
4 changes: 2 additions & 2 deletions tests/JoliTypo/Tests/EnglishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class EnglishTest extends \PHPUnit_Framework_TestCase
{
private $en_fixers = array('Ellipsis', 'Dimension', 'Dash', 'EnglishQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');
private $en_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'EnglishQuotes', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<!-- From https://en.wikipedia.org/wiki/Gif#Pronunciation -->
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testDoubleQuoteMess()
public function testHtmlHeart()
{
$fixed = <<<HTML
<p>We &lt;3 web.</p>
<p>We &lt;3&nbsp;web.</p>
HTML;

$to_fix = <<<HTML
Expand Down
27 changes: 27 additions & 0 deletions tests/JoliTypo/Tests/Fixer/NumericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace JoliTypo\Tests\Fixer;

use JoliTypo\Fixer;

class NumericTest extends \PHPUnit_Framework_TestCase
{
public function testNumericUnits()
{
$fixer = new Fixer\Numeric();
$this->assertInstanceOf('JoliTypo\Fixer\Numeric', $fixer);

$this->assertEquals("Test", $fixer->fix("Test"));
$this->assertEquals("1".Fixer::NO_BREAK_SPACE."h", $fixer->fix("1 h"));
$this->assertEquals("2".Fixer::NO_BREAK_SPACE."฿", $fixer->fix("2 ฿"));
$this->assertEquals("3".Fixer::NO_BREAK_SPACE."things", $fixer->fix("3 things"));
$this->assertEquals("4,5".Fixer::NO_BREAK_SPACE."km", $fixer->fix("4,5 km"));
$this->assertEquals("5.1".Fixer::NO_BREAK_SPACE."deg", $fixer->fix("5.1 deg"));
$this->assertEquals("6".Fixer::NO_BREAK_SPACE."ºC", $fixer->fix("6 ºC"));
$this->assertEquals("7".Fixer::NO_BREAK_SPACE."$", $fixer->fix("7 $"));
$this->assertEquals("".Fixer::NO_BREAK_SPACE."18′ 30″", $fixer->fix("8º 18′ 30″"));
$this->assertEquals("9".Fixer::NO_BREAK_SPACE."hours", $fixer->fix("9 hours"));
$this->assertEquals("10e position", $fixer->fix("10e position")); // We can't fix this reliably
$this->assertEquals("".Fixer::NO_BREAK_SPACE."11", $fixer->fix("nº 11"));
$this->assertEquals("12".Fixer::NO_BREAK_SPACE."%", $fixer->fix("12 %"));
}
}
29 changes: 25 additions & 4 deletions tests/JoliTypo/Tests/FrenchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class FrenchTest extends \PHPUnit_Framework_TestCase
{
private $fr_fixers = array('Ellipsis', 'Dimension', 'Dash', 'FrenchQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');
private $fr_fixers = array('Numeric', 'Ellipsis', 'Dimension', 'Dash', 'FrenchQuotes', 'FrenchNoBreakSpace', 'CurlyQuote', 'Hyphen', 'Trademark');

const TOFIX = <<<TOFIX
<p>Ceci est à remplacer par une fâble :p</p>
Expand All @@ -22,7 +22,7 @@ class FrenchTest extends \PHPUnit_Framework_TestCase
pre
</code></pre>
<p>Ceci &eacute;té un "CHOQUE"&nbsp;! Son salon fait 4x4m, ce qui est plutôt petit.</p>
<p>Ceci &eacute;té un "CHOQUE"&nbsp;! Son salon fait 4x4 m, ce qui est plutôt petit.</p>
<p>Les trés long mots sont tronqués, comme "renseignements" par exemple.</p>
Expand All @@ -46,7 +46,7 @@ class FrenchTest extends \PHPUnit_Framework_TestCase
pre
</code></pre>
<p>Ceci &eacute;t&eacute; un &laquo;&nbsp;CHOQUE&nbsp;&raquo;&#8239;! Son salon fait 4&times;4m, ce qui est plut&ocirc;t petit.</p>
<p>Ceci &eacute;t&eacute; un &laquo;&nbsp;CHOQUE&nbsp;&raquo;&#8239;! Son salon fait 4&times;4&nbsp;m, ce qui est plut&ocirc;t petit.</p>
<p>Les tr&eacute;s long mots sont tronqu&eacute;s, comme &laquo;&nbsp;rensei&shy;gne&shy;ments&nbsp;&raquo; par exemple.</p>
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testDoubleQuoteMess()

$fixed = <<<HTML
<p>A la sauce &laquo;&nbsp;<a href="http://composer.json.jolicode.com">compo&shy;ser.json</a>&nbsp;&raquo;
atti&shy;rera forc&eacute;&shy;ment plus notre atten&shy;tion qu&rsquo;une lettre de moti&shy;va&shy;tion de 4 pages en &laquo;&nbsp;.docx&nbsp;&raquo;</p>
atti&shy;rera forc&eacute;&shy;ment plus notre atten&shy;tion qu&rsquo;une lettre de moti&shy;va&shy;tion de 4&nbsp;pages en &laquo;&nbsp;.docx&nbsp;&raquo;</p>
HTML;

$to_fix = <<<HTML
Expand Down Expand Up @@ -131,6 +131,27 @@ public function testNoBreakingSpaceInsideGoodQuotes()

$to_fix = <<<HTML
&laquo; test &raquo; et &laquo;test&raquo; sont dans un bateau.
HTML;

$this->assertEquals($fixed, $fixer->fix($to_fix));
}

/**
* @see https://github.com/jolicode/JoliTypo/issues/15
*/
public function testNumericDoesNotBreakOtherFixers()
{
$fixer = new Fixer($this->fr_fixers);

$fixer->setLocale('fr');
$this->assertInstanceOf('JoliTypo\Fixer', $fixer);

$fixed = <<<HTML
2&nbsp;&times;&nbsp;5&nbsp;doit &ecirc;tre corrig&eacute;, et 2&nbsp;h aussi.
HTML;

$to_fix = <<<HTML
2 x 5 doit être corrigé, et 2 h aussi.
HTML;

$this->assertEquals($fixed, $fixer->fix($to_fix));
Expand Down

0 comments on commit 351221f

Please sign in to comment.