]> git.cameronkatri.com Git - mandoc.git/blob - html.c
Basic implementation of the roff(7) .ta (define tab stops) request.
[mandoc.git] / html.c
1 /* $Id: html.c,v 1.210 2017/03/15 11:29:53 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 case 'W':
613 if ((arg2 = va_arg(ap, char *)) == NULL)
614 break;
615 su = &mysu;
616 a2width(arg2, su);
617 /* Increase width to make even bold text fit. */
618 su->scale *= 1.1;
619 if (fmt[-1] == 'W')
620 su->scale *= -1.0;
621 break;
622 default:
623 abort();
624 }
625
626 /* Second letter: style name. */
627
628 switch (*fmt++) {
629 case 'b':
630 attr = "margin-bottom";
631 break;
632 case 'h':
633 attr = "height";
634 break;
635 case 'i':
636 attr = "text-indent";
637 break;
638 case 'l':
639 attr = "margin-left";
640 break;
641 case 't':
642 attr = "margin-top";
643 break;
644 case 'w':
645 attr = "width";
646 break;
647 case 'W':
648 attr = "min-width";
649 break;
650 case '?':
651 attr = arg1;
652 arg1 = va_arg(ap, char *);
653 break;
654 default:
655 abort();
656 }
657 if (su == NULL && arg1 == NULL)
658 continue;
659
660 if (have_style == 1)
661 print_word(h, " style=\"");
662 else
663 print_byte(h, ' ');
664 print_word(h, attr);
665 print_byte(h, ':');
666 print_byte(h, ' ');
667 if (su != NULL) {
668 v = su->scale;
669 if (su->unit == SCALE_MM && (v /= 100.0) == 0.0)
670 v = 1.0;
671 else if (su->unit == SCALE_BU)
672 v /= 24.0;
673 (void)snprintf(numbuf, sizeof(numbuf), "%.2f", v);
674 print_word(h, numbuf);
675 print_word(h, roffscales[su->unit]);
676 } else
677 print_word(h, arg1);
678 print_byte(h, ';');
679 have_style = 2;
680 }
681 if (have_style == 2)
682 print_byte(h, '"');
683
684 va_end(ap);
685
686 /* Accommodate for "well-formed" singleton escaping. */
687
688 if (HTML_AUTOCLOSE & htmltags[tag].flags)
689 print_byte(h, '/');
690
691 print_byte(h, '>');
692
693 if (tflags & HTML_NLBEGIN)
694 print_endline(h);
695 else
696 h->flags |= HTML_NOSPACE;
697
698 if (tflags & HTML_INDENT)
699 h->indent++;
700 if (tflags & HTML_NOINDENT)
701 h->noindent++;
702
703 return t;
704 }
705
706 static void
707 print_ctag(struct html *h, struct tag *tag)
708 {
709 int tflags;
710
711 /*
712 * Remember to close out and nullify the current
713 * meta-font and table, if applicable.
714 */
715 if (tag == h->metaf)
716 h->metaf = NULL;
717 if (tag == h->tblt)
718 h->tblt = NULL;
719
720 tflags = htmltags[tag->tag].flags;
721
722 if (tflags & HTML_INDENT)
723 h->indent--;
724 if (tflags & HTML_NOINDENT)
725 h->noindent--;
726 if (tflags & HTML_NLEND)
727 print_endline(h);
728 print_indent(h);
729 print_byte(h, '<');
730 print_byte(h, '/');
731 print_word(h, htmltags[tag->tag].name);
732 print_byte(h, '>');
733 if (tflags & HTML_NLAFTER)
734 print_endline(h);
735
736 h->tag = tag->next;
737 free(tag);
738 }
739
740 void
741 print_gen_decls(struct html *h)
742 {
743 print_word(h, "<!DOCTYPE html>");
744 print_endline(h);
745 }
746
747 void
748 print_text(struct html *h, const char *word)
749 {
750 if (h->col && (h->flags & HTML_NOSPACE) == 0) {
751 if ( ! (HTML_KEEP & h->flags)) {
752 if (HTML_PREKEEP & h->flags)
753 h->flags |= HTML_KEEP;
754 print_endword(h);
755 } else
756 print_word(h, "&#160;");
757 }
758
759 assert(NULL == h->metaf);
760 switch (h->metac) {
761 case HTMLFONT_ITALIC:
762 h->metaf = print_otag(h, TAG_I, "");
763 break;
764 case HTMLFONT_BOLD:
765 h->metaf = print_otag(h, TAG_B, "");
766 break;
767 case HTMLFONT_BI:
768 h->metaf = print_otag(h, TAG_B, "");
769 print_otag(h, TAG_I, "");
770 break;
771 default:
772 print_indent(h);
773 break;
774 }
775
776 assert(word);
777 if ( ! print_encode(h, word, NULL, 0)) {
778 if ( ! (h->flags & HTML_NONOSPACE))
779 h->flags &= ~HTML_NOSPACE;
780 h->flags &= ~HTML_NONEWLINE;
781 } else
782 h->flags |= HTML_NOSPACE | HTML_NONEWLINE;
783
784 if (h->metaf) {
785 print_tagq(h, h->metaf);
786 h->metaf = NULL;
787 }
788
789 h->flags &= ~HTML_IGNDELIM;
790 }
791
792 void
793 print_tagq(struct html *h, const struct tag *until)
794 {
795 struct tag *tag;
796
797 while ((tag = h->tag) != NULL) {
798 print_ctag(h, tag);
799 if (until && tag == until)
800 return;
801 }
802 }
803
804 void
805 print_stagq(struct html *h, const struct tag *suntil)
806 {
807 struct tag *tag;
808
809 while ((tag = h->tag) != NULL) {
810 if (suntil && tag == suntil)
811 return;
812 print_ctag(h, tag);
813 }
814 }
815
816 void
817 print_paragraph(struct html *h)
818 {
819 struct tag *t;
820
821 t = print_otag(h, TAG_DIV, "c", "Pp");
822 print_tagq(h, t);
823 }
824
825
826 /***********************************************************************
827 * Low level output functions.
828 * They implement line breaking using a short static buffer.
829 ***********************************************************************/
830
831 /*
832 * Buffer one HTML output byte.
833 * If the buffer is full, flush and deactivate it and start a new line.
834 * If the buffer is inactive, print directly.
835 */
836 static void
837 print_byte(struct html *h, char c)
838 {
839 if ((h->flags & HTML_BUFFER) == 0) {
840 putchar(c);
841 h->col++;
842 return;
843 }
844
845 if (h->col + h->bufcol < sizeof(h->buf)) {
846 h->buf[h->bufcol++] = c;
847 return;
848 }
849
850 putchar('\n');
851 h->col = 0;
852 print_indent(h);
853 putchar(' ');
854 putchar(' ');
855 fwrite(h->buf, h->bufcol, 1, stdout);
856 putchar(c);
857 h->col = (h->indent + 1) * 2 + h->bufcol + 1;
858 h->bufcol = 0;
859 h->flags &= ~HTML_BUFFER;
860 }
861
862 /*
863 * If something was printed on the current output line, end it.
864 * Not to be called right after print_indent().
865 */
866 void
867 print_endline(struct html *h)
868 {
869 if (h->col == 0)
870 return;
871
872 if (h->bufcol) {
873 putchar(' ');
874 fwrite(h->buf, h->bufcol, 1, stdout);
875 h->bufcol = 0;
876 }
877 putchar('\n');
878 h->col = 0;
879 h->flags |= HTML_NOSPACE;
880 h->flags &= ~HTML_BUFFER;
881 }
882
883 /*
884 * Flush the HTML output buffer.
885 * If it is inactive, activate it.
886 */
887 static void
888 print_endword(struct html *h)
889 {
890 if (h->noindent) {
891 print_byte(h, ' ');
892 return;
893 }
894
895 if ((h->flags & HTML_BUFFER) == 0) {
896 h->col++;
897 h->flags |= HTML_BUFFER;
898 } else if (h->bufcol) {
899 putchar(' ');
900 fwrite(h->buf, h->bufcol, 1, stdout);
901 h->col += h->bufcol + 1;
902 }
903 h->bufcol = 0;
904 }
905
906 /*
907 * If at the beginning of a new output line,
908 * perform indentation and mark the line as containing output.
909 * Make sure to really produce some output right afterwards,
910 * but do not use print_otag() for producing it.
911 */
912 static void
913 print_indent(struct html *h)
914 {
915 size_t i;
916
917 if (h->col)
918 return;
919
920 if (h->noindent == 0) {
921 h->col = h->indent * 2;
922 for (i = 0; i < h->col; i++)
923 putchar(' ');
924 }
925 h->flags &= ~HTML_NOSPACE;
926 }
927
928 /*
929 * Print or buffer some characters
930 * depending on the current HTML output buffer state.
931 */
932 static void
933 print_word(struct html *h, const char *cp)
934 {
935 while (*cp != '\0')
936 print_byte(h, *cp++);
937 }
938
939 /*
940 * Calculate the scaling unit passed in a `-width' argument. This uses
941 * either a native scaling unit (e.g., 1i, 2m) or the string length of
942 * the value.
943 */
944 static void
945 a2width(const char *p, struct roffsu *su)
946 {
947 if (a2roffsu(p, su, SCALE_MAX) < 2) {
948 su->unit = SCALE_EN;
949 su->scale = html_strlen(p);
950 } else if (su->scale < 0.0)
951 su->scale = 0.0;
952 }