]> granicus.if.org Git - multimarkdown/commitdiff
FIXED: Improve transclusion search folder resolution
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Wed, 7 Jun 2017 23:37:46 +0000 (19:37 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Wed, 7 Jun 2017 23:37:46 +0000 (19:37 -0400)
Sources/libMultiMarkdown/transclude.c
Sources/multimarkdown/main.c

index 2bd2c69a821d99372ed9cc7dc81fa27cbdfe0d84..547cb2f0d57ee687cdb07b5231d0ccaca8e9c28e 100644 (file)
@@ -195,8 +195,8 @@ void Test_path_from_dir_base(CuTest* tc) {
 /// Separate filename and directory from a full path
 ///
 /// See http://stackoverflow.com/questions/1575278/function-to-split-a-filepath-into-path-and-file
-void split_path_file(char ** dir, char ** file, char * path) {
-    char * slash = path, * next;
+void split_path_file(char ** dir, char ** file, const char * path) {
+    const char * slash = path, * next;
 
 #if defined(__WIN32)
        const char sep[] = "\\/";       // Windows allows either variant
@@ -280,6 +280,10 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha
 
        // Ensure search_folder is tidied up
        char * search_folder = path_from_dir_base(search_path, NULL);
+       char * source_folder;
+       char * source_file;
+
+       split_path_file(&source_folder, &source_file, source_path);
 
        char * start, * stop;
        char text[1100];
@@ -298,11 +302,14 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha
                        // The new file overrides the search path
                        free(search_folder);
 
-                       // Calculate new search path relative to source
-                       search_folder = path_from_dir_base(source_path, temp);
+                       // Calculate new search path relative to source document
+                       search_folder = path_from_dir_base(source_folder, temp);
                }
        }
 
+       free(source_folder);
+       free(source_file);
+
        mmd_engine_free(e, false);
 
        if (search_folder == NULL) {
@@ -423,14 +430,7 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha
                                d_string_erase(source, start - source->str, 2 + stop - start);
 
                                // Recursively check this file for transclusions
-                               char * new_search_path;
-                               char * source_filename;
-                               split_path_file(&new_search_path, &source_filename, file_path->str);
-
-                               mmd_transclude_source(buffer, search_folder, new_search_path, format, parse_stack, manifest);
-
-                               free(new_search_path);
-                               free(source_filename);
+                               mmd_transclude_source(buffer, search_folder, file_path->str, format, parse_stack, manifest);
                                
                                // Strip metadata from buffer now that we have parsed it
                                e = mmd_engine_create_with_dstring(buffer, EXT_TRANSCLUDE);
index a77b5d6f770d41a320508b7e0315c188fb923947..fd49bd1a19ab4a5ae00306d4073676a184cdce06 100644 (file)
@@ -347,7 +347,7 @@ int main(int argc, char** argv) {
                        char * folder = dirname((char *) a_file->filename[i]);
 
                        if (extensions & EXT_TRANSCLUDE) {
-                               mmd_transclude_source(buffer, folder, "", format, NULL, NULL);
+                               mmd_transclude_source(buffer, folder, a_file->filename[i], format, NULL, NULL);
        
                                // Don't free folder -- owned by dirname
                        }
@@ -443,7 +443,7 @@ int main(int argc, char** argv) {
 
                if ((extensions & EXT_TRANSCLUDE) && (a_file->count == 1)) {
                        // Perform transclusion(s)
-                       mmd_transclude_source(buffer, folder, "", format, NULL, NULL);
+                       mmd_transclude_source(buffer, folder, a_file->filename[0], format, NULL, NULL);
 
                        // Don't free folder -- owned by dirname
                }