You are here

protected function Pagination::parseText in Pagination (Node) 7

Same name and namespace in other branches
  1. 6 pagination.module \Pagination::parseText()

Handle pagination depending on pagination style

Parameters

$text: The text to paginate.

$cutoff: The length of text to create sections from.

Return value

A string representing a section of text.

1 call to Pagination::parseText()
Pagination::paginate in includes/Pagination.inc
Paginate text by character count.

File

includes/Pagination.inc, line 303

Class

Pagination
Handles all the pagination logic

Code

protected function parseText(&$text, $cutoff) {
  $page_count = empty($this->headers) ? count($this->headers) + 1 : count($this->headers);
  $section = '';
  switch ($cutoff) {
    case 1:

      // Manual breaks w/custom headers, for [ header = random ] we expect:
      // 0:  [ header = HEADER TEXT ]
      // 4:  HEADER TEXT
      // for [ pagebreak ] we expect:
      // 0:  [ pagebreak ]
      preg_match($this->re_custom, $text, $matches);
      if (isset($matches[0])) {
        $header = isset($matches[4]) && $matches[4] ? $matches[4] : t('Page !num', array(
          '!num' => $page_count + 1,
        ));
        $section = $this
          ->parseSection($text, $matches, 1, 5);
        $this
          ->setHeader($header);
      }
      else {

        // No matches, send back full text
        $section = $text;
        $text = '';
      }
      break;
    case 2:

      // Manual breaks w/h3 tags, we except:
      // 0:  <h3>HEADER TEXT</h3>
      // 2:  HEADER TEXT
      $tag = 'h' . variable_get('pagination_header', 3);
      if ('h3' !== $tag) {
        $this->re_tag = str_replace('h3', $tag, $this->re_tag);
      }
      preg_match($this->re_tag, $text, $matches);
      if (isset($matches[0])) {
        $header = isset($matches[2]) ? $matches[2] : t('Page !num', array(
          '!num' => $page_count + 1,
        ));
        $section = $this
          ->parseSection($text, $matches, 1, 3);
        $this
          ->setHeader($header);
      }
      else {

        // No matches, send back full text
        $section = $text;
        $text = '';
      }
      break;
    default:

      // Paging by words, we'll use the english approximate 5 chars per word (+ space)
      $header = t('Page !num', array(
        '!num' => $page_count + 1,
      ));
      $section = $this
        ->parseChunk($text, $cutoff * 6);
      $text = substr($text, strlen($section));
      $this
        ->setHeader($header);
      break;
  }
  return $section;
}