Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize whitespaces in codespans #1085

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions doc/odoc_for_authors.mld
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions src/parser/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
gpetiot marked this conversation as resolved.
Show resolved Hide resolved
| newline horizontal_space*
{ Buffer.add_char buffer ' ';
code_span buffer nesting_level start_offset input lexbuf }

| eof
Expand Down
12 changes: 4 additions & 8 deletions src/parser/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand All @@ -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)."))) |}]
Expand All @@ -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)."))) |}]
Expand Down
4 changes: 4 additions & 0 deletions test/generators/cases/markup.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
5 changes: 5 additions & 0 deletions test/generators/html/Markup.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ <h6 id="subparagraph"><a href="#subparagraph" class="anchor"></a>
<p>Code can appear <b>inside <code>other</code> markup</b>. Its display
shouldn't be affected.
</p>
<p>There is no differences between <code>a b</code> and <code>a b</code>.
</p>
<p>Consecutive whitespaces not after a newline are conserved as they
are: <code>a b</code>.
</p>
<h2 id="links-and-references">
<a href="#links-and-references" class="anchor"></a>Links and references
</h2>
Expand Down
4 changes: 4 additions & 0 deletions test/generators/latex/Markup.tex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ \subsection{Styling\label{styling}}%

Code can appear \bold{inside \ocamlinlinecode{other} markup}. Its display shouldn't be affected.

There is no differences between \ocamlinlinecode{a b} and \ocamlinlinecode{a b}.

Consecutive whitespaces not after a newline are conserved as they are: \ocamlinlinecode{a b}.

\subsection{Links and references\label{links-and-references}}%
This is a \href{\#}{link}\footnote{\url{\#}}. It sends you to the top of this page. Links can have markup inside them: \href{\#}{\bold{bold}}\footnote{\url{\#}}, \href{\#}{\emph{italics}}\footnote{\url{\#}}, \href{\#}{\emph{emphasis}}\footnote{\url{\#}}, \href{\#}{super\textsuperscript{script}}\footnote{\url{\#}}, \href{\#}{sub\textsubscript{script}}\footnote{\url{\#}}, and \href{\#}{\ocamlinlinecode{code}}\footnote{\url{\#}}. Links can also be nested \emph{\href{\#}{inside}\footnote{\url{\#}}} markup. Links cannot be nested inside each other. This link has no replacement text: \href{\#}{\#}\footnote{\url{\#}}. The text is filled in by odoc. This is a shorthand link: \href{\#}{\#}\footnote{\url{\#}}. The text is also filled in by odoc in this case.

Expand Down
4 changes: 4 additions & 0 deletions test/generators/man/Markup.3o
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ It's possible for two markup elements to appear \fBnext\fR \fIto\fR each other a
This is also true between non-code markup and code\.
.sp
Code can appear \fBinside other markup\fR\. Its display shouldn't be affected\.
.sp
There is no differences between a b and a b\.
.sp
Consecutive whitespaces not after a newline are conserved as they are: a b\.
.nf
.sp
.in 3
Expand Down
2 changes: 1 addition & 1 deletion test/model/semantics/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ let%expect_test _ =
test "{2 [foo\nbar\r\nbaz]}";
[%expect
{|
{"value":[{"`Heading":[{"heading_level":"`Subsection","heading_label_explicit":"false"},{"`Label":[{"`Page":["None","f.ml"]},"foo-bar--baz"]},[{"`Code_span":"foo\nbar\r\nbaz"}]]}],"warnings":[]} |}]
{"value":[{"`Heading":[{"heading_level":"`Subsection","heading_label_explicit":"false"},{"`Label":[{"`Page":["None","f.ml"]},"foo-bar-baz"]},[{"`Code_span":"foo bar baz"}]]}],"warnings":[]} |}]

let nested_style =
test "{2 {e foo bar}}";
Expand Down
Loading