| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libepubgen project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "EPUBHTMLManager.h" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cassert> |
| 14 |
#include <iomanip> |
| 15 |
#include <sstream> |
| 16 |
|
| 17 |
#include "EPUBHTMLGenerator.h" |
| 18 |
#include "EPUBManifest.h" |
| 19 |
|
| 20 |
namespace libepubgen |
| 21 |
{ |
| 22 |
|
| 23 |
namespace |
| 24 |
{ |
| 25 |
|
| 26 |
/// Extracts a title string from a path and provides a fallback if it would be empty. |
| 27 |
void getPathTitle(std::ostringstream &label, const EPUBPath &path, EPUBLayoutMethod layout, std::vector<EPUBPath>::size_type index) |
| 28 |
{ |
| 29 |
if (path.getTitle().empty()) |
| 30 |
{ |
| 31 |
if (layout == EPUB_LAYOUT_METHOD_FIXED) |
| 32 |
label << "Page "; |
| 33 |
else |
| 34 |
label << "Section "; |
| 35 |
label << (index + 1); |
| 36 |
} |
| 37 |
else |
| 38 |
label << path.getTitle(); |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
EPUBHTMLManager::EPUBHTMLManager(EPUBManifest &manifest) |
| 44 |
: m_manifest(manifest) |
| 45 |
, m_paths() |
| 46 |
, m_contents() |
| 47 |
, m_ids() |
| 48 |
, m_number() |
| 49 |
{ |
| 50 |
} |
| 51 |
|
| 52 |
const std::shared_ptr<EPUBHTMLGenerator> EPUBHTMLManager::create(EPUBImageManager &imageManager, EPUBFontManager &fontManager, EPUBListStyleManager &listStyleManager, EPUBParagraphStyleManager ¶graphStyleManager, EPUBSpanStyleManager &spanStyleManager, EPUBSpanStyleManager &bodyStyleManager, EPUBTableStyleManager &tableStyleManager, const EPUBPath &stylesheetPath, EPUBStylesMethod stylesMethod, EPUBLayoutMethod layoutMethod, int version) |
| 53 |
{ |
| 54 |
std::ostringstream nameBuf; |
| 55 |
nameBuf << "section" << std::setw(4) << std::setfill('0') << m_number.next(); |
| 56 |
m_ids.push_back(nameBuf.str()); |
| 57 |
|
| 58 |
nameBuf << ".xhtml"; |
| 59 |
m_paths.push_back(EPUBPath("OEBPS/sections") / nameBuf.str()); |
| 60 |
|
| 61 |
m_manifest.insert(m_paths.back(), "application/xhtml+xml", m_ids.back(), ""); |
| 62 |
|
| 63 |
m_contents.push_back(EPUBXMLContent()); |
| 64 |
|
| 65 |
const std::shared_ptr<EPUBHTMLGenerator> gen( |
| 66 |
new EPUBHTMLGenerator(m_contents.back(), imageManager, fontManager, listStyleManager, paragraphStyleManager, spanStyleManager, bodyStyleManager, tableStyleManager, m_paths.back(), stylesheetPath, stylesMethod, layoutMethod, version)); |
| 67 |
|
| 68 |
return gen; |
| 69 |
} |
| 70 |
|
| 71 |
void EPUBHTMLManager::writeTo(EPUBPackage &package) |
| 72 |
{ |
| 73 |
assert(m_contents.size() == m_paths.size()); |
| 74 |
|
| 75 |
std::vector<EPUBPath>::const_iterator pathIt = m_paths.begin(); |
| 76 |
auto contentIt = m_contents.begin(); |
| 77 |
|
| 78 |
for (; (m_paths.end() != pathIt) && (m_contents.end() != contentIt); ++pathIt, ++contentIt) |
| 79 |
contentIt->writeTo(package, pathIt->str().c_str()); |
| 80 |
} |
| 81 |
|
| 82 |
void EPUBHTMLManager::writeSpineTo(EPUBXMLContent &xml) |
| 83 |
{ |
| 84 |
for (std::vector<std::string>::const_iterator it = m_ids.begin(); m_ids.end() != it; ++it) |
| 85 |
{ |
| 86 |
librevenge::RVNGPropertyList itemrefAttrs; |
| 87 |
itemrefAttrs.insert("idref", it->c_str()); |
| 88 |
xml.insertEmptyElement("itemref", itemrefAttrs); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
void EPUBHTMLManager::writeTocTo(EPUBXMLContent &xml, const EPUBPath &tocPath, int version, EPUBLayoutMethod layout) |
| 93 |
{ |
| 94 |
if (version >= 30) |
| 95 |
{ |
| 96 |
bool hasChapterNames = std::find_if(m_paths.begin(), m_paths.end(), [](const EPUBPath &path) |
| 97 |
{ |
| 98 |
return !path.getChapters().empty(); |
| 99 |
}) != m_paths.end(); |
| 100 |
for (std::vector<EPUBPath>::size_type i = 0; m_paths.size() != i; ++i) |
| 101 |
{ |
| 102 |
const std::vector<std::string> &chapters = m_paths[i].getChapters(); |
| 103 |
for (const auto &chapter : chapters) |
| 104 |
{ |
| 105 |
xml.openElement("li"); |
| 106 |
librevenge::RVNGPropertyList anchorAttrs; |
| 107 |
anchorAttrs.insert("href", m_paths[i].relativeTo(tocPath).str().c_str()); |
| 108 |
xml.openElement("a", anchorAttrs); |
| 109 |
std::ostringstream label; |
| 110 |
xml.insertCharacters(chapter.c_str()); |
| 111 |
xml.closeElement("a"); |
| 112 |
xml.closeElement("li"); |
| 113 |
} |
| 114 |
if (hasChapterNames) |
| 115 |
// Chapter names are provided for this document, so never write Page |
| 116 |
// <N> entries. |
| 117 |
continue; |
| 118 |
|
| 119 |
xml.openElement("li"); |
| 120 |
librevenge::RVNGPropertyList anchorAttrs; |
| 121 |
anchorAttrs.insert("href", m_paths[i].relativeTo(tocPath).str().c_str()); |
| 122 |
xml.openElement("a", anchorAttrs); |
| 123 |
std::ostringstream label; |
| 124 |
getPathTitle(label, m_paths[i], layout, i); |
| 125 |
xml.insertCharacters(label.str().c_str()); |
| 126 |
xml.closeElement("a"); |
| 127 |
xml.closeElement("li"); |
| 128 |
} |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
librevenge::RVNGPropertyList navPointAttrs; |
| 134 |
for (std::vector<EPUBPath>::size_type i = 0; m_paths.size() != i; ++i) |
| 135 |
{ |
| 136 |
std::ostringstream id; |
| 137 |
id << "section" << (i + 1); |
| 138 |
navPointAttrs.insert("id", id.str().c_str()); |
| 139 |
navPointAttrs.insert("class", "document"); |
| 140 |
std::ostringstream playOrder; |
| 141 |
playOrder << (i + 1); |
| 142 |
navPointAttrs.insert("playOrder", playOrder.str().c_str()); |
| 143 |
xml.openElement("navPoint", navPointAttrs); |
| 144 |
xml.openElement("navLabel"); |
| 145 |
xml.openElement("text"); |
| 146 |
std::ostringstream label; |
| 147 |
getPathTitle(label, m_paths[i], layout, i); |
| 148 |
xml.insertCharacters(label.str().c_str()); |
| 149 |
xml.closeElement("text"); |
| 150 |
xml.closeElement("navLabel"); |
| 151 |
librevenge::RVNGPropertyList contentAttrs; |
| 152 |
contentAttrs.insert("src", m_paths[i].relativeTo(tocPath).str().c_str()); |
| 153 |
xml.insertEmptyElement("content", contentAttrs); |
| 154 |
xml.closeElement("navPoint"); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void EPUBHTMLManager::insertHeadingText(const std::string &text) |
| 159 |
{ |
| 160 |
if (m_paths.empty()) |
| 161 |
return; |
| 162 |
|
| 163 |
m_paths.back().appendTitle(text); |
| 164 |
} |
| 165 |
|
| 166 |
void EPUBHTMLManager::addChapterName(const std::string &text) |
| 167 |
{ |
| 168 |
if (m_paths.empty()) |
| 169 |
return; |
| 170 |
|
| 171 |
m_paths.back().addChapter(text); |
| 172 |
} |
| 173 |
|
| 174 |
bool EPUBHTMLManager::hasHeadingText() const |
| 175 |
{ |
| 176 |
if (m_paths.empty()) |
| 177 |
return false; |
| 178 |
|
| 179 |
return !m_paths.back().getTitle().empty(); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |