From 612c72d85df391a87d0eb70d8c80082c77fe4e69 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 2 Aug 2014 14:46:52 +0000 Subject: Permit tables of contents in HTML single-file mode. Patch due to J. Lewis Muir, who points out that the HTML backend's current policy of _always_ disabling the TOC in single-file mode is excessively harsh: in a long or formal enough document, you might still want a TOC to make navigating around within the file easier, even if it's not necessary to use it to get between multiple files. So this change removes the unconditional prohibition against TOCs in single-file documents, but they're still disabled by default, because a single file counts as a leaf file and the existing default settings disable TOCs in leaf files anyway. So if you do want a TOC in a single file, you can reconfigure 'html-leaf-contains-contents' to true. [originally from svn r10212] --- bk_html.c | 108 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 36 deletions(-) diff --git a/bk_html.c b/bk_html.c index 0c624e4..f7ae7cf 100644 --- a/bk_html.c +++ b/bk_html.c @@ -1134,6 +1134,28 @@ void html_backend(paragraph *sourceform, keywordlist *keywords, prevf = f; /* + * Special case: for single-file mode, we output the top + * section title before the TOC. + */ + if (files.head == files.tail && sects.head->type == TOP) { + element_open(&ho, "h1"); + + /* + * Provide anchor(s) for cross-links to target. + */ + { + int i; + for (i=0; i < conf.ntfragments; i++) + if (sects.head->fragments[i]) + html_fragment(&ho, sects.head->fragments[i]); + } + + html_section_title(&ho, sects.head, f, keywords, &conf, TRUE); + + element_close(&ho, "h1"); + } + + /* * Write out a prefix TOC for the file (if a leaf file). * * We start by going through the section list and @@ -1143,12 +1165,9 @@ void html_backend(paragraph *sourceform, keywordlist *keywords, * contains all descendants of any section it * contains), because this will play a part in our * decision on whether or not to _output_ the TOC. - * - * Special case: we absolutely do not do this if we're - * in single-file mode. */ - if (files.head != files.tail) { - int ntoc = 0, tocsize = 0; + { + int ntoc = 0, tocsize = 0, tocstartidx = 0; htmlsect **toc = NULL; int leaf = TRUE; @@ -1185,16 +1204,28 @@ void html_backend(paragraph *sourceform, keywordlist *keywords, } } + /* + * Special case: for single-file mode, we don't output + * the first section TOC entry if it's the top since we + * have already output a section title for it above the + * TOC, and since we don't output the top TOC entry, we + * reduce the level of the remaining TOC entries by one + * so that they are output correctly one level up from + * where they would have been. + */ + tocstartidx = (files.head == files.tail && ntoc > 0 && + toc[0]->type == TOP) ? 1 : 0; if (leaf && conf.leaf_contains_contents && - ntoc >= conf.leaf_smallest_contents) { + ntoc >= conf.leaf_smallest_contents && + tocstartidx < ntoc) { int i; - for (i = 0; i < ntoc; i++) { + for (i = tocstartidx; i < ntoc; i++) { htmlsect *s = toc[i]; int hlevel = (s->type == TOP ? -1 : s->type == INDEX ? 0 : heading_depth(s->title)) - - f->min_heading_depth + 1; + - tocstartidx - f->min_heading_depth + 1; assert(hlevel >= 1); html_contents_entry(&ho, hlevel, s, @@ -1266,38 +1297,43 @@ void html_backend(paragraph *sourceform, keywordlist *keywords, /* * Display the section heading. + * + * Special case: for single-file mode, we don't + * output the top section title since we have + * already output it before the TOC. */ + if (files.head != files.tail || s->type != TOP) { + hlevel = (s->type == TOP ? -1 : + s->type == INDEX ? 0 : + heading_depth(s->title)) + - f->min_heading_depth + 1; + assert(hlevel >= 1); + /* HTML headings only go up to
*/ + if (hlevel > 6) + hlevel = 6; + htag[0] = 'h'; + htag[1] = '0' + hlevel; + htag[2] = '\0'; + element_open(&ho, htag); - hlevel = (s->type == TOP ? -1 : - s->type == INDEX ? 0 : - heading_depth(s->title)) - - f->min_heading_depth + 1; - assert(hlevel >= 1); - /* HTML headings only go up to
*/ - if (hlevel > 6) - hlevel = 6; - htag[0] = 'h'; - htag[1] = '0' + hlevel; - htag[2] = '\0'; - element_open(&ho, htag); - - /* - * Provide anchor(s) for cross-links to target. - * - * (Also we'll have to do this separately in - * other paragraph types - NumberedList and - * BiblioCited.) - */ - { - int i; - for (i=0; i < conf.ntfragments; i++) - if (s->fragments[i]) - html_fragment(&ho, s->fragments[i]); - } + /* + * Provide anchor(s) for cross-links to target. + * + * (Also we'll have to do this separately in + * other paragraph types - NumberedList and + * BiblioCited.) + */ + { + int i; + for (i=0; i < conf.ntfragments; i++) + if (s->fragments[i]) + html_fragment(&ho, s->fragments[i]); + } - html_section_title(&ho, s, f, keywords, &conf, TRUE); + html_section_title(&ho, s, f, keywords, &conf, TRUE); - element_close(&ho, htag); + element_close(&ho, htag); + } /* * Now display the section text. -- cgit v1.1