From efcda8faf3871b1ea0d40b86c74731ca25ab1903 Mon Sep 17 00:00:00 2001 From: David Parsons Date: Fri, 26 Sep 2008 22:08:30 -0700 Subject: [PATCH] Forgot to check in toc.c --- toc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 toc.c diff --git a/toc.c b/toc.c new file mode 100644 index 0000000..8fe240d --- /dev/null +++ b/toc.c @@ -0,0 +1,62 @@ +/* + * toc -- spit out a table of contents based on header blocks + * + * Copyright (C) 2008 Jjgod Jiang, David L Parsons. + * The redistribution terms are provided in the COPYRIGHT file that must + * be distributed with this source code. + */ +#include "config.h" +#include +#include +#include + +#include "cstring.h" +#include "markdown.h" +#include "amalloc.h" + +/* write an header index + */ +int +mkd_generatetoc(Document *p, FILE *output) +{ + Paragraph *pp; + + int last_hnumber = 0, + first_hnumber = 0; + + if ( !(p && p->ctx) ) return -1; + if ( ! (p->ctx->flags & TOC) ) return 0; + + for ( pp = p->code; pp ; pp = pp->next ) { + if ( pp->typ == HDR && pp->text ) { + + if ( last_hnumber == pp->hnumber ) + fprintf(output, "%*s\n", pp->hnumber, ""); + else while ( last_hnumber > pp->hnumber ) { + fprintf(output, "%*s\n%*s\n", + last_hnumber, "", + last_hnumber-1,""); + --last_hnumber; + } + + while ( pp->hnumber > last_hnumber ) { + fprintf(output, "\n%*s\n", + last_hnumber, "", last_hnumber, ""); + --last_hnumber; + } + + return 0; +} +