Source of paginate.js

/*
 
doPagination: transforms linear HTML text into DIV-paginated one.
 
Input:  none
Output: 
 
  DIV tags are created in the HTML content and the linear text is split into sections and
  inserted into the newly-created DIV tags
  
Parameters:
 
  The routine relies on the following (parametrizable) functions:
  
    - firstPageStart() - returns the first DOM element of the first page
    - newPageStart(e) - true if the input element should start a new page
    - endOfContent(e) - true if the element should no longer be part of pagination structure
*/
 
function doPagination() {
  var elementPosition = firstPageStart() ;
  var pageDiv ;
  if (!elementPosition) throw "Cannot find the first element to paginate" ;
  
  while (! endOfContent(elementPosition)) {
    if (newPageStart(elementPosition) || (! pageDiv)) {
      pageDiv = document.createElement("div");
      setDivAttributes(pageDiv) ;
      elementPosition.parentNode.insertBefore(pageDiv,elementPosition); // Insert the new page DIV in front of the current element
    }
    
    var nextElement = elementPosition.nextSibling;            // Save the next element
    elementPosition.parentNode.removeChild(elementPosition);  // remove the element from the linear text
    pageDiv.appendChild(elementPosition);                     // insert the element into the page DIV
    elementPosition = nextElement;                            // proceeed to the next element
  }
}
 
/*
isEmptyElement(e) - returns true if the element is "empty"
 
Input:  DOM element
Output: TRUE if the element is empty
 
  Empty elements are whitespace-only text nodes, comments and SCRIPT tags
*/
 
function isEmptyElement(e) {
  if (e.nodeType == 8) return true; /* Comment */
  if (e.nodeType == 3) {
    var d = e.data;
    var s = d.search(/^\s*$/) ;
    return d.search(/^\s*$/) != -1 ;
  }
  if (e.nodeType == 1) return e.tagName.toLowerCase == "script" ;
  return false;
}
 
/*
 
nonEmptyNextSibling - returns the first non-empty next sibling
 
Input:
  e  ... current element
  nt ... desired node type of next element (false if not important)
 
Output:
 
  The function returns the first non-empty next sibling element of the desired type.
*/
 
function nonEmptyNextSibling(e,nt) {
  do {
    e = e.nextSibling;
    if (!e) return false;
  } while (isEmptyElement(e) || (e.nodeType != nt && nt)) ;
  return e ;
}
 
/*
 
firstPageStart - returns the first element to be paginated
 
Input: none
Output:
 
  The function returns the first element to be paginated. Parametrize this function to suit
  your needs, currently it returns the first "non-empty" element after the first H1 tag.
  
Definition:
 
  "Empty" elements are whitespace-only text nodes, comments and SCRIPT tags.
 
*/
 
function firstPageStart() {
  var elem = findPaginationStart(); /* Find the element just prior to pagination start point */ 
  if (!elem) return false;
  elem = nonEmptyNextSibling(elem,false);
  return elem ;
}
 
/*
 
findPaginationStart - returns the element just prior to first page (currently first H1 element)
 
Input: none
Output:
 
  The function returns the element just prior to the first page. Parametrize this function to suit
  your needs, currently it returns the first H1 tag.
*/
 
function findPaginationStart() {
  var hlist = document.getElementsByTagName("h1") ;
  if (hlist.length == 0) return false;
  return hlist[0] ; /* first H1 element */
}
 
/*
 
endOfContent - checks whether the pagination should stop
 
Input:  HTML element
Output: TRUE if the pagination should stop
 
  Currently the pagination runs until the end of DIV tag or table cell. Parametrize this function to suit
  your needs.
*/
 

Top

function endOfContent(e) { return (!e); }
 
/*
 
newPageStart - checks whether a new page should be started
 
Input:  HTML element
Output: TRUE if the pagination routing should start a new page
 
  H1 or H2 tags start new page. Parametrize this function to suit your needs.
*/
 
function newPageStart(e) { 
  if (e.nodeType != 1) return false; /* non-element DOM nodes */
  var nName = e.tagName;
  nName = nName.toLowerCase();
  return (nName == "h1") || (nName == "h2") ;
}
 
/*
 
setDivAttributes - set attributes of page DIV tags
 
Input:  HTML element
Output: ID and Class attributes of the DIV tag are set
 
  ID attribute is set to "pageX" where X is the page number, class attribute is set to "page".
  Parametrize this function to suit your needs.
*/
 
var pageCount = 0;
 
function setDivAttributes(e) { 
  pageCount ++ ; e.id = "page"+pageCount ;
  e.className = "page" ;
}