asb / lua-discount
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (3)
- Wiki (1)
- Graphs
-
Tree:
01331fc
lua-discount / toc.c
| 0ff51469 » | asb | 2009-02-19 | 1 | /* | |
| 2 | * toc -- spit out a table of contents based on header blocks | ||||
| 3 | * | ||||
| 4 | * Copyright (C) 2008 Jjgod Jiang, David L Parsons. | ||||
| 5 | * The redistribution terms are provided in the COPYRIGHT file that must | ||||
| 6 | * be distributed with this source code. | ||||
| 7 | */ | ||||
| 8 | #include "config.h" | ||||
| 9 | #include <stdio.h> | ||||
| 10 | #include <stdlib.h> | ||||
| 11 | #include <ctype.h> | ||||
| 12 | |||||
| 13 | #include "cstring.h" | ||||
| 14 | #include "markdown.h" | ||||
| 15 | #include "amalloc.h" | ||||
| 16 | |||||
| 17 | /* write an header index | ||||
| 18 | */ | ||||
| 19 | int | ||||
| 20 | mkd_generatetoc(Document *p, FILE *output) | ||||
| 21 | { | ||||
| 22 | Paragraph *pp; | ||||
| 23 | |||||
| 24 | int last_hnumber = 0, | ||||
| 25 | first_hnumber = 0; | ||||
| 26 | |||||
| 27 | if ( !(p && p->ctx) ) return -1; | ||||
| 28 | if ( ! (p->ctx->flags & TOC) ) return 0; | ||||
| 29 | |||||
| 30 | for ( pp = p->code; pp ; pp = pp->next ) { | ||||
| 31 | if ( pp->typ == HDR && pp->text ) { | ||||
| 32 | |||||
| 33 | if ( last_hnumber == pp->hnumber ) | ||||
| 34 | fprintf(output, "%*s</li>\n", pp->hnumber, ""); | ||||
| 35 | else while ( last_hnumber > pp->hnumber ) { | ||||
| 36 | fprintf(output, "%*s</li>\n%*s</ul>\n", | ||||
| 37 | last_hnumber, "", | ||||
| 38 | last_hnumber-1,""); | ||||
| 39 | --last_hnumber; | ||||
| 40 | } | ||||
| 41 | |||||
| 42 | while ( pp->hnumber > last_hnumber ) { | ||||
| 43 | fprintf(output, "\n%*s<ul>\n", pp->hnumber, ""); | ||||
| 44 | ++last_hnumber; | ||||
| 45 | } | ||||
| 46 | fprintf(output, "%*s<li><a href=\"#", pp->hnumber, ""); | ||||
| 47 | mkd_string_to_anchor(T(pp->text->text), S(pp->text->text), putc, output); | ||||
| 48 | fprintf(output, "\">"); | ||||
| 49 | mkd_text(T(pp->text->text), S(pp->text->text), output, 0); | ||||
| 50 | fprintf(output, "</a>"); | ||||
| 51 | } | ||||
| 52 | } | ||||
| 53 | |||||
| 54 | while ( last_hnumber > 0 ) { | ||||
| 55 | fprintf(output, "%*s</li>\n%*s</ul>\n", | ||||
| 56 | last_hnumber, "", last_hnumber, ""); | ||||
| 57 | --last_hnumber; | ||||
| 58 | } | ||||
| 59 | |||||
| 60 | return 0; | ||||
| 61 | } | ||||
| 62 | |||||
