add Tiny_httpd_html.to_writer

This commit is contained in:
Simon Cruanes 2023-07-18 14:27:48 -04:00
parent 4a78eeb69c
commit 3332fa906b
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 12 additions and 2 deletions

View file

@ -19,7 +19,8 @@ include Tiny_httpd_html_
let to_out_channel ?(top = false) (self : elt) (out : IO.Out_channel.t) : unit =
let out = Out.create_of_out out in
if top then Out.add_string out "<!DOCTYPE html>\n";
self out
self out;
Out.flush out
(** Convert a HTML element to a string.
@param top if true, add DOCTYPE at the beginning. The top element should then
@ -50,6 +51,13 @@ let to_string_top = to_string ~top:true
@since NEXT_RELEASE *)
let to_out_channel_top = to_out_channel ~top:true
(** Produce a streaming writer from this HTML element.
@param top if true, add a DOCTYPE. See {!to_out_channel}.
@since NEXT_RELEASE *)
let to_writer ?top (self : elt) : IO.Writer.t =
let write oc = to_out_channel ?top self oc in
IO.Writer.make ~write ()
(** Convert a HTML element to a stream. This might just convert
it to a string first, do not assume it to be more efficient. *)
let to_stream (self : elt) : Tiny_httpd_stream.t =

View file

@ -295,6 +295,7 @@ module Out : sig
type t
val create_of_buffer : Buffer.t -> t
val create_of_out: Tiny_httpd_io.Out_channel.t -> t
val flush : t -> unit
val add_char : t -> char -> unit
val add_string : t -> string -> unit
val add_format_nl : t -> unit
@ -307,9 +308,10 @@ end = struct
}
let create_of_out out = {out; fmt_nl=true}
let create_of_buffer buf : t = create_of_out (IO.Out_channel.of_buffer buf)
let[@inline] flush self : unit = IO.Out_channel.flush self.out
let[@inline] add_char self c = IO.Out_channel.output_char self.out c
let[@inline] add_string self s = IO.Out_channel.output_string self.out s
let add_format_nl self = if self.fmt_nl then add_char self '\n'
let[@inline] add_format_nl self = if self.fmt_nl then add_char self '\n'
let with_no_format_nl self f =
if self.fmt_nl then (
self.fmt_nl <- false;