/** * @param array $book_contents * @param array $metadata */ protected function echoPartsAndChapters($book_contents, $metadata) { $part_printf = '<div class="part %s" id="%s">'; $part_printf .= '<div class="part-title-wrap"><h3 class="part-number">%s</h3><h1 class="part-title">%s</h1></div>%s'; $part_printf .= '</div>'; $chapter_printf = '<div class="chapter %s" id="%s">'; $chapter_printf .= '<div class="chapter-title-wrap"><h3 class="chapter-number">%s</h3><h2 class="chapter-title">%s</h2></div>'; $chapter_printf .= '<div class="ugc chapter-ugc">%s</div>%s'; $chapter_printf .= '</div>'; $s = $i = $j = 1; foreach ($book_contents['part'] as $part) { $invisibility = get_post_meta($part['ID'], 'pb_part_invisible', true) == 'on' ? 'invisible' : ''; $part_printf_changed = ''; $slug = $part['post_name']; $title = $part['post_title']; $part_content = trim(get_post_meta($part['ID'], 'pb_part_content', true)); // Inject introduction class? if ($invisibility !== 'invisible') { // visible if (count($book_contents['part']) == 1) { // only part if ($part_content) { // has content if (!$this->hasIntroduction) { $part_printf_changed = str_replace('<div class="part %s" id=', '<div class="part introduction %s" id=', $part_printf); $this->hasIntroduction = true; } } } elseif (count($book_contents['part']) > 1) { // multiple parts if (!$this->hasIntroduction) { $part_printf_changed = str_replace('<div class="part %s" id=', '<div class="part introduction %s" id=', $part_printf); $this->hasIntroduction = true; } } } // Inject part content? if ($part_content) { $part_content = $this->preProcessPostContent($part_content); if ($part_printf_changed) { $part_printf_changed = str_replace('</h1></div>%s</div>', "</h1></div><div class=\"ugc part-ugc\">%s</div></div>", $part_printf_changed); } else { $part_printf_changed = str_replace('</h1></div>%s</div>', "</h1></div><div class=\"ugc part-ugc\">%s</div></div>", $part_printf); } } $m = $invisibility == 'invisible' ? '' : $i; $my_part = sprintf($part_printf_changed ? $part_printf_changed : $part_printf, $invisibility, $slug, $m, Sanitize\decode($title), $part_content) . "\n"; $my_chapters = ''; foreach ($part['chapters'] as $chapter) { if (!$chapter['export']) { continue; } // Skip $chapter_printf_changed = ''; $id = $chapter['ID']; $subclass = \PressBooks\Taxonomy\chapter_type($id); $slug = $chapter['post_name']; $title = get_post_meta($id, 'pb_show_title', true) ? $chapter['post_title'] : '<span class="display-none">' . $chapter['post_title'] . '</span>'; // Preserve auto-indexing in Prince using hidden span $content = $chapter['post_content']; $short_title = trim(get_post_meta($id, 'pb_short_title', true)); $subtitle = trim(get_post_meta($id, 'pb_subtitle', true)); $author = trim(get_post_meta($id, 'pb_section_author', true)); $sections = \PressBooks\Book::getSubsections($id); if ($sections) { $content = \PressBooks\Book::tagSubsections($content); } if ($author) { $content = '<h2 class="chapter-author">' . Sanitize\decode($author) . '</h2>' . $content; } if ($subtitle) { $content = '<h2 class="chapter-subtitle">' . Sanitize\decode($subtitle) . '</h2>' . $content; } if ($short_title) { $content = '<h6 class="short-title">' . Sanitize\decode($short_title) . '</h6>' . $content; } // Inject introduction class? if (!$this->hasIntroduction) { $chapter_printf_changed = str_replace('<div class="chapter %s" id=', '<div class="chapter introduction %s" id=', $chapter_printf); $this->hasIntroduction = true; } $n = $subclass == 'numberless' ? '' : $j; $my_chapters .= sprintf($chapter_printf_changed ? $chapter_printf_changed : $chapter_printf, $subclass, $slug, $n, Sanitize\decode($title), $content, $this->doEndnotes($id)) . "\n"; if ($subclass !== 'numberless') { ++$j; } } // Echo with parts? if ($invisibility !== 'invisible') { // visible if (count($book_contents['part']) == 1) { // only part if ($part_content) { // has content echo $my_part; // show if ($my_chapters) { echo $my_chapters; } } else { // no content if ($my_chapters) { echo $my_chapters; } } } elseif (count($book_contents['part']) > 1) { // multiple parts if ($my_chapters) { // has chapter echo $my_part . $my_chapters; // show } else { // no chapter if ($part_content) { // has content echo $my_part; } // show } } ++$i; } elseif ($invisibility == 'invisible') { // invisible if ($my_chapters) { echo $my_chapters; } } } }
/** * Uses $this->manifest to generate itself. * * @param array $book_contents * @param array $metadata */ protected function createToc($book_contents, $metadata) { $vars = array('post_title' => '', 'stylesheet' => $this->stylesheet, 'post_content' => '', 'isbn' => @$metadata['pb_ebook_isbn']); $options = get_option('pressbooks_theme_options_global'); // Start by inserting self into correct manifest position $array_pos = $this->positionOfToc(); $file_id = 'table-of-contents'; $filename = "{$file_id}.{$this->filext}"; $vars['post_title'] = __('Table Of Contents', 'pressbooks'); $this->manifest = array_slice($this->manifest, 0, $array_pos + 1, true) + array($file_id => array('ID' => -1, 'post_title' => $vars['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos + 1, count($this->manifest) - 1, true); // HTML $li_count = 0; $i = 1; $html = '<div id="toc"><h1>' . __('Contents', 'pressbooks') . '</h1><ul>'; foreach ($this->manifest as $k => $v) { // We only care about front-matter, part, chapter, back-matter // Skip the rest $subtitle = ''; $author = ''; $license = ''; $title = Sanitize\strip_br($v['post_title']); if (preg_match('/^front-matter-/', $k)) { $class = 'front-matter '; $class .= \PressBooks\Taxonomy\front_matter_type($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; } elseif (preg_match('/^part-/', $k)) { $class = 'part'; if (get_post_meta($v['ID'], 'pb_part_invisible', true) == 'on') { $class .= ' display-none'; } } elseif (preg_match('/^chapter-/', $k)) { $class = 'chapter'; $class .= \PressBooks\Taxonomy\chapter_type($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; if ($this->numbered && \PressBooks\Taxonomy\chapter_type($v['ID']) !== 'numberless') { $title = " {$i}. " . $title; } if (\PressBooks\Taxonomy\chapter_type($v['ID']) !== 'numberless') { ++$i; } } elseif (preg_match('/^back-matter-/', $k)) { $class = 'back-matter '; $class .= \PressBooks\Taxonomy\back_matter_type($v['ID']); $subtitle = trim(get_post_meta($v['ID'], 'pb_subtitle', true)); $author = trim(get_post_meta($v['ID'], 'pb_section_author', true)); $license = $options['copyright_license'] ? get_post_meta($v['ID'], 'pb_section_license', true) : ''; } else { continue; } $html .= sprintf('<li class="%s"><a href="%s"><span class="toc-chapter-title">%s</span>', $class, $v['filename'], Sanitize\decode($title)); if ($subtitle) { $html .= ' <span class="chapter-subtitle">' . Sanitize\decode($subtitle) . '</span>'; } if ($author) { $html .= ' <span class="chapter-author">' . Sanitize\decode($author) . '</span>'; } if ($license) { $html .= ' <span class="chapter-license">' . $license . '</span> '; } $html .= "</a>"; if (\PressBooks\Export\Export::shouldParseSections() == true) { $sections = \PressBooks\Book::getSubsections($v['ID']); if ($sections) { $s = 1; $html .= '<ul class="sections">'; foreach ($sections as $section) { $html .= '<li class="section"><a href="' . $v['filename'] . '#section-' . $s . '"><span class="toc-subsection-title">' . Sanitize\decode($section) . '</span></a></li>'; ++$s; } $html .= '</ul>'; } } $html .= "</li>\n"; ++$li_count; } if (0 == $li_count) { $html .= '<li></li>'; } $html .= "</ul></div>\n"; // Create file $vars['post_content'] = $html; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/xhtml.php', $vars)); }
/** * Get chapter, front or back matter type * * @param $post * * @return string */ function pb_get_section_type($post) { $type = $post->post_type; switch ($type) { case 'chapter': $type = \PressBooks\Taxonomy\chapter_type($post->ID); break; case 'front-matter': $type = \PressBooks\Taxonomy\front_matter_type($post->ID); break; case 'back-matter': $type = \PressBooks\Taxonomy\back_matter_type($post->ID); break; } return $type; }