From 84a6cacd05134a598f329d26ab9422c25db63169 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Wed, 8 Feb 2017 12:06:57 -0500 Subject: [PATCH] ADDED: Metadata support for base header level --- README.md | 2 +- src/html.c | 6 ++--- src/libMultiMarkdown.h | 4 ++- src/writer.c | 45 ++++++++++++++++++++++------------ src/writer.h | 5 +++- templates/README.md.in | 2 +- tests/MMD6Tests/Metadata.html | 2 ++ tests/MMD6Tests/Metadata.htmlc | 6 ++++- tests/MMD6Tests/Metadata.text | 4 +++ 9 files changed, 52 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2ce91ca..701f0e2 100644 --- a/README.md +++ b/README.md @@ -421,6 +421,7 @@ features have been implemented: * Basic Citation support * CriticMarkup support * Definition lists +* Figures * Footnotes * Inline and reference footnotes * Image and Link attributes (attributes can now be used with inline links as @@ -453,7 +454,6 @@ Things yet to be completed: * Abbreviations * Glossaries * File Transclusion -* Figures * Table Captions diff --git a/src/html.c b/src/html.c index 8acf7cf..12cc378 100644 --- a/src/html.c +++ b/src/html.c @@ -399,14 +399,14 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t pad(out, 2, scratch); temp_short = t->type - BLOCK_H1 + 1; if (scratch->extensions & EXT_NO_LABELS) { - printf("", temp_short); + printf("", temp_short + scratch->base_header_level - 1); } else { temp_char = label_from_token(source, t); - printf("", temp_short, temp_char); + printf("", temp_short + scratch->base_header_level - 1, temp_char); free(temp_char); } mmd_export_token_tree_html(out, source, t->child, t->start + offset, scratch); - printf("", temp_short); + printf("", temp_short + scratch->base_header_level - 1); scratch->padded = 0; break; case BLOCK_HR: diff --git a/src/libMultiMarkdown.h b/src/libMultiMarkdown.h index aace3a6..0e70d30 100644 --- a/src/libMultiMarkdown.h +++ b/src/libMultiMarkdown.h @@ -282,7 +282,9 @@ enum smart_quotes_language { enum output_format { - FORMAT_HTML + FORMAT_HTML, + FORMAT_LATEX, + FORMAT_ODF, }; diff --git a/src/writer.c b/src/writer.c index 197b8bf..5a0ebe3 100644 --- a/src/writer.c +++ b/src/writer.c @@ -78,7 +78,7 @@ void store_metadata(scratch_pad * scratch, meta * m); /// Temporary storage while exporting parse tree to output format -scratch_pad * scratch_pad_new(mmd_engine * e) { +scratch_pad * scratch_pad_new(mmd_engine * e, short format) { scratch_pad * p = malloc(sizeof(scratch_pad)); if (p) { @@ -88,6 +88,7 @@ scratch_pad * scratch_pad_new(mmd_engine * e) { p->close_para = true; p->extensions = e->extensions; + p->output_format = format; p->quotes_lang = e->quotes_lang; p->language = e->language; @@ -95,6 +96,8 @@ scratch_pad * scratch_pad_new(mmd_engine * e) { p->recurse_depth = 0; + p->base_header_level = 1; + // Store links in a hash for rapid retrieval when exporting p->link_hash = NULL; link * l; @@ -1143,29 +1146,39 @@ void process_metadata_stack(mmd_engine * e, scratch_pad * scratch) { return; meta * m; + short header_level = -10; for (int i = 0; i < e->metadata_stack->size; ++i) { // Check for certain metadata keys m = stack_peek_index(e->metadata_stack, i); - // Certain keys do not force complete documents - if (!(scratch->extensions & EXT_COMPLETE) && - !(scratch->extensions & EXT_SNIPPET)) { - if ((strcmp(m->key, "baseheaderlevel") != 0) && - (strcmp(m->key, "xhtmlheaderlevel") != 0) && - (strcmp(m->key, "htmlheaderlevel") != 0) && - (strcmp(m->key, "latexheaderlevel") != 0) && - (strcmp(m->key, "odfheaderlevel") != 0) && - (strcmp(m->key, "xhtmlheader") != 0) && - (strcmp(m->key, "htmlheader") != 0) && - (strcmp(m->key, "quoteslanguage") != 0)) { - // We found a key that is not in the list, so - // Force a complete document + if (strcmp(m->key, "baseheaderlevel") == 0) { + if (header_level == -10) + header_level = atoi(m->value); + } else if (strcmp(m->key, "htmlheaderlevel") == 0) { + if (scratch->output_format == FORMAT_HTML) + header_level = atoi(m->value); + } else if (strcmp(m->key, "xhtmlheaderlevel") == 0) { + if (scratch->output_format == FORMAT_HTML) + header_level = atoi(m->value); + } else if (strcmp(m->key, "latexheaderlevel") == 0) { + if (scratch->output_format == FORMAT_LATEX) + header_level = atoi(m->value); + } else if (strcmp(m->key, "odfheaderlevel") == 0) { + if (scratch->output_format == FORMAT_ODF) + header_level = atoi(m->value); + } else if (strcmp(m->key, "quoteslanguage") == 0) { + } else { + // Any other key triggers complete document + if (!(scratch->extensions & EXT_SNIPPET)) scratch->extensions |= EXT_COMPLETE; - } } + } + + if (header_level != -10) + scratch->base_header_level = header_level; } @@ -1178,7 +1191,7 @@ void mmd_export_token_tree(DString * out, mmd_engine * e, short format) { process_header_stack(e); // Create scratch pad - scratch_pad * scratch = scratch_pad_new(e); + scratch_pad * scratch = scratch_pad_new(e, format); // Process metadata process_metadata_stack(e, scratch); diff --git a/src/writer.h b/src/writer.h index 2e62fa6..bc35c2c 100644 --- a/src/writer.h +++ b/src/writer.h @@ -76,6 +76,7 @@ typedef struct { struct meta * meta_hash; unsigned long extensions; + short output_format; short padded; //!< How many empty lines at end output buffer short list_is_tight; short close_para; @@ -95,6 +96,8 @@ typedef struct { short language; short quotes_lang; + short base_header_level; + stack * header_stack; short recurse_depth; @@ -159,7 +162,7 @@ typedef struct meta meta; /// Temporary storage while exporting parse tree to output format -scratch_pad * scratch_pad_new(mmd_engine * e); +scratch_pad * scratch_pad_new(mmd_engine * e, short format); void scratch_pad_free(scratch_pad * scratch); diff --git a/templates/README.md.in b/templates/README.md.in index d6e6a3f..345cd8b 100644 --- a/templates/README.md.in +++ b/templates/README.md.in @@ -421,6 +421,7 @@ features have been implemented: * Basic Citation support * CriticMarkup support * Definition lists +* Figures * Footnotes * Inline and reference footnotes * Image and Link attributes (attributes can now be used with inline links as @@ -453,7 +454,6 @@ Things yet to be completed: * Abbreviations * Glossaries * File Transclusion -* Figures * Table Captions diff --git a/tests/MMD6Tests/Metadata.html b/tests/MMD6Tests/Metadata.html index 1aa81c5..8dc807f 100644 --- a/tests/MMD6Tests/Metadata.html +++ b/tests/MMD6Tests/Metadata.html @@ -11,5 +11,7 @@

foo: bar

+

foo

+ diff --git a/tests/MMD6Tests/Metadata.htmlc b/tests/MMD6Tests/Metadata.htmlc index c4e6218..10041dc 100644 --- a/tests/MMD6Tests/Metadata.htmlc +++ b/tests/MMD6Tests/Metadata.htmlc @@ -6,6 +6,10 @@ HTML header: