<?php if (!defined('PmWiki')) exit();
/*  Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  

    This program adds (:breakpage:) markup to PmWiki.  A page containing
    one or more (:breakpage:) directives is displayed in smaller chunks,
    with a "Page 1 2 3 ..." navigation at the end to provide access
    to the other chunks of the page.

    (:breakpage:) is only processed this way if ?action=browse.
    If ?action=edit, then (:breakpage:) is converted to a visual
    indicator of a page break; for all other actions (:breakpage:)
    is converted to a null string.
*/

function PageBreak($pagename,$text) {
  $p = explode('(:breakpage:)',$text);
  $n = @$_REQUEST['p'];
  if ($n<1) $n=1;  if ($n>count($p)) $n=count($p);
  $out[] = "<div class='breaklist'>Page";
  for($i=1;$i<=count($p);$i++) {
    if ($i==$n) $out[] = " $n";
    else $out[] = FmtPageName(" <a href='\$PageUrl?p=$i'>$i</a>",$pagename);
  }
  $out[] = '</div>';
  return $p[$n-1].Keep(implode('',$out));
}

if ($action=='browse') 
  Markup('breakpage','>include','/^.*\\(:breakpage:\\).*$/se',
    "PageBreak(\$pagename,PSS('$0'))");
elseif ($action=='edit')
  Markup('breakpage','>include','/\\(:breakpage:\\)/',
    "\n<div class='breakpage'>&mdash; Page Break &mdash;</div>\n");
else
  Markup('breakpage','directives','/\\(:breakpage:\\)/','');