settings; static::settingsUnserialize($settings); $form['help'] = array( '#type' => 'markup', '#value' => '

' . t('Enable the following typographic refinements:') . '

', ); // Smartypants settings. $form['smartypants_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use typographers quotation marks and dashes (!smartylink)', array( '!smartylink' => \Drupal::l('SmartyPants', Url::fromUri('http://daringfireball.net/projects/smartypants/')), )), '#default_value' => $settings['smartypants_enabled'], ); // Smartypants hyphenation settings. // Uses the same values as the parse attributes in the // SmartyPants::process() function. $form['smartypants_hyphens'] = array( '#type' => 'select', '#title' => t('Hyphenation settings for SmartyPants'), '#default_value' => $settings['smartypants_hyphens'], '#options' => array( 1 => t('“--” for em-dashes; no en-dash support'), 3 => t('“--” for em-dashes; “---” for en-dashes'), 2 => t('“---” for em-dashes; “--” for en-dashes'), ), ); // Replace space_hyphens with em-dash. $form['space_hyphens'] = array( '#type' => 'checkbox', '#title' => t('Replace stand-alone dashes (normal dashes between whitespace) em-dashes.'), '#description' => t('" - " will turn into " — ".'), '#default_value' => $settings['space_hyphens'], ); // Remove widows settings. $form['widont_enabled'] = array( '#type' => 'checkbox', '#title' => t('Remove widows'), '#default_value' => $settings['widont_enabled'], ); // Remove widows settings. $form['hyphenate_shy'] = array( '#type' => 'checkbox', '#title' => t('Replace = with &shy;'), '#description' => t('Words may be broken at the hyphenation points marked by “=”.'), '#default_value' => $settings['hyphenate_shy'], ); // Replace normal spaces with non-breaking spaces before "double punctuation // marks". This is especially useful in french. $form['space_to_nbsp'] = array( '#type' => 'checkbox', '#title' => t('Replace normal spaces with non-breaking spaces before "double punctuation marks" !marks.', array('!marks' => '(!?:;)')), '#description' => t('This is especially useful for french.'), '#default_value' => $settings['space_to_nbsp'], ); // Wrap caps settings. $form['wrap_caps'] = array( '#type' => 'checkbox', '#title' => t('Wrap caps'), '#default_value' => $settings['wrap_caps'], ); // Wrap ampersand settings. $form['wrap_ampersand'] = array( '#type' => 'checkbox', '#title' => t('Wrap ampersands'), '#default_value' => $settings['wrap_ampersand'], ); $form['wrap_abbr'] = array( '#type' => 'select', '#title' => t('Thin space in abbreviations'), '#description' => t('Wraps abbreviations with !span and inserts space after the dots.', array('!span' => '<span class="abbr">…</span>')), '#default_value' => $settings['wrap_abbr'], '#options' => array( 0 => t('Do nothing'), 4 => t('Insert no space'), 1 => t('“U+202F“ Narrow no-break space'), 2 => t('“U+2009“ Thin space'), 3 => t('span with margin-left: 0.167em'), ), ); $form['wrap_numbers'] = array( '#type' => 'select', '#title' => t('Digit grouping in numbers'), '#description' => t('Wraps numbers with !span and inserts thin space for digit grouping.', array('!span' => '<span class="number">…</span>')), '#default_value' => $settings['wrap_numbers'], '#options' => array( 0 => t('Do nothing'), 1 => t('“U+202F“ Narrow no-break space'), 2 => t('“U+2009“ Thin space'), 3 => t('span with margin-left: 0.167em'), 4 => t('just wrap numbers'), ), ); // Wrap initial quotes settings. $form['wrap_initial_quotes'] = array( '#type' => 'checkbox', '#title' => t('Wrap quotation marks'), '#default_value' => $settings['wrap_initial_quotes'], ); // Ligature conversion settings. $ligature_options = array(); foreach (UnicodeConversion::map('ligature') as $ascii => $unicode) { $ligature_options[$ascii] = t('Convert @ascii to !unicode', array( '@ascii' => $ascii, '!unicode' => $unicode, )); } $form['ligatures'] = array( '#type' => 'checkboxes', '#title' => t('Ligatures'), '#options' => $ligature_options, '#default_value' => $settings['ligatures'], ); // Arrow conversion settings. $arrow_options = array(); foreach (UnicodeConversion::map('arrow') as $ascii => $unicode) { $arrow_options[$ascii] = t('Convert @ascii to !unicode', array( '@ascii' => $this->unquote($ascii), '!unicode' => $unicode, )); } $form['arrows'] = array( '#type' => 'checkboxes', '#title' => t('Arrows'), '#options' => $arrow_options, '#default_value' => $settings['arrows'], ); // Fraction conversion settings. $fraction_options = array(); foreach (UnicodeConversion::map('fraction') as $ascii => $unicode) { $fraction_options[$ascii] = t('Convert @ascii to !unicode', array( '@ascii' => $ascii, '!unicode' => $unicode, )); } $form['fractions'] = array( '#type' => 'checkboxes', '#title' => t('Fractions'), '#options' => $fraction_options, '#default_value' => $settings['fractions'], ); // Quotes conversion settings. $quotes_options = array(); foreach (UnicodeConversion::map('quotes') as $quotes => $unicode) { $quotes_options[$quotes] = t('Convert @ascii to !unicode', array( '@ascii' => $this->unquote($quotes), '!unicode' => $unicode, )); } $form['quotes'] = array( '#type' => 'checkboxes', '#title' => t('Quotes'), '#options' => $quotes_options, '#default_value' => $settings['quotes'], ); // Version Information Settings. $version_strings = array(); $version_strings[] = t('SmartyPants PHP version: !version', array( '!version' => \Drupal::l(SmartyPants::SMARTYPANTS_PHP_VERSION, Url::fromUri('http://www.michelf.com/projects/php-smartypants/')), )); $version_strings[] = t('PHP Typogrify Version: !version', array( '!version' => \Drupal::l(PHP_TYPOGRIFY_VERSION, Url::fromUri('http://blog.hamstu.com/')), )); $form['info']['typogrify_status'] = [ '#theme' => 'item_list', '#items' => $version_strings, '#title' => t('Versions'), ]; return $form; } /** * {@inheritdoc} */ public function setConfiguration(array $configuration) { static::settingsSerialize($configuration['settings']); parent::setConfiguration($configuration); } /** * {@inheritdoc} */ public function process($text, $langcode) { $settings = $this->settings; static::settingsUnserialize($settings); $characters_to_convert = array(); $ctx = array(); if ($langcode == 'und') { $language = \Drupal::languageManager()->getCurrentLanguage(); // @fixme, check language business for d8 $ctx['langcode'] = $language->language; } else { $ctx['langcode'] = $langcode; } // Build a list of ligatures to convert. foreach (UnicodeConversion::map('ligature') as $ascii => $unicode) { if (isset($settings['ligatures'][$ascii]) && $settings['ligatures'][$ascii]) { $characters_to_convert[] = $ascii; } } // Wrap caps. if ($settings['wrap_caps']) { $text = Typogrify::caps($text); } // Build a list of arrows to convert. foreach (UnicodeConversion::map('arrow') as $ascii => $unicode) { $htmle = $this->unquote($ascii); if ((isset($settings['arrows'][$ascii]) && $settings['arrows'][$ascii]) || (isset($settings['arrows'][$htmle]) && $settings['arrows'][$htmle])) { $characters_to_convert[] = $ascii; } } // Build a list of fractions to convert. foreach (UnicodeConversion::map('fraction') as $ascii => $unicode) { if (isset($settings['fractions'][$ascii]) && $settings['fractions'][$ascii]) { $characters_to_convert[] = $ascii; } } // Build a list of quotation marks to convert. foreach (UnicodeConversion::map('quotes') as $ascii => $unicode) { if (isset($settings['quotes'][$ascii]) && $settings['quotes'][$ascii]) { $characters_to_convert[] = $ascii; } } // Convert ligatures and arrows. if (count($characters_to_convert) > 0) { $text = UnicodeConversion::convertCharacters($text, $characters_to_convert); } // Wrap ampersands. if ($settings['wrap_ampersand']) { $text = SmartyPants::smartAmpersand($text); } // Smartypants formatting. if ($settings['smartypants_enabled']) { $text = SmartyPants::process($text, $settings['smartypants_hyphens'], $ctx); } // Wrap abbreviations. if ($settings['wrap_abbr'] > 0) { $text = SmartyPants::smartAbbreviation($text, $settings['wrap_abbr']); } // Wrap huge numbers. if ($settings['wrap_numbers'] > 0) { $text = SmartyPants::smartNumbers($text, $settings['wrap_numbers']); } // Wrap initial quotes. if ($settings['wrap_initial_quotes']) { $text = Typogrify::initial_quotes($text); } // Wrap initial quotes. if ($settings['hyphenate_shy']) { $text = SmartyPants::hyphenate($text); } // Remove widows. if ($settings['widont_enabled']) { $text = Typogrify::widont($text); } // Replace normal spaces with non-breaking spaces before "double punctuation // marks". This is especially useful in french. if (isset($settings['space_to_nbsp']) && $settings['space_to_nbsp']) { $text = SmartyPants::spaceToNbsp($text); } // Replace normal whitespace '-' whitespace with em-dash. if (isset($settings['space_hyphens']) && $settings['space_hyphens']) { $text = SmartyPants::spaceHyphens($text); } return new FilterProcessResult($text); } /** * {@inheritdoc} */ public function tips($long = FALSE) { $settings = $this->settings; if ($long) { $output = t('Typogrify.module brings the typographic refinements of Typogrify to Drupal.'); $output .= ''; } else { $output = t('Typographic refinements will be added.'); } return $output; } /** * Helper function to unquote a string. * * Unquotes a string. * * @param string|array $text * String or array of strings to be unquoted. * * @return string|array * Original $text with simple '<' and '>' instead of HTML entities. */ private function unquote($text) { $text = str_replace( array('<', '>'), array('<', '>'), $text); return $text; } }