From: Fletcher T. Penney Date: Sat, 3 Jun 2017 20:10:33 +0000 (-0400) Subject: FIXED: Fix issue with transclusion and relative paths X-Git-Tag: 6.1.0^2~25 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c5ce51ba7ddfd59939bde89bd20c037d0e8dfb6;p=multimarkdown FIXED: Fix issue with transclusion and relative paths --- diff --git a/Sources/libMultiMarkdown/transclude.c b/Sources/libMultiMarkdown/transclude.c index 36ca65b..2bd2c69 100644 --- a/Sources/libMultiMarkdown/transclude.c +++ b/Sources/libMultiMarkdown/transclude.c @@ -298,13 +298,8 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha // The new file overrides the search path free(search_folder); - // First, calculate path to this source file - char * temp_path = path_from_dir_base(search_path, source_path); - - // Then, calculate new search path relative to source - search_folder = path_from_dir_base(temp_path, temp); - - free(temp_path); + // Calculate new search path relative to source + search_folder = path_from_dir_base(source_path, temp); } } @@ -323,6 +318,9 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha parse_stack = stack_new(0); } + // Remember where we currently are in the stack + size_t stack_depth = parse_stack->size; + // Iterate through source text, looking for `{{foo}}` start = strstr(source->str, "{{"); @@ -385,7 +383,7 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha } // Prevent infinite recursive loops - for (int i = 0; i < parse_stack->size; ++i) + for (int i = 0; i < stack_depth; ++i) { temp = stack_peek_index(parse_stack, i); if (strcmp(file_path->str, temp) == 0) { @@ -480,6 +478,9 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha if (parsed == NULL) { // Free temp stack stack_free(parse_stack); + } else { + // Reset stack depth + parse_stack->size = stack_depth; } free(search_folder); diff --git a/Sources/multimarkdown/main.c b/Sources/multimarkdown/main.c index 0366efb..04bbc7e 100644 --- a/Sources/multimarkdown/main.c +++ b/Sources/multimarkdown/main.c @@ -340,7 +340,6 @@ 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); // Don't free folder -- owned by dirname @@ -437,7 +436,6 @@ 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); // Don't free folder -- owned by dirname diff --git a/tests/MMD6Tests/Transclusion.fodt b/tests/MMD6Tests/Transclusion.fodt index ea2ac6b..6694267 100644 --- a/tests/MMD6Tests/Transclusion.fodt +++ b/tests/MMD6Tests/Transclusion.fodt @@ -282,7 +282,10 @@ This text is included in bar.txt. -This can be transcluded without causing an infinite loop – {{foo.txt}} +This can be transcluded without causing an infinite loop – +This text is included in foo.txt. + +This should not be transcluded to avoid an infinite loop – {{foo.txt}} This is a file with no metadata. @@ -297,7 +300,10 @@ does not override the This text is included in bar.txt. -This can be transcluded without causing an infinite loop – {{foo.txt}} +This can be transcluded without causing an infinite loop – +This text is included in foo.txt. + +This should not be transcluded to avoid an infinite loop – {{foo.txt}} This is a file with no metadata. diff --git a/tests/MMD6Tests/Transclusion.html b/tests/MMD6Tests/Transclusion.html index 2f380d4..8ebeb50 100644 --- a/tests/MMD6Tests/Transclusion.html +++ b/tests/MMD6Tests/Transclusion.html @@ -12,7 +12,10 @@

This text is included in bar.txt.

-

This can be transcluded without causing an infinite loop – {{foo.txt}}

+

This can be transcluded without causing an infinite loop – +This text is included in foo.txt.

+ +

This should not be transcluded to avoid an infinite loop – {{foo.txt}}

This is a file with no metadata.
 
@@ -29,7 +32,10 @@ does not override the transclude base metadata.

This text is included in bar.txt.

-

This can be transcluded without causing an infinite loop – {{foo.txt}}

+

This can be transcluded without causing an infinite loop – +This text is included in foo.txt.

+ +

This should not be transcluded to avoid an infinite loop – {{foo.txt}}

This is a file with no metadata.
 
diff --git a/tests/MMD6Tests/Transclusion.tex b/tests/MMD6Tests/Transclusion.tex index 9c2dc64..630f3d9 100644 --- a/tests/MMD6Tests/Transclusion.tex +++ b/tests/MMD6Tests/Transclusion.tex @@ -8,7 +8,10 @@ This should not be transcluded to avoid an infinite loop -- \{\{foo.txt\}\} This text is included in \texttt{bar.txt}. -This can be transcluded without causing an infinite loop -- \{\{foo.txt\}\} +This can be transcluded without causing an infinite loop -- +This text is included in \texttt{foo.txt}. + +This should not be transcluded to avoid an infinite loop -- \{\{foo.txt\}\} \begin{verbatim} This is a file with no metadata. @@ -27,7 +30,10 @@ does \emph{not} override the \texttt{transclude base} metadata. This text is included in \texttt{bar.txt}. -This can be transcluded without causing an infinite loop -- \{\{foo.txt\}\} +This can be transcluded without causing an infinite loop -- +This text is included in \texttt{foo.txt}. + +This should not be transcluded to avoid an infinite loop -- \{\{foo.txt\}\} \begin{verbatim} This is a file with no metadata. diff --git a/tests/MMD6Tests/bar.txt b/tests/MMD6Tests/bar.txt index bbab1a3..cb8b462 100644 --- a/tests/MMD6Tests/bar.txt +++ b/tests/MMD6Tests/bar.txt @@ -3,7 +3,7 @@ transclude base: transclusion This text is included in `bar.txt`. -This can be transcluded without causing an infinite loop -- {{foo.txt}} +This can be transcluded without causing an infinite loop -- {{../foo.txt}} ``` {{nometa.txt}}