diff --git a/CHANGES.md b/CHANGES.md index b567b5957b..ad192926be 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,8 @@ - Updated colors for code fragments (@EmileTrotignon, #1023) - Fixed complexity of looking up `.odoc` files (@panglesd, #1075) +- Normalize whitespaces in codespans (@gpetiot, #1085) + A newline followed by any whitespaces is normalized as one space character. ### Changed diff --git a/doc/odoc_for_authors.mld b/doc/odoc_for_authors.mld index 11d32bbbd2..0e39402ee7 100644 --- a/doc/odoc_for_authors.mld +++ b/doc/odoc_for_authors.mld @@ -276,6 +276,22 @@ For inline, language agnostic source code style, use square brackets: [ [ ... ] (** Here, [f 0] is [None] *) ]} +Two newlines in a row in an inline codeblock are forbidden, but in the interest +of the 80 char rule, a single newline followed by horizontal space in an inline +codeblock is considered as a single space: + +{[ +(** + A very loooooooooooooooooooooooong line and [List.map (fun x -> x +1)]. + + is equivalent to: + + A very loooooooooooooooooooooooong line and [List.map + (fun x -> x +1)]. + +*) +]} + All the other existing ways to insert code are meant to be used for code blocks, not inline code fragments. Those will be formatted as a code block and will end the paragraph. Attempting to use them inline will trigger a warning. diff --git a/src/parser/lexer.mll b/src/parser/lexer.mll index 0cde0b4343..6e678463d8 100644 --- a/src/parser/lexer.mll +++ b/src/parser/lexer.mll @@ -609,13 +609,16 @@ and code_span buffer nesting_level start_offset input = parse { Buffer.add_char buffer c; code_span buffer nesting_level start_offset input lexbuf } - | newline newline + | newline horizontal_space* (newline horizontal_space*)+ { warning input (Parse_error.not_allowed ~what:(Token.describe (`Blank_line "\n\n")) ~in_what:(Token.describe (`Code_span ""))); - Buffer.add_char buffer '\n'; + Buffer.add_char buffer ' '; + code_span buffer nesting_level start_offset input lexbuf } + | newline horizontal_space* + { Buffer.add_char buffer ' '; code_span buffer nesting_level start_offset input lexbuf } | eof diff --git a/src/parser/test/test.ml b/src/parser/test/test.ml index caed0e7f6e..5d74485c16 100644 --- a/src/parser/test/test.ml +++ b/src/parser/test/test.ml @@ -797,8 +797,7 @@ let%expect_test _ = {| ((output (((f.ml (1 0) (2 4)) - (paragraph (((f.ml (1 0) (2 4)) (code_span "foo\ - \nbar"))))))) + (paragraph (((f.ml (1 0) (2 4)) (code_span "foo bar"))))))) (warnings ())) |}] let cr_lf_preserved = @@ -807,8 +806,7 @@ let%expect_test _ = {| ((output (((f.ml (1 0) (2 4)) - (paragraph (((f.ml (1 0) (2 4)) (code_span "foo\r\ - \nbar"))))))) + (paragraph (((f.ml (1 0) (2 4)) (code_span "foo bar"))))))) (warnings ())) |}] let no_double_new_line = @@ -817,8 +815,7 @@ let%expect_test _ = {| ((output (((f.ml (1 0) (3 4)) - (paragraph (((f.ml (1 0) (3 4)) (code_span "foo\ - \nbar"))))))) + (paragraph (((f.ml (1 0) (3 4)) (code_span "foo bar"))))))) (warnings ( "File \"f.ml\", line 1, character 4 to line 3, character 0:\ \nBlank line is not allowed in '[...]' (code)."))) |}] @@ -829,8 +826,7 @@ let%expect_test _ = {| ((output (((f.ml (1 0) (3 4)) - (paragraph (((f.ml (1 0) (3 4)) (code_span "foo\ - \nbar"))))))) + (paragraph (((f.ml (1 0) (3 4)) (code_span "foo bar"))))))) (warnings ( "File \"f.ml\", line 1, character 4 to line 3, character 0:\ \nBlank line is not allowed in '[...]' (code)."))) |}] diff --git a/test/generators/cases/markup.mli b/test/generators/cases/markup.mli index 81d20ec473..233d122a60 100644 --- a/test/generators/cases/markup.mli +++ b/test/generators/cases/markup.mli @@ -57,6 +57,10 @@ Code can appear {b inside [other] markup}. Its display shouldn't be affected. + There is no differences between [a b] and [a + b]. + + Consecutive whitespaces not after a newline are conserved as they are: [a b]. {1 Links and references} diff --git a/test/generators/html/Markup.html b/test/generators/html/Markup.html index 056e53af61..5b71cd28ce 100644 --- a/test/generators/html/Markup.html +++ b/test/generators/html/Markup.html @@ -123,6 +123,11 @@
Code can appear inside other
markup. Its display
shouldn't be affected.
There is no differences between a b
and a b
.
+
Consecutive whitespaces not after a newline are conserved as they
+ are: a b
.
+