/** * @param array $book_contents * @param array $metadata */ protected function createPartsAndChapters($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>'; $vars = array('post_title' => '', 'stylesheet' => $this->stylesheet, 'post_content' => '', 'append_chapter_content' => '', 'isbn' => @$metadata['pb_ebook_isbn'], 'lang' => $this->lang); // Parts, Chapters $i = $j = $c = $p = 1; foreach ($book_contents['part'] as $part) { $invisibility = get_post_meta($part['ID'], 'pb_part_invisible', true) == 'on' ? 'invisible' : ''; $part_printf_changed = ''; $array_pos = count($this->manifest); $has_chapters = false; // Inject introduction class? if (!$this->hasIntroduction && count($book_contents['part']) > 1) { $part_printf_changed = str_replace('<div class="part %s" id=', '<div class="part introduction %s" id=', $part_printf); $this->hasIntroduction = true; } // Inject part content? $part_content = trim(get_post_meta($part['ID'], 'pb_part_content', true)); if ($part_content) { $part_content = $this->kneadHtml($this->preProcessPostContent($part_content), 'custom', $p); $part_printf_changed = str_replace('</h1></div>%s</div>', '</h1></div><div class="ugc part-ugc">%s</div></div>', $part_printf); } foreach ($part['chapters'] as $chapter) { if (!$chapter['export']) { continue; // Skip } $chapter_printf_changed = ''; $chapter_id = $chapter['ID']; $subclass = \Pressbooks\Taxonomy::getChapterType($chapter_id); $slug = $chapter['post_name']; $title = get_post_meta($chapter_id, 'pb_show_title', true) ? $chapter['post_title'] : ''; $content = $this->kneadHtml($chapter['post_content'], 'chapter', $j); $append_chapter_content = $this->kneadHtml(apply_filters('pb_append_chapter_content', '', $chapter_id), 'chapter'); $short_title = false; // Ie. running header title is not used in EPUB $subtitle = trim(get_post_meta($chapter_id, 'pb_subtitle', true)); $author = trim(get_post_meta($chapter_id, 'pb_section_author', true)); if (\Pressbooks\Modules\Export\Export::isParsingSubsections() == true) { $sections = \Pressbooks\Book::getSubsections($chapter_id); if ($sections) { $content = \Pressbooks\Book::tagSubsections($content, $chapter_id); } } 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 = 'numberless' == $subclass ? '' : $c; $vars['post_title'] = $chapter['post_title']; $vars['post_content'] = sprintf($chapter_printf_changed ? $chapter_printf_changed : $chapter_printf, $subclass, $slug, $this->numbered ? $n : '', Sanitize\decode($title), $content, $var['append_chapter_content'] = $append_chapter_content, ''); $file_id = 'chapter-' . sprintf('%03s', $j); $filename = "{$file_id}-{$slug}.{$this->filext}"; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars)); $this->manifest[$file_id] = array('ID' => $chapter['ID'], 'post_title' => $chapter['post_title'], 'filename' => $filename); $has_chapters = true; $j++; if ('numberless' !== $subclass) { ++$c; } } if (count($book_contents['part']) == 1 && $part_content) { // only part, has content $slug = $part['post_name']; $m = 'invisible' == $invisibility ? '' : $p; $vars['post_title'] = $part['post_title']; $vars['post_content'] = sprintf($part_printf_changed ? $part_printf_changed : $part_printf, $invisibility, $slug, $this->numbered ? $this->romanizePartNumbers ? \Pressbooks\L10n\romanize($m) : $m : '', Sanitize\decode($part['post_title']), $part_content); $file_id = 'part-' . sprintf('%03s', $i); $filename = "{$file_id}-{$slug}.{$this->filext}"; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars)); // Insert into correct pos $this->manifest = array_slice($this->manifest, 0, $array_pos, true) + array($file_id => array('ID' => $part['ID'], 'post_title' => $part['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos, count($this->manifest) - 1, true); ++$i; if ('invisible' !== $invisibility) { ++$p; } } elseif (count($book_contents['part']) > 1) { // multiple parts if ($has_chapters) { // has chapter $slug = $part['post_name']; $m = 'invisible' == $invisibility ? '' : $p; $vars['post_title'] = $part['post_title']; $vars['post_content'] = sprintf($part_printf_changed ? $part_printf_changed : $part_printf, $invisibility, $slug, $this->numbered ? $this->romanizePartNumbers ? \Pressbooks\L10n\romanize($m) : $m : '', Sanitize\decode($part['post_title']), $part_content); $file_id = 'part-' . sprintf('%03s', $i); $filename = "{$file_id}-{$slug}.{$this->filext}"; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars)); // Insert into correct pos $this->manifest = array_slice($this->manifest, 0, $array_pos, true) + array($file_id => array('ID' => $part['ID'], 'post_title' => $part['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos, count($this->manifest) - 1, true); ++$i; if ('invisible' !== $invisibility) { ++$p; } } else { // no chapter if ($part_content) { // has content $slug = $part['post_name']; $m = 'invisible' == $invisibility ? '' : $p; $vars['post_title'] = $part['post_title']; $vars['post_content'] = sprintf($part_printf_changed ? $part_printf_changed : $part_printf, $invisibility, $slug, $this->numbered ? $this->romanizePartNumbers ? \Pressbooks\L10n\romanize($m) : $m : '', Sanitize\decode($part['post_title']), $part_content); $file_id = 'part-' . sprintf('%03s', $i); $filename = "{$file_id}-{$slug}.{$this->filext}"; file_put_contents($this->tmpDir . "/OEBPS/{$filename}", $this->loadTemplate($this->dir . '/templates/epub201/html.php', $vars)); // Insert into correct pos $this->manifest = array_slice($this->manifest, 0, $array_pos, true) + array($file_id => array('ID' => $part['ID'], 'post_title' => $part['post_title'], 'filename' => $filename)) + array_slice($this->manifest, $array_pos, count($this->manifest) - 1, true); ++$i; if ('invisible' !== $invisibility) { ++$p; } } } } // Did we actually inject the introduction class? if ($part_printf_changed && !$has_chapters) { $this->hasIntroduction = false; } } }
/** * @covers \Pressbooks\L10n\romanize */ public function test_romanize() { $this->assertEquals(\Pressbooks\L10n\romanize(1), 'I'); $this->assertEquals(\Pressbooks\L10n\romanize(2), 'II'); $this->assertEquals(\Pressbooks\L10n\romanize(3), 'III'); $this->assertEquals(\Pressbooks\L10n\romanize(4), 'IV'); $this->assertEquals(\Pressbooks\L10n\romanize(1975), 'MCMLXXV'); }