Skip to content

Commit abe6bf5

Browse files
panglesdjonludlam
authored andcommitted
Media: Fix loc and tests for media alt text
1 parent 6b7fbac commit abe6bf5

File tree

5 files changed

+229
-10
lines changed

5 files changed

+229
-10
lines changed

src/parser/loc.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ let span spans =
2323
let nudge_start offset span =
2424
{ span with start = { span.start with column = span.start.column + offset } }
2525

26+
let nudge_end offset span =
27+
{ span with end_ = { span.end_ with column = span.end_.column - offset } }
28+
2629
let spans_multiple_lines = function
2730
| {
2831
location =

src/parser/loc.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ val nudge_start : int -> span -> span
1919
(** This adjusts only the column number, implicitly assuming that the offset does
2020
not move the location across a newline character. *)
2121

22+
val nudge_end : int -> span -> span
23+
(** This adjusts only the column number, implicitly assuming that the offset does
24+
not move the location across a newline character. *)
25+
2226
(** {2 Located values} *)
2327

2428
type +'a with_location = { location : span; value : 'a }

src/parser/syntax.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,11 +1159,22 @@ let rec block_element_list :
11591159
Loc.nudge_start
11601160
(String.length @@ Token.s_of_media `Replaced media)
11611161
location
1162+
|> Loc.nudge_end (String.length content + 1)
1163+
(* +1 for closing character *)
11621164
in
1165+
let c_location =
1166+
Loc.nudge_start
1167+
(String.length (Token.s_of_media `Replaced media)
1168+
+ String.length (match href with `Reference s | `Link s -> s))
1169+
location
1170+
|> Loc.nudge_end 1
1171+
in
1172+
let content = String.trim content in
11631173
let href = href |> Loc.at r_location in
11641174

11651175
if content = "" then
1166-
Parse_error.should_not_be_empty ~what:(Token.describe token) location
1176+
Parse_error.should_not_be_empty ~what:(Token.describe token)
1177+
c_location
11671178
|> add_warning input;
11681179

11691180
let block = `Media (`Simple, href, content, media) in
@@ -1178,8 +1189,9 @@ let rec block_element_list :
11781189

11791190
let r_location =
11801191
Loc.nudge_start
1181-
(String.length @@ Token.s_of_media `Replaced media)
1192+
(String.length @@ Token.s_of_media `Simple media)
11821193
location
1194+
|> Loc.nudge_end 1
11831195
in
11841196
let href = href |> Loc.at r_location in
11851197
let block = `Media (`Simple, href, "", media) in

src/parser/test/test.ml

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,206 @@ let%expect_test _ =
19731973
end in
19741974
()
19751975

1976+
let%expect_test _ =
1977+
let module Medias = struct
1978+
let basic_simple =
1979+
test
1980+
"{image!foo}\n\n\
1981+
{audio!foo}\n\n\
1982+
{video!foo}\n\n\
1983+
{image:foo}\n\n\
1984+
{audio:foo}\n\n\
1985+
{video:foo}";
1986+
[%expect
1987+
{|
1988+
((output
1989+
(((f.ml (1 0) (1 11))
1990+
(simple ((f.ml (1 7) (1 10)) (Reference foo)) "" image))
1991+
((f.ml (3 0) (3 11))
1992+
(simple ((f.ml (3 7) (3 10)) (Reference foo)) "" audio))
1993+
((f.ml (5 0) (5 11))
1994+
(simple ((f.ml (5 7) (5 10)) (Reference foo)) "" video))
1995+
((f.ml (7 0) (7 11)) (simple ((f.ml (7 7) (7 10)) (Link foo)) "" image))
1996+
((f.ml (9 0) (9 11)) (simple ((f.ml (9 7) (9 10)) (Link foo)) "" audio))
1997+
((f.ml (11 0) (11 11))
1998+
(simple ((f.ml (11 7) (11 10)) (Link foo)) "" video))))
1999+
(warnings ())) |}]
2000+
2001+
let basic =
2002+
test
2003+
"{{image!foo}bar}\n\n\
2004+
{{audio!foo}bar}\n\n\
2005+
{{video!foo}bar}\n\n\
2006+
{{image:foo}bar}\n\n\
2007+
{{audio:foo}bar}\n\n\
2008+
{{video:foo}bar}";
2009+
[%expect
2010+
{|
2011+
((output
2012+
(((f.ml (1 0) (1 16))
2013+
(simple ((f.ml (1 8) (1 12)) (Reference foo)) bar image))
2014+
((f.ml (3 0) (3 16))
2015+
(simple ((f.ml (3 8) (3 12)) (Reference foo)) bar audio))
2016+
((f.ml (5 0) (5 16))
2017+
(simple ((f.ml (5 8) (5 12)) (Reference foo)) bar video))
2018+
((f.ml (7 0) (7 16)) (simple ((f.ml (7 8) (7 12)) (Link foo)) bar image))
2019+
((f.ml (9 0) (9 16)) (simple ((f.ml (9 8) (9 12)) (Link foo)) bar audio))
2020+
((f.ml (11 0) (11 16))
2021+
(simple ((f.ml (11 8) (11 12)) (Link foo)) bar video))))
2022+
(warnings ())) |}]
2023+
2024+
let empty =
2025+
test "{{image!foo}}";
2026+
[%expect
2027+
{|
2028+
((output
2029+
(((f.ml (1 0) (1 13))
2030+
(simple ((f.ml (1 8) (1 12)) (Reference foo)) "" image))))
2031+
(warnings
2032+
( "File \"f.ml\", line 1, characters 11-12:\
2033+
\n'{{image!...} ...}' (image-reference) should not be empty."))) |}]
2034+
2035+
let whitespace =
2036+
test "{{image!foo} }";
2037+
[%expect
2038+
{|
2039+
((output
2040+
(((f.ml (1 0) (1 16))
2041+
(simple ((f.ml (1 8) (1 12)) (Reference foo)) "" image))))
2042+
(warnings
2043+
( "File \"f.ml\", line 1, characters 11-15:\
2044+
\n'{{image!...} ...}' (image-reference) should not be empty."))) |}]
2045+
2046+
let trimming =
2047+
test "{{image!foo} hello }";
2048+
[%expect
2049+
{|
2050+
((output
2051+
(((f.ml (1 0) (1 27))
2052+
(simple ((f.ml (1 8) (1 12)) (Reference foo)) hello image))))
2053+
(warnings ())) |}]
2054+
2055+
let nested_markup_is_uninterpreted =
2056+
test "{{image!foo}{b bar}}";
2057+
[%expect
2058+
{|
2059+
((output
2060+
(((f.ml (1 0) (1 20))
2061+
(simple ((f.ml (1 8) (1 12)) (Reference foo)) "{b bar}" image))))
2062+
(warnings ())) |}]
2063+
2064+
let in_markup =
2065+
test "{ul {li {{image!foo}bar}}}";
2066+
[%expect
2067+
{|
2068+
((output
2069+
(((f.ml (1 0) (1 26))
2070+
(unordered heavy
2071+
((((f.ml (1 8) (1 24))
2072+
(simple ((f.ml (1 16) (1 20)) (Reference foo)) bar image))))))))
2073+
(warnings ())) |}]
2074+
2075+
let unterminated_image =
2076+
test "{{image!foo";
2077+
[%expect
2078+
{|
2079+
((output
2080+
(((f.ml (1 0) (1 11))
2081+
(simple ((f.ml (1 8) (1 10)) (Reference foo)) "" image))))
2082+
(warnings
2083+
( "File \"f.ml\", line 1, characters 0-11:\
2084+
\nOpen bracket '{{image!' is never closed."
2085+
"File \"f.ml\", line 1, characters 11-11:\
2086+
\nEnd of text is not allowed in '{{image!...} ...}' (image-reference)."
2087+
"File \"f.ml\", line 1, characters 11-10:\
2088+
\n'{{image!...} ...}' (image-reference) should not be empty."))) |}]
2089+
2090+
let unterminated_image_simple =
2091+
test "{image!foo";
2092+
[%expect
2093+
{|
2094+
((output
2095+
(((f.ml (1 0) (1 10))
2096+
(simple ((f.ml (1 7) (1 9)) (Reference foo)) "" image))))
2097+
(warnings
2098+
( "File \"f.ml\", line 1, characters 0-10:\
2099+
\nOpen bracket '{image!' is never closed."))) |}]
2100+
2101+
let unterminated_video =
2102+
test "{{video!foo";
2103+
[%expect
2104+
{|
2105+
((output
2106+
(((f.ml (1 0) (1 11))
2107+
(simple ((f.ml (1 8) (1 10)) (Reference foo)) "" video))))
2108+
(warnings
2109+
( "File \"f.ml\", line 1, characters 0-11:\
2110+
\nOpen bracket '{{video!' is never closed."
2111+
"File \"f.ml\", line 1, characters 11-11:\
2112+
\nEnd of text is not allowed in '{{video!...} ...}' (video-reference)."
2113+
"File \"f.ml\", line 1, characters 11-10:\
2114+
\n'{{video!...} ...}' (video-reference) should not be empty."))) |}]
2115+
2116+
let unterminated_video_simple =
2117+
test "{video!foo";
2118+
[%expect
2119+
{|
2120+
((output
2121+
(((f.ml (1 0) (1 10))
2122+
(simple ((f.ml (1 7) (1 9)) (Reference foo)) "" video))))
2123+
(warnings
2124+
( "File \"f.ml\", line 1, characters 0-10:\
2125+
\nOpen bracket '{video!' is never closed."))) |}]
2126+
2127+
let unterminated_audio =
2128+
test "{{audio!foo";
2129+
[%expect
2130+
{|
2131+
((output
2132+
(((f.ml (1 0) (1 11))
2133+
(simple ((f.ml (1 8) (1 10)) (Reference foo)) "" audio))))
2134+
(warnings
2135+
( "File \"f.ml\", line 1, characters 0-11:\
2136+
\nOpen bracket '{{audio!' is never closed."
2137+
"File \"f.ml\", line 1, characters 11-11:\
2138+
\nEnd of text is not allowed in '{{audio!...} ...}' (audio-reference)."
2139+
"File \"f.ml\", line 1, characters 11-10:\
2140+
\n'{{audio!...} ...}' (audio-reference) should not be empty."))) |}]
2141+
2142+
let unterminated_audio_simple =
2143+
test "{audio!foo";
2144+
[%expect
2145+
{|
2146+
((output
2147+
(((f.ml (1 0) (1 10))
2148+
(simple ((f.ml (1 7) (1 9)) (Reference foo)) "" audio))))
2149+
(warnings
2150+
( "File \"f.ml\", line 1, characters 0-10:\
2151+
\nOpen bracket '{audio!' is never closed."))) |}]
2152+
2153+
let unterminated_content =
2154+
test "{{image!foo} bar";
2155+
[%expect
2156+
{|
2157+
((output
2158+
(((f.ml (1 0) (1 16))
2159+
(simple ((f.ml (1 8) (1 11)) (Reference foo)) bar image))))
2160+
(warnings
2161+
( "File \"f.ml\", line 1, characters 16-16:\
2162+
\nEnd of text is not allowed in '{{image!...} ...}' (image-reference)."))) |}]
2163+
2164+
let newline_in_content =
2165+
test "{{image!foo} bar \n baz}";
2166+
[%expect
2167+
{|
2168+
((output
2169+
(((f.ml (1 0) (2 5))
2170+
(simple ((f.ml (1 8) (2 -6)) (Reference foo)) "bar \
2171+
\n baz" image))))
2172+
(warnings ())) |}]
2173+
end in
2174+
()
2175+
19762176
let%expect_test _ =
19772177
let module Link = struct
19782178
let basic =

test/pages/medias.t/run.t

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Testing the working references:
2929
<a href="caml.gif" class="img-link">
3030
<img src="caml.gif" alt="caml.gif"/>
3131
<a href="caml.gif" class="img-link">
32-
<img src="caml.gif" alt="With alt text and emphasis"/>
32+
<img src="caml.gif" alt="With alt text and {b emphasis}"/>
3333
<a href="https://picsum.photos/200/300" class="img-link">
3434
<img src="https://picsum.photos/200/300" alt="reference"/>
3535
<a href="https://picsum.photos/200/300" class="img-link">
@@ -53,21 +53,21 @@ Testing the working references:
5353
Testing the unresolved references:
5454

5555
$ cat html/pkg1/doc/index.html | grep xref-unresolved
56-
<div><span class="xref-unresolved"><code>./caqzdqzdml.gif</code></span>
57-
<span class="xref-unresolved">With alt text and <b>emphasis</b></span>
58-
<div><span class="xref-unresolved"><code>./module-x</code></span></div>
56+
<div><span class="xref-unresolved">./caqzdqzdml.gif</span></div>
57+
<div><span class="xref-unresolved">With alt text and {b emphasis}</span>
58+
<div><span class="xref-unresolved">./module-x</span></div>
5959

6060
Testing latex and manpages
6161

6262
$ odoc latex-generate -o latex _odoc/pkg1/doc/page-index.odocl
6363
$ cat latex/pkg1/doc/index.tex | grep gif
64-
\ocamlinlinecode{caml.\allowbreak{}gif}
65-
\ocamlinlinecode{.\allowbreak{}/caqzdqzdml.\allowbreak{}gif}
64+
caml.gif
65+
./caqzdqzdml.gif
6666

6767
$ odoc man-generate -o man _odoc/pkg1/doc/page-index.odocl
6868
$ cat man/pkg1/doc/index.3o | grep gif
6969
caml\.gif
7070
\./caqzdqzdml\.gif
7171
$ cat man/pkg1/doc/index.3o | grep "With alt text"
72-
With alt text and \fBemphasis\fR
73-
With alt text and \fBemphasis\fR
72+
With alt text and {b emphasis}
73+
With alt text and {b emphasis}

0 commit comments

Comments
 (0)