You are here

function theme_biblio_page_number in Bibliography Module 5

Same name and namespace in other branches
  1. 6.2 includes/biblio_theme.inc \theme_biblio_page_number()
  2. 6 biblio_theme.inc \theme_biblio_page_number()
  3. 7 includes/biblio_theme.inc \theme_biblio_page_number()
  4. 7.2 includes/biblio.theme.inc \theme_biblio_page_number()
4 calls to theme_biblio_page_number()
biblio_style_ama in ./biblio_style_ama.inc
biblio_style_chicago in ./biblio_style_chicago.inc
Apply a bibliographic style to the node
biblio_style_mla in ./biblio_style_mla.inc
Apply a bibliographic style to the node
biblio_style_vancouver in ./biblio_style_vancouver.inc
1 theme call to theme_biblio_page_number()
biblio_style_ama in ./biblio_style_ama.inc

File

./biblio.module, line 4087

Code

function theme_biblio_page_number($origPageInfo, $pageRangeDelim = "-", $singlePagePrefix = "", $pageRangePrefix = "", $totalPagesPrefix = "", $singlePageSuffix = "", $pageRangeSuffix = "", $totalPagesSuffix = "", $shortenPageRangeEnd = false) {
  list($alnum, $alpha, $cntrl, $dash, $digit, $graph, $lower, $print, $punct, $space, $upper, $word, $patternModifiers) = _biblio_get_utf8_regex();

  // Check original page info for any recognized page locators, and process them appropriately:
  if (preg_match("/\\w*\\d+\\w* *[{$dash}]+ *(?:\\w*\\d+\\w*)?/{$patternModifiers}", $origPageInfo)) {

    // the original page info contains a page range (like: "127-127", "127-132", "A1 - A3", "4a-4c", or "127-" if only start page given)
    // Remove any whitespace around dashes or hyphens that indicate a page range:
    $origPageInfo = preg_replace("/(\\w*\\d+\\w*) *([{$dash}]+) *(\\w*\\d+\\w*)?(?=[^\\w\\d]|\$)/{$patternModifiers}", "\\1\\2\\3", $origPageInfo);

    // Split original page info into its functional parts:
    // NOTE: ATM, we simply split on any whitespace characters, then process all parts with page ranges
    //       (this will also reduce runs of whitespace to a single space)
    $partsArray = preg_split("/ +/", $origPageInfo);
    $partsCount = count($partsArray);
    for ($i = 0; $i < $partsCount; $i++) {

      // Format parts with page ranges:
      // - single-page item:
      if (preg_match("/(\\w*\\d+\\w*)[{$dash}]+\\1(?=[^\\w\\d]|\$)/{$patternModifiers}", $partsArray[$i])) {

        // this part contains a page range with identical start & end numbers (like: "127-127")
        $partsArray[$i] = preg_replace("/(\\w*\\d+\\w*)[{$dash}]+\\1(?=[^\\w\\d]|\$)/{$patternModifiers}", $singlePagePrefix . "\\1" . $singlePageSuffix, $partsArray[$i]);
      }
      elseif (preg_match("/\\w*\\d+\\w*[{$dash}]+(?:\\w*\\d+\\w*)?(?=[^\\w\\d]|\$)/{$patternModifiers}", $partsArray[$i])) {

        // this part contains a page range (like: "127-132", or "127-" if only start page given)
        // In case of '$shortenPageRangeEnd=true', we abbreviate ending page numbers so that digits aren't repeated unnecessarily:
        if ($shortenPageRangeEnd and preg_match("/\\d+[{$dash}]+\\d+/{$patternModifiers}", $partsArray[$i])) {

          // ATM, only digit-only page locators (like: "127-132") are supported
          // NOTE: the logic of this 'if' clause doesn't work if the original page info contains something like "173-190; 195-195" (where, for the first page range, '$endPage' would be "190;" and not "190")
          list($startPage, $endPage) = preg_split("/[{$dash}]+/{$patternModifiers}", $partsArray[$i]);
          $countStartPage = strlen($startPage);
          $countEndPage = strlen($endPage);
          if ($countStartPage == $countEndPage and $startPage < $endPage) {
            for ($j = 0; $j < $countStartPage; $j++) {
              if (preg_match("/^" . substr($startPage, $j, 1) . "/", $endPage)) {

                // if the ending page number has a digit that's identical to the starting page number (at the same digit offset)
                $endPage = substr($endPage, 1);
              }
              else {
                break;
              }
            }
          }
          $partsArray[$i] = $pageRangePrefix . $startPage . $pageRangeDelim . $endPage . $pageRangeSuffix;
        }
        else {

          // don't abbreviate ending page numbers:
          $partsArray[$i] = preg_replace("/(\\w*\\d+\\w*)[{$dash}]+(\\w*\\d+\\w*)?(?=[^\\w\\d]|\$)/{$patternModifiers}", $pageRangePrefix . "\\1" . $pageRangeDelim . "\\2" . $pageRangeSuffix, $partsArray[$i]);
        }
      }
    }
    $newPageInfo = join(" ", $partsArray);

    // merge again all parts
  }
  else {
    $newPageInfo = $origPageInfo;
  }

  // page info is ambiguous, so we don't mess with it
  return $newPageInfo;
}