Giter Club home page Giter Club logo

Comments (6)

nilshoerrmann avatar nilshoerrmann commented on August 21, 2024 1

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.

nilshoerrmann avatar nilshoerrmann commented on August 21, 2024

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 = 'β€Ή'; // &lsaquo;
    const RSAQUO = 'β€Ί'; // &rsaquo;
    const LSQUO = 'β€˜'; // &lsquo;
    const RSQUO = '’ '; // &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.

damienalexandre avatar damienalexandre commented on August 21, 2024

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.

nilshoerrmann avatar nilshoerrmann commented on August 21, 2024

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.

damienalexandre avatar damienalexandre commented on August 21, 2024

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.

nilshoerrmann avatar nilshoerrmann commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.