You are here

public function TypogrifyFilter::process in Typogrify 8

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

src/Plugin/Filter/TypogrifyFilter.php, line 315

Class

TypogrifyFilter
Provides a filter to restrict images to site.

Namespace

Drupal\typogrify\Plugin\Filter

Code

public function process($text, $langcode) {
  $settings = $this->settings;
  static::settingsUnserialize($settings);
  $characters_to_convert = [];
  $ctx = [];
  if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
    $ctx['langcode'] = $language
      ->getId();
  }
  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::initialQuotes($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);
  }
  $result = new FilterProcessResult($text);
  $result
    ->setAttachments([
    'library' => [
      'typogrify/typogrify',
    ],
  ]);
  return $result;
}