From d098f79c9b7dd52c534fed83bb1eb6a3080a03af Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 6 Dec 2001 08:37:54 +0000 Subject: Create a .CNT contents file alongside the .HLP. (The .CNT file is deliberately incorrect, to compensate for a bug in WinHelp. With any luck MS will never fix the bug and this .CNT will continue to work forever. Since WinHelp is theoretically obsolete, this sounds reasonably likely.) [originally from svn r1456] --- bk_whlp.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/bk_whlp.c b/bk_whlp.c index 5341919..cf4757d 100644 --- a/bk_whlp.c +++ b/bk_whlp.c @@ -17,6 +17,8 @@ struct bk_whlp_state { indexdata *idx; keywordlist *keywords; WHLP_TOPIC curr_topic; + FILE *cntfp; + int cnt_last_level, cnt_workaround; }; /* @@ -37,11 +39,13 @@ static int whlp_convert(wchar_t *s, char **result, int hard_spaces); static void whlp_mkparagraph(struct bk_whlp_state *state, int font, word *text, int subsidiary); static void whlp_navmenu(struct bk_whlp_state *state, paragraph *p); +static void whlp_contents_write(struct bk_whlp_state *state, + int level, char *text, WHLP_TOPIC topic); void whlp_backend(paragraph *sourceform, keywordlist *keywords, indexdata *idx) { WHLP h; - char *filename; + char *filename, *cntname; paragraph *p, *lastsect; struct bk_whlp_state state; WHLP_TOPIC contents_topic; @@ -49,7 +53,11 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, indexentry *ie; filename = "output.hlp"; /* FIXME: configurability */ - + cntname = "output.cnt"; /* corresponding contents file */ + + state.cntfp = fopen(cntname, "wb"); + state.cnt_last_level = -1; state.cnt_workaround = 0; + h = state.h = whlp_new(); state.keywords = keywords; state.idx = idx; @@ -125,8 +133,11 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, } if (rs.text) { whlp_title(h, rs.text); + fprintf(state.cntfp, ":Title %s\r\n", rs.text); sfree(rs.text); } + whlp_contents_write(&state, 1, "Title page", contents_topic); + /* FIXME: configurability in that string */ } /* @@ -239,6 +250,27 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, rs.text ? rs.text : "", macro, NULL); sfree(macro); + + { + /* + * Output the .cnt entry. + */ + int i; + paragraph *q; + i = 1; + for (q = p; q->parent; q = q->parent) i++; + if (p->child) { + /* + * Need two entries: one directory, and then + * one actual link. The link is on the next + * level down. + */ + whlp_contents_write(&state, i, rs.text, NULL); + i++; + } + whlp_contents_write(&state, i, rs.text, new_topic); + } + sfree(rs.text); whlp_begin_para(h, WHLP_PARA_NONSCROLL); @@ -326,9 +358,9 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, break; } + fclose(state.cntfp); whlp_close(h, filename); - /* * Loop over the index entries, cleaning up our final text * forms. @@ -338,6 +370,31 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, } } +static void whlp_contents_write(struct bk_whlp_state *state, + int level, char *text, WHLP_TOPIC topic) { + /* + * Horrifying bug in WinHelp. When dropping a section level or + * more without using a folder-type entry, WinHelp accidentally + * adds one to the section level. So we correct for that here. + */ + if (state->cnt_last_level > level && topic) + state->cnt_workaround = -1; + else if (!topic) + state->cnt_workaround = 0; + state->cnt_last_level = level; + + fprintf(state->cntfp, "%d ", level + state->cnt_workaround); + while (*text) { + if (*text == '=') + fputc('\\', state->cntfp); + fputc(*text, state->cntfp); + text++; + } + if (topic) + fprintf(state->cntfp, "=%s", whlp_topic_id(topic)); + fputc('\n', state->cntfp); +} + static void whlp_navmenu(struct bk_whlp_state *state, paragraph *p) { whlp_begin_para(state->h, WHLP_PARA_NONSCROLL); whlp_start_hyperlink(state->h, (WHLP_TOPIC)p->private_data); -- cgit v1.1