]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/mdocml/html.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r307894, and update
[FreeBSD/FreeBSD.git] / contrib / mdocml / html.c
1 /*      $Id: html.c,v 1.213 2017/06/08 12:54:58 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008-2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2011-2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "mandoc_aux.h"
32 #include "mandoc.h"
33 #include "roff.h"
34 #include "out.h"
35 #include "html.h"
36 #include "manconf.h"
37 #include "main.h"
38
39 struct  htmldata {
40         const char       *name;
41         int               flags;
42 #define HTML_NOSTACK     (1 << 0)
43 #define HTML_AUTOCLOSE   (1 << 1)
44 #define HTML_NLBEFORE    (1 << 2)
45 #define HTML_NLBEGIN     (1 << 3)
46 #define HTML_NLEND       (1 << 4)
47 #define HTML_NLAFTER     (1 << 5)
48 #define HTML_NLAROUND    (HTML_NLBEFORE | HTML_NLAFTER)
49 #define HTML_NLINSIDE    (HTML_NLBEGIN | HTML_NLEND)
50 #define HTML_NLALL       (HTML_NLAROUND | HTML_NLINSIDE)
51 #define HTML_INDENT      (1 << 6)
52 #define HTML_NOINDENT    (1 << 7)
53 };
54
55 static  const struct htmldata htmltags[TAG_MAX] = {
56         {"html",        HTML_NLALL},
57         {"head",        HTML_NLALL | HTML_INDENT},
58         {"body",        HTML_NLALL},
59         {"meta",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
60         {"title",       HTML_NLAROUND},
61         {"div",         HTML_NLAROUND},
62         {"h1",          HTML_NLAROUND},
63         {"h2",          HTML_NLAROUND},
64         {"span",        0},
65         {"link",        HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
66         {"br",          HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
67         {"a",           0},
68         {"table",       HTML_NLALL | HTML_INDENT},
69         {"colgroup",    HTML_NLALL | HTML_INDENT},
70         {"col",         HTML_NOSTACK | HTML_AUTOCLOSE | HTML_NLALL},
71         {"tr",          HTML_NLALL | HTML_INDENT},
72         {"td",          HTML_NLAROUND},
73         {"li",          HTML_NLAROUND | HTML_INDENT},
74         {"ul",          HTML_NLALL | HTML_INDENT},
75         {"ol",          HTML_NLALL | HTML_INDENT},
76         {"dl",          HTML_NLALL | HTML_INDENT},
77         {"dt",          HTML_NLAROUND},
78         {"dd",          HTML_NLAROUND | HTML_INDENT},
79         {"pre",         HTML_NLALL | HTML_NOINDENT},
80         {"var",         0},
81         {"cite",        0},
82         {"b",           0},
83         {"i",           0},
84         {"code",        0},
85         {"small",       0},
86         {"style",       HTML_NLALL | HTML_INDENT},
87         {"math",        HTML_NLALL | HTML_INDENT},
88         {"mrow",        0},
89         {"mi",          0},
90         {"mo",          0},
91         {"msup",        0},
92         {"msub",        0},
93         {"msubsup",     0},
94         {"mfrac",       0},
95         {"msqrt",       0},
96         {"mfenced",     0},
97         {"mtable",      0},
98         {"mtr",         0},
99         {"mtd",         0},
100         {"munderover",  0},
101         {"munder",      0},
102         {"mover",       0},
103 };
104
105 static  const char      *const roffscales[SCALE_MAX] = {
106         "cm", /* SCALE_CM */
107         "in", /* SCALE_IN */
108         "pc", /* SCALE_PC */
109         "pt", /* SCALE_PT */
110         "em", /* SCALE_EM */
111         "em", /* SCALE_MM */
112         "ex", /* SCALE_EN */
113         "ex", /* SCALE_BU */
114         "em", /* SCALE_VS */
115         "ex", /* SCALE_FS */
116 };
117
118 static  void     a2width(const char *, struct roffsu *);
119 static  void     print_byte(struct html *, char);
120 static  void     print_endword(struct html *);
121 static  void     print_indent(struct html *);
122 static  void     print_word(struct html *, const char *);
123
124 static  void     print_ctag(struct html *, struct tag *);
125 static  int      print_escape(struct html *, char);
126 static  int      print_encode(struct html *, const char *, const char *, int);
127 static  void     print_href(struct html *, const char *, const char *, int);
128 static  void     print_metaf(struct html *, enum mandoc_esc);
129
130
131 void *
132 html_alloc(const struct manoutput *outopts)
133 {
134         struct html     *h;
135
136         h = mandoc_calloc(1, sizeof(struct html));
137
138         h->tag = NULL;
139         h->style = outopts->style;
140         h->base_man = outopts->man;
141         h->base_includes = outopts->includes;
142         if (outopts->fragment)
143                 h->oflags |= HTML_FRAGMENT;
144
145         return h;
146 }
147
148 void
149 html_free(void *p)
150 {
151         struct tag      *tag;
152         struct html     *h;
153
154         h = (struct html *)p;
155
156         while ((tag = h->tag) != NULL) {
157                 h->tag = tag->next;
158                 free(tag);
159         }
160
161         free(h);
162 }
163
164 void
165 print_gen_head(struct html *h)
166 {
167         struct tag      *t;
168
169         print_otag(h, TAG_META, "?", "charset", "utf-8");
170
171         /*
172          * Print a default style-sheet.
173          */
174
175         t = print_otag(h, TAG_STYLE, "");
176         print_text(h, "table.head, table.foot { width: 100%; }");
177         print_endline(h);
178         print_text(h, "td.head-rtitle, td.foot-os { text-align: right; }");
179         print_endline(h);
180         print_text(h, "td.head-vol { text-align: center; }");
181         print_endline(h);
182         print_text(h, "div.Pp { margin: 1ex 0ex; }");
183         print_tagq(h, t);
184
185         if (h->style)
186                 print_otag(h, TAG_LINK, "?h??", "rel", "stylesheet",
187                     h->style, "type", "text/css", "media", "all");
188 }
189
190 static void
191 print_metaf(struct html *h, enum mandoc_esc deco)
192 {
193         enum htmlfont    font;
194
195         switch (deco) {
196         case ESCAPE_FONTPREV:
197                 font = h->metal;
198                 break;
199         case ESCAPE_FONTITALIC:
200                 font = HTMLFONT_ITALIC;
201                 break;
202         case ESCAPE_FONTBOLD:
203                 font = HTMLFONT_BOLD;
204                 break;
205         case ESCAPE_FONTBI:
206                 font = HTMLFONT_BI;
207                 break;
208         case ESCAPE_FONT:
209         case ESCAPE_FONTROMAN:
210                 font = HTMLFONT_NONE;
211                 break;
212         default:
213                 abort();
214         }
215
216         if (h->metaf) {
217                 print_tagq(h, h->metaf);
218                 h->metaf = NULL;
219         }
220
221         h->metal = h->metac;
222         h->metac = font;
223
224         switch (font) {
225         case HTMLFONT_ITALIC:
226                 h->metaf = print_otag(h, TAG_I, "");
227                 break;
228         case HTMLFONT_BOLD:
229                 h->metaf = print_otag(h, TAG_B, "");
230                 break;
231         case HTMLFONT_BI:
232                 h->metaf = print_otag(h, TAG_B, "");
233                 print_otag(h, TAG_I, "");
234                 break;
235         default:
236                 break;
237         }
238 }
239
240 char *
241 html_make_id(const struct roff_node *n)
242 {
243         const struct roff_node  *nch;
244         char                    *buf, *cp;
245
246         for (nch = n->child; nch != NULL; nch = nch->next)
247                 if (nch->type != ROFFT_TEXT)
248                         return NULL;
249
250         buf = NULL;
251         deroff(&buf, n);
252
253         /* http://www.w3.org/TR/html5/dom.html#the-id-attribute */
254
255         for (cp = buf; *cp != '\0'; cp++)
256                 if (*cp == ' ')
257                         *cp = '_';
258
259         return buf;
260 }
261
262 int
263 html_strlen(const char *cp)
264 {
265         size_t           rsz;
266         int              skip, sz;
267
268         /*
269          * Account for escaped sequences within string length
270          * calculations.  This follows the logic in term_strlen() as we
271          * must calculate the width of produced strings.
272          * Assume that characters are always width of "1".  This is
273          * hacky, but it gets the job done for approximation of widths.
274          */
275
276         sz = 0;
277         skip = 0;
278         while (1) {
279                 rsz = strcspn(cp, "\\");
280                 if (rsz) {
281                         cp += rsz;
282                         if (skip) {
283                                 skip = 0;
284                                 rsz--;
285                         }
286                         sz += rsz;
287                 }
288                 if ('\0' == *cp)
289                         break;
290                 cp++;
291                 switch (mandoc_escape(&cp, NULL, NULL)) {
292                 case ESCAPE_ERROR:
293                         return sz;
294                 case ESCAPE_UNICODE:
295                 case ESCAPE_NUMBERED:
296                 case ESCAPE_SPECIAL:
297                 case ESCAPE_OVERSTRIKE:
298                         if (skip)
299                                 skip = 0;
300                         else
301                                 sz++;
302                         break;
303                 case ESCAPE_SKIPCHAR:
304                         skip = 1;
305                         break;
306                 default:
307                         break;
308                 }
309         }
310         return sz;
311 }
312
313 static int
314 print_escape(struct html *h, char c)
315 {
316
317         switch (c) {
318         case '<':
319                 print_word(h, "&lt;");
320                 break;
321         case '>':
322                 print_word(h, "&gt;");
323                 break;
324         case '&':
325                 print_word(h, "&amp;");
326                 break;
327         case '"':
328                 print_word(h, "&quot;");
329                 break;
330         case ASCII_NBRSP:
331                 print_word(h, "&nbsp;");
332                 break;
333         case ASCII_HYPH:
334                 print_byte(h, '-');
335                 break;
336         case ASCII_BREAK:
337                 break;
338         default:
339                 return 0;
340         }
341         return 1;
342 }
343
344 static int
345 print_encode(struct html *h, const char *p, const char *pend, int norecurse)
346 {
347         char             numbuf[16];
348         size_t           sz;
349         int              c, len, nospace;
350         const char      *seq;
351         enum mandoc_esc  esc;
352         static const char rejs[9] = { '\\', '<', '>', '&', '"',
353                 ASCII_NBRSP, ASCII_HYPH, ASCII_BREAK, '\0' };
354
355         if (pend == NULL)
356                 pend = strchr(p, '\0');
357
358         nospace = 0;
359
360         while (p < pend) {
361                 if (HTML_SKIPCHAR & h->flags && '\\' != *p) {
362                         h->flags &= ~HTML_SKIPCHAR;
363                         p++;
364                         continue;
365                 }
366
367                 for (sz = strcspn(p, rejs); sz-- && p < pend; p++)
368                         if (*p == ' ')
369                                 print_endword(h);
370                         else
371                                 print_byte(h, *p);
372
373                 if (p >= pend)
374                         break;
375
376                 if (print_escape(h, *p++))
377                         continue;
378
379                 esc = mandoc_escape(&p, &seq, &len);
380                 if (ESCAPE_ERROR == esc)
381                         break;
382
383                 switch (esc) {
384                 case ESCAPE_FONT:
385                 case ESCAPE_FONTPREV:
386                 case ESCAPE_FONTBOLD:
387                 case ESCAPE_FONTITALIC:
388                 case ESCAPE_FONTBI:
389                 case ESCAPE_FONTROMAN:
390                         if (0 == norecurse)
391                                 print_metaf(h, esc);
392                         continue;
393                 case ESCAPE_SKIPCHAR:
394                         h->flags |= HTML_SKIPCHAR;
395                         continue;
396                 default:
397                         break;
398                 }
399
400                 if (h->flags & HTML_SKIPCHAR) {
401                         h->flags &= ~HTML_SKIPCHAR;
402                         continue;
403                 }
404
405                 switch (esc) {
406                 case ESCAPE_UNICODE:
407                         /* Skip past "u" header. */
408                         c = mchars_num2uc(seq + 1, len - 1);
409                         break;
410                 case ESCAPE_NUMBERED:
411                         c = mchars_num2char(seq, len);
412                         if (c < 0)
413                                 continue;
414                         break;
415                 case ESCAPE_SPECIAL:
416                         c = mchars_spec2cp(seq, len);
417                         if (c <= 0)
418                                 continue;
419                         break;
420                 case ESCAPE_NOSPACE:
421                         if ('\0' == *p)
422                                 nospace = 1;
423                         continue;
424                 case ESCAPE_OVERSTRIKE:
425                         if (len == 0)
426                                 continue;
427                         c = seq[len - 1];
428                         break;
429                 default:
430                         continue;
431                 }
432                 if ((c < 0x20 && c != 0x09) ||
433                     (c > 0x7E && c < 0xA0))
434                         c = 0xFFFD;
435                 if (c > 0x7E) {
436                         (void)snprintf(numbuf, sizeof(numbuf), "&#%d;", c);
437                         print_word(h, numbuf);
438                 } else if (print_escape(h, c) == 0)
439                         print_byte(h, c);
440         }
441
442         return nospace;
443 }
444
445 static void
446 print_href(struct html *h, const char *name, const char *sec, int man)
447 {
448         const char      *p, *pp;
449
450         pp = man ? h->base_man : h->base_includes;
451         while ((p = strchr(pp, '%')) != NULL) {
452                 print_encode(h, pp, p, 1);
453                 if (man && p[1] == 'S') {
454                         if (sec == NULL)
455                                 print_byte(h, '1');
456                         else
457                                 print_encode(h, sec, NULL, 1);
458                 } else if ((man && p[1] == 'N') ||
459                     (man == 0 && p[1] == 'I'))
460                         print_encode(h, name, NULL, 1);
461                 else
462                         print_encode(h, p, p + 2, 1);
463                 pp = p + 2;
464         }
465         if (*pp != '\0')
466                 print_encode(h, pp, NULL, 1);
467 }
468
469 struct tag *
470 print_otag(struct html *h, enum htmltag tag, const char *fmt, ...)
471 {
472         va_list          ap;
473         struct roffsu    mysu, *su;
474         char             numbuf[16];
475         struct tag      *t;
476         const char      *attr;
477         char            *arg1, *arg2;
478         double           v;
479         int              i, have_style, tflags;
480
481         tflags = htmltags[tag].flags;
482
483         /* Push this tag onto the stack of open scopes. */
484
485         if ((tflags & HTML_NOSTACK) == 0) {
486                 t = mandoc_malloc(sizeof(struct tag));
487                 t->tag = tag;
488                 t->next = h->tag;
489                 h->tag = t;
490         } else
491                 t = NULL;
492
493         if (tflags & HTML_NLBEFORE)
494                 print_endline(h);
495         if (h->col == 0)
496                 print_indent(h);
497         else if ((h->flags & HTML_NOSPACE) == 0) {
498                 if (h->flags & HTML_KEEP)
499                         print_word(h, "&#160;");
500                 else {
501                         if (h->flags & HTML_PREKEEP)
502                                 h->flags |= HTML_KEEP;
503                         print_endword(h);
504                 }
505         }
506
507         if ( ! (h->flags & HTML_NONOSPACE))
508                 h->flags &= ~HTML_NOSPACE;
509         else
510                 h->flags |= HTML_NOSPACE;
511
512         /* Print out the tag name and attributes. */
513
514         print_byte(h, '<');
515         print_word(h, htmltags[tag].name);
516
517         va_start(ap, fmt);
518
519         have_style = 0;
520         while (*fmt != '\0') {
521                 if (*fmt == 's') {
522                         have_style = 1;
523                         fmt++;
524                         break;
525                 }
526
527                 /* Parse a non-style attribute and its arguments. */
528
529                 arg1 = va_arg(ap, char *);
530                 switch (*fmt++) {
531                 case 'c':
532                         attr = "class";
533                         break;
534                 case 'h':
535                         attr = "href";
536                         break;
537                 case 'i':
538                         attr = "id";
539                         break;
540                 case '?':
541                         attr = arg1;
542                         arg1 = va_arg(ap, char *);
543                         break;
544                 default:
545                         abort();
546                 }
547                 arg2 = NULL;
548                 if (*fmt == 'M')
549                         arg2 = va_arg(ap, char *);
550                 if (arg1 == NULL)
551                         continue;
552
553                 /* Print the non-style attributes. */
554
555                 print_byte(h, ' ');
556                 print_word(h, attr);
557                 print_byte(h, '=');
558                 print_byte(h, '"');
559                 switch (*fmt) {
560                 case 'I':
561                         print_href(h, arg1, NULL, 0);
562                         fmt++;
563                         break;
564                 case 'M':
565                         print_href(h, arg1, arg2, 1);
566                         fmt++;
567                         break;
568                 case 'R':
569                         print_byte(h, '#');
570                         print_encode(h, arg1, NULL, 1);
571                         fmt++;
572                         break;
573                 case 'T':
574                         print_encode(h, arg1, NULL, 1);
575                         print_word(h, "\" title=\"");
576                         print_encode(h, arg1, NULL, 1);
577                         fmt++;
578                         break;
579                 default:
580                         print_encode(h, arg1, NULL, 1);
581                         break;
582                 }
583                 print_byte(h, '"');
584         }
585
586         /* Print out styles. */
587
588         while (*fmt != '\0') {
589                 arg1 = NULL;
590                 su = NULL;
591
592                 /* First letter: input argument type. */
593
594                 switch (*fmt++) {
595                 case 'h':
596                         i = va_arg(ap, int);
597                         su = &mysu;
598                         SCALE_HS_INIT(su, i);
599                         break;
600                 case 's':
601                         arg1 = va_arg(ap, char *);
602                         break;
603                 case 'u':
604                         su = va_arg(ap, struct roffsu *);
605                         break;
606                 case 'v':
607                         i = va_arg(ap, int);
608                         su = &mysu;
609                         SCALE_VS_INIT(su, i);
610                         break;
611                 case 'w':
612                         if ((arg2 = va_arg(ap, char *)) == NULL)
613                                 break;
614                         su = &mysu;
615                         a2width(arg2, su);
616                         if (*fmt == '+') {
617                                 /* Increase to make even bold text fit. */
618                                 su->scale *= 1.2;
619                                 /* Add padding. */
620                                 su->scale += 3.0;
621                                 fmt++;
622                         }
623                         if (*fmt == '-') {
624                                 su->scale *= -1.0;
625                                 fmt++;
626                         }
627                         break;
628                 default:
629                         abort();
630                 }
631
632                 /* Second letter: style name. */
633
634                 switch (*fmt++) {
635                 case 'b':
636                         attr = "margin-bottom";
637                         break;
638                 case 'h':
639                         attr = "height";
640                         break;
641                 case 'i':
642                         attr = "text-indent";
643                         break;
644                 case 'l':
645                         attr = "margin-left";
646                         break;
647                 case 't':
648                         attr = "margin-top";
649                         break;
650                 case 'w':
651                         attr = "width";
652                         break;
653                 case 'W':
654                         attr = "min-width";
655                         break;
656                 case '?':
657                         attr = arg1;
658                         arg1 = va_arg(ap, char *);
659                         break;
660                 default:
661                         abort();
662                 }
663                 if (su == NULL && arg1 == NULL)
664                         continue;
665
666                 if (have_style == 1)
667                         print_word(h, " style=\"");
668                 else
669                         print_byte(h, ' ');
670                 print_word(h, attr);
671                 print_byte(h, ':');
672                 print_byte(h, ' ');
673                 if (su != NULL) {
674                         v = su->scale;
675                         if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
676                                 v = 1.0;
677                         else if (su->unit == SCALE_BU)
678                                 v /= 24.0;
679                         (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
680                         print_word(h, numbuf);
681                         print_word(h, roffscales[su->unit]);
682                 } else
683                         print_word(h, arg1);
684                 print_byte(h, ';');
685                 have_style = 2;
686         }
687         if (have_style == 2)
688                 print_byte(h, '"');
689
690         va_end(ap);
691
692         /* Accommodate for "well-formed" singleton escaping. */
693
694         if (HTML_AUTOCLOSE & htmltags[tag].flags)
695                 print_byte(h, '/');
696
697         print_byte(h, '>');
698
699         if (tflags & HTML_NLBEGIN)
700                 print_endline(h);
701         else
702                 h->flags |= HTML_NOSPACE;
703
704         if (tflags & HTML_INDENT)
705                 h->indent++;
706         if (tflags & HTML_NOINDENT)
707                 h->noindent++;
708
709         return t;
710 }
711
712 static void
713 print_ctag(struct html *h, struct tag *tag)
714 {
715         int      tflags;
716
717         /*
718          * Remember to close out and nullify the current
719          * meta-font and table, if applicable.
720          */
721         if (tag == h->metaf)
722                 h->metaf = NULL;
723         if (tag == h->tblt)
724                 h->tblt = NULL;
725
726         tflags = htmltags[tag->tag].flags;
727
728         if (tflags & HTML_INDENT)
729                 h->indent--;
730         if (tflags & HTML_NOINDENT)
731                 h->noindent--;
732         if (tflags & HTML_NLEND)
733                 print_endline(h);
734         print_indent(h);
735         print_byte(h, '<');
736         print_byte(h, '/');
737         print_word(h, htmltags[tag->tag].name);
738         print_byte(h, '>');
739         if (tflags & HTML_NLAFTER)
740                 print_endline(h);
741
742         h->tag = tag->next;
743         free(tag);
744 }
745
746 void
747 print_gen_decls(struct html *h)
748 {
749         print_word(h, "<!DOCTYPE html>");
750         print_endline(h);
751 }
752
753 void
754 print_text(struct html *h, const char *word)
755 {
756         if (h->col && (h->flags & HTML_NOSPACE) == 0) {
757                 if ( ! (HTML_KEEP & h->flags)) {
758                         if (HTML_PREKEEP & h->flags)
759                                 h->flags |= HTML_KEEP;
760                         print_endword(h);
761                 } else
762                         print_word(h, "&#160;");
763         }
764
765         assert(NULL == h->metaf);
766         switch (h->metac) {
767         case HTMLFONT_ITALIC:
768                 h->metaf = print_otag(h, TAG_I, "");
769                 break;
770         case HTMLFONT_BOLD:
771                 h->metaf = print_otag(h, TAG_B, "");
772                 break;
773         case HTMLFONT_BI:
774                 h->metaf = print_otag(h, TAG_B, "");
775                 print_otag(h, TAG_I, "");
776                 break;
777         default:
778                 print_indent(h);
779                 break;
780         }
781
782         assert(word);
783         if ( ! print_encode(h, word, NULL, 0)) {
784                 if ( ! (h->flags & HTML_NONOSPACE))
785                         h->flags &= ~HTML_NOSPACE;
786                 h->flags &= ~HTML_NONEWLINE;
787         } else
788                 h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
789
790         if (h->metaf) {
791                 print_tagq(h, h->metaf);
792                 h->metaf = NULL;
793         }
794
795         h->flags &= ~HTML_IGNDELIM;
796 }
797
798 void
799 print_tagq(struct html *h, const struct tag *until)
800 {
801         struct tag      *tag;
802
803         while ((tag = h->tag) != NULL) {
804                 print_ctag(h, tag);
805                 if (until && tag == until)
806                         return;
807         }
808 }
809
810 void
811 print_stagq(struct html *h, const struct tag *suntil)
812 {
813         struct tag      *tag;
814
815         while ((tag = h->tag) != NULL) {
816                 if (suntil && tag == suntil)
817                         return;
818                 print_ctag(h, tag);
819         }
820 }
821
822 void
823 print_paragraph(struct html *h)
824 {
825         struct tag      *t;
826
827         t = print_otag(h, TAG_DIV, "c", "Pp");
828         print_tagq(h, t);
829 }
830
831
832 /***********************************************************************
833  * Low level output functions.
834  * They implement line breaking using a short static buffer.
835  ***********************************************************************/
836
837 /*
838  * Buffer one HTML output byte.
839  * If the buffer is full, flush and deactivate it and start a new line.
840  * If the buffer is inactive, print directly.
841  */
842 static void
843 print_byte(struct html *h, char c)
844 {
845         if ((h->flags & HTML_BUFFER) == 0) {
846                 putchar(c);
847                 h->col++;
848                 return;
849         }
850
851         if (h->col + h->bufcol < sizeof(h->buf)) {
852                 h->buf[h->bufcol++] = c;
853                 return;
854         }
855
856         putchar('\n');
857         h->col = 0;
858         print_indent(h);
859         putchar(' ');
860         putchar(' ');
861         fwrite(h->buf, h->bufcol, 1, stdout);
862         putchar(c);
863         h->col = (h->indent + 1) * 2 + h->bufcol + 1;
864         h->bufcol = 0;
865         h->flags &= ~HTML_BUFFER;
866 }
867
868 /*
869  * If something was printed on the current output line, end it.
870  * Not to be called right after print_indent().
871  */
872 void
873 print_endline(struct html *h)
874 {
875         if (h->col == 0)
876                 return;
877
878         if (h->bufcol) {
879                 putchar(' ');
880                 fwrite(h->buf, h->bufcol, 1, stdout);
881                 h->bufcol = 0;
882         }
883         putchar('\n');
884         h->col = 0;
885         h->flags |= HTML_NOSPACE;
886         h->flags &= ~HTML_BUFFER;
887 }
888
889 /*
890  * Flush the HTML output buffer.
891  * If it is inactive, activate it.
892  */
893 static void
894 print_endword(struct html *h)
895 {
896         if (h->noindent) {
897                 print_byte(h, ' ');
898                 return;
899         }
900
901         if ((h->flags & HTML_BUFFER) == 0) {
902                 h->col++;
903                 h->flags |= HTML_BUFFER;
904         } else if (h->bufcol) {
905                 putchar(' ');
906                 fwrite(h->buf, h->bufcol, 1, stdout);
907                 h->col += h->bufcol + 1;
908         }
909         h->bufcol = 0;
910 }
911
912 /*
913  * If at the beginning of a new output line,
914  * perform indentation and mark the line as containing output.
915  * Make sure to really produce some output right afterwards,
916  * but do not use print_otag() for producing it.
917  */
918 static void
919 print_indent(struct html *h)
920 {
921         size_t   i;
922
923         if (h->col)
924                 return;
925
926         if (h->noindent == 0) {
927                 h->col = h->indent * 2;
928                 for (i = 0; i < h->col; i++)
929                         putchar(' ');
930         }
931         h->flags &= ~HTML_NOSPACE;
932 }
933
934 /*
935  * Print or buffer some characters
936  * depending on the current HTML output buffer state.
937  */
938 static void
939 print_word(struct html *h, const char *cp)
940 {
941         while (*cp != '\0')
942                 print_byte(h, *cp++);
943 }
944
945 /*
946  * Calculate the scaling unit passed in a `-width' argument.  This uses
947  * either a native scaling unit (e.g., 1i, 2m) or the string length of
948  * the value.
949  */
950 static void
951 a2width(const char *p, struct roffsu *su)
952 {
953         const char      *end;
954
955         end = a2roffsu(p, su, SCALE_MAX);
956         if (end == NULL || *end != '\0') {
957                 su->unit = SCALE_EN;
958                 su->scale = html_strlen(p);
959         } else if (su->scale < 0.0)
960                 su->scale = 0.0;
961 }