# PLUGIN PREVIEW BY TEXTPATTERN.INFO
// Register public tags.
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('soo_article_page')
->register('soo_article_page_title')
->register('soo_article_page_number')
->register('soo_article_page_link')
->register('soo_article_page_nav')
->register('soo_if_article_page')
->register('soo_article_page_search_url')
;
}
function soo_article_page($atts)
{
global $thisarticle, $is_article_body, $pg, $soo_article_pages;
static $soo_article_page_id;
assert_article();
extract(lAtts(array(
'delimiter' => '[break]',
'quiet' => 0,
), $atts));
if ($soo_article_page_id != $thisarticle['thisid']) {
$soo_article_page_id = $thisarticle['thisid'];
$soo_article_pages = array();
$delimiter = preg_quote($delimiter, '%');
$delimiter = '%(<p>)?'.$delimiter.'\s*(</p>)?%';
$pages = preg_split($delimiter, $thisarticle['body']);
$pages = array_map('trim', $pages);
$matches = array();
foreach ($pages as $page) {
if (preg_match('/<(h).*?>(.+)?<\/\1>/', $page, $matches)) {
$header = $matches[2];
} else {
$header = '';
}
$soo_article_pages[] = array('header' => $header, 'body' => $page);
}
$soo_article_pages = array_combine(range(1, count($pages)), $soo_article_pages);
}
$page_number = $pg ? $pg : 1;
if ($page_number > count($soo_article_pages)) {
return '';
}
if ($quiet) {
return '';
}
$was_article_body = $is_article_body;
$is_article_body = 1;
$out = parse($soo_article_pages[$page_number]['body']);
$is_article_body = $was_article_body;
return $out;
}
function soo_article_page_title($atts)
{
global $pg, $soo_article_pages;
extract(lAtts(array(
'page_number' => $pg ? $pg : 1,
'class' => '',
'wraptag' => '',
), $atts));
assert_article();
if (! is_int($page_number) || $page_number < 1) return '';
$num_pages = count($soo_article_pages);
if ($num_pages == 1 || $num_pages < $page_number) return '';
if ($title = $soo_article_pages[$page_number]['header']) {
return tag($title, $wraptag, array('class'=>$class));
}
}
function soo_article_page_number($atts)
{
global $pg, $soo_article_pages;
assert_article();
extract(lAtts(array(
'text' => '{page} {pg} {of} {total}',
'class' => '',
'wraptag' => '',
), $atts));
if (count($soo_article_pages) < 2) {
return '';
}
$text = _soo_article_page_replace($text, array(
'{total}' => count($soo_article_pages),
'{pg}' => $pg ? $pg : 1,
));
return tag($text, $wraptag, array('class'=>$class));
}
function soo_article_page_link($atts)
{
global $thisarticle, $pg, $soo_article_pages;
assert_article();
extract(lAtts(array(
'page_number' => 0,
'text' => '{next}',
'rel' => 'next',
'rev' => '',
'showalways' => 0,
'title' => '',
'class' => '',
'escape' => 'html',
), $atts));
$thispage = $pg ? (int) $pg : 1;
$total = count($soo_article_pages);
if (($page_number > $total) || ($total == 1)) {
return $showalways ? $text : '';
}
$goto = array(
'prev' => $thispage > 1 ? $thispage - 1 : null,
'next' => $thispage < $total ? $thispage + 1 : null,
'first' => 1,
'last' => $total,
);
if (! array_key_exists($rel, $goto)) {
if ($page_number > 0 && $page_number <= $total) {
if (! $rel) {
if (in_array($page_number, $goto)) {
$rel = array_search($page_number, $goto);
} else {
$rel = 'page '.$page_number;
}
}
$goto[$rel] = $page_number;
} else {
trigger_error(gTxt('invalid_attribute_value', array('{name}' => $page_number)), E_USER_NOTICE);
return '';
}
}
$dict = array(
'{pg}' => $goto[$rel],
'{title}' => soo_article_page_title(array('page_number' => $goto[$rel])),
);
$text = _soo_article_page_replace($text, $dict);
if ($goto[$rel] === null) {
return $showalways ? $text : '';
}
if ($title) {
$title = _soo_article_page_replace($title, $dict);
}
$url = permlinkurl($thisarticle);
if ($url) {
if (preg_match('/\?\w+=/', $url)) {
$url .= '&pg=';
} else {
$url .= '?pg=';
}
$url .= $goto[$rel];
if ($escape == 'html') {
$title = escape_title($title);
}
$revler = array('next' => 'prev', 'prev' => 'next');
if (isset($revler[$rel])) {
$rev = $revler[$rel];
} elseif (! $rev) {
if ($thispage == 1) {
$rev = 'first';
} elseif ($thispage == $total) {
$rev = 'last';
} else {
$rev = 'page '.$thispage;
}
}
return tag($text, 'a', array(
'rel' => $rel,
'rev' => $rev,
'href' => $url,
'title' => $title,
'class' => $class
)
);
}
}
function soo_article_page_nav($atts)
{
global $pg, $soo_article_pages;
$total = count($soo_article_pages);
if ($total < 2) {
return '';
}
assert_article();
extract(lAtts(array(
'text' => '{pg}',
'class' => __FUNCTION__,
'active_class' => 'here',
'wraptag' => '',
'break' => 'br',
), $atts));
$out = array();
$link_atts = array(
'class' => $class,
'text' => $text,
'rel' => '',
);
$active_class = $active_class ? "class=\"$active_class\"" : '';
$page_number = $pg ? (int) $pg : 1;
for ($n = 1; $n <= $total; $n++) {
if ($n == $page_number) {
$text = _soo_article_page_replace($text, array(
'{title}' => soo_article_page_title(array('page_number' => $n)),
'{pg}' => $n,
));
$out[] = tag($text, 'span', $active_class);
} else {
$link_atts['page_number'] = $n;
$link_atts['title'] = gTxt('page').' '.$n;
$out[] = soo_article_page_link($link_atts);
}
}
return $out ? doWrap($out, $wraptag, $break, $class) : '';
}
function soo_if_article_page($atts, $thing)
{
global $pg, $soo_article_pages;
assert_article();
if (empty($soo_article_pages)) {
trigger_error(gTxt('tag_error').' '.__FUNCTION__.': soo_article_page '.gTxt('required'), E_USER_NOTICE);
return parse($thing);
}
extract(lAtts(array(
'first' => null,
'last' => null,
), $atts));
$page = $pg ? $pg : 1;
if (count($soo_article_pages) < 2) {
$x = false;
} elseif (! empty($last)) {
$x = $page == count($soo_article_pages);
} elseif (! empty($first)) {
$x = $page == 1;
} elseif (($first === "0") && ($last === "0")) {
$x = ($page != 1) && ($page != count($soo_article_pages));
} elseif ($last === "0") {
$x = $page != count($soo_article_pages);
} elseif ($first === "0") {
$x = $page != 1;
} else {
$x = true;
}
return $thing ? parse($thing, $x) : $x;
}
function soo_article_page_search_url($atts, $thing)
{
extract(lAtts(array(
'class' => '',
'title' => '',
'escape' => 'html',
), $atts));
global $thisarticle, $q, $soo_article_pages;
assert_article();
if (empty($soo_article_pages)) {
trigger_error(gTxt('tag_error').' '.__FUNCTION__.': soo_article_page '.gTxt('required'), E_USER_NOTICE);
return parse($thing);
}
$found = false;
foreach ($soo_article_pages as $n => $page) {
if (stripos($page['body'], $q)) {
$found = true;
break;
}
}
if (! $found) {
if (stripos($thisarticle['title'], $q) !== false) {
$found = $n = 1;
}
}
if (! $found) return '';
$url = permlinkurl($thisarticle);
if (count($soo_article_pages) > 1) {
if (preg_match('/\?\w+=/', $url)) {
$url .= '&pg=';
} else {
$url .= '?pg=';
}
$url .= $n;
}
if ($thing === null) {
$thing = $url;
}
if ($escape == 'html') {
$title = escape_title($title);
}
return tag(parse($thing), 'a', array(
'href' => $url,
'title' => $title,
'class' => $class
));
}
function _soo_article_page_replace($text, $extra = array())
{
$dict = array(
'{page}' => gTxt('page'),
'{of}' => gTxt('of'),
'{next}' => gTxt('next'),
'{prev}' => gTxt('prev'),
) + $extra;
return str_replace(array_keys($dict), $dict, $text);
}