From a1e8884121c88a204719024a2a1e5b57c2729618 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Wed, 7 Jun 2017 19:37:46 -0400 Subject: [PATCH] FIXED: Improve transclusion search folder resolution --- Sources/libMultiMarkdown/transclude.c | 24 ++++++++++++------------ Sources/multimarkdown/main.c | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Sources/libMultiMarkdown/transclude.c b/Sources/libMultiMarkdown/transclude.c index 2bd2c69..547cb2f 100644 --- a/Sources/libMultiMarkdown/transclude.c +++ b/Sources/libMultiMarkdown/transclude.c @@ -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); diff --git a/Sources/multimarkdown/main.c b/Sources/multimarkdown/main.c index a77b5d6..fd49bd1 100644 --- a/Sources/multimarkdown/main.c +++ b/Sources/multimarkdown/main.c @@ -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 } -- 2.40.0