Changeset 653


Ignore:
Timestamp:
Nov 28, 2018 1:40:13 AM (5 years ago)
Author:
anonymous
Message:

Update fancyTxt()

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Prefs.inc.php

    r644 r653  
    518518        // User_id must not be empty.
    519519        if ('' == $this->getParam('user_id')) {
    520             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
     520            $app->logMsg(sprintf('Cannot load prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
    521521            return false;
    522522        }
     
    591591        // Skip this method if not using the db.
    592592        if ('database' != $this->getParam('storagetype')) {
    593             $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_NOTICE, __FILE__, __LINE__);
     593            $app->logMsg('Prefs->save() does nothing unless using a database storagetype.', LOG_DEBUG, __FILE__, __LINE__);
    594594            return true;
    595595        }
     
    597597        // User_id must not be empty.
    598598        if ('' == $this->getParam('user_id')) {
    599             $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_WARNING, __FILE__, __LINE__);
     599            $app->logMsg(sprintf('Cannot save prefs because user_id not set.', null), LOG_DEBUG, __FILE__, __LINE__);
    600600            return false;
    601601        }
  • trunk/lib/Utilities.inc.php

    r652 r653  
    221221 * @return string         Cleaned text.
    222222 */
    223 function fancyTxt($text)
     223function fancyTxt($text, $extra_search=null, $extra_replace=null)
    224224{
    225225    $search = array();
    226226    $replace = array();
    227227
    228     // "double quoted text"  becomes  “double quoted text”
    229     $search['double_quotes']    = '/(^|[^\w=])(?:"|"|"|"|“)([^"]+?)(?:"|"|"|"|”)([^\w]|$)/ms'; // " is the same as " and " and "
    230     $replace['double_quotes']   = '$1“$2”$3';
    231 
    232     // text's apostrophes  become  text’s apostrophes
    233     $search['apostrophe']       = '/(\w)(?:\'|'|')(\w)/ms';
    234     $replace['apostrophe']      = '$1’$2';
    235 
    236     // 'single quoted text'  becomes  ‘single quoted text’
    237     $search['single_quotes']    = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/ms';
    238     $replace['single_quotes']   = '$1‘$2’$3';
    239 
    240     // plural posessives' apostrophes become posessives’
    241     $search['apostrophes']      = '/(s)(?:\'|'|')(\s)/ms';
    242     $replace['apostrophes']     = '$1’$2';
    243 
    244     // em--dashes  become em—dashes
    245     $search['em_dash']          = '/(\s*[^!<-])--([^>-]\s*)/';
    246     $replace['em_dash']         = '$1&mdash;$2';
    247 
    248     return preg_replace($search, $replace, $text);
     228    // "double quoted text"  →  “double quoted text”
     229    $search['_double_quotes']    = '/(?<=^|[^\w=(])(?:"|&quot;|&#0?34;|&#x22;|&ldquo;)([\w\'.
(—–-][^"]*?)(?:"|&quot;|&#0?34;|&#x22;|&rdquo;)(?=[^)\w]|$)/imsu'; // " is the same as &quot; and &#34; and &#034; and &#x22;
     230    $replace['_double_quotes']   = '“$1”';
     231
     232    // text's apostrophes  →  text’s apostrophes (except foot marks: 6'3")
     233    $search['_apostrophe']       = '/(?<=[a-z])(?:\'|&#0?39;)(?=\w)/imsu';
     234    $replace['_apostrophe']      = '’';
     235
     236    // 'single quoted text'  →  ‘single quoted text’
     237    $search['_single_quotes']    = '/(?<=^|[^\w=(])(?:\'|&#0?39;|&lsquo;)([\w"][^\']+?)(?:\'|&#0?39;|&rsquo;)(?=[^)\w]|$)/imsu';
     238    $replace['_single_quotes']   = '‘$1’';
     239
     240    // plural posessives' apostrophes  →  posessives’  (except foot marks: 6')
     241    $search['_apostrophes']      = '/(?<=s)(?:\'|&#0?39;|&rsquo;)(?=\s)/imsu';
     242    $replace['_apostrophes']     = '’';
     243
     244    // double--hyphens  →  en — dashes
     245    $search['_em_dash']          = '/(?<=[\w\s"\'”’)])--(?=[\w\s“”‘"\'(?])/imsu';
     246    $replace['_em_dash']         = ' – ';
     247
     248    // ...  →  

     249    $search['_elipsis']          = '/(?<=^|[^.])\.\.\.(?=[^.]|$)/imsu';
     250    $replace['_elipsis']         = '
';
     251
     252    if (is_array($extra_search) && is_array($extra_replace) && sizeof($extra_search) == sizeof($extra_replace)) {
     253        // Append additional search replacements.
     254        $search = array_merge($search, $extra_search);
     255        $replace = array_merge($replace, $extra_replace);
     256    }
     257
     258    return trim(preg_replace($search, $replace, $text));
    249259}
    250260
  • trunk/tests/EmailTest.php

    r622 r653  
    9999        $this->Email->setString('This is a {TEST}');
    100100        $this->Email->replace(array(
    101             'test' => '– you guessed it – a test!'
     101            'test' => '– you guessed it – a test! Sent from ' . __FILE__
    102102        ));
    103103        $result   = $this->Email->send();
  • trunk/tests/UtilitiesTest.php

    r545 r653  
    3535    function setUp()
    3636    {
    37         define('_CLI', true);
     37        // define('_CLI', true);
    3838        require dirname(__FILE__) . '/_config.inc.php';
    3939        require_once '../lib/Utilities.inc.php';
     
    255255    }
    256256
     257    function test_fancyTxt()
     258    {
     259        $strings = [
     260            'Due to the <a href="https://en.wikipedia.org/wiki/Camp_Fire_(2018)">fires in "Paradise," CA</a> it\'s a bad day.' => '• Due to the <a href="https://en.wikipedia.org/wiki/Camp_Fire_(2018)">fires in “Paradise,” CA</a> it’s a bad day.',
     261            'Simply--"\'check back\' later"--in 6\'3"...' => '• Simply – “‘check back’ later” – in 6\'3"
',
     262            '"Double "double \'single\'""--?' => '• "Double “double ‘single’”" – ?',
     263            '<a href="javascript:openPopup(\'/foo/bar.php\')">Click here</a>....' => '• <a href="javascript:openPopup(\'/foo/bar.php\')">Click here</a>....',
     264            'If you have any questions please <a href="/contact/">contact the ants\' mailboxes</a>.' => '• If you have any questions please <a href="/contact/">contact the ants’ mailboxes</a>.',
     265            'This URL: "http://䟋子.卷筒纞/?x=y&1=2#asdf"' => '• This URL: “http://䟋子.卷筒纞/?x=y&1=2#asdf”',
     266            // '\'...\' and "..."' => '• ‘
’ and “
”', // FIXME: this one doesn't work.
     267            '\'1...\' and "2..."' => '• ‘1
’ and “2
”',
     268            '\'One\'s self...\' and "Twosome"' => '• ‘One’s self
’ and “Twosome”',
     269            '"\'Well,\' said I, \'the gentleman at No. 4.\'"' => '• “‘Well,’ said I, ‘the gentleman at No. 4.’”',
     270            '"...an office in Leadenhall Street--and--"
     271"O\'Reilly\'s office?"
     272"What!" he roared.
     273"Yes, to-day."* She stood smiling.'
     274=>
     275'• “
an office in Leadenhall Street – and – ”
     276“O’Reilly’s office?”
     277“What!” he roared.
     278“Yes, to-day.”* She stood smiling.',
     279        ];
     280        foreach ($strings as $input => $expected) {
     281            $result = fancyTxt($input, ['/^/', '/^·/'], ['· ', '•']);
     282            $this->assertEquals($expected, $result, sprintf('Failed with input: %s', $input));
     283        }
     284    }
    257285}
  • trunk/tests/run_tests.sh

    r622 r653  
    77#  export DB_USER="test"
    88#  export DB_PASS="..."
     9
     10# Run an individual test like this:
     11# ./tests/run_tests.sh /Users/q/src/codebase/trunk/tests/UtilitiesTest.php
    912
    1013function err {
     
    3841# Config options go in phpunit.xml
    3942# phpunit --tap | grep -v '^ok '
    40 ../vendor/phpunit/phpunit/composer/bin/phpunit --stderr || err "\nSomething went wrong (code $?). If there is no output above, check the php_error_log";
     43../vendor/phpunit/phpunit/composer/bin/phpunit --stderr "$@" || err "\nSomething went wrong (code $?). If there is no output above, check the php_error_log";
    4144
Note: See TracChangeset for help on using the changeset viewer.