diff options
Diffstat (limited to 'support/dktools/hbtool.ctr')
-rw-r--r-- | support/dktools/hbtool.ctr | 432 |
1 files changed, 432 insertions, 0 deletions
diff --git a/support/dktools/hbtool.ctr b/support/dktools/hbtool.ctr new file mode 100644 index 0000000000..31075a254e --- /dev/null +++ b/support/dktools/hbtool.ctr @@ -0,0 +1,432 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2013-xxxx +license = bsd + + + +%% header + + +#ifdef __cplusplus +extern "C" { +#endif + +/** Add one file name to the list of files to copy. + @param job Job structure. + @param fn File name. + @return 1 on success, 0 on error. +*/ +int +hbtool_add_file(hb_job_t *job, dkChar const *fn); + +/** Add one file (probably with CGI arguments) to list of files to copy. + The URL is only added if it references a local file name + in the current working directory without any protocol + specification, (src="filename.png"). For all other URLs we skip + adding and return 1. + @param job Job structure. + @param url URL as specified. + @return 1 on success, 0 on error. +*/ +int +hbtool_add_url(hb_job_t *job, dkChar const *url); + +/** Check file name. The file must be specified without a path. + The file name must be encodable as URL + so it must be in range 0x00000000 to 0x000000FF. + For Windows systems the file name must be encodable as ANSI, + so the following codes are excluded from the range above: + 0x80 to 0x8C, 0x8E, 0x91 to 0x9C, 0x9E to 0x9F. + @param job Job structure. + @param fn File name. + @return 1 on success, 0 on error. +*/ +int +hbtool_check_filename(hb_job_t *job, dkChar const *fn); + +/** Check file name encoding. + The file name must be encodable as URL + so it must be in range 0x00000000 to 0x000000FF. + For Windows systems the file name must be encodable as ANSI, + so the following codes are excluded from the range above: + 0x80 to 0x8C, 0x8E, 0x91 to 0x9C, 0x9E to 0x9F. + @param job Job structure. + @param fn File name. + @return 1 on success, 0 on error. +*/ +int +hbtool_check_filename_encoding(hb_job_t *job, dkChar const *fn); + +/** Retrieve start character from text. + @param text Text to obtain character from. + @param enc Text encoding. + @return Start character on success, 0UL on error. +*/ +dk3_c32_t +hbtool_get_start_char(dkChar const *text, int enc); + +/** Enumerate section and subsections. + @param job Job structure. +*/ +void +hbtool_enumerate(hb_job_t *job); + +/** Write hiearchical header number. + @param job Job structure. + @param nptr Node to write header for. + @return 1 if number was written, 0 otherwise. +*/ +int +hbtool_write_header_number(hb_job_t *job, hb_node_t *nptr); + +/** Write all titles, separated by arrows. + For the primary node we write the full header, for parent + nodes the short header. + @param job Job structure. + @param nptr Current node to write title for. + @param isprim Flag: Is primary node. +*/ +int +hbtool_write_all_titles(hb_job_t *job, hb_node_t *nptr, int isprim); + +/** Correct backslashes to slash on Windows systems. + @param str String to correct. +*/ +void +hbtool_correct_backslash_to_slash(dkChar *str); + +/** Write text (file name) UTF-8 encoded to jobs output file. + @param job Job structure. + @param src String to write. + @return 1 on success, 0 on error. +*/ +int +hbtool_write_text_utf(hb_job_t *job, dkChar const *src); + +#ifdef __cplusplus +} +#endif + +%% module + + +#include "dk3all.h" +#include "dk3se.h" +#include "htmlbook.h" + + + +$!trace-include + + + +int +hbtool_add_file(hb_job_t *job, dkChar const *fn) +{ + dkChar bu[DK3_MAX_PATH]; /* File name buffer */ + dkChar *p1; /* Start of file name */ + int back = 0; + $? "+ hbtool_add_file \"%!ds\"", TR_STR(fn) + if(fn) { + if(dk3str_len(fn) < DK3_SIZEOF(bu,dkChar)) { + p1 = dk3str_start(fn, NULL); + if(p1) { + dk3str_cpy(bu, p1); + dk3str_chomp(bu, NULL); + if(dk3sto_it_find_like(job->i_files, bu, 0)) { + back = 1; + } else { + p1 = dk3str_dup_app(bu, job->app); + if(p1) { + if(dk3sto_add(job->s_files, p1)) { + back = 1; + } else { + dk3_delete(p1); + } + } + } + } + } else { + dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, fn); + } + } $? "- hbtool_add_file %d", back + return back; +} + + + +int +hbtool_check_filename_encoding(hb_job_t *job, dkChar const *fn) +{ + int back; + int se; /* Standard encoding to check */ + $? "+ hbtool_check_filename_encoding \"%!ds\"", TR_STR(fn) +#if DK3_ON_WINDOWS + se = (DK3SE_ANSI | DK3SE_URL); +#else + if((job->options) & HB_JOB_OPT_CHM) { + se = (DK3SE_ANSI | DK3SE_URL); + } else { + se = DK3SE_URL; + } +#endif + back = dk3se_check(fn,dk3app_get_encoding(job->app),se,NULL,NULL,job->app); + $? "- hbtool_check_filename_encoding %d", back + return back; +} + + + +int +hbtool_check_filename(hb_job_t *job, dkChar const *fn) +{ + int back; + $? "+ hbtool_check_filename \"%!ds\"", TR_STR(fn) + back = hbtool_check_filename_encoding(job, fn); + if(dk3str_chr(fn, DK3_CHAR_SEP)) { + dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 12); + back = 0; + } $? "- hbtool_check_filename %d", back + return back; +} + + + + +dk3_c32_t +hbtool_get_start_char(dkChar const *text, int enc) +{ + dk3_c32_t back = 0UL; + dk3_c32_t c32 = 0UL; /* Start character */ + size_t sl; /* String length */ + size_t used; /* Number of bytes used */ +#if DK3_CHAR_SIZE <= 1 + char c; /* Result character */ +#endif + $? "+ hbtool_get_start_char" + if(text) { + sl = dk3str_len(text); +#if DK3_CHAR_SIZE > 1 +#if DK3_CHAR_SIZE > 2 + back = *text; +#else + used = 0; + if(dk3enc_utf162uc(&c32, text, sl, &used)) { + back = c32; + } +#endif +#else + if(enc == DK3_ENCODING_UTF8) { + used = 0; + if(dk3enc_utf82uc(&c32, (unsigned char const *)text, sl, &used)) { + back = c32; + } + } else { + c = *text; + back = (dk3_c32_t)c; + back &= ((dk3_c32_t)0x000000FFUL); + } +#endif + } $? "- hbtool_get_start_char 0x%lx %lu", (unsigned long)back, (unsigned long)back + return back; +} + + + +void +hbtool_enumerate(hb_job_t *job) +{ + hb_node_t *nptr; /* Next node */ + hb_node_t *cptr; /* Current node */ + dk3sto_it_reset(job->i_nodes); + while(NULL != (nptr = (hb_node_t *)dk3sto_it_next(job->i_nodes))) { + nptr->headno = 0UL; nptr->nextsubheadno = 0UL; + nptr->tocstate = HB_TOC_STATE_INIT; + } + cptr = job->rootnode; + while(cptr) { + switch(cptr->tocstate) { + case HB_TOC_STATE_INIT: { + dk3sto_it_reset(cptr->i_subnodes); + nptr = (hb_node_t *)dk3sto_it_next(cptr->i_subnodes); + if(nptr) { + cptr->tocstate = HB_TOC_STATE_IN_PROGRESS; + nptr->headno = cptr->nextsubheadno; + cptr->nextsubheadno += 1UL; + cptr = nptr; + } else { + cptr->tocstate = HB_TOC_STATE_FINISHED; + cptr = cptr->parent; + } + } break; + case HB_TOC_STATE_IN_PROGRESS: { + nptr = (hb_node_t *)dk3sto_it_next(cptr->i_subnodes); + if(nptr) { + nptr->headno = cptr->nextsubheadno; + cptr->nextsubheadno += 1UL; + cptr = nptr; + } else { + cptr->tocstate = HB_TOC_STATE_FINISHED; + cptr = cptr->parent; + } + } break; + case HB_TOC_STATE_FINISHED: { + cptr = cptr->parent; + } break; + } + } + dk3sto_it_reset(job->i_nodes); + while(NULL != (nptr = (hb_node_t *)dk3sto_it_next(job->i_nodes))) { + nptr->tocstate = HB_TOC_STATE_INIT; + } +} + + + +int +hbtool_write_header_number(hb_job_t *job, hb_node_t *nptr) +{ + dkChar bu[32]; + int back = 0; + $? "+ hbtool_write_header_number" + if((job) && (nptr)) { + if(nptr->parent) { $? ". have parent" + if(nptr->depth <= job->headlevels) { $? ". within limit" + back = 1; + if(job->impressumnode) { + if((job->impressumnode)->objno == nptr->objno) { + back = 0; $? ". this is impressum" + } + } + if(back) { $? ". ok to write number" + hbtool_write_header_number(job, nptr->parent); + if(1 < nptr->depth) { $? ". write dot" + hbhtml_output_for_text(job, dkT(".")); + } +#if VERSION_BEFORE_20140716 + dk3sf_sprintf3(bu, dkT("%lu"), (1UL + nptr->headno)); + hbhtml_output_for_text(job, bu); $? ". number written" +#else + if (dk3ma_um_to_string(bu, DK3_SIZEOF(bu,dkChar), (dk3_um_t)(1UL + nptr->headno))) { + hbhtml_output_for_text(job, bu); + } +#endif + } else { $? ". do not write number" + } + } else { $? ". not in limit" + } + } else { $? ". no parent" + } + } $? "- hbtool_write_header_number %d", back + return back; +} + + + +int +hbtool_add_url(hb_job_t *job, dkChar const *url) +{ + dkChar bu[DK3_MAX_PATH]; /* Copy of URL */ + dkChar *p1; /* Start of text in url */ + int back = 1; + if(!hbhtml_check_link_for_external(url)) { + if(dk3str_len(url) < DK3_SIZEOF(bu,dkChar)) { + dk3str_cpy(bu, url); + p1 = dk3str_chr(bu, dkT('?')); + if(p1) { + *p1 = dkT('\0'); + } else { + p1 = dk3str_chr(bu, dkT('#')); + if(p1) { + *p1 = dkT('\0'); + } + } + if(NULL == dk3str_chr(bu, dkT('/'))) { + if(0 < dk3str_len(bu)) { + back = hbtool_add_file(job, bu); + } + } + } else { + /* ERROR: File name too long! */ + dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, url); + back = 0; + } + } + return back; +} + + + +void +hbtool_correct_backslash_to_slash(dkChar *str) +{ +#if DK3_ON_WINDOWS + dkChar *ptr; + ptr = str; + while(*ptr) { + if(dkT('\\') == *ptr) { *ptr = dkT('/'); } + ptr++; + } +#endif +} + + + +int +hbtool_write_all_titles(hb_job_t *job, hb_node_t *nptr, int isprim) +{ + int back = 0; + /* hbhtml_output_for_text(job, dkT(" ")); */ + if ((job) && (nptr)) { + if (nptr->parent) { + back = 1; + if (job->impressumnode) { + if ((job->impressumnode)->objno == nptr->objno) { + back = 0; + } + } + if (back) { + if (hbtool_write_all_titles(job, nptr->parent, 0)) { + hbhtml_output_for_text(job, dkT(" → ")); + } + if (isprim) { + if (nptr->title) { + hbhtml_output_for_text(job, nptr->title); + } + } else { + if (nptr->shorttitle) { + hbhtml_output_for_text(job, nptr->shorttitle); + } else { + if (nptr->title) { + hbhtml_output_for_text(job, nptr->title); + } + } + } + } + } + } + return back; +} + + + +int +hbtool_write_text_utf(hb_job_t *job, dkChar const *src) +{ + char bu[(1024 < DK3_MAX_PATH) ? DK3_MAX_PATH : 1024]; + int back = 0; + int ie; + if ((job) && (src)) { + ie = dk3app_get_encoding(job->app); + if (dk3str_to_c8u_app(bu, sizeof(bu), src, ie, job->app)) { + if (EOF != fputs(bu, job->of)) { + back = 1; + } + } + } + return back; +} + + |