From 50d6b4bd6bf28be2f31eb2b176911948163984f8 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 1 Apr 2004 17:08:59 +0000 Subject: [PATCH] Add \cfg / -C directives to allow the user to choose the output file name (or name schema, in HTML). git-svn-id: svn://svn.tartarus.org/sgt/halibut@4017 cda61777-01e9-0310-a592-d414129be87e --- bk_man.c | 16 +++++--- bk_text.c | 15 +++++--- bk_whlp.c | 45 +++++++++++++++++----- bk_xhtml.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++-------------- halibut.h | 1 + ustring.c | 15 ++++++++ 6 files changed, 170 insertions(+), 50 deletions(-) diff --git a/bk_man.c b/bk_man.c index 9a18d20..276855e 100644 --- a/bk_man.c +++ b/bk_man.c @@ -16,6 +16,7 @@ typedef struct { wchar_t *th; int headnumbers; int mindepth; + char *filename; } manconfig; static manconfig man_configure(paragraph *source) { @@ -27,6 +28,7 @@ static manconfig man_configure(paragraph *source) { ret.th = NULL; ret.headnumbers = FALSE; ret.mindepth = 0; + ret.filename = dupstr("output.1"); for (; source; source = source->next) { if (source->type == para_Config) { @@ -37,12 +39,16 @@ static manconfig man_configure(paragraph *source) { ep = wp; while (*ep) ep = uadv(ep); + sfree(ret.th); ret.th = mknewa(wchar_t, ep - wp + 1); memcpy(ret.th, wp, (ep - wp + 1) * sizeof(wchar_t)); } else if (!ustricmp(source->keyword, L"man-headnumbers")) { ret.headnumbers = utob(uadv(source->keyword)); } else if (!ustricmp(source->keyword, L"man-mindepth")) { ret.mindepth = utoi(uadv(source->keyword)); + } else if (!ustricmp(source->keyword, L"man-filename")) { + sfree(ret.filename); + ret.filename = utoa_dup(uadv(source->keyword)); } } } @@ -53,6 +59,7 @@ static manconfig man_configure(paragraph *source) { static void man_conf_cleanup(manconfig cf) { sfree(cf.th); + sfree(cf.filename); } #define QUOTE_INITCTRL 1 /* quote initial . and ' on a line */ @@ -70,14 +77,11 @@ void man_backend(paragraph *sourceform, keywordlist *keywords, conf = man_configure(sourceform); /* - * Determine the output file name, and open the output file - * - * FIXME: want configurable output file names here. For the - * moment, we'll just call it `output.1'. + * Open the output file. */ - fp = fopen("output.1", "w"); + fp = fopen(conf.filename, "w"); if (!fp) { - error(err_cantopenw, "output.1"); + error(err_cantopenw, conf.filename); return; } diff --git a/bk_text.c b/bk_text.c index df8ae62..a0ebf82 100644 --- a/bk_text.c +++ b/bk_text.c @@ -24,6 +24,7 @@ typedef struct { int include_version_id; int indent_preambles; word bullet; + char *filename; } textconfig; static int text_convert(wchar_t *, char **); @@ -76,11 +77,15 @@ static textconfig text_configure(paragraph *source) { ret.include_version_id = TRUE; ret.indent_preambles = FALSE; ret.bullet.text = L"-"; + ret.filename = dupstr("output.txt"); for (; source; source = source->next) { if (source->type == para_Config) { if (!ustricmp(source->keyword, L"text-indent")) { ret.indent = utoi(uadv(source->keyword)); + } else if (!ustricmp(source->keyword, L"text-filename")) { + sfree(ret.filename); + ret.filename = utoa_dup(uadv(source->keyword)); } else if (!ustricmp(source->keyword, L"text-indent-code")) { ret.indent_code = utoi(uadv(source->keyword)); } else if (!ustricmp(source->keyword, L"text-width")) { @@ -192,14 +197,11 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, conf = text_configure(sourceform); /* - * Determine the output file name, and open the output file - * - * FIXME: want configurable output file names here. For the - * moment, we'll just call it `output.txt'. + * Open the output file. */ - fp = fopen("output.txt", "w"); + fp = fopen(conf.filename, "w"); if (!fp) { - error(err_cantopenw, "output.txt"); + error(err_cantopenw, conf.filename); return; } @@ -332,6 +334,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, */ fclose(fp); sfree(conf.asect); + sfree(conf.filename); } /* diff --git a/bk_whlp.c b/bk_whlp.c index 7c51894..6eee4d4 100644 --- a/bk_whlp.c +++ b/bk_whlp.c @@ -1,12 +1,10 @@ /* * Windows Help backend for Halibut - * - * TODO: - * - allow user to specify section contexts. */ #include #include +#include #include #include "halibut.h" @@ -57,12 +55,6 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, indexentry *ie; int done_contents_topic = FALSE; - 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; @@ -92,8 +84,10 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, /* * Loop over the source form finding out whether the user has - * specified particular help topic names for anything. + * specified particular help topic names for anything. Also + * pick out the output file name at this stage. */ + filename = dupstr("output.hlp"); for (p = sourceform; p; p = p->next) { p->private_data = NULL; if (p->type == para_Config && p->parent) { @@ -103,11 +97,39 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, /* Store the topic name in the private_data field of the * containing section. */ p->parent->private_data = topicname; + } else if (!ustricmp(p->keyword, L"winhelp-filename")) { + sfree(filename); + filename = utoa_dup(uadv(p->keyword)); } } } /* + * Ensure the output file name has a .hlp extension. This is + * required since we must create the .cnt file in parallel with + * it. + */ + { + int len = strlen(filename); + if (len < 4 || filename[len-4] != '.' || + tolower(filename[len-3] != 'h') || + tolower(filename[len-2] != 'l') || + tolower(filename[len-1] != 'p')) { + char *newf; + newf = mknewa(char, len + 5); + sprintf(newf, "%s.hlp", filename); + sfree(filename); + filename = newf; + len = strlen(newf); + } + cntname = mknewa(char, len); + sprintf(cntname, "%.*s.cnt", len-4, filename); + } + + state.cntfp = fopen(cntname, "wb"); + state.cnt_last_level = -1; state.cnt_workaround = 0; + + /* * Loop over the source form registering WHLP_TOPICs for * everything. */ @@ -449,6 +471,9 @@ void whlp_backend(paragraph *sourceform, keywordlist *keywords, for (i = 0; (ie = index234(idx->entries, i)) != NULL; i++) { sfree(ie->backend_data); } + + sfree(filename); + sfree(cntname); } static void whlp_contents_write(struct bk_whlp_state *state, diff --git a/bk_xhtml.c b/bk_xhtml.c index cba84ed..7614778 100644 --- a/bk_xhtml.c +++ b/bk_xhtml.c @@ -32,6 +32,28 @@ #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 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). + */ + +#define FILENAME_SINGLE "Manual.html" +#define FILENAME_CONTENTS "Contents.html" +#define FILENAME_INDEX "IndexPage.html" +#define FILENAME_TEMPLATE "%n.html" + 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 +100,8 @@ typedef struct { int suppress_address; xhtmlheadfmt fchapter, *fsect; int nfsect; + char *contents_filename, *index_filename; + char *single_filename, *template_filename; } xhtmlconfig; /*static void xhtml_level(paragraph *, int); @@ -151,12 +175,28 @@ static xhtmlconfig xhtml_configure(paragraph *source) ret.fsect[0].number_suffix = L": "; ret.fsect[1].just_numbers = TRUE; 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); 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 = utoa_dup(uadv(source->keyword)); + } else if (!ustricmp(source->keyword, L"xhtml-single-filename")) { + sfree(ret.single_filename); + ret.single_filename = utoa_dup(uadv(source->keyword)); + } else if (!ustricmp(source->keyword, L"xhtml-index-filename")) { + sfree(ret.index_filename); + ret.index_filename = utoa_dup(uadv(source->keyword)); + } else if (!ustricmp(source->keyword, L"xhtml-template-filename")) { + sfree(ret.template_filename); + ret.template_filename = utoa_dup(uadv(source->keyword)); + } 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)); @@ -299,34 +339,68 @@ 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; + char *c, *t; 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; + wchar_t *ws; + + t = conf.template_filename; + while (*t) { + if (*t == '%' && t[1]) { + int fmt; + + t++; + fmt = *t++; + + if (fmt == '%') { + rdaddc(&fname_c, 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(&fname_c,c); + sfree(c); + break; + } + w = w->next; + } + if (ws) { + xhtml_utostr(ws, &c); + rdaddsc(&fname_c,c); + sfree(c); + } + } else { + rdaddc(&fname_c, *t++); } } - rdaddsc(&fname_c, ".html"); + ret->filename = rdtrimc(&fname_c); } /* printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/ @@ -704,8 +778,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 */ @@ -723,11 +795,11 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file) } else { 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) @@ -741,7 +813,7 @@ 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); @@ -793,10 +865,10 @@ 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"); + 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); diff --git a/halibut.h b/halibut.h index 30a1280..c4f4721 100644 --- a/halibut.h +++ b/halibut.h @@ -248,6 +248,7 @@ char *dupstr(char *s); */ wchar_t *ustrdup(wchar_t *s); char *ustrtoa(wchar_t *s, char *outbuf, int size); +char *utoa_dup(wchar_t *s); int ustrlen(wchar_t *s); wchar_t *uadv(wchar_t *s); wchar_t *ustrcpy(wchar_t *dest, wchar_t *source); diff --git a/ustring.c b/ustring.c index 0741dcf..a64d155 100644 --- a/ustring.c +++ b/ustring.c @@ -33,6 +33,21 @@ char *ustrtoa(wchar_t *s, char *outbuf, int size) { return outbuf; } +char *utoa_dup(wchar_t *s) { + int len; + char *buf = NULL; + + len = ustrlen(s) + 1; + do { + buf = resize(buf, len); + ustrtoa(s, buf, len); + len = (3 * len) / 2 + 1; /* this guarantees a strict increase */ + } while ((int)strlen(buf) >= len-1); + + buf = resize(buf, strlen(buf)+1); + return buf; +} + int ustrlen(wchar_t *s) { int len = 0; while (*s++) len++; -- 2.11.0