Cut pages into multiple parts

This module allows you to create a mini site from a single page. This can be used to create a site from documents which are created in a seperate process.

Methods

(array) CutPage Returns an array with the sections found
(void) ShowSection Shows a single section, as cut with CutPage.
(void) ShowSections Shows all sections.

CutPage

(array) CutPage($tag, $withtags=true, $withtitles=true, $page=false)

(string) $tag The tag on which to split the html, e.g. 'H1'.
(bool) $withtags Whether or not to include $tag in the sections.
(bool) $withtitles Whether or not to include the title in the section content.
(string) $page Optional html fragment, if not set GetPage() is used.

CutPage() divides the html page into seperate sections. It returns these sections in an array. The array has the format:

array( 
array(
'title' => $section_title,
'content' => $section_content
)
);

$section_title is always a string, but may be empty and $section_content is also a string. The array starts at 1, except when a 'leader' section is found, that is returned at position 0, with no title set. So the following html:

<p>This is a leader</p>
<h1>Section 1</h1>
<p>This is section 1</p>
<h1>Section 2</h1>
<p>This is section 2</p>

 with the following code:

  multipart::CutPage('h1');

would return:

array (
  0 => array (
    'content' => '<p>This is a leader</p>'
  ),
  1 => array (
    'title' => 'Section 1',
    'content' => '<h1>Section 1</h1><p>This is section 1</p>'
  ),
  2 => array (
    'title' => 'Section 2',
    'content' => '<h1>Section 2</h1><p>This is section 2</p>'
)
)

ShowSection

(void) ShowSection($section, $template="", $args="", $recurse=false, 
$level=0)

(array) $section The section title and content from the CutPage result array.
(string) $template The template to call to display this section.
(array) $args The arguments to pass to the template
(bool) $recurse Whether or not to recurse into the section content if that is an array.
(int) $level Level counter which gets incremented when recursing through the sections.

If no template is given, a default layout will be shown. The template called is given two extra arguments: $section_title and $section_content. Of these $section_title may be empty.

ShowSections

(void) ShowSections($sections, $template="", $args="", 
$recurse=false, $level=0)

See ShowSection above, except $sections is an array of sections, e.g. the result of CutPage().

One of the things you can do now is create a nested list of sections. You can first cut the page on <HR> tags, and then for each content section cut that section on <H1> tags, etc.

Examples

An example template that shows a FAQ style list of links first. 

view.html:

<pinp>
load('mod_page.php')
 load('mod_multipart.php');
 $page = page::getBody(page::parse($nlsdata->page));
 $chapters = multipart::CutPage('h1', false, true, $page);
echo '<ol>';
putvar('counter', 0);
multipart::ShowSections($chapters, 'show.title.html');
echo '</ol>';
  putvar('counter', 0);
multipart::ShowSections($chapters, 'show.section.html');
</pinp>

show.title.html:

<pinp>
$counter = getvar('counter');
putvar('counter', $counter+1);
</pinp>
<li><a href="#section_<pinp> echo $counter; </pinp>"><pinp>
echo getvar('section_title');
</pinp></a></li>

show.section.html:

<pinp>
$counter = getvar('counter');
  putvar('counter', $counter+1);
</pinp>
<h2><a name="section_<pinp> echo $counter; </pinp>"><pinp>
echo getvar('section_title');
</pinp></a></h2>
<pinp>
echo getvar('section_content');
</pinp>

An example template which shows a recursive unordered list with anchor links:

<pinp>
  $section_title = getvar('section_title');
  $section_content = getvar('section_content');
  $prefix = getvar('prefix');
  if ($prefix) {
    $prefix .= ' - ';
  }
  if ($section_title) {
    // don't link to the leader sections
echo '<li><a href="#'.RawUrlEncode($prefix.$section_title).
'">'.$section_title.'</a></li>';
  }
  if (is_array($section_content)) {
echo '<ul>';
multipart::ShowSections($section_content, 'show.title.html',
        Array('prefix' => $section_title));
echo '</ul>';
  }
</pinp>

An example template which simply shows all sections, but with anchors and h1 - h7 depending on nesting level.

<pinp>
  $section_title = getvar('section_title');
  $section_content = getvar('section_content');
  $prefix = getvar('prefix');
  if ($prefix) {
$prefix .= ' - ';
}
  if (!$level) {
    $level = 1;
  }
  if ($section_title) {
echo '<h'.$level.'>';
echo '<a name="'.RawUrlEncode($prefix.$section_title).
'">'.$section_title.'</a>';
echo '</h'.$level.'>';
  }
  if (is_array($section_content)) {
    $level++;
    multipart::ShowSections($section_content, 'show.section.html',
      Array('prefix' => $section_title));
    $level--;
  } else {
    echo $section_content;   
  }
</pinp>