| 1 | /* AbiSource |
| 2 | * |
| 3 | * Copyright (C) 2011 Volodymyr Rudyj <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #include "ut_std_string.h" |
| 22 | #include "ie_exp_EPUB.h" |
| 23 | |
| 24 | /*****************************************************************************/ |
| 25 | /*****************************************************************************/ |
| 26 | IE_Exp_EPUB::IE_Exp_EPUB(PD_Document * pDocument) : |
| 27 | IE_Exp(pDocument), |
| 28 | m_pHmtlExporter(NULL) |
| 29 | { |
| 30 | registerDialogs(); |
| 31 | //FIXME:FIDENCIO: Remove this clause when Cocoa's dialog is implemented |
| 32 | #ifndef TOOLKIT_COCOA |
| 33 | AP_Dialog_EpubExportOptions::getEpubExportDefaults( |
| 34 | &m_exp_opt, XAP_App::getApp()); |
| 35 | #endif |
| 36 | } |
| 37 | IE_Exp_EPUB::~IE_Exp_EPUB() |
| 38 | { |
| 39 | DELETEP(m_pHmtlExporter); |
| 40 | } |
| 41 | |
| 42 | UT_Error IE_Exp_EPUB::_writeDocument() |
| 43 | { |
| 44 | UT_Error errOptions = doOptions(); |
| 45 | |
| 46 | if (errOptions == UT_SAVE_CANCELLED) //see Bug 10840 |
| 47 | { |
| 48 | return UT_SAVE_CANCELLED; |
| 49 | } |
| 50 | else if (errOptions != UT_OK) { |
| 51 | return UT_ERROR; |
| 52 | } |
| 53 | |
| 54 | m_root = gsf_outfile_zip_new(getFp(), NULL); |
| 55 | |
| 56 | if (m_root == NULL) |
| 57 | { |
| 58 | UT_DEBUGMSG(("ZIP output is null\n")); |
| 59 | return UT_ERROR; |
| 60 | } |
| 61 | |
| 62 | m_oebps = gsf_outfile_new_child(m_root, "OEBPS", TRUE); |
| 63 | if (m_oebps == NULL) |
| 64 | { |
| 65 | UT_DEBUGMSG(("Can`t create oebps output object\n")); |
| 66 | return UT_ERROR; |
| 67 | } |
| 68 | |
| 69 | // mimetype must a first file in archive |
| 70 | GsfOutput *mimetype = gsf_outfile_new_child_full(m_root, "mimetype", FALSE, |
| 71 | "compression-level", 0, NULL); |
| 72 | gsf_output_write(mimetype, strlen(EPUB_MIMETYPE), |
| 73 | (const guint8*) EPUB_MIMETYPE); |
| 74 | gsf_output_close(mimetype); |
| 75 | |
| 76 | // We need to create temporary directory to which |
| 77 | // HTML plugin will export our document |
| 78 | m_baseTempDir = UT_go_filename_to_uri(g_get_tmp_dir()); |
| 79 | m_baseTempDir += G_DIR_SEPARATOR_S; |
| 80 | |
| 81 | // To generate unique directory name we`ll use document UUID |
| 82 | m_baseTempDir += getDoc()->getDocUUIDString(); |
| 83 | // We should delete any previous temporary data for this document to prevent |
| 84 | // odd files appearing in the container |
| 85 | UT_go_file_remove(m_baseTempDir.c_str(), NULL); |
| 86 | UT_go_directory_create(m_baseTempDir.c_str(), 0644, NULL); |
| 87 | |
| 88 | if (writeContainer() != UT_OK) |
| 89 | { |
| 90 | UT_DEBUGMSG(("Failed to write container\n")); |
| 91 | return UT_ERROR; |
| 92 | } |
| 93 | if (writeStructure() != UT_OK) |
| 94 | { |
| 95 | UT_DEBUGMSG(("Failed to write document structure\n")); |
| 96 | return UT_ERROR; |
| 97 | } |
| 98 | if (writeNavigation() != UT_OK) |
| 99 | { |
| 100 | UT_DEBUGMSG(("Failed to write navigation\n")); |
| 101 | return UT_ERROR; |
| 102 | } |
| 103 | if (package() != UT_OK) |
| 104 | { |
| 105 | UT_DEBUGMSG(("Failed to package document\n")); |
| 106 | return UT_ERROR; |
| 107 | } |
| 108 | |
| 109 | gsf_output_close(m_oebps); |
| 110 | gsf_output_close(GSF_OUTPUT(m_root)); |
| 111 | |
| 112 | // After doing all job we should delete temporary files |
| 113 | UT_go_file_remove(m_baseTempDir.c_str(), NULL); |
| 114 | return UT_OK; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | UT_Error IE_Exp_EPUB::writeContainer() |
| 119 | { |
| 120 | GsfOutput* metaInf = gsf_outfile_new_child(m_root, "META-INF", TRUE); |
| 121 | |
| 122 | if (metaInf == NULL) |
| 123 | { |
| 124 | UT_DEBUGMSG(("Can`t create META-INF dir\n")); |
| 125 | return UT_ERROR; |
| 126 | } |
| 127 | GsfOutput* container = gsf_outfile_new_child(GSF_OUTFILE(metaInf), |
| 128 | "container.xml", FALSE); |
| 129 | if (container == NULL) |
| 130 | { |
| 131 | UT_DEBUGMSG(("Can`t create container.xml\n")); |
| 132 | gsf_output_close(metaInf); |
| 133 | return UT_ERROR; |
| 134 | } |
| 135 | GsfXMLOut * containerXml = gsf_xml_out_new(container); |
| 136 | |
| 137 | // <container> |
| 138 | gsf_xml_out_start_element(containerXml, "container"); |
| 139 | gsf_xml_out_add_cstr(containerXml, "version", "1.0"); |
| 140 | gsf_xml_out_add_cstr(containerXml, "xmlns", OCF201_NAMESPACE); |
| 141 | // <rootfiles> |
| 142 | gsf_xml_out_start_element(containerXml, "rootfiles"); |
| 143 | // <rootfile> |
| 144 | gsf_xml_out_start_element(containerXml, "rootfile"); |
| 145 | gsf_xml_out_add_cstr(containerXml, "full-path", "OEBPS/book.opf"); |
| 146 | gsf_xml_out_add_cstr(containerXml, "media-type", OPF_MIMETYPE); |
| 147 | // </rootfile> |
| 148 | gsf_xml_out_end_element(containerXml); |
| 149 | // </rootfiles> |
| 150 | gsf_xml_out_end_element(containerXml); |
| 151 | // </container> |
| 152 | gsf_xml_out_end_element(containerXml); |
| 153 | |
| 154 | gsf_output_close(container); |
| 155 | gsf_output_close(metaInf); |
| 156 | return UT_OK; |
| 157 | } |
| 158 | |
| 159 | UT_Error IE_Exp_EPUB::writeNavigation() |
| 160 | { |
| 161 | if (m_exp_opt.bEpub2) |
| 162 | { |
| 163 | return EPUB2_writeNavigation(); |
| 164 | } else |
| 165 | { |
| 166 | if (EPUB2_writeNavigation() == UT_ERROR) |
| 167 | return UT_ERROR; |
| 168 | if ( EPUB3_writeNavigation() == UT_ERROR) |
| 169 | return UT_ERROR; |
| 170 | } |
| 171 | |
| 172 | return UT_OK; |
| 173 | } |
| 174 | |
| 175 | UT_Error IE_Exp_EPUB::writeStructure() |
| 176 | { |
| 177 | if (m_exp_opt.bEpub2) |
| 178 | { |
| 179 | return EPUB2_writeStructure(); |
| 180 | } else |
| 181 | { |
| 182 | return EPUB3_writeStructure(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | UT_Error IE_Exp_EPUB::EPUB2_writeStructure() |
| 187 | { |
| 188 | m_oebpsDir = m_baseTempDir + G_DIR_SEPARATOR_S; |
| 189 | m_oebpsDir += "OEBPS"; |
| 190 | |
| 191 | UT_go_directory_create(m_oebpsDir.c_str(), 0644, NULL); |
| 192 | |
| 193 | std::string indexPath = m_oebpsDir + G_DIR_SEPARATOR_S; |
| 194 | indexPath += "index.xhtml"; |
| 195 | |
| 196 | // Exporting document to XHTML using HTML export plugin |
| 197 | // We need to setup options for HTML exporter according to current settings of EPUB exporter |
| 198 | std::string htmlProps = |
| 199 | UT_std_string_sprintf("embed-css:no;html4:no;use-awml:no;declare-xml:yes;mathml-render-png:%s;split-document:%s;add-identifiers:yes;", |
| 200 | m_exp_opt.bRenderMathMLToPNG ? "yes" : "no", |
| 201 | m_exp_opt.bSplitDocument ? "yes" : "no"); |
| 202 | |
| 203 | m_pHmtlExporter = new IE_Exp_HTML(getDoc()); |
| 204 | m_pHmtlExporter->suppressDialog(true); |
| 205 | m_pHmtlExporter->setProps(htmlProps.c_str()); |
| 206 | m_pHmtlExporter->writeFile(indexPath.c_str()); |
| 207 | |
| 208 | |
| 209 | return UT_OK; |
| 210 | } |
| 211 | |
| 212 | UT_Error IE_Exp_EPUB::EPUB2_writeNavigation() |
| 213 | { |
| 214 | GsfOutput* ncx = gsf_outfile_new_child(GSF_OUTFILE(m_oebps), "toc.ncx", |
| 215 | FALSE); |
| 216 | if (ncx == NULL) |
| 217 | { |
| 218 | UT_DEBUGMSG(("Can`t create toc.ncx file\n")); |
| 219 | return UT_ERROR; |
| 220 | } |
| 221 | GsfXMLOut* ncxXml = gsf_xml_out_new(ncx); |
| 222 | |
| 223 | // <ncx> |
| 224 | gsf_xml_out_start_element(ncxXml, "ncx"); |
| 225 | gsf_xml_out_add_cstr(ncxXml, "xmlns", NCX_NAMESPACE); |
| 226 | gsf_xml_out_add_cstr(ncxXml, "version", "2005-1"); |
| 227 | gsf_xml_out_add_cstr(ncxXml, "xml:lang", NULL); |
| 228 | // <head> |
| 229 | gsf_xml_out_start_element(ncxXml, "head"); |
| 230 | // <meta name="dtb:uid" content=... > |
| 231 | gsf_xml_out_start_element(ncxXml, "meta"); |
| 232 | gsf_xml_out_add_cstr(ncxXml, "name", "dtb:uid"); |
| 233 | gsf_xml_out_add_cstr(ncxXml, "content", getDoc()->getDocUUIDString()); |
| 234 | // </meta> |
| 235 | gsf_xml_out_end_element(ncxXml); |
| 236 | // <meta name="epub-creator" content=... > |
| 237 | gsf_xml_out_start_element(ncxXml, "meta"); |
| 238 | gsf_xml_out_add_cstr(ncxXml, "name", "epub-creator"); |
| 239 | gsf_xml_out_add_cstr(ncxXml, "content", |
| 240 | "AbiWord (http://www.abisource.com/)"); |
| 241 | // </meta> |
| 242 | gsf_xml_out_end_element(ncxXml); |
| 243 | // <meta name="dtb:depth" content=... > |
| 244 | gsf_xml_out_start_element(ncxXml, "meta"); |
| 245 | gsf_xml_out_add_cstr(ncxXml, "name", "dtb:depth"); |
| 246 | gsf_xml_out_add_cstr(ncxXml, "content", "1"); |
| 247 | // </meta> |
| 248 | gsf_xml_out_end_element(ncxXml); |
| 249 | // <meta name="dtb:totalPageCount" content=... > |
| 250 | gsf_xml_out_start_element(ncxXml, "meta"); |
| 251 | gsf_xml_out_add_cstr(ncxXml, "name", "dtb:totalPageCount"); |
| 252 | gsf_xml_out_add_cstr(ncxXml, "content", "0"); |
| 253 | // </meta> |
| 254 | gsf_xml_out_end_element(ncxXml); |
| 255 | // <meta name="dtb:totalPageCount" content=... > |
| 256 | gsf_xml_out_start_element(ncxXml, "meta"); |
| 257 | gsf_xml_out_add_cstr(ncxXml, "name", "dtb:maxPageCount"); |
| 258 | gsf_xml_out_add_cstr(ncxXml, "content", "0"); |
| 259 | // </meta> |
| 260 | gsf_xml_out_end_element(ncxXml); |
| 261 | // </head> |
| 262 | gsf_xml_out_end_element(ncxXml); |
| 263 | |
| 264 | // <docTitle> |
| 265 | gsf_xml_out_start_element(ncxXml, "docTitle"); |
| 266 | gsf_xml_out_start_element(ncxXml, "text"); |
| 267 | gsf_xml_out_add_cstr(ncxXml, NULL, getTitle().c_str()); |
| 268 | gsf_xml_out_end_element(ncxXml); |
| 269 | // </docTitle> |
| 270 | gsf_xml_out_end_element(ncxXml); |
| 271 | |
| 272 | // <docAuthor> |
| 273 | gsf_xml_out_start_element(ncxXml, "docAuthor"); |
| 274 | gsf_xml_out_start_element(ncxXml, "text"); |
| 275 | gsf_xml_out_add_cstr(ncxXml, NULL, getAuthor().c_str()); |
| 276 | gsf_xml_out_end_element(ncxXml); |
| 277 | // </docAuthor> |
| 278 | gsf_xml_out_end_element(ncxXml); |
| 279 | |
| 280 | |
| 281 | // <navMap> |
| 282 | gsf_xml_out_start_element(ncxXml, "navMap"); |
| 283 | if (m_pHmtlExporter->getNavigationHelper()->hasTOC()) |
| 284 | { |
| 285 | int lastItemLevel; |
| 286 | int curItemLevel = 0; |
| 287 | std::vector<int> tagLevels; |
| 288 | int tocNum = 0; |
| 289 | for (int currentItem = 0; |
| 290 | currentItem < m_pHmtlExporter->getNavigationHelper()->getNumTOCEntries(); |
| 291 | currentItem++) |
| 292 | { |
| 293 | lastItemLevel = curItemLevel; |
| 294 | std::string itemStr = m_pHmtlExporter->getNavigationHelper() |
| 295 | ->getNthTOCEntry(currentItem, &curItemLevel).utf8_str(); |
| 296 | PT_DocPosition itemPos; |
| 297 | m_pHmtlExporter->getNavigationHelper()->getNthTOCEntryPos(currentItem, itemPos); |
| 298 | |
| 299 | std::string itemFilename; |
| 300 | if (m_exp_opt.bSplitDocument) |
| 301 | { |
| 302 | itemFilename = m_pHmtlExporter->getNavigationHelper() |
| 303 | ->getFilenameByPosition(itemPos).utf8_str(); |
| 304 | |
| 305 | if (itemFilename.length() == 0 || (itemFilename[0] == '.')) |
| 306 | { |
| 307 | itemFilename = "index.xhtml"; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | itemFilename += + ".xhtml"; |
| 312 | } |
| 313 | } else |
| 314 | { |
| 315 | itemFilename = "index.xhtml"; |
| 316 | } |
| 317 | |
| 318 | if (std::find(m_opsId.begin(), m_opsId.end(), |
| 319 | escapeForId(itemFilename)) == m_opsId.end()) |
| 320 | { |
| 321 | m_opsId.push_back(escapeForId(itemFilename)); |
| 322 | tocNum = 0; |
| 323 | } |
| 324 | |
| 325 | UT_DEBUGMSG(("Item filename %s at pos %d\n", |
| 326 | itemFilename.c_str(),itemPos)); |
| 327 | |
| 328 | if ((lastItemLevel >= curItemLevel) && (currentItem != 0)) |
| 329 | { |
| 330 | while ((tagLevels.size() > 0) |
| 331 | && (tagLevels.back() >= curItemLevel)) |
| 332 | { |
| 333 | gsf_xml_out_end_element(ncxXml); |
| 334 | tagLevels.pop_back(); |
| 335 | } |
| 336 | |
| 337 | } |
| 338 | |
| 339 | std::string navClass = UT_std_string_sprintf("h%d", curItemLevel); |
| 340 | std::string navId = UT_std_string_sprintf("AbiTOC%d", tocNum); |
| 341 | std::string navSrc = std::string(itemFilename.c_str()) + "#" + navId; |
| 342 | gsf_xml_out_start_element(ncxXml, "navPoint"); |
| 343 | gsf_xml_out_add_cstr(ncxXml, "playOrder", |
| 344 | UT_std_string_sprintf("%d", currentItem + 1).c_str()); |
| 345 | gsf_xml_out_add_cstr(ncxXml, "class", navClass.c_str()); |
| 346 | gsf_xml_out_add_cstr(ncxXml, "id", navId.c_str()); |
| 347 | gsf_xml_out_start_element(ncxXml, "navLabel"); |
| 348 | gsf_xml_out_start_element(ncxXml, "text"); |
| 349 | gsf_xml_out_add_cstr(ncxXml, NULL, itemStr.c_str()); |
| 350 | gsf_xml_out_end_element(ncxXml); |
| 351 | gsf_xml_out_end_element(ncxXml); |
| 352 | gsf_xml_out_start_element(ncxXml, "content"); |
| 353 | gsf_xml_out_add_cstr(ncxXml, "src", navSrc.c_str()); |
| 354 | gsf_xml_out_end_element(ncxXml); |
| 355 | |
| 356 | tagLevels.push_back(curItemLevel); |
| 357 | tocNum++; |
| 358 | |
| 359 | } |
| 360 | |
| 361 | closeNTags(ncxXml, tagLevels.size()); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | m_opsId.push_back(escapeForId("index.xhtml")); |
| 366 | gsf_xml_out_start_element(ncxXml, "navPoint"); |
| 367 | gsf_xml_out_add_cstr(ncxXml, "playOrder", "1"); |
| 368 | gsf_xml_out_add_cstr(ncxXml, "class", "h1"); |
| 369 | gsf_xml_out_add_cstr(ncxXml, "id", "index"); |
| 370 | |
| 371 | gsf_xml_out_start_element(ncxXml, "navLabel"); |
| 372 | gsf_xml_out_start_element(ncxXml, "text"); |
| 373 | gsf_xml_out_add_cstr(ncxXml, NULL, getTitle().c_str()); |
| 374 | gsf_xml_out_end_element(ncxXml); |
| 375 | gsf_xml_out_end_element(ncxXml); |
| 376 | |
| 377 | gsf_xml_out_start_element(ncxXml, "content"); |
| 378 | gsf_xml_out_add_cstr(ncxXml, "src", "index.xhtml"); |
| 379 | gsf_xml_out_end_element(ncxXml); |
| 380 | gsf_xml_out_end_element(ncxXml); |
| 381 | } |
| 382 | // </navMap> |
| 383 | gsf_xml_out_end_element(ncxXml); |
| 384 | |
| 385 | // </ncx> |
| 386 | gsf_xml_out_end_element(ncxXml); |
| 387 | gsf_output_close(ncx); |
| 388 | |
| 389 | return UT_OK; |
| 390 | } |
| 391 | |
| 392 | UT_Error IE_Exp_EPUB::EPUB3_writeNavigation() |
| 393 | { |
| 394 | GsfOutput* nav = gsf_outfile_new_child(GSF_OUTFILE(m_oebps), "toc.xhtml", |
| 395 | FALSE); |
| 396 | if (nav == NULL) |
| 397 | { |
| 398 | UT_DEBUGMSG(("Can`t create toc.xhtml file\n")); |
| 399 | return UT_ERROR; |
| 400 | } |
| 401 | GsfXMLOut* navXHTML = gsf_xml_out_new(nav); |
| 402 | |
| 403 | gsf_xml_out_start_element(navXHTML, "html"); |
| 404 | gsf_xml_out_add_cstr(navXHTML, "xmlns", XHTML_NS); |
| 405 | gsf_xml_out_add_cstr(navXHTML, "xmlns:epub", OPS201_NAMESPACE); |
| 406 | gsf_xml_out_add_cstr(navXHTML, "profile", EPUB3_CONTENT_PROFILE); |
| 407 | |
| 408 | |
| 409 | gsf_xml_out_start_element(navXHTML, "head"); |
| 410 | gsf_xml_out_start_element(navXHTML, "title"); |
| 411 | gsf_xml_out_add_cstr(navXHTML, NULL, "Table of Contents"); |
| 412 | gsf_xml_out_end_element(navXHTML); |
| 413 | gsf_xml_out_end_element(navXHTML); |
| 414 | |
| 415 | gsf_xml_out_start_element(navXHTML, "body"); |
| 416 | gsf_xml_out_start_element(navXHTML, "section"); |
| 417 | gsf_xml_out_add_cstr(navXHTML, "class", "frontmatter TableOfContents"); |
| 418 | gsf_xml_out_start_element(navXHTML, "header"); |
| 419 | gsf_xml_out_start_element(navXHTML, "h1"); |
| 420 | gsf_xml_out_add_cstr(navXHTML, NULL, "Contents"); |
| 421 | gsf_xml_out_end_element(navXHTML); |
| 422 | gsf_xml_out_end_element(navXHTML); |
| 423 | gsf_xml_out_start_element(navXHTML, "nav"); |
| 424 | gsf_xml_out_add_cstr(navXHTML, "epub:type", "toc"); |
| 425 | gsf_xml_out_add_cstr(navXHTML, "id", "toc"); |
| 426 | if (m_pHmtlExporter->getNavigationHelper()->hasTOC()) |
| 427 | { |
| 428 | int lastItemLevel; |
| 429 | int curItemLevel; |
| 430 | std::vector<int> tagLevels; |
| 431 | int tocNum = 0; |
| 432 | bool newList = true; |
| 433 | for (int currentItem = 0; |
| 434 | currentItem < m_pHmtlExporter->getNavigationHelper()->getNumTOCEntries(); |
| 435 | currentItem++) |
| 436 | { |
| 437 | lastItemLevel = curItemLevel; |
| 438 | UT_UTF8String itemStr = m_pHmtlExporter->getNavigationHelper() |
| 439 | ->getNthTOCEntry(currentItem, &curItemLevel); |
| 440 | PT_DocPosition itemPos; |
| 441 | m_pHmtlExporter->getNavigationHelper()->getNthTOCEntryPos(currentItem, itemPos); |
| 442 | |
| 443 | std::string itemFilename; |
| 444 | |
| 445 | if (m_exp_opt.bSplitDocument) |
| 446 | { |
| 447 | itemFilename = m_pHmtlExporter->getNavigationHelper() |
| 448 | ->getFilenameByPosition(itemPos).utf8_str(); |
| 449 | |
| 450 | if ((itemFilename == "") || itemFilename.length() == 0) |
| 451 | { |
| 452 | itemFilename = "index.xhtml"; |
| 453 | } else |
| 454 | { |
| 455 | itemFilename += ".xhtml"; |
| 456 | } |
| 457 | } else |
| 458 | { |
| 459 | itemFilename = "index.xhtml"; |
| 460 | } |
| 461 | |
| 462 | if (std::find(m_opsId.begin(), m_opsId.end(), |
| 463 | escapeForId(itemFilename)) == m_opsId.end()) |
| 464 | { |
| 465 | m_opsId.push_back(escapeForId(itemFilename)); |
| 466 | tocNum = 0; |
| 467 | } |
| 468 | |
| 469 | UT_DEBUGMSG(("Item filename %s at pos %d\n", |
| 470 | itemFilename.c_str(),itemPos)); |
| 471 | |
| 472 | if ((lastItemLevel >= curItemLevel) && (currentItem != 0)) |
| 473 | { |
| 474 | while ((tagLevels.size() > 0) |
| 475 | && (tagLevels.back() >= curItemLevel)) |
| 476 | { |
| 477 | if (tagLevels.back() == curItemLevel) |
| 478 | { |
| 479 | gsf_xml_out_end_element(navXHTML); |
| 480 | } else |
| 481 | { |
| 482 | closeNTags(navXHTML, 2); |
| 483 | } |
| 484 | tagLevels.pop_back(); |
| 485 | } |
| 486 | |
| 487 | } else |
| 488 | if ((lastItemLevel < curItemLevel) || newList) |
| 489 | { |
| 490 | gsf_xml_out_start_element(navXHTML, "ol"); |
| 491 | newList = false; |
| 492 | |
| 493 | } |
| 494 | |
| 495 | std::string navClass = UT_std_string_sprintf("h%d", curItemLevel); |
| 496 | std::string navId = UT_std_string_sprintf("AbiTOC%d", |
| 497 | tocNum); |
| 498 | std::string navSrc = std::string(itemFilename.c_str()) + "#" + navId; |
| 499 | gsf_xml_out_start_element(navXHTML, "li"); |
| 500 | gsf_xml_out_add_cstr(navXHTML, "class", navClass.c_str()); |
| 501 | gsf_xml_out_add_cstr(navXHTML, "id", navId.c_str()); |
| 502 | gsf_xml_out_start_element(navXHTML, "a"); |
| 503 | gsf_xml_out_add_cstr(navXHTML, "href", navSrc.c_str()); |
| 504 | gsf_xml_out_add_cstr(navXHTML, NULL, itemStr.utf8_str()); |
| 505 | gsf_xml_out_end_element(navXHTML); |
| 506 | // gsf_xml_out_end_element(navXHTML); |
| 507 | |
| 508 | tagLevels.push_back(curItemLevel); |
| 509 | tocNum++; |
| 510 | |
| 511 | } |
| 512 | |
| 513 | closeNTags(navXHTML, tagLevels.size()*2); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | gsf_xml_out_start_element(navXHTML, "ol"); |
| 518 | gsf_xml_out_start_element(navXHTML, "li"); |
| 519 | gsf_xml_out_add_cstr(navXHTML, "class", "h1"); |
| 520 | gsf_xml_out_add_cstr(navXHTML, "id", "index"); |
| 521 | gsf_xml_out_start_element(navXHTML, "a"); |
| 522 | gsf_xml_out_add_cstr(navXHTML, "href", "index.xhtml"); |
| 523 | gsf_xml_out_add_cstr(navXHTML, NULL, getTitle().c_str()); |
| 524 | gsf_xml_out_end_element(navXHTML); |
| 525 | gsf_xml_out_end_element(navXHTML); |
| 526 | gsf_xml_out_end_element(navXHTML); |
| 527 | } |
| 528 | |
| 529 | gsf_xml_out_end_element(navXHTML); |
| 530 | // </section> |
| 531 | gsf_xml_out_end_element(navXHTML); |
| 532 | gsf_xml_out_end_element(navXHTML); |
| 533 | |
| 534 | |
| 535 | gsf_xml_out_end_element(navXHTML); |
| 536 | gsf_output_close(nav); |
| 537 | return UT_OK; |
| 538 | } |
| 539 | |
| 540 | UT_Error IE_Exp_EPUB::EPUB3_writeStructure() |
| 541 | { |
| 542 | m_oebpsDir = m_baseTempDir + G_DIR_SEPARATOR_S; |
| 543 | m_oebpsDir += "OEBPS"; |
| 544 | |
| 545 | UT_go_directory_create(m_oebpsDir.c_str(), 0644, NULL); |
| 546 | |
| 547 | std::string indexPath = m_oebpsDir + G_DIR_SEPARATOR_S; |
| 548 | indexPath += "index.xhtml"; |
| 549 | |
| 550 | // Exporting document to XHTML using HTML export plugin |
| 551 | char *szIndexPath = (char*) g_malloc(strlen(indexPath.c_str()) + 1); |
| 552 | strcpy(szIndexPath, indexPath.c_str()); |
| 553 | IE_Exp_HTML_WriterFactory *pWriterFactory = new IE_Exp_EPUB_EPUB3WriterFactory(); |
| 554 | m_pHmtlExporter = new IE_Exp_HTML(getDoc()); |
| 555 | m_pHmtlExporter->setWriterFactory(pWriterFactory); |
| 556 | m_pHmtlExporter->suppressDialog(true); |
| 557 | m_pHmtlExporter->setProps( |
| 558 | "embed-css:no;html4:no;use-awml:no;declare-xml:yes;add-identifiers:yes;"); |
| 559 | |
| 560 | m_pHmtlExporter->set_SplitDocument(m_exp_opt.bSplitDocument); |
| 561 | m_pHmtlExporter->set_MathMLRenderPNG(m_exp_opt.bRenderMathMLToPNG); |
| 562 | m_pHmtlExporter->writeFile(szIndexPath); |
| 563 | g_free(szIndexPath); |
| 564 | DELETEP(pWriterFactory); |
| 565 | return UT_OK; |
| 566 | } |
| 567 | |
| 568 | UT_Error IE_Exp_EPUB::package() |
| 569 | { |
| 570 | GsfOutput* opf = gsf_outfile_new_child(GSF_OUTFILE(m_oebps), "book.opf", |
| 571 | FALSE); |
| 572 | if (opf == NULL) |
| 573 | { |
| 574 | UT_DEBUGMSG(("Can`t create book.opf\n")); |
| 575 | return UT_ERROR; |
| 576 | } |
| 577 | GsfXMLOut* opfXml = gsf_xml_out_new(opf); |
| 578 | // <package> |
| 579 | gsf_xml_out_start_element(opfXml, "package"); |
| 580 | if (m_exp_opt.bEpub2) |
| 581 | { |
| 582 | gsf_xml_out_add_cstr(opfXml, "version", "2.0"); |
| 583 | } else |
| 584 | { |
| 585 | gsf_xml_out_add_cstr(opfXml, "version", "3.0"); |
| 586 | } |
| 587 | gsf_xml_out_add_cstr(opfXml, "xmlns", OPF201_NAMESPACE); |
| 588 | gsf_xml_out_add_cstr(opfXml, "unique-identifier", "BookId"); |
| 589 | |
| 590 | if (!m_exp_opt.bEpub2) |
| 591 | { |
| 592 | gsf_xml_out_add_cstr(opfXml, "profile", EPUB3_PACKAGE_PROFILE); |
| 593 | gsf_xml_out_add_cstr(opfXml, "xml:lang", getLanguage().c_str()); |
| 594 | } |
| 595 | |
| 596 | // <metadata> |
| 597 | gsf_xml_out_start_element(opfXml, "metadata"); |
| 598 | gsf_xml_out_add_cstr(opfXml, "xmlns:dc", DC_NAMESPACE); |
| 599 | gsf_xml_out_add_cstr(opfXml, "xmlns:opf", OPF201_NAMESPACE); |
| 600 | // Generation of required Dublin Core metadata |
| 601 | gsf_xml_out_start_element(opfXml, "dc:title"); |
| 602 | gsf_xml_out_add_cstr(opfXml, NULL, getTitle().c_str()); |
| 603 | gsf_xml_out_end_element(opfXml); |
| 604 | gsf_xml_out_start_element(opfXml, "dc:identifier"); |
| 605 | gsf_xml_out_add_cstr(opfXml, "id", "BookId"); |
| 606 | gsf_xml_out_add_cstr(opfXml, NULL, getDoc()->getDocUUIDString()); |
| 607 | gsf_xml_out_end_element(opfXml); |
| 608 | gsf_xml_out_start_element(opfXml, "dc:language"); |
| 609 | gsf_xml_out_add_cstr(opfXml, NULL, getLanguage().c_str()); |
| 610 | gsf_xml_out_end_element(opfXml); |
| 611 | gsf_xml_out_start_element(opfXml, "dc:creator"); |
| 612 | gsf_xml_out_add_cstr(opfXml, "opf:role", "aut"); |
| 613 | gsf_xml_out_add_cstr(opfXml, NULL, getAuthor().c_str()); |
| 614 | gsf_xml_out_end_element(opfXml); |
| 615 | // </metadata> |
| 616 | gsf_xml_out_end_element(opfXml); |
| 617 | |
| 618 | // <manifest> |
| 619 | gsf_xml_out_start_element(opfXml, "manifest"); |
| 620 | gchar *basedir = g_filename_from_uri(m_oebpsDir.c_str(),NULL,NULL); |
| 621 | UT_ASSERT(basedir); |
| 622 | std::string _baseDir = basedir; |
| 623 | std::vector<std::string> listing = getFileList(_baseDir); |
| 624 | FREEP(basedir); |
| 625 | |
| 626 | for (std::vector<std::string>::iterator i = listing.begin(); i |
| 627 | != listing.end(); i++) |
| 628 | { |
| 629 | std::string idStr = escapeForId(*i); |
| 630 | std::string fullItemPath = m_oebpsDir + G_DIR_SEPARATOR_S + *i; |
| 631 | gsf_xml_out_start_element(opfXml, "item"); |
| 632 | if (m_pHmtlExporter->hasMathML((*i))) |
| 633 | { |
| 634 | gsf_xml_out_add_cstr(opfXml, "mathml", "true"); |
| 635 | } |
| 636 | gsf_xml_out_add_cstr(opfXml, "id", idStr.c_str()); |
| 637 | gsf_xml_out_add_cstr(opfXml, "href", (*i).c_str()); |
| 638 | gsf_xml_out_add_cstr(opfXml, "media-type", |
| 639 | getMimeType(fullItemPath).c_str()); |
| 640 | gsf_xml_out_end_element(opfXml); |
| 641 | } |
| 642 | |
| 643 | // We`ll add navigation files manually |
| 644 | gsf_xml_out_start_element(opfXml, "item"); |
| 645 | gsf_xml_out_add_cstr(opfXml, "id", "ncx"); |
| 646 | gsf_xml_out_add_cstr(opfXml, "href", "toc.ncx"); |
| 647 | gsf_xml_out_add_cstr(opfXml, "media-type", "application/x-dtbncx+xml"); |
| 648 | gsf_xml_out_end_element(opfXml); |
| 649 | if (!m_exp_opt.bEpub2) |
| 650 | { |
| 651 | gsf_xml_out_start_element(opfXml, "item"); |
| 652 | gsf_xml_out_add_cstr(opfXml, "id", "toc"); |
| 653 | gsf_xml_out_add_cstr(opfXml, "href", "toc.xhtml"); |
| 654 | gsf_xml_out_add_cstr(opfXml, "media-type", "application/xhtml+xml"); |
| 655 | gsf_xml_out_end_element(opfXml); |
| 656 | } |
| 657 | // </manifest> |
| 658 | gsf_xml_out_end_element(opfXml); |
| 659 | |
| 660 | // <spine> |
| 661 | gsf_xml_out_start_element(opfXml, "spine"); |
| 662 | gsf_xml_out_add_cstr(opfXml, "toc", "ncx"); |
| 663 | |
| 664 | |
| 665 | if (!m_exp_opt.bEpub2) |
| 666 | { |
| 667 | gsf_xml_out_start_element(opfXml, "itemref"); |
| 668 | gsf_xml_out_add_cstr(opfXml, "idref","toc"); |
| 669 | gsf_xml_out_end_element(opfXml); |
| 670 | } |
| 671 | |
| 672 | for(std::vector<std::string>::iterator i = m_opsId.begin(); i != m_opsId.end(); i++) |
| 673 | { |
| 674 | gsf_xml_out_start_element(opfXml, "itemref"); |
| 675 | gsf_xml_out_add_cstr(opfXml, "idref", (*i).c_str()); |
| 676 | gsf_xml_out_end_element(opfXml); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | |
| 681 | // </spine> |
| 682 | gsf_xml_out_end_element(opfXml); |
| 683 | |
| 684 | // </package> |
| 685 | gsf_xml_out_end_element(opfXml); |
| 686 | gsf_output_close(opf); |
| 687 | return compress(); |
| 688 | } |
| 689 | |
| 690 | std::vector<std::string> IE_Exp_EPUB::getFileList( |
| 691 | const std::string &directory) |
| 692 | { |
| 693 | std::vector<std::string> result; |
| 694 | std::vector<std::string> dirs; |
| 695 | |
| 696 | dirs.push_back(directory); |
| 697 | |
| 698 | while (dirs.size() > 0) |
| 699 | { |
| 700 | std::string currentDir = dirs.back(); |
| 701 | dirs.pop_back(); |
| 702 | GDir* baseDir = g_dir_open(currentDir.c_str(), 0, NULL); |
| 703 | |
| 704 | gchar const *entryName = NULL; |
| 705 | while ((entryName = g_dir_read_name(baseDir)) != NULL) |
| 706 | { |
| 707 | if (entryName[0] == '.') |
| 708 | { |
| 709 | // Files starting with dot should be skipped - it can be temporary files |
| 710 | // created by gsf |
| 711 | continue; |
| 712 | } |
| 713 | std::string entryFullPath = currentDir + G_DIR_SEPARATOR_S; |
| 714 | entryFullPath += entryName; |
| 715 | |
| 716 | if (g_file_test(entryFullPath.c_str(), G_FILE_TEST_IS_DIR)) |
| 717 | { |
| 718 | dirs.push_back(entryFullPath); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | result.push_back( |
| 723 | entryFullPath.substr(directory.length() + 1, |
| 724 | entryFullPath.length() - directory.length())); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | g_dir_close(baseDir); |
| 729 | |
| 730 | } |
| 731 | |
| 732 | return result; |
| 733 | } |
| 734 | |
| 735 | UT_Error IE_Exp_EPUB::compress() |
| 736 | { |
| 737 | |
| 738 | GsfInfile* oebpsDir = gsf_infile_stdio_new( |
| 739 | UT_go_filename_from_uri(m_oebpsDir.c_str()), NULL); |
| 740 | |
| 741 | if (oebpsDir == NULL) |
| 742 | { |
| 743 | UT_DEBUGMSG(("RUDYJ: Can`t open temporary OEBPS directory\n")); |
| 744 | return UT_ERROR; |
| 745 | } |
| 746 | |
| 747 | std::vector<std::string> listing = getFileList( |
| 748 | UT_go_filename_from_uri(m_oebpsDir.c_str())); |
| 749 | for (std::vector<std::string>::iterator i = listing.begin(); i |
| 750 | != listing.end(); i++) |
| 751 | { |
| 752 | GsfOutput* item = gsf_outfile_new_child(GSF_OUTFILE(m_oebps), |
| 753 | (*i).c_str(), FALSE); |
| 754 | std::string fullPath = m_oebpsDir + G_DIR_SEPARATOR_S + *i; |
| 755 | GsfInput* file = UT_go_file_open(fullPath.c_str(), NULL); |
| 756 | |
| 757 | if (file == NULL) |
| 758 | { |
| 759 | UT_DEBUGMSG(("RUDYJ: Can`t open file\n")); |
| 760 | return UT_ERROR; |
| 761 | } |
| 762 | |
| 763 | gsf_output_seek(item, 0, G_SEEK_SET); |
| 764 | gsf_input_seek(file, 0, G_SEEK_SET); |
| 765 | gsf_input_copy(file, item); |
| 766 | gsf_output_close(item); |
| 767 | // Time to delete temporary file |
| 768 | UT_go_file_remove(fullPath.c_str(), NULL); |
| 769 | } |
| 770 | |
| 771 | UT_go_file_remove((m_oebpsDir + G_DIR_SEPARATOR_S + "index.xhtml_files").c_str(), NULL); |
| 772 | UT_go_file_remove(m_oebpsDir.c_str(), NULL); |
| 773 | return UT_OK; |
| 774 | } |
| 775 | |
| 776 | void IE_Exp_EPUB::closeNTags(GsfXMLOut* xml, int n) |
| 777 | { |
| 778 | for (int i = 0; i < n; i++) |
| 779 | { |
| 780 | gsf_xml_out_end_element(xml); |
| 781 | } |
| 782 | |
| 783 | } |
| 784 | |
| 785 | std::string IE_Exp_EPUB::escapeForId(const std::string& src) |
| 786 | { |
| 787 | |
| 788 | return UT_escapeXML(src); |
| 789 | } |
| 790 | |
| 791 | std::string IE_Exp_EPUB::getMimeType(const std::string &uri) |
| 792 | { |
| 793 | const gchar *extension = strchr(uri.c_str(), '.'); |
| 794 | |
| 795 | if (extension == NULL) |
| 796 | { |
| 797 | return UT_go_get_mime_type(uri.c_str()); |
| 798 | } |
| 799 | else |
| 800 | { |
| 801 | if (!UT_go_utf8_collate_casefold(extension + 1, "xhtml")) |
| 802 | { |
| 803 | return "application/xhtml+xml"; |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | return UT_go_get_mime_type(uri.c_str()); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | std::string IE_Exp_EPUB::getAuthor() const |
| 813 | { |
| 814 | std::string property(""); |
| 815 | |
| 816 | if (getDoc()->getMetaDataProp(PD_META_KEY_CREATOR, property) |
| 817 | && property.size()) |
| 818 | { |
| 819 | return property; |
| 820 | } |
| 821 | return "Converted by AbiWord(http://www.abisource.com/)"; |
| 822 | } |
| 823 | |
| 824 | std::string IE_Exp_EPUB::getTitle() const |
| 825 | { |
| 826 | std::string property(""); |
| 827 | |
| 828 | if (getDoc()->getMetaDataProp(PD_META_KEY_TITLE, property) |
| 829 | && property.size()) |
| 830 | { |
| 831 | return property; |
| 832 | } |
| 833 | return "Untitled"; |
| 834 | } |
| 835 | |
| 836 | std::string IE_Exp_EPUB::getLanguage() const |
| 837 | { |
| 838 | std::string property(""); |
| 839 | |
| 840 | if (getDoc()->getMetaDataProp(PD_META_KEY_LANGUAGE, property) |
| 841 | && property.size()) |
| 842 | { |
| 843 | return property; |
| 844 | } |
| 845 | return "en_US"; |
| 846 | } |
| 847 | |
| 848 | UT_Error IE_Exp_EPUB::doOptions() |
| 849 | { |
| 850 | XAP_Frame * pFrame = XAP_App::getApp()->getLastFocussedFrame(); |
| 851 | |
| 852 | if (!pFrame || isCopying()) return UT_OK; |
| 853 | if (pFrame) |
| 854 | { |
| 855 | AV_View * pView = pFrame->getCurrentView(); |
| 856 | if (pView) |
| 857 | { |
| 858 | GR_Graphics * pG = pView->getGraphics(); |
| 859 | if (pG && pG->queryProperties(GR_Graphics::DGP_PAPER)) |
| 860 | { |
| 861 | return UT_OK; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | //FIXME:FIDENCIO: Remove this clause when Cocoa's dialog is implemented |
| 867 | #ifdef TOOLKIT_COCOA |
| 868 | return UT_OK; |
| 869 | #else |
| 870 | /* run the dialog |
| 871 | */ |
| 872 | |
| 873 | XAP_Dialog_Id id = m_iDialogExport; |
| 874 | |
| 875 | XAP_DialogFactory * pDialogFactory |
| 876 | = static_cast<XAP_DialogFactory *> (XAP_App::getApp()->getDialogFactory()); |
| 877 | |
| 878 | AP_Dialog_EpubExportOptions* pDialog |
| 879 | = static_cast<AP_Dialog_EpubExportOptions*> (pDialogFactory->requestDialog(id)); |
| 880 | |
| 881 | if (pDialog == NULL) |
| 882 | { |
| 883 | return UT_OK; |
| 884 | } |
| 885 | |
| 886 | pDialog->setEpubExportOptions(&m_exp_opt, XAP_App::getApp()); |
| 887 | |
| 888 | pDialog->runModal(pFrame); |
| 889 | |
| 890 | /* extract what they did |
| 891 | */ |
| 892 | bool bSave = pDialog->shouldSave(); |
| 893 | |
| 894 | pDialogFactory->releaseDialog(pDialog); |
| 895 | |
| 896 | if (!bSave) |
| 897 | { |
| 898 | return UT_SAVE_CANCELLED; |
| 899 | } |
| 900 | return UT_OK; |
| 901 | #endif |
| 902 | } |
| 903 | |
| 904 | void IE_Exp_EPUB::registerDialogs() |
| 905 | { |
| 906 | // Because there is no implementation of export options dialog |
| 907 | // for Mac OS we just use defaults for that platform |
| 908 | #ifdef WIN32 |
| 909 | XAP_DialogFactory * pFactory = static_cast<XAP_DialogFactory *>(XAP_App::getApp()->getDialogFactory()); |
| 910 | m_iDialogExport = pFactory->registerDialog(ap_Dialog_EpubExportOptions_Constructor, XAP_DLGT_NON_PERSISTENT); |
| 911 | #elif defined TOOLKIT_COCOA |
| 912 | #else |
| 913 | XAP_DialogFactory * pFactory = static_cast<XAP_DialogFactory *>(XAP_App::getApp()->getDialogFactory()); |
| 914 | m_iDialogExport = pFactory->registerDialog(ap_Dialog_EpubExportOptions_Constructor, XAP_DLGT_NON_PERSISTENT); |
| 915 | #endif |
| 916 | } |