1 .\" $Id: mandoc_html.3,v 1.22 2020/04/19 15:16:56 schwarze Exp $
3 .\" Copyright (c) 2014, 2017, 2018 Ingo Schwarze <schwarze@openbsd.org>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: April 19 2020 $
22 .Nd internals of the mandoc HTML formatter
25 .Fd #include """mandoc.h"""
26 .Fd #include """roff.h"""
27 .Fd #include """out.h"""
28 .Fd #include """html.h"""
30 .Fn print_gen_decls "struct html *h"
32 .Fn print_gen_comment "struct html *h" "struct roff_node *n"
34 .Fn print_gen_head "struct html *h"
38 .Fa "enum htmltag tag"
45 .Fa "const struct tag *until"
50 .Fa "const struct tag *suntil"
53 .Fn html_close_paragraph "struct html *h"
57 .Fa "enum roff_tok tok"
62 .Fa "enum mandoc_esc font"
67 .Fa "const char *word"
72 .Fa "const char *word"
73 .Fa "struct roff_node *n"
77 .Fa "const struct roff_node *n"
83 .Fa "enum htmltag tag"
84 .Fa "const char *cattr"
85 .Fa "struct roff_node *n"
88 .Fn print_endline "struct html *h"
90 The mandoc HTML formatter is not a formal library.
91 However, as it is compiled into more than one program, in particular
95 and because it may be security-critical in some contexts,
96 some documentation is useful to help to use it correctly and
97 to prevent XSS vulnerabilities.
99 The formatter produces HTML output on the standard output.
100 Since proper escaping is usually required and best taken care of
101 at one central place, the language-specific formatters
107 are not supposed to print directly to
115 Instead, they are expected to use the output functions declared in
117 and implemented as part of the main HTML formatting engine in
120 These structures are declared in
124 Internal state of the HTML formatter.
126 One entry for the LIFO stack of HTML elements.
128 .Fa "enum htmltag tag"
130 .Fa "struct tag *next" .
132 .Ss Private interface functions
136 .Aq Pf \&! Ic DOCTYPE
140 .Fn print_gen_comment
141 prints the leading comments, usually containing a Copyright notice
142 and license, as an HTML comment.
143 It is intended to be called right after opening the
157 elements for the document
167 which takes care of properly encoding attributes,
168 which is relevant for the
174 prints the start tag of an HTML element with the name
176 optionally including the attributes specified by
180 is the empty string, no attributes are written.
183 specifies one attribute to write.
184 Most attributes require one
186 argument which becomes the value of the attribute.
187 The arguments have to be given in the same order as the attribute letters.
190 the respective attribute is not written.
191 .Bl -tag -width 1n -offset indent
200 This attribute letter can optionally be followed by a modifier letter.
203 it formats the link as a local one by prefixing a
208 it interpretes the argument as a header file name
209 and generates a link using the
215 it takes two arguments instead of one, a manual page name and
216 section, and formats them as a link to a manual page using the
225 Print an arbitrary attribute.
226 This format letter requires two
228 arguments, the attribute name and the value.
234 uses the private function
236 to take care of HTML encoding.
237 If required by the element type, it remembers in
239 that the element is open.
242 is used to close out all open elements up to and including
245 is a variant to close out all open elements up to but excluding
248 .Fn html_close_paragraph
249 closes all open elements that establish phrasing context,
250 thus returning to the innermost flow context.
254 switches to fill mode if
258 or to no-fill mode if
262 Switching from fill mode to no-fill mode closes the current paragraph
266 Switching in the opposite direction closes the
268 element, but does not open a new paragraph.
271 matches the mode that is already active, no elements are closed nor opened.
276 the mode remains as it is.
283 .Dv ESCAPE_FONTROMAN ,
284 .Dv ESCAPE_FONTBOLD ,
285 .Dv ESCAPE_FONTITALIC ,
289 for future text output and internally remembers
290 the font that was active before the change.
294 .Dv ESCAPE_FONTPREV ,
295 the current and the previous font are exchanged.
296 This function only changes the internal state of the
298 object; no HTML elements are written yet.
299 Subsequent text output will write font elements when needed.
303 prints HTML element content.
304 It uses the private function
306 to take care of HTML encoding.
307 If the document has requested a non-standard font, for example using a
310 font escape sequence,
314 in an HTML font selection element using the
321 .Fn print_tagged_text
334 and yields a segment identifier when passed to
339 allocates a string to be used for the
341 attribute of an HTML element and/or as a segment identifier for a URI in an
348 attribute, it is used; otherwise, child nodes are used.
358 node, the resulting string is the concatenation of the child strings;
359 for other node types, only the first child is used.
360 Bytes not permitted in URI-fragment strings are replaced by underscores.
361 If any of the children to be used is not a text node,
362 no string is generated and
367 argument is non-zero, deduplication is performed by appending an
368 underscore and a decimal integer, if necessary.
371 argument is 1, this is assumed to be the first call for this tag
372 at this location, typically for use by
374 so the integer is incremented before use.
377 argument is 2, this is ssumed to be the second call for this tag
378 at this location, typically for use by
380 so the existing integer, if any, is used without incrementing it.
394 it attempts to generate an
409 generates an element that can only occur in phrasing context,
411 This function is a wrapper around
415 automatically chosing the
417 argument appropriately and setting the
427 makes sure subsequent output starts on a new HTML output line.
428 If nothing was printed on the current output line yet, it has no effect.
429 Otherwise, it appends any buffered text to the current output line,
430 ends the line, and updates the internal state of the
439 are not yet documented.
445 return a pointer to a new element on the stack of HTML elements.
448 opens two elements, a pointer to the outer one is returned.
449 The memory pointed to is owned by the library and is automatically
453 is called on it or when
455 is called on a parent element.
461 if fill mode was active before the call or
467 returns a newly allocated string or
471 lacks text data to create the attribute from.
472 The caller is responsible for
474 the returned string after using it.
478 failure, these functions do not return but call
481 .Bl -tag -width mandoc_aux.c -compact
483 declarations of public functions for use by the main program,
486 declarations of data types and private functions
487 for use by language-specific HTML formatters
489 main HTML formatting engine and utility functions
504 HTML formatter, handling requests like
514 declarations of data types and private functions
515 for shared use by all mandoc formatters,
518 private functions for shared use by all mandoc formatters
520 declarations of common mandoc utility functions, see
523 implementation of common mandoc utility functions
531 The mandoc HTML formatter was written by
532 .An Kristaps Dzonsons Aq Mt kristaps@bsd.lv .
534 .An Ingo Schwarze Aq Mt schwarze@openbsd.org ,
535 who also wrote this manual.