mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2026-03-10 07:35:49 -04:00
add Tiny_httpd_html.to_writer
This commit is contained in:
parent
4a78eeb69c
commit
3332fa906b
2 changed files with 12 additions and 2 deletions
|
|
@ -19,7 +19,8 @@ include Tiny_httpd_html_
|
||||||
let to_out_channel ?(top = false) (self : elt) (out : IO.Out_channel.t) : unit =
|
let to_out_channel ?(top = false) (self : elt) (out : IO.Out_channel.t) : unit =
|
||||||
let out = Out.create_of_out out in
|
let out = Out.create_of_out out in
|
||||||
if top then Out.add_string out "<!DOCTYPE html>\n";
|
if top then Out.add_string out "<!DOCTYPE html>\n";
|
||||||
self out
|
self out;
|
||||||
|
Out.flush out
|
||||||
|
|
||||||
(** Convert a HTML element to a string.
|
(** Convert a HTML element to a string.
|
||||||
@param top if true, add DOCTYPE at the beginning. The top element should then
|
@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 *)
|
@since NEXT_RELEASE *)
|
||||||
let to_out_channel_top = to_out_channel ~top:true
|
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
|
(** Convert a HTML element to a stream. This might just convert
|
||||||
it to a string first, do not assume it to be more efficient. *)
|
it to a string first, do not assume it to be more efficient. *)
|
||||||
let to_stream (self : elt) : Tiny_httpd_stream.t =
|
let to_stream (self : elt) : Tiny_httpd_stream.t =
|
||||||
|
|
|
||||||
|
|
@ -295,6 +295,7 @@ module Out : sig
|
||||||
type t
|
type t
|
||||||
val create_of_buffer : Buffer.t -> t
|
val create_of_buffer : Buffer.t -> t
|
||||||
val create_of_out: Tiny_httpd_io.Out_channel.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_char : t -> char -> unit
|
||||||
val add_string : t -> string -> unit
|
val add_string : t -> string -> unit
|
||||||
val add_format_nl : t -> 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_out out = {out; fmt_nl=true}
|
||||||
let create_of_buffer buf : t = create_of_out (IO.Out_channel.of_buffer buf)
|
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_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[@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 =
|
let with_no_format_nl self f =
|
||||||
if self.fmt_nl then (
|
if self.fmt_nl then (
|
||||||
self.fmt_nl <- false;
|
self.fmt_nl <- false;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue