Comments (6)
Yes, I'd like to do that but first need to understand the library a bit better (first time using it today). The StateBag
is still a blackbox to me and the smart quote handling seems to be the most complex part to start with :)
from jolitypo.
As a quick fix, I duplicated the SmartQuotes fixer with rules for nested quotes. If this is run additionally, everything is converted correctly:
<?php
namespace JoliTypo\Fixer;
use JoliTypo\Exception\BadFixerConfigurationException;
use JoliTypo\Fixer;
use JoliTypo\FixerInterface;
use JoliTypo\LocaleAwareFixerInterface;
use JoliTypo\StateBag;
use JoliTypo\Fixer\BaseOpenClosePair;
class NestedQuotes extends BaseOpenClosePair implements
FixerInterface,
LocaleAwareFixerInterface
{
const LSAQUO = 'βΉ'; // ‹
const RSAQUO = 'βΊ'; // ›
const LSQUO = 'β'; // ‘
const RSQUO = 'β '; // ’
const SBQUO = 'β'; // &sdquo;
protected $opening;
protected $openingSuffix = '';
protected $closing;
protected $closingPrefix = '';
public function __construct($locale)
{
$this->setLocale($locale);
}
public function fix($content, StateBag $stateBag = null)
{
if (!$this->opening || !$this->closing) {
throw new BadFixerConfigurationException();
}
// Fix complex siblings cases
if ($stateBag) {
$content = $this->fixViaState(
$content,
$stateBag,
'SmartQuotesOpenSolo',
"@(^|\s|\()'([^']*)$@im",
"@(^|[^']+)'@im",
$this->opening . $this->openingSuffix,
$this->closingPrefix . $this->closing
);
}
// Fix simple cases
$content = preg_replace(
"@(^|\s|\()'([^']+)'@im",
'$1' .
$this->opening .
$this->openingSuffix .
'$2' .
$this->closingPrefix .
$this->closing,
$content
);
return $content;
}
/**
* Default configuration for supported lang.
*
* @param string $locale
*/
public function setLocale($locale)
{
// Handle from locale + country
switch (strtolower($locale)) {
// ββ¦β
case 'pt-br':
$this->opening = NestedQuotes::LSQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::RSQUO;
$this->closingPrefix = '';
return;
// Β«β¦Β»
case 'de-ch':
$this->opening = NestedQuotes::LSAQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::RSAQUO;
$this->closingPrefix = '';
return;
}
// Handle from locale only
$short = Fixer::getLanguageFromLocale($locale);
switch ($short) {
// Β« β¦ Β»
case 'fr':
$this->opening = NestedQuotes::LSAQUO;
$this->openingSuffix = NestedQuotes::NO_BREAK_SPACE;
$this->closing = NestedQuotes::RSAQUO;
$this->closingPrefix = NestedQuotes::NO_BREAK_SPACE;
break;
// Β«β¦Β»
case 'hy':
case 'az':
case 'hz':
case 'eu':
case 'be':
case 'ca':
case 'el':
case 'it':
case 'no':
case 'fa':
case 'lv':
case 'pt':
case 'ru':
case 'es':
case 'uk':
$this->opening = NestedQuotes::LSAQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::RSAQUO;
$this->closingPrefix = '';
break;
// ββ¦β
case 'de':
case 'ka':
case 'cs':
case 'et':
case 'is':
case 'lt':
case 'mk':
case 'ro':
case 'sk':
case 'sl':
case 'wen':
$this->opening = NestedQuotes::SBQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::LSQUO;
$this->closingPrefix = '';
break;
// ββ¦β
case 'en':
case 'us':
case 'gb':
case 'af':
case 'ar':
case 'eo':
case 'id':
case 'ga':
case 'ko':
case 'br':
case 'th':
case 'tr':
case 'vi':
$this->opening = NestedQuotes::LSQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::RSQUO;
$this->closingPrefix = '';
break;
// ββ¦β
case 'fi':
case 'sv':
case 'bs':
$this->opening = NestedQuotes::RSQUO;
$this->openingSuffix = '';
$this->closing = NestedQuotes::RSQUO;
$this->closingPrefix = '';
break;
}
}
/**
* @param string $opening
*/
public function setOpening($opening)
{
$this->opening = $opening;
}
/**
* @param string $openingSuffix
*/
public function setOpeningSuffix($openingSuffix)
{
$this->openingSuffix = $openingSuffix;
}
/**
* @param string $closing
*/
public function setClosing($closing)
{
$this->closing = $closing;
}
/**
* @param string $closingPrefix
*/
public function setClosingPrefix($closingPrefix)
{
$this->closingPrefix = $closingPrefix;
}
}
Maybe this can be incorporated in the Smartquotes fixer itself.
from jolitypo.
Thanks for reporting the issue and sharing your solution, it looks good to me - the SmartQuote fixer could do that by running first with '
and second with "
I guess.
How do you feel about creation a Pull Request with this in mind, and a new test case to make sure this bug is taken care of for good?
Thanks!
from jolitypo.
Thanks for reporting the issue and sharing your solution, it looks good to me - the SmartQuote fixer could do that by running first with ' and second with " I guess.
I noticed during testing that this cannot be merged with the smart quotes fixer because this would conflict with the curly quotes fixer. Instead it needs to be merged with the curly quotes fixer to handle edge cases as well.
from jolitypo.
Interesting π
Have you tried removing the CurlyQuote fixer and using only the SmartQuote fixer?
Because the text you are fixing could match the CurlyQuote fixer.
from jolitypo.
Yes, I did try that but in the end curly quotes and nested quotes have similar matching pattern so they need to be looked at simultaneously:
- If curly quote are handled first, it takes away all the closing single quotes.
- If nested quotes are handled first, it takes inner apostropes for closing quotes.
It's a question of execution order and lookahead.
from jolitypo.
Related Issues (20)
- Improve Symfony / Twig documentation with the filter tag
- Fix deprecation in tree builder for Symfony 4.2+ HOT 2
- Quotes for Switzerland are not correct HOT 3
- Fix autoload of tests classes with composer HOT 3
- Expose Hyphen options
- Deprecating notice message HOT 1
- Release a standalone version (.phar) with external config file
- Add gender-neutral Filter to handle interpuncts in French HOT 4
- Prevent shy space from being added multiple times
- Run test suite through github action
- Distribute a PHAR version of JoliTypo HOT 1
- Automatically build PHAR and upload it for each release
- Add an "unfix" method HOT 1
- German uses both βquotesβ and Β»quotesΒ« HOT 1
- FrenchNoBreakSpace do not add spaces for : ; ! and ? characters if they don't already exist HOT 2
- \s matches
- Issue Unit fixer , \n are removed HOT 1
- issue deconding special character HOT 3
- Add Dark Mode to the website HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jolitypo.