]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Add regular TextBundle format support
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Mon, 3 Jul 2017 10:47:48 +0000 (06:47 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Mon, 3 Jul 2017 10:47:48 +0000 (06:47 -0400)
Sources/libMultiMarkdown/mmd.c
Sources/libMultiMarkdown/textbundle.c
Sources/libMultiMarkdown/textbundle.h
Sources/libMultiMarkdown/writer.c
Sources/libMultiMarkdown/zip.c
Sources/libMultiMarkdown/zip.h
Sources/multimarkdown/main.c

index 78f743c05289c2dca631209e8e2bea5b600f7ba7..a493d8ecc6c8e4ba58c4037b66f243eee1d4f060 100644 (file)
@@ -2220,6 +2220,9 @@ void mmd_engine_convert_to_file(mmd_engine * e, short format, const char * direc
                case FORMAT_EPUB:
                        epub_write_wrapper(filepath, output->str, e, directory);
                        break;
+               case FORMAT_TEXTBUNDLE:
+                       // TODO: Need to implement this
+                       break;
                case FORMAT_TEXTBUNDLE_COMPRESSED:
                        textbundle_write_wrapper(filepath, output->str, e, directory);
                        break;
@@ -2280,6 +2283,7 @@ DString * mmd_engine_convert_to_data(mmd_engine * e, short format, const char *
 
                        d_string_free(output, true);
                        break;
+               case FORMAT_TEXTBUNDLE:
                case FORMAT_TEXTBUNDLE_COMPRESSED:
                        result = textbundle_create(output->str, e, directory);
 
index e24640b1e9d0a97b27950e07b71f1c01c0a03623..9559f588f4b085ba40a29480688aae9e7e8864cb 100644 (file)
 #endif
 
 #include "textbundle.h"
-#include "html.h"
 #include "miniz.h"
 #include "transclude.h"
 #include "writer.h"
@@ -360,24 +359,6 @@ void sub_asset_paths(DString * text, mmd_engine * e) {
 }
 
 
-// Use the miniz library to create a zip archive for the TEXTBUNDLE_COMPRESSED document
-void textbundle_write_wrapper(const char * filepath, const char * body, mmd_engine * e, const char * directory) {
-       FILE * output_stream;
-
-       DString * result = textbundle_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 * textbundle_create(const char * body, mmd_engine * e, const char * directory) {
        DString * result = d_string_new("");
        scratch_pad * scratch = scratch_pad_new(e, FORMAT_TEXTBUNDLE_COMPRESSED);
@@ -438,3 +419,22 @@ DString * textbundle_create(const char * body, mmd_engine * e, const char * dire
 
        return result;
 }
+
+
+
+// Use the miniz library to create a zip archive for the TEXTBUNDLE_COMPRESSED document
+void textbundle_write_wrapper(const char * filepath, const char * body, mmd_engine * e, const char * directory) {
+       FILE * output_stream;
+
+       DString * result = textbundle_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);
+}
index 7b7aa7ebdf5c18b07d7a6297208ef840641ee18f..68d8bfd7b40c923d77f12192b070d98aab7da60c 100644 (file)
 #include "d_string.h"
 #include "mmd.h"
 
-void textbundle_write_wrapper(const char * root_path, const char * body, mmd_engine * e, const char * directory);
+void textbundle_write_wrapper(const char * filepath, const char * body, mmd_engine * e, const char * directory);
 
 DString * textbundle_create(const char * body, mmd_engine * e, const char * directory);
 
index db58f6e21da77ac8e820b50492fa9c3d05a528f8..95e1f423c60fad781ae63ed28a08e6e887e25707 100644 (file)
@@ -1731,6 +1731,7 @@ void mmd_engine_export_token_tree(DString * out, mmd_engine * e, short format) {
 
                        break;
                case FORMAT_EPUB:
+               case FORMAT_TEXTBUNDLE:
                case FORMAT_TEXTBUNDLE_COMPRESSED:
                        scratch->store_assets = true;
 
index 88cba3e47245c8b89b8881322356d053c6c1f1b6..f5b102ab607fbd861797b8bff4cba046b51abbb9 100644 (file)
 
 */
 
-
-#include "miniz.h"
 #include "zip.h"
 
+#include <dirent.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
 
+// Create new zip archive
 void zip_new_archive(mz_zip_archive * pZip) {
        memset(pZip, 0, sizeof(mz_zip_archive));
 
@@ -118,3 +122,77 @@ void zip_new_archive(mz_zip_archive * pZip) {
        }
 }
 
+
+// Unzip archive to specified file path
+mz_bool unzip_archive_to_path(mz_zip_archive * pZip, const char * path) {
+       // Ensure folder 'path' exists
+
+       DIR * dir = opendir(path);
+       mz_bool status;
+
+       if (!dir) {
+               // path is not an existing directory
+
+               if (access(path, F_OK) == 0) {
+                       // path is an existing file
+                       fprintf(stderr, "'%s' is an existing file.\n", path);
+                       return -1;
+               } else {
+                       // path doesn't exist - create directory
+                       mkdir(path, 0755);
+               }
+       }
+
+       dir = opendir(path);
+
+       if (dir) {
+               // Directory 'path' exists
+               
+               // Remember current working directory
+               char cwd[PATH_MAX + 1];
+               getcwd(cwd, sizeof(cwd));
+
+               // Move into the desired directory
+               chdir(path);
+
+               int file_count = mz_zip_reader_get_num_files(pZip);
+
+               mz_zip_archive_file_stat pStat;
+
+               for (int i = 0; i < file_count; ++i) {
+                       mz_zip_reader_file_stat(pZip, i, &pStat);
+                       if (pStat.m_is_directory) {
+                               // Create the directory
+                               mkdir(pStat.m_filename, 0755);
+                       } else {
+                               status = mz_zip_reader_extract_to_file(pZip, i, pStat.m_filename, 0);
+                               if (!status) {
+                                       fprintf(stderr, "Error extracting file from zip archive.\n");
+                                       return status;
+                               }
+                       }
+               }
+
+               // Return to prior working directory
+               chdir(cwd);
+       }
+
+
+       return 0;
+}
+
+
+// Unzip archive (as plain binary data) to specified file path
+mz_bool unzip_data_to_path(const void * data, size_t size, const char * path) {
+       mz_zip_archive pZip;
+       memset(&pZip, 0, sizeof(mz_zip_archive));
+
+       mz_bool status = mz_zip_reader_init_mem(&pZip, data, size, 0);
+       if (!status) {
+               fprintf(stderr, "mz_zip_reader_init_mem() failed.\n");
+               return status;
+       }
+
+       return unzip_archive_to_path(&pZip, path);
+}
+
index 64c3549d5e549ac96373429b43c3051ed3d958dc..4c774e2fc29f3da3c3e83bf7b574274c7bf80d48 100644 (file)
 #ifndef ZIP_MULTIMARKDOWN_H
 #define ZIP_MULTIMARKDOWN_H
 
+#include "miniz.h"
+
+// Create new zip archive
 void zip_new_archive(mz_zip_archive * pZip);
 
+// Unzip archive to specified file path
+mz_bool unzip_archive_to_path(mz_zip_archive * pZip, const char * path);
+
+// Unzip archive (as plain binary data) to specified file path
+mz_bool unzip_data_to_path(const void * data, size_t len, const char * path);
+
 
 #endif
index 40a76ef37eca0db94bc3952fb5195775a06ee67b..a47890065256c6bed87d5dec770799ae2f6e4764 100644 (file)
@@ -67,6 +67,7 @@
 #include "token.h"
 #include "uuid.h"
 #include "version.h"
+#include "zip.h"
 
 #define kBUFFERSIZE 4096       // How many bytes to read at a time
 
@@ -153,7 +154,7 @@ int main(int argc, char** argv) {
 
                a_rem2                  = arg_rem("", ""),
 
-               a_format                = arg_str0("t", "to", "FORMAT", "convert to FORMAT, FORMAT = html|latex|beamer|memoir|mmd|odf|epub|bundlezip"),
+               a_format                = arg_str0("t", "to", "FORMAT", "convert to FORMAT, FORMAT = html|latex|beamer|memoir|mmd|odf|epub|bundle|bundlezip"),
                a_o                             = arg_file0("o", "output", "FILE", "send output to FILE"),
 
                a_rem3                  = arg_rem("",""),
@@ -280,8 +281,8 @@ int main(int argc, char** argv) {
                        format = FORMAT_ODF;
                else if (strcmp(a_format->sval[0], "epub") == 0)
                        format = FORMAT_EPUB;
-//             else if (strcmp(a_format->sval[0], "bundle") == 0)
-//                     format = FORMAT_TEXTBUNDLE;
+               else if (strcmp(a_format->sval[0], "bundle") == 0)
+                       format = FORMAT_TEXTBUNDLE;
                else if (strcmp(a_format->sval[0], "bundlezip") == 0)
                        format = FORMAT_TEXTBUNDLE_COMPRESSED;
                else {
@@ -407,12 +408,16 @@ int main(int argc, char** argv) {
                                        result = mmd_d_string_convert_to_data(buffer, extensions, format, language, folder);
                                }
 
-                               if (!(output_stream = fopen(output_filename, "w"))) {
-                                       // Failed to open file
-                                       perror(output_filename);
+                               if (FORMAT_TEXTBUNDLE == format) {
+                                       unzip_data_to_path(result->str, result->currentStringLength, output_filename);
                                } else {
-                                       fwrite(result->str, result->currentStringLength, 1, output_stream);
-                                       fclose(output_stream);
+                                       if (!(output_stream = fopen(output_filename, "w"))) {
+                                               // Failed to open file
+                                               perror(output_filename);
+                                       } else {
+                                               fwrite(result->str, result->currentStringLength, 1, output_stream);
+                                               fclose(output_stream);
+                                       }
                                }
 
                                if (FORMAT_MMD != format) {