You are here

protected function Html2Text::convertBlockquotes in Swift Mailer 7

Helper function for BLOCKQUOTE body conversion.

Parameters

string $text HTML content:

1 call to Html2Text::convertBlockquotes()
Html2Text::converter in includes/classes/Html2Text.inc

File

includes/classes/Html2Text.inc, line 462

Class

Html2Text

Code

protected function convertBlockquotes(&$text) {
  if (preg_match_all('/<\\/*blockquote[^>]*>/i', $text, $matches, PREG_OFFSET_CAPTURE)) {
    $start = 0;
    $taglen = 0;
    $level = 0;
    $diff = 0;
    foreach ($matches[0] as $m) {
      if ($m[0][0] == '<' && $m[0][1] == '/') {
        $level--;
        if ($level < 0) {
          $level = 0;

          // malformed HTML: go to next blockquote
        }
        elseif ($level > 0) {

          // skip inner blockquote
        }
        else {
          $end = $m[1];
          $len = $end - $taglen - $start;

          // Get blockquote content
          $body = substr($text, $start + $taglen - $diff, $len);

          // Set text width
          $pWidth = $this->options['width'];
          if ($this->options['width'] > 0) {
            $this->options['width'] -= 2;
          }

          // Convert blockquote content
          $body = trim($body);
          $this
            ->converter($body);

          // Add citation markers and create PRE block
          $body = preg_replace('/((^|\\n)>*)/', '\\1> ', trim($body));
          $body = '<pre>' . htmlspecialchars($body) . '</pre>';

          // Re-set text width
          $this->options['width'] = $pWidth;

          // Replace content
          $text = substr($text, 0, $start - $diff) . $body . substr($text, $end + strlen($m[0]) - $diff);
          $diff = $len + $taglen + strlen($m[0]) - strlen($body);
          unset($body);
        }
      }
      else {
        if ($level == 0) {
          $start = $m[1];
          $taglen = strlen($m[0]);
        }
        $level++;
      }
    }
  }
}