You are here

function theme_biblio_page_number in Bibliography Module 6.2

Same name and namespace in other branches
  1. 5 biblio.module \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()

Creates HTML string for a biblio page number or range.

// Format page information: // // NOTES: - this function (and refbase in general) assumes following rules for the original formatting of page information in '$origPageInfo': // - single-page items are given as a page range with identical start & end numbers (e.g. "127-127") // - multi-page items are given as a page range where the end number is greater than the start number (e.g. "127-132") // - for multi-page items where only the start page is known, a hyphen is appended to the start page (e.g. "127-") // - total number of pages are given with a "pp" suffix (e.g. "498 pp"), see TODO // - the given page info is left as is if it does not match any of the above rules (e.g. a single page number is ambiguous since it // could mean a single page or the total number of pages) // - the function attempts to deal with page locators that contain letters (e.g. "A1 - A3" or "4a-4c") but, ATM, locator parts (e.g. "A1") // must contain at least one digit character & must not contain any whitespace // // TODO: - should we only use Unicode-aware regex expressions (i.e. always use '$space', '$digit' or '$word' instead of ' ', '\d' or '\w', etc)? // - recognize & process total number of pages // - for '$shortenPageRangeEnd=true', add support for page locators that contain letters (e.g. "A1 - A3" or "4a-4c")

// @todo: There are too many parameters here. Changed to an $options array?

Parameters

$origPageInfo:

string $pageRangeDelim: (optional)

string $singlePagePrefix: (optional)

string $pageRangePrefix: (optional)

string $totalPagesPrefix: (optional)

string $singlePageSuffix: (optional)

string $pageRangeSuffix: (optional)

string $totalPagesSuffix: (optional)

bool $shortenPageRangeEnd: (optional) A logical flag with a default FALSE value.

4 calls to theme_biblio_page_number()
biblio_style_ama in styles/biblio_style_ama.inc
biblio_style_chicago in styles/biblio_style_chicago.inc
Apply a bibliographic style to the node
biblio_style_mla in styles/biblio_style_mla.inc
Apply a bibliographic style to the node
biblio_style_vancouver in styles/biblio_style_vancouver.inc
1 theme call to theme_biblio_page_number()
biblio_style_ama in styles/biblio_style_ama.inc

File

includes/biblio_theme.inc, line 774

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_regex_patterns();

  // 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;
}