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