]> granicus.if.org Git - multimarkdown/commitdiff
FIXED: Fix issue with transclusion and relative paths
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 3 Jun 2017 20:10:33 +0000 (16:10 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 3 Jun 2017 20:10:33 +0000 (16:10 -0400)
Sources/libMultiMarkdown/transclude.c
Sources/multimarkdown/main.c
tests/MMD6Tests/Transclusion.fodt
tests/MMD6Tests/Transclusion.html
tests/MMD6Tests/Transclusion.tex
tests/MMD6Tests/bar.txt

index 36ca65b2df0989f9e3f9047600c0d6e06c00ed21..2bd2c69a821d99372ed9cc7dc81fa27cbdfe0d84 100644 (file)
@@ -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);
index 0366efb7f2341d8ec4b4bef916de59af90a21b0d..04bbc7e86fea8e0a495c75c79e3bf202cc4368b4 100644 (file)
@@ -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
index ea2ac6bbcc9bfa6da7f4d4841796413742682b62..6694267a94e41944c0e4d67ef43f14f10b5c4345 100644 (file)
 
 <text:p text:style-name="Standard">This text is included in <text:span text:style-name="Source_20_Text">bar.txt</text:span>.</text:p>
 
-<text:p text:style-name="Standard">This can be transcluded without causing an infinite loop &#8211; {{foo.txt}}</text:p>
+<text:p text:style-name="Standard">This can be transcluded without causing an infinite loop &#8211;
+This text is included in <text:span text:style-name="Source_20_Text">foo.txt</text:span>.</text:p>
+
+<text:p text:style-name="Standard">This should not be transcluded to avoid an infinite loop &#8211; {{foo.txt}}</text:p>
 
 <text:p text:style-name="Preformatted Text">This is a file with no metadata.<text:line-break/></text:p>
 
@@ -297,7 +300,10 @@ does <text:span text:style-name="MMD-Italic">not</text:span> override the <text:
 
 <text:p text:style-name="Standard">This text is included in <text:span text:style-name="Source_20_Text">bar.txt</text:span>.</text:p>
 
-<text:p text:style-name="Standard">This can be transcluded without causing an infinite loop &#8211; {{foo.txt}}</text:p>
+<text:p text:style-name="Standard">This can be transcluded without causing an infinite loop &#8211;
+This text is included in <text:span text:style-name="Source_20_Text">foo.txt</text:span>.</text:p>
+
+<text:p text:style-name="Standard">This should not be transcluded to avoid an infinite loop &#8211; {{foo.txt}}</text:p>
 
 <text:p text:style-name="Preformatted Text">This is a file with no metadata.<text:line-break/></text:p>
 
index 2f380d42930e642cbe356876a367b19481c6b265..8ebeb50f07ecd2bb0e724ba38b795bdd6787fa5d 100644 (file)
 
 <p>This text is included in <code>bar.txt</code>.</p>
 
-<p>This can be transcluded without causing an infinite loop &#8211; {{foo.txt}}</p>
+<p>This can be transcluded without causing an infinite loop &#8211;
+This text is included in <code>foo.txt</code>.</p>
+
+<p>This should not be transcluded to avoid an infinite loop &#8211; {{foo.txt}}</p>
 
 <pre><code>This is a file with no metadata.
 </code></pre>
@@ -29,7 +32,10 @@ does <em>not</em> override the <code>transclude base</code> metadata.</p>
 
 <p>This text is included in <code>bar.txt</code>.</p>
 
-<p>This can be transcluded without causing an infinite loop &#8211; {{foo.txt}}</p>
+<p>This can be transcluded without causing an infinite loop &#8211;
+This text is included in <code>foo.txt</code>.</p>
+
+<p>This should not be transcluded to avoid an infinite loop &#8211; {{foo.txt}}</p>
 
 <pre><code>This is a file with no metadata.
 </code></pre>
index 9c2dc64ad3814eab241e0140e0b60e265d730b37..630f3d99fa87ca008a92a3633be8dec6a3197f41 100644 (file)
@@ -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.
index bbab1a30edf78bffdf4e93ec2b3dcb74eff38900..cb8b462448a9424aec58d2b78182b99708a7819e 100644 (file)
@@ -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}}