Skip to content

Commit

Permalink
Markup.pretty_print: add newline after doctype
Browse files Browse the repository at this point in the history
Fixes #58.
  • Loading branch information
aantron committed Oct 22, 2020
1 parent b4b59ae commit 34cdf1e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/markup.mli
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,15 @@ foo</bar>
example, if the first signal would be [`Doctype], the context is set to
[`Document], but if the first signal would be [`Start_element "td"], the
context is set to [`Fragment "tr"]. If the first signal would be
[`Start_element "g"], the context is set to [`Fragment "svg"].
*)
[`Start_element "g"], the context is set to [`Fragment "svg"]. *)

val write_html :
?escape_attribute:(string -> string) ->
?escape_text:(string -> string) ->
([< signal ], 's) stream -> (char, 's) stream
(** Similar to {!write_xml}, but emits HTML5 instead of XML.
If [~escape_attribute] and/or [~escape_text] are provided,
they are used instead of default escaping functions.
*)
they are used instead of default escaping functions. *)



Expand Down Expand Up @@ -707,7 +704,9 @@ val normalize_text :
after parsing, or generating streams from scratch, and would like to clean
up the [`Text] signals. *)

val pretty_print : ([> content_signal ] as 'a, 's) stream -> ('a, 's) stream
val pretty_print :
([> content_signal | `Doctype of doctype ] as 'a, 's) stream ->
('a, 's) stream
(** Adjusts the whitespace in the [`Text] signals in the given stream so that
the output appears nicely-indented when the stream is converted to bytes and
written.
Expand Down
7 changes: 7 additions & 0 deletions src/utility.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ let pretty_print signals =
[`Text [indent indentation]]
(phrasing indentation 0) throw e k

| `Doctype _ ->
list
[signal; `Text ["\n"]]
(flow indentation) throw e k

| _ ->
list
[signal]
Expand All @@ -362,6 +367,8 @@ let pretty_print signals =
[signal]
(phrasing indentation phrasing_nesting_level) throw e k

(* | `Start_element _ | `End_element | `Doctype _ | `Xml _ | `PI _
| `Comment _ -> *)
| _ ->
push signals signal;
list
Expand Down
16 changes: 14 additions & 2 deletions test/test_integration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ let tests = [
assert_equal ~msg:"fi" (assemble ()) (Some ());
assert_equal ~msg:"fi" (assemble ()) None);

("integration.doctype.roundtrip" >:: fun _ ->
("integration.doctype.round-trip" >:: fun _ ->
({|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |} ^
{|"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">|})
|> string
Expand All @@ -103,5 +103,17 @@ let tests = [
|> assert_equal
({|<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" |} ^
{|"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">|} ^
{|<html><head></head><body></body></html>|}))
{|<html><head></head><body></body></html>|}));

("integration.doctype.pretty_print" >:: fun _ ->
"<!DOCTYPE html><div></div>"
|> string
|> parse_html
|> signals
|> pretty_print
|> write_html
|> to_string
|> assert_equal
("<!DOCTYPE html>\n<html>\n <head></head>\n" ^
" <body>\n <div></div>\n </body>\n</html>\n"));
]
16 changes: 16 additions & 0 deletions test/test_utility.ml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ let tests = [
|> to_list
|> assert_equal [
doctype;
`Text ["\n"];
start_element "div";
`Text ["\n"; " "];
start_element "p";
Expand All @@ -224,6 +225,21 @@ let tests = [
`End_element;
`Text ["\n"]]);

("utility.pretty_print.doctype.existing-newline" >:: fun _ ->
[doctype;
`Text ["\n"];
start_element "div";
`End_element]
|> of_list
|> pretty_print
|> to_list
|> assert_equal [
doctype;
`Text ["\n"];
start_element "div";
`End_element;
`Text ["\n"]]);

("utility.html5" >:: fun _ ->
[doctype;
doctype;
Expand Down

0 comments on commit 34cdf1e

Please sign in to comment.