From fe30868a7e26a39d9c948560c5ca912dd5bec382 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Thu, 16 Feb 2017 12:25:23 -0500 Subject: [PATCH] ADDED: 'finalize' beamer support --- src/beamer.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/beamer.h | 5 ++ src/writer.c | 11 ++++- src/writer.h | 2 + 4 files changed, 152 insertions(+), 3 deletions(-) diff --git a/src/beamer.c b/src/beamer.c index 866d803..85c59b0 100644 --- a/src/beamer.c +++ b/src/beamer.c @@ -63,6 +63,87 @@ #define print_localized(x) mmd_print_localized_char_latex(out, x, scratch) +void mmd_outline_add_beamer(DString * out, token * current, scratch_pad * scratch) { + token * t; + short level; // Header level we are adding + short t_level; + stack * s = scratch->outline_stack; + + if (current) { + switch (current->type) { + case BLOCK_SETEXT_1: + level = 1; + break; + case BLOCK_SETEXT_2: + level = 2; + default: + level = 1 + current->type - BLOCK_H1; + } + + level += scratch->base_header_level - 1; + } else { + level = 0; + } + + if (s->size) { + t = stack_peek(s); + + while (t) { + switch (t->type) { + case BLOCK_SETEXT_1: + t_level = 1; + break; + case BLOCK_SETEXT_2: + t_level = 2; + default: + t_level = 1 + t->type - BLOCK_H1; + } + + t_level += scratch->base_header_level - 1; + + if (t_level >= level) { + // Close out level + switch (t_level) { + case 3: + pad(out, 2, scratch); + print("\\end{frame}\n\n"); + scratch->padded = 2; + break; + case 4: + pad(out, 2, scratch); + print("}\n\n"); + scratch->padded = 2; + break; + } + + stack_pop(s); + t = stack_peek(s); + } else { + // Nothing to close + t = NULL; + } + } + } + + + // Add current level to stack and open + switch (level) { + case 3: + pad(out, 2, scratch); + print("\\begin{frame}[fragile]\n"); + scratch->padded = 1; + stack_push(s, current); + break; + case 4: + pad(out, 2, scratch); + print("\\mode
{"); + scratch->padded = 0; + stack_push(s, current); + break; + } +} + + void mmd_export_token_beamer(DString * out, const char * source, token * t, scratch_pad * scratch) { if (t == NULL) return; @@ -117,6 +198,9 @@ void mmd_export_token_beamer(DString * out, const char * source, token * t, scra case BLOCK_SETEXT_1: case BLOCK_SETEXT_2: pad(out, 2, scratch); + + mmd_outline_add_beamer(out, t, scratch); + switch (t->type) { case BLOCK_SETEXT_1: temp_short = 1; @@ -136,7 +220,7 @@ void mmd_export_token_beamer(DString * out, const char * source, token * t, scra print("\\section{"); break; case 3: - print("\\begin{frame}\n\\frametitle{"); + print("\\frametitle{"); break; default: print("\\emph{"); @@ -190,3 +274,54 @@ void mmd_export_token_tree_beamer(DString * out, const char * source, token * t, scratch->recurse_depth--; } + +void mmd_export_citation_list_beamer(DString * out, const char * source, scratch_pad * scratch) { + if (scratch->used_citations->size > 0) { + footnote * note; + token * content; + + pad(out, 2, scratch); + print("\\part{Bibliography}\n\\begin{frame}[allowframebreaks]\n\\frametitle{Bibliography}\n\\def\\newblock{}\n\\begin{thebibliography}{0}"); + scratch->padded = 0; + + for (int i = 0; i < scratch->used_citations->size; ++i) + { + // Export footnote + pad(out, 2, scratch); + + note = stack_peek_index(scratch->used_citations, i); + content = note->content; + + printf("\\bibitem{%s}\n", note->label_text); + scratch->padded = 6; + + scratch->footnote_para_counter = 0; + + content = note->content; + scratch->citation_being_printed = i + 1; + + mmd_export_token_tree_latex(out, source, content, scratch); + } + + pad(out, 1, scratch); + print("\\end{thebibliography}\n\\end{frame}"); + scratch->padded = 0; + scratch->citation_being_printed = 0; + } +} + + +void mmd_end_complete_beamer(DString * out, const char * source, scratch_pad * scratch) { + pad(out, 2, scratch); + + print("\\mode\n"); + meta * m = extract_meta_from_stack(scratch, "latexfooter"); + + if (m) { + printf("\\input{%s}\n\n", m->value); + } + + print("\\end{document}"); + print("\\mode*\n"); + scratch->padded = 0; +} diff --git a/src/beamer.h b/src/beamer.h index f23f089..eca3beb 100644 --- a/src/beamer.h +++ b/src/beamer.h @@ -63,5 +63,10 @@ void mmd_export_token_beamer(DString * out, const char * source, token * t, scratch_pad * scratch); void mmd_export_token_tree_beamer(DString * out, const char * source, token * t, scratch_pad * scratch); +void mmd_outline_add_beamer(DString * out, token * current, scratch_pad * scratch); + +void mmd_export_citation_list_beamer(DString * out, const char * source, scratch_pad * scratch); +void mmd_end_complete_beamer(DString * out, const char * source, scratch_pad * scratch); + #endif diff --git a/src/writer.c b/src/writer.c index bf02130..b96dcd7 100644 --- a/src/writer.c +++ b/src/writer.c @@ -98,6 +98,8 @@ scratch_pad * scratch_pad_new(mmd_engine * e, short format) { p->header_stack = e->header_stack; + p->outline_stack = stack_new(0); + p->recurse_depth = 0; p->base_header_level = 1; @@ -163,6 +165,8 @@ scratch_pad * scratch_pad_new(mmd_engine * e, short format) { void scratch_pad_free(scratch_pad * scratch) { // HASH_CLEAR(hh, scratch->link_hash); + stack_free(scratch->outline_stack); + link * l, * l_tmp; // Free link hash @@ -1325,10 +1329,13 @@ void mmd_export_token_tree(DString * out, mmd_engine * e, short format) { mmd_start_complete_latex(out, e->dstr->str, scratch); mmd_export_token_tree_beamer(out, e->dstr->str, e->root, scratch); - mmd_export_citation_list_latex(out, e->dstr->str, scratch); + + mmd_outline_add_beamer(out, NULL, scratch); + + mmd_export_citation_list_beamer(out, e->dstr->str, scratch); if (scratch->extensions & EXT_COMPLETE) - mmd_end_complete_latex(out, e->dstr->str, scratch); + mmd_end_complete_beamer(out, e->dstr->str, scratch); break; case FORMAT_HTML: diff --git a/src/writer.h b/src/writer.h index 98c29a0..68d9134 100644 --- a/src/writer.h +++ b/src/writer.h @@ -100,6 +100,8 @@ typedef struct { stack * header_stack; + stack * outline_stack; + short recurse_depth; short in_table_header; -- 2.40.0