X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/25acf71d995f135e72a64316730e625a3f7c1e6c..5b1d0032b0eb6f4a347c0c2cfdbe6e4bb4f959ab:/bk_xhtml.c diff --git a/bk_xhtml.c b/bk_xhtml.c index 4cbe1d6..4d5069a 100644 --- a/bk_xhtml.c +++ b/bk_xhtml.c @@ -32,6 +32,33 @@ #include #include "halibut.h" +/* + * FILENAME_TEMPLATE (overridable in config of course) allows you + * to choose the general form for your HTML file names. It is + * slightly printf-styled (% followed by a single character is a + * formatting directive, %% is a literal %). Formatting directives + * are: + * + * - %n is the section type-plus-number, minus whitespace (`Chapter1.2'). + * - %b is the section number on its own (`1.2'). + * - %k is the section's _internal_ keyword. + * - %N is the section's visible title in the output, again minus + * whitespace. + * + * %n, %b and %k will all default to %N if the section is + * unnumbered (`Bibliography' is often a good example). + * + * FRAGMENT_TEMPLATE is the same, but defines the + * markers used to cross-reference to particular subsections of a + * file. + */ + +#define FILENAME_SINGLE "Manual.html" +#define FILENAME_CONTENTS "Contents.html" +#define FILENAME_INDEX "IndexPage.html" +#define FILENAME_TEMPLATE "%n.html" +#define FRAGMENT_TEMPLATE "%b" + struct xhtmlsection_Struct { struct xhtmlsection_Struct *next; /* next sibling (NULL if split across files) */ struct xhtmlsection_Struct *child; /* NULL if split across files */ @@ -78,6 +105,8 @@ typedef struct { int suppress_address; xhtmlheadfmt fchapter, *fsect; int nfsect; + char *contents_filename, *index_filename; + char *single_filename, *template_filename, *template_fragment; } xhtmlconfig; /*static void xhtml_level(paragraph *, int); @@ -112,7 +141,7 @@ static xhtmlsection *topsection; static paragraph *sourceparas; static xhtmlfile *lastfile; static xhtmlfile *xhtml_last_file = NULL; -static int last_level=-1; +static int last_level=-1, start_level; static xhtmlsection *currentsection; static xhtmlconfig xhtml_configure(paragraph *source) @@ -144,19 +173,39 @@ static xhtmlconfig xhtml_configure(paragraph *source) ret.suppress_address = FALSE; ret.fchapter.just_numbers = FALSE; - ret.fchapter.number_suffix = ustrdup(L": "); + ret.fchapter.number_suffix = L": "; ret.nfsect = 2; ret.fsect = mknewa(xhtmlheadfmt, ret.nfsect); ret.fsect[0].just_numbers = FALSE; - ret.fsect[0].number_suffix = ustrdup(L": "); + ret.fsect[0].number_suffix = L": "; ret.fsect[1].just_numbers = TRUE; - ret.fsect[1].number_suffix = ustrdup(L" "); + ret.fsect[1].number_suffix = L" "; + ret.contents_filename = strdup(FILENAME_CONTENTS); + ret.single_filename = strdup(FILENAME_SINGLE); + ret.index_filename = strdup(FILENAME_INDEX); + ret.template_filename = strdup(FILENAME_TEMPLATE); + ret.template_fragment = strdup(FRAGMENT_TEMPLATE); for (; source; source = source->next) { if (source->type == para_Config) { - if (!ustricmp(source->keyword, L"xhtml-contents-depth-0")) { + if (!ustricmp(source->keyword, L"xhtml-contents-filename")) { + sfree(ret.contents_filename); + ret.contents_filename = dupstr(adv(source->origkeyword)); + } else if (!ustricmp(source->keyword, L"xhtml-single-filename")) { + sfree(ret.single_filename); + ret.single_filename = dupstr(adv(source->origkeyword)); + } else if (!ustricmp(source->keyword, L"xhtml-index-filename")) { + sfree(ret.index_filename); + ret.index_filename = dupstr(adv(source->origkeyword)); + } else if (!ustricmp(source->keyword, L"xhtml-template-filename")) { + sfree(ret.template_filename); + ret.template_filename = dupstr(adv(source->origkeyword)); + } else if (!ustricmp(source->keyword, L"xhtml-template-fragment")) { + sfree(ret.template_fragment); + ret.template_fragment = utoa_dup(uadv(source->keyword), CS_ASCII); + } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-0")) { ret.contents_depth[0] = utoi(uadv(source->keyword)); } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-1")) { ret.contents_depth[1] = utoi(uadv(source->keyword)); @@ -199,7 +248,7 @@ static xhtmlconfig xhtml_configure(paragraph *source) } else if (!ustricmp(source->keyword, L"xhtml-chapter-numeric")) { ret.fchapter.just_numbers = utob(uadv(source->keyword)); } else if (!ustricmp(source->keyword, L"xhtml-chapter-suffix")) { - ret.fchapter.number_suffix = ustrdup(uadv(source->keyword)); + ret.fchapter.number_suffix = uadv(source->keyword); } else if (!ustricmp(source->keyword, L"xhtml-section-numeric")) { wchar_t *p = uadv(source->keyword); int n = 0; @@ -229,7 +278,7 @@ static xhtmlconfig xhtml_configure(paragraph *source) ret.fsect[i] = ret.fsect[ret.nfsect-1]; ret.nfsect = n+1; } - ret.fsect[n].number_suffix = ustrdup(p); + ret.fsect[n].number_suffix = p; } } } @@ -245,6 +294,24 @@ static xhtmlconfig xhtml_configure(paragraph *source) return ret; } +paragraph *xhtml_config_filename(char *filename) +{ + /* + * If the user passes in a single filename as a parameter to + * the `--html' command-line option, then we should assume it + * to imply _two_ config directives: + * \cfg{xhtml-single-filename}{whatever} and + * \cfg{xhtml-leaf-level}{0}; the rationale being that the user + * wants their output _in that file_. + */ + paragraph *p, *q; + + p = cmdline_cfg_simple("xhtml-single-filename", filename, NULL); + q = cmdline_cfg_simple("xhtml-leaf-level", "0", NULL); + p->next = q; + return p; +} + static xhtmlsection *xhtml_new_section(xhtmlsection *last) { xhtmlsection *ret = mknew(xhtmlsection); @@ -287,6 +354,62 @@ static xhtmlsection *xhtml_find_section(paragraph *p) return ret; } +static void xhtml_format(paragraph *p, char *template_string, rdstringc *r) +{ + char *c, *t; + word *w; + wchar_t *ws; + + t = template_string; + while (*t) { + if (*t == '%' && t[1]) { + int fmt; + + t++; + fmt = *t++; + + if (fmt == '%') { + rdaddc(r, fmt); + continue; + } + + w = NULL; + ws = NULL; + + if (p->kwtext && fmt == 'n') + w = p->kwtext; + else if (p->kwtext2 && fmt == 'b') + w = p->kwtext2; + else if (p->keyword && *p->keyword && fmt == 'k') + ws = p->keyword; + else + w = p->words; + + while (w) { + switch (removeattr(w->type)) + { + case word_Normal: + /*case word_Emph: + case word_Code: + case word_WeakCode:*/ + xhtml_utostr(w->text, &c); + rdaddsc(r,c); + sfree(c); + break; + } + w = w->next; + } + if (ws) { + xhtml_utostr(ws, &c); + rdaddsc(r,c); + sfree(c); + } + } else { + rdaddc(r, *t++); + } + } +} + static xhtmlfile *xhtml_new_file(xhtmlsection *sect) { xhtmlfile *ret = mknew(xhtmlfile); @@ -299,34 +422,16 @@ static xhtmlfile *xhtml_new_file(xhtmlsection *sect) ret->is_leaf=(sect!=NULL && sect->level==conf.leaf_level); if (sect==NULL) { if (conf.leaf_level==0) { /* currently unused */ -#define FILENAME_MANUAL "Manual.html" -#define FILENAME_CONTENTS "Contents.html" - ret->filename = smalloc(strlen(FILENAME_MANUAL)+1); - sprintf(ret->filename, FILENAME_MANUAL); + ret->filename = smalloc(strlen(conf.single_filename)+1); + sprintf(ret->filename, conf.single_filename); } else { - ret->filename = smalloc(strlen(FILENAME_CONTENTS)+1); - sprintf(ret->filename, FILENAME_CONTENTS); + ret->filename = smalloc(strlen(conf.contents_filename)+1); + sprintf(ret->filename, conf.contents_filename); } } else { paragraph *p = sect->para; rdstringc fname_c = { 0, 0, NULL }; - char *c; - word *w; - for (w=(p->kwtext)?(p->kwtext):(p->words); w; w=w->next) - { - switch (removeattr(w->type)) - { - case word_Normal: - /*case word_Emph: - case word_Code: - case word_WeakCode:*/ - xhtml_utostr(w->text, &c); - rdaddsc(&fname_c,c); - sfree(c); - break; - } - } - rdaddsc(&fname_c, ".html"); + xhtml_format(p, conf.template_filename, &fname_c); ret->filename = rdtrimc(&fname_c); } /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/ @@ -450,29 +555,14 @@ static void xhtml_ponder_layout(paragraph *p) if (level>0) /* actually a section */ { xhtmlsection *sect; - word *w; - char *c; - rdstringc fname_c = { 0, 0, NULL }; + rdstringc frag_c = { 0, 0, NULL }; sect = xhtml_new_section(lastsection); lastsection = sect; sect->para = p; - for (w=(p->kwtext2)?(p->kwtext2):(p->words); w; w=w->next) /* kwtext2 because we want numbers only! */ - { - switch (removeattr(w->type)) - { - case word_Normal: - /*case word_Emph: - case word_Code: - case word_WeakCode:*/ - xhtml_utostr(w->text, &c); - rdaddsc(&fname_c,c); - sfree(c); - break; - } - } -/* rdaddsc(&fname_c, ".html");*/ - sect->fragment = rdtrimc(&fname_c); + + xhtml_format(p, conf.template_fragment, &frag_c); + sect->fragment = rdtrimc(&frag_c); sect->level = level; /* printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/ @@ -621,13 +711,15 @@ static void xhtml_free_file(xhtmlfile* xfile) * Main function. */ void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords, - indexdata *in_idx) + indexdata *in_idx, void *unused) { /* int i;*/ indexentry *ientry; int ti; xhtmlsection *xsect; + IGNORE(unused); + sourceparas = sourceform; conf = xhtml_configure(sourceform); keywords = in_keywords; @@ -676,13 +768,7 @@ void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords, } ientry->backend_data = NULL; } - { - int i; - sfree(conf.fchapter.number_suffix); - for (i = 0; i < conf.nfsect; i++) - sfree(conf.fsect[i].number_suffix); - sfree(conf.fsect); - } + sfree(conf.fsect); } static int xhtml_para_level(paragraph *p) @@ -710,8 +796,6 @@ static int xhtml_para_level(paragraph *p) } } -static char* xhtml_index_filename = "IndexPage.html"; - /* Output the nav links for the current file. * file == NULL means we're doing the index */ @@ -727,13 +811,13 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file) if (xhtml_last_file==NULL) { fprintf(fp, "Previous | "); } else { - fprintf(fp, "Previous | ", xhtml_last_file->filename); + fprintf(fp, "Previous | ", xhtml_last_file->filename); } - fprintf(fp, "Contents | "); + fprintf(fp, "Contents | ", conf.contents_filename); if (file == NULL) { fprintf(fp, "Index | "); } else { - fprintf(fp, "Index | ", xhtml_index_filename); + fprintf(fp, "Index | ", conf.index_filename); } if (file != NULL) { /* otherwise we're doing nav links for the index */ if (xhtml_next_file==NULL) @@ -747,10 +831,10 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file) if (file==NULL) { /* index, so no next file */ fprintf(fp, "Next "); } else { - fprintf(fp, "Next", xhtml_index_filename); + fprintf(fp, "Next", conf.index_filename); } } else { - fprintf(fp, "Next", xhtml_next_file->filename); + fprintf(fp, "Next", xhtml_next_file->filename); } fprintf(fp, "

\n"); } @@ -779,7 +863,7 @@ static void xhtml_do_index_body(FILE *fp) for (i=0; insection; i++) { xhtmlsection *sect = xi->sections[i]; if (sect) { - fprintf(fp, "", sect->file->filename, sect->fragment); + fprintf(fp, "", sect->file->filename, sect->fragment); if (sect->para->kwtext) { xhtml_para(fp, sect->para->kwtext, FALSE); } else if (sect->para->words) { @@ -798,11 +882,12 @@ static void xhtml_do_index_body(FILE *fp) } static void xhtml_do_index() { - word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index", { NULL, 0, 0} }; - FILE *fp = fopen(xhtml_index_filename, "w"); + word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index", + { NULL, 0, 0}, NULL }; + FILE *fp = fopen(conf.index_filename, "w"); if (fp==NULL) - fatal(err_cantopenw, xhtml_index_filename); + fatal(err_cantopenw, conf.index_filename); xhtml_doheader(fp, &temp_word); xhtml_donavlinks(fp, NULL); @@ -933,7 +1018,7 @@ static void xhtml_utostr(wchar_t *in, char **out) */ static int xhtml_do_contents(FILE *fp, xhtmlfile *file) { - int level, limit, start_level, count = 0; + int level, limit, count = 0; if (!file) return 0; @@ -947,7 +1032,7 @@ static int xhtml_do_contents(FILE *fp, xhtmlfile *file) if (fp!=NULL) { while (last_level > start_level) { last_level--; - fprintf(fp, "\n"); + fprintf(fp, "\n"); } } return count; @@ -969,7 +1054,7 @@ static int xhtml_do_naked_contents(FILE *fp, xhtmlfile *file) if (fp!=NULL) { while (last_level > start_level) { last_level--; - fprintf(fp, "\n"); + fprintf(fp, "\n"); } } return count; @@ -1037,13 +1122,18 @@ static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit) return FALSE; if (fp==NULL || section->level < 0) return TRUE; - while (last_level > section->level) { - last_level--; - fprintf(fp, "\n"); - } - while (last_level < section->level) { + if (last_level > section->level) { + while (last_level > section->level) { + last_level--; + fprintf(fp, "\n"); + } + fprintf(fp, "\n"); + } else if (last_level < section->level) { + assert(last_level == section->level - 1); last_level++; fprintf(fp, "