/** MultiMarkdown -- Lightweight markup processor to produce HTML, LaTeX, and more. @file epub.c @brief @author Fletcher T. Penney @bug **/ /* Copyright © 2016 - 2017 Fletcher T. Penney. The `MultiMarkdown 6` project is released under the MIT License.. GLibFacade.c and GLibFacade.h are from the MultiMarkdown v4 project: https://github.com/fletcher/MultiMarkdown-4/ MMD 4 is released under both the MIT License and GPL. CuTest is released under the zlib/libpng license. See CuTest.c for the text of the license. ## The MIT License ## Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #ifdef USE_CURL #include #endif #include "epub.h" #include "file.h" #include "html.h" #include "i18n.h" #include "miniz.h" #include "uuid.h" #include "writer.h" #include "zip.h" #define print(x) d_string_append(out, x) #define print_const(x) d_string_append_c_array(out, x, sizeof(x) - 1) #define print_char(x) d_string_append_c(out, x) #define printf(...) d_string_append_printf(out, __VA_ARGS__) #define print_token(t) d_string_append_c_array(out, &(source[t->start]), t->len) #define print_localized(x) mmd_print_localized_char_html(out, x, scratch) /// strdup() not available on all platforms static char * my_strdup(const char * source) { if (source == NULL) { return NULL; } char * result = malloc(strlen(source) + 1); if (result) { strcpy(result, source); } return result; } char * epub_mimetype(void) { return my_strdup("application/epub+zip"); } char * epub_container_xml(void) { DString * container = d_string_new(""); d_string_append(container, "\n"); d_string_append(container, "\n"); d_string_append(container, "\n"); d_string_append(container, "\n"); d_string_append(container, "\n"); d_string_append(container, "\n"); char * result = container->str; d_string_free(container, false); return result; } char * epub_package_document(scratch_pad * scratch) { DString * out = d_string_new(""); meta * m; d_string_append(out, "\n"); d_string_append(out, "\n"); // Metadata d_string_append(out, "\n"); // Identifier HASH_FIND_STR(scratch->meta_hash, "uuid", m); if (m) { print_const("urn:uuid:"); mmd_print_string_html(out, m->value, false); print_const("\n"); } else { print_const("urn:uuid:"); char * id = uuid_new(); print(id); print_const("\n"); free(id); } // Title HASH_FIND_STR(scratch->meta_hash, "title", m); if (m) { print_const(""); mmd_print_string_html(out, m->value, false); print_const("\n"); } else { print_const("Untitled\n"); } // Author HASH_FIND_STR(scratch->meta_hash, "author", m); if (m) { print_const(""); mmd_print_string_html(out, m->value, false); print_const("\n"); } // Language HASH_FIND_STR(scratch->meta_hash, "language", m); if (m) { print_const(""); mmd_print_string_html(out, m->value, false); print_const("\n"); } else { switch (scratch->language) { case LC_ES: print_const("es\n"); break; case LC_DE: print_const("de\n"); break; case LC_FR: print_const("fr\n"); break; case LC_NL: print_const("nl\n"); break; case LC_SV: print_const("sv\n"); break; default: print_const("en\n"); } } // Date HASH_FIND_STR(scratch->meta_hash, "date", m); if (m) { print_const(""); mmd_print_string_html(out, m->value, false); print_const("\n"); } else { time_t t = time(NULL); struct tm * today = localtime(&t); d_string_append_printf(out, "%d-%02d-%02d\n", today->tm_year + 1900, today->tm_mon + 1, today->tm_mday); } d_string_append(out, "\n"); // Manifest d_string_append(out, "\n"); d_string_append(out, "\n"); d_string_append(out, "\n"); d_string_append(out, "\n"); // Spine d_string_append(out, "\n"); d_string_append(out, ""); d_string_append(out, "\n"); d_string_append(out, "\n"); char * result = out->str; d_string_free(out, false); return result; } void epub_export_nav_entry(DString * out, const char * source, scratch_pad * scratch, size_t * counter, short level) { token * entry, * next; short entry_level, next_level; char * temp_char; print_const("\n
    \n"); // Iterate over tokens while (*counter < scratch->header_stack->size) { // Get token for header entry = stack_peek_index(scratch->header_stack, *counter); entry_level = raw_level_for_header(entry); if (entry_level >= level) { // This entry is a direct descendant of the parent temp_char = label_from_header(source, entry); printf("
  1. ", temp_char); mmd_export_token_tree_html(out, source, entry->child, scratch); print_const(""); if (*counter < scratch->header_stack->size - 1) { next = stack_peek_index(scratch->header_stack, *counter + 1); next_level = next->type - BLOCK_H1 + 1; if (next_level > entry_level) { // This entry has children (*counter)++; epub_export_nav_entry(out, source, scratch, counter, entry_level + 1); } } print_const("
  2. \n"); free(temp_char); } else if (entry_level < level ) { // If entry < level, exit this level // Decrement counter first, so that we can test it again later (*counter)--; break; } // Increment counter (*counter)++; } print_const("
\n"); } void epub_export_nav(DString * out, mmd_engine * e, scratch_pad * scratch) { size_t counter = 0; epub_export_nav_entry(out, e->dstr->str, scratch, &counter, 0); } char * epub_nav(mmd_engine * e, scratch_pad * scratch) { meta * temp; DString * out = d_string_new(""); d_string_append(out, "\n\n"); d_string_append(out, "\n"); HASH_FIND_STR(scratch->meta_hash, "title", temp); if (temp) { mmd_print_string_html(out, temp->value, false); } else { print_const("Untitled"); } print_const("\n\n"); print_const("\n\n\n\n"); char * result = out->str; d_string_free(out, false); return result; } static bool add_asset_from_file(mz_zip_archive * pZip, asset * a, const char * destination, const char * directory) { if (!directory) { return false; } char * path = path_from_dir_base(directory, a->url); mz_bool status; bool result = false; DString * buffer = scan_file(path); if (buffer && buffer->currentStringLength > 0) { status = mz_zip_writer_add_mem(pZip, destination, buffer->str, buffer->currentStringLength, MZ_BEST_COMPRESSION); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } d_string_free(buffer, true); result = true; } free(path); return result; } #ifdef USE_CURL // Dynamic buffer for downloading files in memory // Based on https://curl.haxx.se/libcurl/c/getinmemory.html struct MemoryStruct { char * memory; size_t size; }; static size_t write_memory(void * contents, size_t size, size_t nmemb, void * userp) { size_t realsize = size * nmemb; struct MemoryStruct * mem = (struct MemoryStruct *)userp; mem->memory = realloc(mem->memory, mem->size + realsize + 1); if (mem->memory == NULL) { // Out of memory fprintf(stderr, "Out of memory\n"); return 0; } memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } // Add assets to zipfile using libcurl static void add_assets(mz_zip_archive * pZip, mmd_engine * e, const char * directory) { asset * a, * a_tmp; if (e->asset_hash) { CURL * curl; CURLcode res; struct MemoryStruct chunk; chunk.memory = malloc(1); chunk.size = 0; char destination[100] = "OEBPS/assets/"; destination[49] = '\0'; mz_bool status; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); HASH_ITER(hh, e->asset_hash, a, a_tmp) { curl_easy_setopt(curl, CURLOPT_URL, a->url); res = curl_easy_perform(curl); memcpy(&destination[13], a->asset_path, 36); if (res != CURLE_OK) { // Attempt to add asset from local file if (!add_asset_from_file(pZip, a, destination, directory)) { fprintf(stderr, "Unable to store '%s' in EPUB\n", a->url); } } else { // Store downloaded file in zip status = mz_zip_writer_add_mem(pZip, destination, chunk.memory, chunk.size, MZ_BEST_COMPRESSION); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } } } } } #else // Add local assets only (libcurl not available) static void add_assets(mz_zip_archive * pZip, mmd_engine * e, const char * directory) { asset * a, * a_tmp; if (e->asset_hash) { char destination[100] = "OEBPS/assets/"; destination[49] = '\0'; HASH_ITER(hh, e->asset_hash, a, a_tmp) { memcpy(&destination[13], a->asset_path, 36); // Attempt to add asset from local file if (!add_asset_from_file(pZip, a, destination, directory)) { fprintf(stderr, "Unable to store '%s' in EPUB\n", a->url); } } } } #endif // Use the miniz library to create a zip archive for the EPUB document void epub_write_wrapper(const char * filepath, const char * body, mmd_engine * e, const char * directory) { FILE * output_stream; DString * result = epub_create(body, e, directory); if (!(output_stream = fopen(filepath, "w"))) { // Failed to open file perror(filepath); } else { fwrite(&(result->str), result->currentStringLength, 1, output_stream); fclose(output_stream); } d_string_free(result, true); } DString * epub_create(const char * body, mmd_engine * e, const char * directory) { DString * result = d_string_new(""); scratch_pad * scratch = scratch_pad_new(e, FORMAT_EPUB); mz_bool status; char * data; size_t len; mz_zip_archive zip; zip_new_archive(&zip); // Add mimetype data = epub_mimetype(); len = strlen(data); status = mz_zip_writer_add_mem(&zip, "mimetype", data, len, MZ_BEST_COMPRESSION); free(data); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Create directories status = mz_zip_writer_add_mem(&zip, "OEBPS/", NULL, 0, MZ_NO_COMPRESSION); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } status = mz_zip_writer_add_mem(&zip, "META-INF/", NULL, 0, MZ_NO_COMPRESSION); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Add container data = epub_container_xml(); len = strlen(data); status = mz_zip_writer_add_mem(&zip, "META-INF/container.xml", data, len, MZ_BEST_COMPRESSION); free(data); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Add package data = epub_package_document(scratch); len = strlen(data); status = mz_zip_writer_add_mem(&zip, "OEBPS/main.opf", data, len, MZ_BEST_COMPRESSION); free(data); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Add nav data = epub_nav(e, scratch); len = strlen(data); status = mz_zip_writer_add_mem(&zip, "OEBPS/nav.xhtml", data, len, MZ_BEST_COMPRESSION); free(data); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Add main document len = strlen(body); status = mz_zip_writer_add_mem(&zip, "OEBPS/main.xhtml", body, len, MZ_BEST_COMPRESSION); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } // Add assets add_assets(&zip, e, directory); scratch_pad_free(scratch); // Finalize zip archive and extract data free(result->str); status = mz_zip_writer_finalize_heap_archive(&zip, (void **) & (result->str), (size_t *) & (result->currentStringLength)); if (!status) { fprintf(stderr, "Error adding asset to zip.\n"); } return result; }