-
Notifications
You must be signed in to change notification settings - Fork 0
/
bk_html.c
3275 lines (2924 loc) · 90.1 KB
/
bk_html.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* HTML backend for Halibut
*/
/*
* TODO:
*
* - I'm never entirely convinced that having a fragment link to
* come in at the start of the real text in the file is
* sensible. Perhaps for the topmost section in the file, no
* fragment should be used? (Though it should probably still be
* _there_ even if unused.)
*
* - In HHK index mode: subsidiary hhk entries (as in replacing
* `foo, bar' with `foo\n\tbar') can be done by embedding
* sub-<UL>s in the hhk file. This requires me getting round to
* supporting that idiom in the rest of Halibut, but I thought
* I'd record how it's done here in case I turn out to have
* forgotten when I get there.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "halibut.h"
#include "winchm.h"
#define is_heading_type(type) ( (type) == para_Title || \
(type) == para_Chapter || \
(type) == para_Appendix || \
(type) == para_UnnumberedChapter || \
(type) == para_Heading || \
(type) == para_Subsect)
#define heading_depth(p) ( (p)->type == para_Subsect ? (p)->aux + 1 : \
(p)->type == para_Heading ? 1 : \
(p)->type == para_Title ? -1 : 0 )
typedef struct {
bool number_at_all, just_numbers;
wchar_t *number_suffix;
} sectlevel;
typedef struct {
int nasect;
sectlevel achapter, *asect;
int *contents_depths; /* 0=main, 1=chapter, 2=sect etc */
int ncdepths;
bool address_section, visible_version_id;
bool leaf_contains_contents;
int leaf_smallest_contents;
bool navlinks;
bool headinghashtaglinks;
bool rellinks;
char *contents_filename;
char *index_filename;
char *template_filename;
char *single_filename;
char *chm_filename, *hhp_filename, *hhc_filename, *hhk_filename;
char **template_fragments;
int ntfragments;
char **chm_extrafiles, **chm_extranames;
int nchmextrafiles, chmextrafilesize;
char *head_end, *body_start, *body_end, *addr_start, *addr_end;
char *body_tag, *nav_attr;
wchar_t *author, *description;
wchar_t *index_text, *contents_text, *preamble_text, *title_separator;
wchar_t *nav_prev_text, *nav_next_text, *nav_up_text, *nav_separator;
wchar_t *index_main_sep, *index_multi_sep;
wchar_t *pre_versionid, *post_versionid;
int restrict_charset, output_charset;
enum {
HTML_3_2, HTML_4, ISO_HTML,
XHTML_1_0_TRANSITIONAL, XHTML_1_0_STRICT
} htmlver;
wchar_t *lquote, *rquote;
int leaf_level;
} htmlconfig;
#define contents_depth(conf, level) \
( (conf).ncdepths > (level) ? (conf).contents_depths[level] : (level)+2 )
#define is_xhtml(ver) ((ver) >= XHTML_1_0_TRANSITIONAL)
typedef struct htmlfile htmlfile;
typedef struct htmlsect htmlsect;
struct htmlfile {
htmlfile *next;
char *filename;
int last_fragment_number;
int min_heading_depth;
htmlsect *first, *last; /* first/last highest-level sections */
/*
* The `temp' field is available for use in individual passes
* over the file list. For example, the HHK index generation
* uses it to ensure no index term references the same file
* more than once.
*/
int temp;
/*
* CHM section structure, if we're generating a CHM.
*/
struct chm_section *chmsect;
};
struct htmlsect {
htmlsect *next, *parent;
htmlfile *file;
paragraph *title, *text;
enum { NORMAL, TOP, INDEX } type;
int contents_depth;
char **fragments;
};
typedef struct {
htmlfile *head, *tail;
htmlfile *single, *index;
tree234 *frags;
tree234 *files;
} htmlfilelist;
typedef struct {
htmlsect *head, *tail;
} htmlsectlist;
typedef struct {
htmlfile *file;
char *fragment;
} htmlfragment;
typedef struct {
int nrefs, refsize;
word **refs;
} htmlindex;
typedef struct {
htmlsect *section;
char *fragment;
bool generated, referenced;
} htmlindexref;
typedef struct {
/*
* This level deals with charset conversion, starting and
* ending tags, and writing to the file. It's the lexical
* level.
*/
void *write_ctx;
void (*write)(void *write_ctx, const char *data, int len);
int charset, restrict_charset;
charset_state cstate;
errorstate *es;
int ver;
enum {
HO_NEUTRAL, HO_IN_TAG, HO_IN_EMPTY_TAG, HO_IN_TEXT
} state;
int hackflags; /* used for icky .HH* stuff */
int hacklimit; /* text size limit, again for .HH* */
/*
* Stuff beyond here deals with the higher syntactic level: it
* tracks how many levels of <ul> are currently open when
* producing a contents list, for example.
*/
int contents_level;
} htmloutput;
void ho_write_ignore(void *write_ctx, const char *data, int len)
{
IGNORE(write_ctx);
IGNORE(data);
IGNORE(len);
}
void ho_write_file(void *write_ctx, const char *data, int len)
{
FILE *fp = (FILE *)write_ctx;
if (len == -1)
fclose(fp);
else
fwrite(data, 1, len, fp);
}
void ho_write_stdio(void *write_ctx, const char *data, int len)
{
/* same as write_file, but we don't close the file */
FILE *fp = (FILE *)write_ctx;
if (len > 0)
fwrite(data, 1, len, fp);
}
void ho_setup_file(htmloutput *ho, const char *filename)
{
FILE *fp = fopen(filename, "w");
if (fp) {
ho->write = ho_write_file;
ho->write_ctx = fp;
} else {
err_cantopenw(ho->es, filename);
ho->write = ho_write_ignore; /* saves conditionalising rest of code */
}
}
void ho_setup_stdio(htmloutput *ho, FILE *fp)
{
ho->write = ho_write_stdio;
ho->write_ctx = fp;
}
struct chm_output {
struct chm *chm;
char *filename;
rdstringc rs;
};
void ho_write_chm(void *write_ctx, const char *data, int len)
{
struct chm_output *co = (struct chm_output *)write_ctx;
if (len == -1) {
chm_add_file(co->chm, co->filename, co->rs.text, co->rs.pos);
sfree(co->filename);
sfree(co->rs.text);
sfree(co);
} else {
rdaddsn(&co->rs, data, len);
}
}
void ho_setup_chm(htmloutput *ho, struct chm *chm, const char *filename)
{
struct chm_output *co = snew(struct chm_output);
co->chm = chm;
co->rs = empty_rdstringc;
co->filename = dupstr(filename);
ho->write_ctx = co;
ho->write = ho_write_chm;
}
void ho_write_rdstringc(void *write_ctx, const char *data, int len)
{
rdstringc *rs = (rdstringc *)write_ctx;
if (len > 0)
rdaddsn(rs, data, len);
}
void ho_setup_rdstringc(htmloutput *ho, rdstringc *rs)
{
ho->write_ctx = rs;
ho->write = ho_write_rdstringc;
}
void ho_string(htmloutput *ho, const char *string)
{
ho->write(ho->write_ctx, string, strlen(string));
}
void ho_finish(htmloutput *ho)
{
ho->write(ho->write_ctx, NULL, -1);
}
/*
* Nasty hacks that modify the behaviour of htmloutput files. All
* of these are flag bits set in ho.hackflags. HO_HACK_QUOTEQUOTES
* has the same effect as the `quote_quotes' parameter to
* html_text_limit_internal, except that it's set globally on an
* entire htmloutput structure; HO_HACK_QUOTENOTHING suppresses
* quoting of any HTML special characters (for .HHP files);
* HO_HACK_OMITQUOTES completely suppresses the generation of
* double quotes at all (turning them into single quotes, for want
* of a better idea).
*/
#define HO_HACK_QUOTEQUOTES 1
#define HO_HACK_QUOTENOTHING 2
#define HO_HACK_OMITQUOTES 4
static int html_fragment_compare(const void *av, const void *bv, void *cmpctx)
{
const htmlfragment *a = (const htmlfragment *)av;
const htmlfragment *b = (const htmlfragment *)bv;
int cmp;
if ((cmp = strcmp(a->file->filename, b->file->filename)) != 0)
return cmp;
else
return strcmp(a->fragment, b->fragment);
}
static int html_filename_compare(const void *av, const void *bv, void *cmpctx)
{
const char *a = (const char *)av;
const char *b = (const char *)bv;
return strcmp(a, b);
}
static void html_file_section(htmlconfig *cfg, htmlfilelist *files,
htmlsect *sect, int depth);
static htmlfile *html_new_file(htmlfilelist *list, char *filename);
static htmlsect *html_new_sect(htmlsectlist *list, paragraph *title,
htmlconfig *cfg);
/* Flags for html_words() flags parameter */
#define NOTHING 0x00
#define MARKUP 0x01
#define LINKS 0x02
#define INDEXENTS 0x04
#define ALL 0x07
static void html_words(htmloutput *ho, word *words, int flags,
htmlfile *file, keywordlist *keywords, htmlconfig *cfg);
static void html_codepara(htmloutput *ho, word *words);
static void element_open(htmloutput *ho, char const *name);
static void element_close(htmloutput *ho, char const *name);
static void element_empty(htmloutput *ho, char const *name);
static void element_attr(htmloutput *ho, char const *name, char const *value);
static void element_attr_w(htmloutput *ho, char const *name,
wchar_t const *value);
static void html_text(htmloutput *ho, wchar_t const *str);
static void html_text_nbsp(htmloutput *ho, wchar_t const *str);
static void html_text_limit(htmloutput *ho, wchar_t const *str, int maxlen);
static void html_text_limit_internal(htmloutput *ho, wchar_t const *text,
int maxlen, bool quote_quotes, bool nbsp);
static void html_nl(htmloutput *ho);
static void html_raw(htmloutput *ho, char *text);
static void html_raw_as_attr(htmloutput *ho, char *text);
static void cleanup(htmloutput *ho);
static void html_href(htmloutput *ho, htmlfile *thisfile,
htmlfile *targetfile, char *targetfrag);
static void html_fragment(htmloutput *ho, char const *fragment);
static char *html_format(paragraph *p, char *template_string);
static char *html_sanitise_fragment(htmlfilelist *files, htmlfile *file,
char *text);
static char *html_sanitise_filename(htmlfilelist *files, char *text);
static void html_contents_entry(htmloutput *ho, int depth, htmlsect *s,
htmlfile *thisfile, keywordlist *keywords,
htmlconfig *cfg);
static void html_section_title(htmloutput *ho, htmlsect *s,
htmlfile *thisfile, keywordlist *keywords,
htmlconfig *cfg, bool real);
static htmlconfig html_configure(paragraph *source, bool chm_mode,
errorstate *es)
{
htmlconfig ret;
paragraph *p;
/*
* Defaults.
*/
ret.leaf_level = chm_mode ? -1 /* infinite */ : 2;
ret.achapter.just_numbers = false;
ret.achapter.number_at_all = true;
ret.achapter.number_suffix = L": ";
ret.nasect = 1;
ret.asect = snewn(ret.nasect, sectlevel);
ret.asect[0].just_numbers = true;
ret.asect[0].number_at_all = true;
ret.asect[0].number_suffix = L" ";
ret.ncdepths = 0;
ret.contents_depths = 0;
ret.visible_version_id = true;
ret.address_section = chm_mode ? false : true;
ret.leaf_contains_contents = false;
ret.leaf_smallest_contents = 4;
ret.navlinks = chm_mode ? false : true;
ret.headinghashtaglinks = false;
ret.rellinks = true;
ret.single_filename = dupstr("Manual.html");
ret.contents_filename = dupstr("Contents.html");
ret.index_filename = dupstr("IndexPage.html");
ret.template_filename = dupstr("%n.html");
if (chm_mode) {
ret.chm_filename = dupstr("output.chm");
ret.hhc_filename = dupstr("contents.hhc");
ret.hhk_filename = dupstr("index.hhk");
ret.hhp_filename = NULL;
} else {
ret.chm_filename = ret.hhp_filename = NULL;
ret.hhc_filename = ret.hhk_filename = NULL;
}
ret.ntfragments = 1;
ret.template_fragments = snewn(ret.ntfragments, char *);
ret.template_fragments[0] = dupstr("%b");
ret.chm_extrafiles = ret.chm_extranames = NULL;
ret.nchmextrafiles = ret.chmextrafilesize = 0;
ret.head_end = ret.body_tag = ret.body_start = ret.body_end =
ret.addr_start = ret.addr_end = ret.nav_attr = NULL;
ret.author = ret.description = NULL;
ret.restrict_charset = CS_UTF8;
ret.output_charset = CS_ASCII;
ret.htmlver = HTML_4;
ret.index_text = L"Index";
ret.contents_text = L"Contents";
ret.preamble_text = L"Preamble";
ret.title_separator = L" - ";
ret.nav_prev_text = L"Previous";
ret.nav_next_text = L"Next";
ret.nav_up_text = L"Up";
ret.nav_separator = L" | ";
ret.index_main_sep = L": ";
ret.index_multi_sep = L", ";
ret.pre_versionid = L"[";
ret.post_versionid = L"]";
/*
* Default quote characters are Unicode matched single quotes,
* falling back to ordinary ASCII ".
*/
ret.lquote = L"\x2018\0\x2019\0\"\0\"\0\0";
ret.rquote = uadv(ret.lquote);
/*
* Two-pass configuration so that we can pick up global config
* (e.g. `quotes') before having it overridden by specific
* config (`html-quotes'), irrespective of the order in which
* they occur.
*/
for (p = source; p; p = p->next) {
if (p->type == para_Config) {
if (!ustricmp(p->keyword, L"quotes")) {
if (*uadv(p->keyword) && *uadv(uadv(p->keyword))) {
ret.lquote = uadv(p->keyword);
ret.rquote = uadv(ret.lquote);
}
} else if (!ustricmp(p->keyword, L"index")) {
ret.index_text = uadv(p->keyword);
} else if (!ustricmp(p->keyword, L"contents")) {
ret.contents_text = uadv(p->keyword);
}
}
}
for (p = source; p; p = p->next) {
if (p->type == para_Config) {
wchar_t *k = p->keyword;
bool generic = false;
if (!chm_mode && !ustrnicmp(k, L"html-", 5)) {
k += 5;
} else if (!chm_mode && !ustrnicmp(k, L"xhtml-", 6)) {
k += 6;
} else if (chm_mode && !ustrnicmp(k, L"chm-", 4)) {
k += 4;
} else if (!ustrnicmp(k, L"htmlall-", 8)) {
k += 8;
/* In this mode, only accept directives that don't
* vary completely between the HTML and CHM output
* types. */
generic = true;
} else {
continue;
}
if (!ustricmp(k, L"restrict-charset")) {
ret.restrict_charset = charset_from_ustr(
&p->fpos, uadv(k), es);
} else if (!ustricmp(k, L"output-charset")) {
ret.output_charset = charset_from_ustr(
&p->fpos, uadv(k), es);
} else if (!ustricmp(k, L"version")) {
wchar_t *vername = uadv(k);
static const struct {
const wchar_t *name;
int ver;
} versions[] = {
{L"html3.2", HTML_3_2},
{L"html4", HTML_4},
{L"iso-html", ISO_HTML},
{L"xhtml1.0transitional", XHTML_1_0_TRANSITIONAL},
{L"xhtml1.0strict", XHTML_1_0_STRICT}
};
int i;
for (i = 0; i < (int)lenof(versions); i++)
if (!ustricmp(versions[i].name, vername))
break;
if (i == lenof(versions))
err_htmlver(es, &p->fpos, vername);
else
ret.htmlver = versions[i].ver;
} else if (!ustricmp(k, L"single-filename")) {
sfree(ret.single_filename);
ret.single_filename = dupstr(adv(p->origkeyword));
} else if (!ustricmp(k, L"contents-filename")) {
sfree(ret.contents_filename);
ret.contents_filename = dupstr(adv(p->origkeyword));
} else if (!ustricmp(k, L"index-filename")) {
sfree(ret.index_filename);
ret.index_filename = dupstr(adv(p->origkeyword));
} else if (!ustricmp(k, L"template-filename")) {
sfree(ret.template_filename);
ret.template_filename = dupstr(adv(p->origkeyword));
} else if (!ustricmp(k, L"template-fragment")) {
char *frag = adv(p->origkeyword);
if (*frag) {
while (ret.ntfragments--)
sfree(ret.template_fragments[ret.ntfragments]);
sfree(ret.template_fragments);
ret.template_fragments = NULL;
ret.ntfragments = 0;
while (*frag) {
ret.ntfragments++;
ret.template_fragments =
sresize(ret.template_fragments,
ret.ntfragments, char *);
ret.template_fragments[ret.ntfragments-1] =
dupstr(frag);
frag = adv(frag);
}
} else
err_cfginsufarg(es, &p->fpos, p->origkeyword, 1);
} else if (!ustricmp(k, L"chapter-numeric")) {
ret.achapter.just_numbers = utob(uadv(k));
} else if (!ustricmp(k, L"chapter-shownumber")) {
ret.achapter.number_at_all = utob(uadv(k));
} else if (!ustricmp(k, L"suppress-navlinks")) {
ret.navlinks = !utob(uadv(k));
} else if (!ustricmp(k, L"heading-hashtag-links")) {
ret.headinghashtaglinks = utob(uadv(k));
} else if (!ustricmp(k, L"rellinks")) {
ret.rellinks = utob(uadv(k));
} else if (!ustricmp(k, L"chapter-suffix")) {
ret.achapter.number_suffix = uadv(k);
} else if (!ustricmp(k, L"leaf-level")) {
wchar_t *u = uadv(k);
if (!ustricmp(u, L"infinite") ||
!ustricmp(u, L"infinity") ||
!ustricmp(u, L"inf"))
ret.leaf_level = -1; /* represents infinity */
else
ret.leaf_level = utoi(u);
} else if (!ustricmp(k, L"section-numeric")) {
wchar_t *q = uadv(k);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.nasect) {
int i;
ret.asect = sresize(ret.asect, n+1, sectlevel);
for (i = ret.nasect; i <= n; i++)
ret.asect[i] = ret.asect[ret.nasect-1];
ret.nasect = n+1;
}
ret.asect[n].just_numbers = utob(q);
} else if (!ustricmp(k, L"section-shownumber")) {
wchar_t *q = uadv(k);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.nasect) {
int i;
ret.asect = sresize(ret.asect, n+1, sectlevel);
for (i = ret.nasect; i <= n; i++)
ret.asect[i] = ret.asect[ret.nasect-1];
ret.nasect = n+1;
}
ret.asect[n].number_at_all = utob(q);
} else if (!ustricmp(k, L"section-suffix")) {
wchar_t *q = uadv(k);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.nasect) {
int i;
ret.asect = sresize(ret.asect, n+1, sectlevel);
for (i = ret.nasect; i <= n; i++) {
ret.asect[i] = ret.asect[ret.nasect-1];
}
ret.nasect = n+1;
}
ret.asect[n].number_suffix = q;
} else if (!ustricmp(k, L"contents-depth") ||
!ustrnicmp(k, L"contents-depth-", 15)) {
/*
* Relic of old implementation: this directive used
* to be written as \cfg{html-contents-depth-3}{2}
* rather than the usual Halibut convention of
* \cfg{html-contents-depth}{3}{2}. We therefore
* support both.
*/
wchar_t *q = k[14] ? k+15 : uadv(k);
int n = 0;
if (uisdigit(*q)) {
n = utoi(q);
q = uadv(q);
}
if (n >= ret.ncdepths) {
int i;
ret.contents_depths =
sresize(ret.contents_depths, n+1, int);
for (i = ret.ncdepths; i <= n; i++) {
ret.contents_depths[i] = i+2;
}
ret.ncdepths = n+1;
}
ret.contents_depths[n] = utoi(q);
} else if (!ustricmp(k, L"head-end")) {
ret.head_end = adv(p->origkeyword);
} else if (!ustricmp(k, L"body-tag")) {
ret.body_tag = adv(p->origkeyword);
} else if (!ustricmp(k, L"body-start")) {
ret.body_start = adv(p->origkeyword);
} else if (!ustricmp(k, L"body-end")) {
ret.body_end = adv(p->origkeyword);
} else if (!ustricmp(k, L"address-start")) {
ret.addr_start = adv(p->origkeyword);
} else if (!ustricmp(k, L"address-end")) {
ret.addr_end = adv(p->origkeyword);
} else if (!ustricmp(k, L"navigation-attributes")) {
ret.nav_attr = adv(p->origkeyword);
} else if (!ustricmp(k, L"author")) {
ret.author = uadv(k);
} else if (!ustricmp(k, L"description")) {
ret.description = uadv(k);
} else if (!ustricmp(k, L"suppress-address")) {
ret.address_section = !utob(uadv(k));
} else if (!ustricmp(k, L"versionid")) {
ret.visible_version_id = utob(uadv(k));
} else if (!ustricmp(k, L"quotes")) {
if (*uadv(k) && *uadv(uadv(k))) {
ret.lquote = uadv(k);
ret.rquote = uadv(ret.lquote);
}
} else if (!ustricmp(k, L"leaf-contains-contents")) {
ret.leaf_contains_contents = utob(uadv(k));
} else if (!ustricmp(k, L"leaf-smallest-contents")) {
ret.leaf_smallest_contents = utoi(uadv(k));
} else if (!ustricmp(k, L"index-text")) {
ret.index_text = uadv(k);
} else if (!ustricmp(k, L"contents-text")) {
ret.contents_text = uadv(k);
} else if (!ustricmp(k, L"preamble-text")) {
ret.preamble_text = uadv(k);
} else if (!ustricmp(k, L"title-separator")) {
ret.title_separator = uadv(k);
} else if (!ustricmp(k, L"nav-prev-text")) {
ret.nav_prev_text = uadv(k);
} else if (!ustricmp(k, L"nav-next-text")) {
ret.nav_next_text = uadv(k);
} else if (!ustricmp(k, L"nav-up-text")) {
ret.nav_up_text = uadv(k);
} else if (!ustricmp(k, L"nav-separator")) {
ret.nav_separator = uadv(k);
} else if (!ustricmp(k, L"index-main-separator")) {
ret.index_main_sep = uadv(k);
} else if (!ustricmp(k, L"index-multiple-separator")) {
ret.index_multi_sep = uadv(k);
} else if (!ustricmp(k, L"pre-versionid")) {
ret.pre_versionid = uadv(k);
} else if (!ustricmp(k, L"post-versionid")) {
ret.post_versionid = uadv(k);
} else if (!generic && !ustricmp(
k, chm_mode ? L"filename" : L"mshtmlhelp-chm")) {
sfree(ret.chm_filename);
ret.chm_filename = dupstr(adv(p->origkeyword));
} else if (!generic && !ustricmp(
k, chm_mode ? L"contents-name" :
L"mshtmlhelp-contents")) {
sfree(ret.hhc_filename);
ret.hhc_filename = dupstr(adv(p->origkeyword));
} else if (!generic && !ustricmp(
k, chm_mode ? L"index-name" :
L"mshtmlhelp-index")) {
sfree(ret.hhk_filename);
ret.hhk_filename = dupstr(adv(p->origkeyword));
} else if (!generic && !chm_mode &&
!ustricmp(k, L"mshtmlhelp-project")) {
sfree(ret.hhp_filename);
ret.hhp_filename = dupstr(adv(p->origkeyword));
} else if (!generic && chm_mode &&
!ustricmp(k, L"extra-file")) {
char *diskname, *chmname;
diskname = adv(p->origkeyword);
if (*diskname) {
chmname = adv(diskname);
if (!*chmname)
chmname = diskname;
if (chmname[0] == '#' || chmname[0] == '$')
err_chm_badname(es, &p->fpos, chmname);
if (ret.nchmextrafiles >= ret.chmextrafilesize) {
ret.chmextrafilesize = ret.nchmextrafiles * 5 / 4 + 32;
ret.chm_extrafiles = sresize(
ret.chm_extrafiles, ret.chmextrafilesize, char *);
ret.chm_extranames = sresize(
ret.chm_extranames, ret.chmextrafilesize, char *);
}
ret.chm_extrafiles[ret.nchmextrafiles] = dupstr(diskname);
ret.chm_extranames[ret.nchmextrafiles] =
dupstr(chmname);
ret.nchmextrafiles++;
}
}
}
}
if (!chm_mode) {
/*
* If we're in HTML mode but using the old-style options to
* output HTML Help Workshop auxiliary files, do some
* consistency checking.
*/
/*
* Enforce that the CHM and HHP filenames must either be both
* present or both absent. If one is present but not the other,
* turn both off.
*/
if (!ret.chm_filename ^ !ret.hhp_filename) {
err_chmnames(es);
sfree(ret.chm_filename); ret.chm_filename = NULL;
sfree(ret.hhp_filename); ret.hhp_filename = NULL;
}
/*
* And if we're not generating an HHP, there's no need for HHC
* or HHK.
*/
if (!ret.hhp_filename) {
sfree(ret.hhc_filename); ret.hhc_filename = NULL;
sfree(ret.hhk_filename); ret.hhk_filename = NULL;
}
}
/*
* Now process fallbacks on quote characters.
*/
while (*uadv(ret.rquote) && *uadv(uadv(ret.rquote)) &&
(!cvt_ok(ret.restrict_charset, ret.lquote) ||
!cvt_ok(ret.restrict_charset, ret.rquote))) {
ret.lquote = uadv(ret.rquote);
ret.rquote = uadv(ret.lquote);
}
return ret;
}
paragraph *html_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{html-single-filename}{whatever} and
* \cfg{html-leaf-level}{0}; the rationale being that the user
* wants their output _in that file_.
*/
paragraph *p, *q;
p = cmdline_cfg_simple("html-single-filename", filename, NULL);
q = cmdline_cfg_simple("html-leaf-level", "0", NULL);
p->next = q;
return p;
}
paragraph *chm_config_filename(char *filename)
{
return cmdline_cfg_simple("chm-filename", filename, NULL);
}
static void html_backend_common(paragraph *sourceform, keywordlist *keywords,
indexdata *idx, errorstate *es, bool chm_mode)
{
paragraph *p;
htmlsect *topsect;
htmlconfig conf;
htmlfilelist files = { NULL, NULL, NULL, NULL, NULL, NULL };
htmlsectlist sects = { NULL, NULL }, nonsects = { NULL, NULL };
struct chm *chm = NULL;
bool has_index, hhk_needed = false;
conf = html_configure(sourceform, chm_mode, es);
/*
* We're going to make heavy use of paragraphs' private data
* fields in the forthcoming code. Clear them first, so we can
* reliably tell whether we have auxiliary data for a
* particular paragraph.
*/
for (p = sourceform; p; p = p->next)
p->private_data = NULL;
files.frags = newtree234(html_fragment_compare, NULL);
files.files = newtree234(html_filename_compare, NULL);
/*
* Start by figuring out into which file each piece of the
* document should be put. We'll do this by inventing an
* `htmlsect' structure and stashing it in the private_data
* field of each section paragraph; we also need one additional
* htmlsect for the document index, which won't show up in the
* source form but needs to be consistently mentioned in
* contents links.
*
* While we're here, we'll also invent the HTML fragment name(s)
* for each section.
*/
{
htmlsect *sect;
int d;
topsect = html_new_sect(§s, NULL, &conf);
topsect->type = TOP;
topsect->title = NULL;
topsect->text = sourceform;
topsect->contents_depth = contents_depth(conf, 0);
html_file_section(&conf, &files, topsect, -1);
for (p = sourceform; p; p = p->next)
if (is_heading_type(p->type)) {
d = heading_depth(p);
if (p->type == para_Title) {
topsect->title = p;
continue;
}
sect = html_new_sect(§s, p, &conf);
sect->text = p->next;
sect->contents_depth = contents_depth(conf, d+1) - (d+1);
if (p->parent) {
sect->parent = (htmlsect *)p->parent->private_data;
assert(sect->parent != NULL);
} else
sect->parent = topsect;
p->private_data = sect;
html_file_section(&conf, &files, sect, d);
{
int i;
for (i=0; i < conf.ntfragments; i++) {
sect->fragments[i] =
html_format(p, conf.template_fragments[i]);
sect->fragments[i] =
html_sanitise_fragment(&files, sect->file,
sect->fragments[i]);
}
}
}
/*
* And the index, if we have one. Note that we don't output
* an index as an HTML file if we're outputting one as a
* .HHK (in either of the HTML or CHM output modes).
*/
has_index = (count234(idx->entries) > 0);
if (has_index && !chm_mode && !conf.hhk_filename) {
sect = html_new_sect(§s, NULL, &conf);
sect->text = NULL;
sect->type = INDEX;
sect->parent = topsect;
sect->contents_depth = 0;
html_file_section(&conf, &files, sect, 0); /* peer of chapters */
sect->fragments[0] = utoa_dup(conf.index_text, CS_ASCII);
sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
sect->fragments[0]);
files.index = sect->file;
}
}
/*
* Go through the keyword list and sort out fragment IDs for
* all the potentially referenced paragraphs which _aren't_
* headings.
*/
{
int i;
keyword *kw;
htmlsect *sect;
for (i = 0; (kw = index234(keywords->keys, i)) != NULL; i++) {
paragraph *q, *p = kw->para;
if (!is_heading_type(p->type)) {
htmlsect *parent;
/*
* Find the paragraph's parent htmlsect, to
* determine which file it will end up in.
*/
q = p->parent;
if (!q) {
/*
* Preamble paragraphs have no parent. So if we
* have a non-heading with no parent, it must
* be preamble, and therefore its parent
* htmlsect must be the preamble one.
*/
assert(sects.head &&
sects.head->type == TOP);
parent = sects.head;
} else
parent = (htmlsect *)q->private_data;
/*
* Now we can construct an htmlsect for this
* paragraph itself, taking care to put it in the
* list of non-sections rather than the list of
* sections (so that traverses of the `sects' list
* won't attempt to add it to the contents or
* anything weird like that).
*/
sect = html_new_sect(&nonsects, p, &conf);
sect->file = parent->file;
sect->parent = parent;
p->private_data = sect;
/*
* Fragment IDs for these paragraphs will simply be
* `p' followed by an integer.
*/
sect->fragments[0] = snewn(40, char);
sprintf(sect->fragments[0], "p%d",
sect->file->last_fragment_number++);
sect->fragments[0] = html_sanitise_fragment(&files, sect->file,
sect->fragments[0]);
}
}
}
/*
* Reset the fragment numbers in each file. I've just used them
* to generate `p' fragment IDs for non-section paragraphs
* (numbered list elements, bibliocited), and now I want to use
* them for `i' fragment IDs for index entries.
*/
{
htmlfile *file;
for (file = files.head; file; file = file->next)
file->last_fragment_number = 0;
}
/*
* Now sort out the index. This involves:
*
* - For each index term, we set up an htmlindex structure to
* store all the references to that term.
*
* - Then we make a pass over the actual document, finding
* every word_IndexRef; for each one, we actually figure out
* the HTML filename/fragment pair we will use to reference
* it, store that information in the private data field of
* the word_IndexRef itself (so we can recreate it when the
* time comes to output our HTML), and add a reference to it
* to the index term in question.
*/
{
int i;
indexentry *entry;
htmlsect *lastsect;
word *w;
/*
* Set up the htmlindex structures.
*/
for (i = 0; (entry = index234(idx->entries, i)) != NULL; i++) {
htmlindex *hi = snew(htmlindex);
hi->nrefs = hi->refsize = 0;
hi->refs = NULL;
entry->backend_data = hi;
}
/*
* Run over the document inventing fragments. Each fragment
* is of the form `i' followed by an integer.
*/
lastsect = sects.head; /* this is always the top section */
for (p = sourceform; p; p = p->next) {
if (is_heading_type(p->type) && p->type != para_Title)
lastsect = (htmlsect *)p->private_data;
for (w = p->words; w; w = w->next)
if (w->type == word_IndexRef) {
htmlindexref *hr = snew(htmlindexref);
indextag *tag;
int i;
hr->referenced = hr->generated = false;
hr->section = lastsect;
{
char buf[40];
sprintf(buf, "i%d",
lastsect->file->last_fragment_number++);
hr->fragment = dupstr(buf);
hr->fragment =
html_sanitise_fragment(&files, hr->section->file,
hr->fragment);