Skip to content

Commit

Permalink
Handle '+' to separate columns in 'align' row
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Petiot committed Feb 20, 2023
1 parent 497fe75 commit 1199bd4
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
(public_name odoc-parser)
(instrumentation
(backend bisect_ppx))
(inline_tests)
(preprocess
(pps ppx_expect))
(flags
(:standard -w -50))
(libraries astring result camlp-streams))
91 changes: 69 additions & 22 deletions src/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,80 @@ let peek input =

module Table = struct
module Light_syntax = struct
let split_on_plus w =
let len = String.length w in
match w with
| "+" -> [ "" ]
| _ when len > 1 ->
let plus = function '+' -> true | _ -> false in
let w =
if plus (String.get w 0) then String.sub w 1 (len - 1) else w
in
let len = String.length w in
let w =
if plus (String.get w (len - 1)) then String.sub w 0 (len - 1)
else w
in
String.split_on_char '+' w
| _ -> [ w ]

let%expect_test _ =
let f x =
let pp x = Printf.printf "%S " x in
List.iter pp (split_on_plus x)
in
f "";
[%expect {| "" |}];
f "+";
[%expect {| "" |}];
f "++";
[%expect {| "" |}];
f "+--+";
[%expect {| "--" |}];
f "--";
[%expect {| "--" |}];
f "--+--+--";
[%expect {| "--" "--" "--" |}];
f "+----+----+----+";
[%expect {| "----" "----" "----" |}]

let valid_align = function
| [ { Loc.value = `Word w; _ } ] -> (
match String.length w with
| 0 -> `Valid None
| 1 -> (
match w with
| "-" -> `Valid None
| ":" -> `Valid (Some `Center)
| _ -> `Invalid)
| len ->
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2)) then
match (String.get w 0, String.get w (len - 1)) with
| ':', ':' -> `Valid (Some `Center)
| ':', '-' -> `Valid (Some `Left)
| '-', ':' -> `Valid (Some `Right)
| '-', '-' -> `Valid None
| _ -> `Invalid
else `Invalid)
| _ -> `Invalid
| [ { Loc.value = `Word w; _ } ] ->
(* We consider [+----+----+----+] a valid row, as it is a common format. *)
let valid_word w =
match String.length w with
| 0 -> `Valid None
| 1 -> (
match w with
| "-" -> `Valid None
| ":" -> `Valid (Some `Center)
| _ -> `Invalid)
| len ->
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2))
then
match (String.get w 0, String.get w (len - 1)) with
| ':', ':' -> `Valid (Some `Center)
| ':', '-' -> `Valid (Some `Left)
| '-', ':' -> `Valid (Some `Right)
| '-', '-' -> `Valid None
| _ -> `Invalid
else `Invalid
in
List.map valid_word (split_on_plus w)
| _ -> [ `Invalid ]

let valid_align_row lx =
let rec loop acc = function
| [] -> Some (List.rev acc)
| x :: q -> (
match valid_align x with
| `Invalid -> None
| `Valid alignment -> loop (alignment :: acc) q)
| x :: q ->
let all_aligns = valid_align x in
let valid_aligns =
List.filter_map
(function `Valid a -> Some a | `Invalid -> None)
all_aligns
in
if List.(length valid_aligns < length all_aligns) then None
else loop (List.rev_append valid_aligns acc) q
in
loop [] lx

Expand Down
65 changes: 65 additions & 0 deletions test/test_tables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -751,5 +751,70 @@ let%expect_test _ =
(((f.ml (5 7) (5 8)) (paragraph (((f.ml (5 7) (5 8)) (word x)))))))))))
(align (default default))))))
(warnings ())) |}]

let with_pluses =
test
{|
{t
xx | yy | zz
---------+-----------+--------------
}

{t
xx | yy | zz
+---------+-----------+-------------+
}

{t
xx | yy | zz
+---------|-----------+-------------|
}
|};
[%expect
{|
((output
(((f.ml (2 8) (5 9))
(table (syntax light)
(data
((row
((header
(((f.ml (3 8) (3 10))
(paragraph (((f.ml (3 8) (3 10)) (word xx)))))))
(header
(((f.ml (3 19) (3 21))
(paragraph (((f.ml (3 19) (3 21)) (word yy)))))))
(header
(((f.ml (3 31) (3 33))
(paragraph (((f.ml (3 31) (3 33)) (word zz)))))))))))
(align (default default default))))
((f.ml (7 8) (10 9))
(table (syntax light)
(data
((row
((header
(((f.ml (8 9) (8 11))
(paragraph (((f.ml (8 9) (8 11)) (word xx)))))))
(header
(((f.ml (8 20) (8 22))
(paragraph (((f.ml (8 20) (8 22)) (word yy)))))))
(header
(((f.ml (8 32) (8 34))
(paragraph (((f.ml (8 32) (8 34)) (word zz)))))))))))
(align (default default default))))
((f.ml (12 8) (15 9))
(table (syntax light)
(data
((row
((header
(((f.ml (13 9) (13 11))
(paragraph (((f.ml (13 9) (13 11)) (word xx)))))))
(header
(((f.ml (13 20) (13 22))
(paragraph (((f.ml (13 20) (13 22)) (word yy)))))))
(header
(((f.ml (13 32) (13 34))
(paragraph (((f.ml (13 32) (13 34)) (word zz)))))))))))
(align (default default default))))))
(warnings ())) |}]
end in
()

0 comments on commit 1199bd4

Please sign in to comment.