From: Fletcher T. Penney Date: Fri, 21 Jul 2017 17:55:05 +0000 (-0400) Subject: ADDED: Add support for MMD Header and MMD Footer metadata; FIXED: Fix transclusion... X-Git-Tag: 6.2.0^2~21 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=148ebd209e9a27ef5a5e7b27d21d33af39de601a;p=multimarkdown ADDED: Add support for MMD Header and MMD Footer metadata; FIXED: Fix transclusion to skip over metadata in main document; FIXED: Fix metavalue_for_key with NULL result --- diff --git a/Sources/libMultiMarkdown/include/libMultiMarkdown.h b/Sources/libMultiMarkdown/include/libMultiMarkdown.h index 540a89d..9bd4f9f 100644 --- a/Sources/libMultiMarkdown/include/libMultiMarkdown.h +++ b/Sources/libMultiMarkdown/include/libMultiMarkdown.h @@ -272,6 +272,14 @@ typedef struct stack stack; void mmd_transclude_source(DString * source, const char * search_path, const char * source_path, short format, struct stack * parsed, struct stack * manifest); +/// If MMD Header metadata used, insert it into appropriate place +void mmd_prepend_mmd_header(DString * source); + + +/// If MMD Footer metadata used, insert it into appropriate place +void mmd_append_mmd_footer(DString * source); + + /// Accept all CriticMarkup changes in the source string void mmd_critic_markup_accept(DString * d); diff --git a/Sources/libMultiMarkdown/mmd.c b/Sources/libMultiMarkdown/mmd.c index 5bbd59f..1be90e5 100644 --- a/Sources/libMultiMarkdown/mmd.c +++ b/Sources/libMultiMarkdown/mmd.c @@ -2103,7 +2103,8 @@ char * mmd_string_metavalue_for_key(char * source, const char * key) { char * result; mmd_engine * e = mmd_engine_create_with_string(source, 0); - result = my_strdup(mmd_engine_metavalue_for_key(e, key)); + result = mmd_engine_metavalue_for_key(e, key); + result = my_strdup(result); mmd_engine_free(e, true); @@ -2117,7 +2118,10 @@ char * mmd_d_string_metavalue_for_key(DString * source, const char * key) { char * result; mmd_engine * e = mmd_engine_create_with_dstring(source, 0); - result = my_strdup(mmd_engine_metavalue_for_key(e, key)); + result = mmd_engine_metavalue_for_key(e, key); + if (result) { + result = my_strdup(result); + } mmd_engine_free(e, false); diff --git a/Sources/libMultiMarkdown/transclude.c b/Sources/libMultiMarkdown/transclude.c index 51adec6..0b733ee 100644 --- a/Sources/libMultiMarkdown/transclude.c +++ b/Sources/libMultiMarkdown/transclude.c @@ -342,7 +342,7 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha // Iterate through source text, looking for `{{foo}}` - start = strstr(source->str, "{{"); + start = strstr(&source->str[offset], "{{"); while (start != NULL) { stop = strstr(start, "}}"); @@ -502,3 +502,32 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha } + +/// If MMD Header metadata used, insert it into appropriate place +void mmd_prepend_mmd_header(DString * source) { + size_t end; + + if (mmd_d_string_has_metadata(source, &end)) { + char * meta = mmd_d_string_metavalue_for_key(source, "mmdheader"); + + if (meta) { + d_string_insert(source, end, "\n\n"); + d_string_insert(source, end + 2, meta); + free(meta); + } + } +} + + +/// If MMD Footer metadata used, insert it into appropriate place +void mmd_append_mmd_footer(DString * source) { + char * meta = mmd_d_string_metavalue_for_key(source, "mmdfooter"); + + if (meta) { + d_string_append(source, "\n\n"); + d_string_append(source, meta); + + free(meta); + } +} + diff --git a/Sources/libMultiMarkdown/transclude.h b/Sources/libMultiMarkdown/transclude.h index 364ab60..f21aff2 100644 --- a/Sources/libMultiMarkdown/transclude.h +++ b/Sources/libMultiMarkdown/transclude.h @@ -76,4 +76,12 @@ DString * scan_file(const char * fname); void mmd_transclude_source(DString * source, const char * search_path, const char * source_path, short format, stack * parsed, stack * manifest); +/// If MMD Header metadata used, insert it into appropriate place +void mmd_prepend_mmd_header(DString * source); + + +/// If MMD Footer metadata used, insert it into appropriate place +void mmd_append_mmd_footer(DString * source); + + #endif diff --git a/Sources/multimarkdown/main.c b/Sources/multimarkdown/main.c index 0697aa1..3a7e66d 100644 --- a/Sources/multimarkdown/main.c +++ b/Sources/multimarkdown/main.c @@ -381,6 +381,11 @@ int main(int argc, char** argv) { // Perform transclusion(s) char * folder = dirname((char *) a_file->filename[i]); + if (!(extensions & EXT_COMPATIBILITY)) { + mmd_prepend_mmd_header(buffer); + mmd_append_mmd_footer(buffer); + } + if (extensions & EXT_TRANSCLUDE) { mmd_transclude_source(buffer, folder, a_file->filename[i], format, NULL, NULL); @@ -476,6 +481,11 @@ int main(int argc, char** argv) { char * folder = NULL; + if (!(extensions & EXT_COMPATIBILITY)) { + mmd_prepend_mmd_header(buffer); + mmd_append_mmd_footer(buffer); + } + if ((extensions & EXT_TRANSCLUDE) && (a_file->count == 1)) { // Perform transclusion(s) @@ -485,6 +495,7 @@ int main(int argc, char** argv) { char absolute[PATH_MAX + 1]; realpath(a_file->filename[0], absolute); folder = dirname((char *) a_file->filename[0]); + mmd_transclude_source(buffer, folder, absolute, format, NULL, NULL); #else // If undefined, then we *should* be able to use a NULL pointer to allocate diff --git a/tests/MMD6Tests/MMD Header and Footer.fodt b/tests/MMD6Tests/MMD Header and Footer.fodt new file mode 100644 index 0000000..75e3936 --- /dev/null +++ b/tests/MMD6Tests/MMD Header and Footer.fodt @@ -0,0 +1,288 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Bibliography + + + + MMD Header and Footer + + + +MultiMarkdown header. + +Main Body + +MultiMarkdown footer. + + + diff --git a/tests/MMD6Tests/MMD Header and Footer.html b/tests/MMD6Tests/MMD Header and Footer.html new file mode 100644 index 0000000..a8eede5 --- /dev/null +++ b/tests/MMD6Tests/MMD Header and Footer.html @@ -0,0 +1,17 @@ + + + + + MMD Header and Footer + + + +

MultiMarkdown header.

+ +

Main Body

+ +

MultiMarkdown footer.

+ + + + diff --git a/tests/MMD6Tests/MMD Header and Footer.htmlc b/tests/MMD6Tests/MMD Header and Footer.htmlc new file mode 100644 index 0000000..a7922b3 --- /dev/null +++ b/tests/MMD6Tests/MMD Header and Footer.htmlc @@ -0,0 +1,6 @@ +

Title: MMD Header and Footer +latex config: article +MMD Header: {{header.txt}} +MMD Footer: {{footer.txt}}

+ +

Main Body

diff --git a/tests/MMD6Tests/MMD Header and Footer.tex b/tests/MMD6Tests/MMD Header and Footer.tex new file mode 100644 index 0000000..de240a3 --- /dev/null +++ b/tests/MMD6Tests/MMD Header and Footer.tex @@ -0,0 +1,12 @@ +\input{mmd6-article-leader} +\def\mytitle{MMD Header and Footer} +\input{mmd6-article-begin} + +MultiMarkdown \emph{header}. + +Main Body + +MultiMarkdown \emph{footer}. + +\input{mmd6-article-footer} +\end{document} diff --git a/tests/MMD6Tests/MMD Header and Footer.text b/tests/MMD6Tests/MMD Header and Footer.text new file mode 100644 index 0000000..a7edbe7 --- /dev/null +++ b/tests/MMD6Tests/MMD Header and Footer.text @@ -0,0 +1,6 @@ +Title: MMD Header and Footer +latex config: article +MMD Header: {{header.txt}} +MMD Footer: {{footer.txt}} + +Main Body diff --git a/tests/MMD6Tests/footer.txt b/tests/MMD6Tests/footer.txt new file mode 100644 index 0000000..5a35449 --- /dev/null +++ b/tests/MMD6Tests/footer.txt @@ -0,0 +1 @@ +MultiMarkdown *footer*. \ No newline at end of file diff --git a/tests/MMD6Tests/header.txt b/tests/MMD6Tests/header.txt new file mode 100644 index 0000000..aef878f --- /dev/null +++ b/tests/MMD6Tests/header.txt @@ -0,0 +1 @@ +MultiMarkdown *header*.