You are here

function epub_get_toc in Epub 8

Same name and namespace in other branches
  1. 7 epub.module \epub_get_toc()
1 call to epub_get_toc()
EpubTableOfContent::viewElements in src/Plugin/Field/FieldFormatter/EpubTableOfContent.php
Builds a renderable array for a field value.

File

./epub.module, line 152

Code

function epub_get_toc($dir, $target = FALSE) {
  $file = epub_get_nav($dir);
  if ($file) {
    $tocXML = simplexml_load_file($file);
    $tocXML
      ->registerXPathNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
    $tocXML
      ->registerXPathNamespace('epub', 'http://www.idpf.org/2007/ops');
    foreach ($tocXML
      ->xpath('//xhtml:a[@href]') as $item) {
      $item[0]
        ->attributes()->href = rawurldecode(file_create_url(dirname($file) . '/' . $item[0]
        ->attributes()->href));
      if ($target) {
        $item[0]
          ->addAttribute('target', $target);
      }
    }
    $toc = $tocXML
      ->xpath('//*[@epub:type="toc"]');
    if ($toc) {
      return $toc[0]
        ->asXML();
    }
  }
  $file = epub_get_ncx($dir);
  if ($file) {
    $tocXML = simplexml_load_file(epub_get_ncx($dir));
    $tocXML = simplexml_load_file($file);
    $tocXML
      ->registerXPathNamespace('ncx', 'http://www.daisy.org/z3986/2005/ncx/');
    foreach ($tocXML
      ->xpath('//@src') as $item) {
      $item[0] = rawurldecode(file_create_url(dirname($file) . '/' . $item[0]));
    }
    $toc = $tocXML
      ->xpath('//ncx:navMap');
    if ($toc) {
      $output = new SimpleXMLElement('<nav type="toc"><h2>Contents</h2></nav>');
      $html = epub_ncx_item($toc[0], $target);
      return $html;
    }
  }
  return FALSE;
}