Skip to content

Commit

Permalink
Don't output anchor self-links for ANSI text output. More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukstafi committed Mar 7, 2024
1 parent 67dc1cf commit 3b314dd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 32 deletions.
33 changes: 14 additions & 19 deletions src/printbox-text/PrintBox_text.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ end = struct
let codes_of_style (self : t) : int list =
let { bold; fg_color; bg_color; preformatted = _ } = self in
(if bold then
[ 1 ]
else
[])
[ 1 ]
else
[])
@ (match bg_color with
| None -> []
| Some c -> [ 40 + int_of_color_ c ])
Expand Down Expand Up @@ -512,11 +512,6 @@ end = struct
lines_ s2 0 k;
List.iter (fun s -> lines_ s 0 k) tl

let is_empty b =
match B.view b with
| B.Empty -> true
| _ -> false

let rec of_box ~ansi (b : B.t) : t =
let shape =
match B.view b with
Expand All @@ -531,10 +526,7 @@ end = struct
| B.Align { h; v; inner } -> Align { h; v; inner = of_box ~ansi inner }
| B.Grid (bars, m) -> Grid (bars, B.map_matrix (of_box ~ansi) m)
| B.Tree (i, b, l) -> Tree (i, of_box ~ansi b, Array.map (of_box ~ansi) l)
| B.Anchor { id; inner } when is_empty inner ->
Text
{ l = []; style = B.Style.default; link_with_uri = Some ("#" ^ id) }
| (B.Link { inner; uri } | B.Anchor { inner; id = uri }) as b when ansi ->
| B.Link { inner; uri } as b when ansi ->
let uri =
match b with
| B.Link _ -> uri
Expand All @@ -557,21 +549,24 @@ end = struct
| B.Link _ | B.Anchor _ ->
(* Inner links override outer links. *)
(of_box ~ansi inner).shape
| B.Text _ ->
(match of_box ~ansi inner with
| { shape = Text { l; style; link_with_uri = _ }; size = _ } ->
Text { l; style; link_with_uri = Some uri }
| _ -> assert false))
| B.Text { l; style } ->
(* split into lines *)
let acc = ref [] in
lines_l_ l (fun s i len -> acc := (s, i, len) :: !acc);
Text { l = List.rev !acc; style; link_with_uri = Some uri })
| B.Link { inner; uri } ->
(* just encode as a record *)
let self =
of_box ~ansi (B.v_record [ "uri", B.text uri; "inner", inner ])
in
self.shape
| B.Anchor { inner; id } ->
(* just encode as a tag: {#ID} INNER. *)
(* Note: no support for self-links for now; just encode as a tag: {#ID} INNER. *)
let uri = "{#" ^ id ^ "}" in
let self =
of_box ~ansi (B.hlist ~bars:false [ B.line ("{#" ^ id ^ "}"); inner ])
match B.view inner with
| B.Text { l = [ s ]; _ } when s = uri -> of_box ~ansi @@ B.line uri
| _ -> of_box ~ansi (B.hlist ~bars:false [ B.line uri; inner ])
in
self.shape
in
Expand Down
103 changes: 91 additions & 12 deletions test/test_text_uri.expected
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Output with ANSI styling:
┌────────────────────────────────┐
│]8;;#SecondAnchor\Link to a within-document anchor]8;;\│
└────────────────────────────────┘
──────────────────────────────────
────────────────────────────────────────────────────
┌───────┐
│]8;;https://example.com/1\child 1]8;;\│
└───────┘
──────────────────────────────────
────────────────────────────────────────────────────
]8;;https://example.com/2\child 2]8;;\
──────────────────────────────────
────────────────────────────────────────────────────
┌──────────────────┐
│──┬────────┐ │
│ │]8;;https://example.com/4\header 3]8;;\│ │
Expand All @@ -16,18 +17,18 @@
│ │]8;;https://example.com/4\subchild 3]8;;\│ │
│ └──────────┘ │
└──────────────────┘
──────────────────────────────────
────────────────────────────────────────────────────
──┬────────┐
│]8;;https://example.com/5\header 4]8;;\│
├────────┘
└─┬──────────┐
│]8;;https://example.com/5\subchild 4]8;;\│
└──────────┘
──────────────────────────────────
────────────────────────────────────────────────────
┌───────┐
│child 5│
└───────┘
──────────────────────────────────
────────────────────────────────────────────────────
┌──────────────────┐
│┌────────┐ │
││]8;;https://example.com/6\header 6]8;;\│ │
Expand All @@ -39,10 +40,88 @@
│ │]8;;https://example.com/7\subchild 6]8;;\│ │
│ └──────────┘ │
└──────────────────┘
──────────────────────────────────
┌──────────────────┐
│]8;;#FirstAnchor\anchor self-link 1]8;;\│
└──────────────────┘
──────────────────────────────────
silent anchor
────────────────────────────────────────────────────
{#FirstAnchor}┌──────────────────┐
│anchor self-link 1│
└──────────────────┘
────────────────────────────────────────────────────
{#SecondAnchor}silent anchor
└─subchild 7
────────────────────────────────────────────────────
{#ThirdAnchor}anchor self-link 2 after anchor link 2
└─subchild 8
────────────────────────────────────────────────────
]8;;https://example.com/8\external link 8]8;;\ after external link 8
Output without ANSI styling:
uri │#SecondAnchor
─────┼──────────────────────────────────────────────
inner│┌────────────────────────────────┐
││Link to a within-document anchor│
│└────────────────────────────────┘
─────┼──────────────────────────────────────────────
uri │https://example.com/1
─────┼──────────────────────────────────────────────
inner│┌───────┐
││child 1│
│└───────┘
─────┼──────────────────────────────────────────────
uri │https://example.com/2
─────┼──────────────────────────────────────────────
inner│child 2
─────┴──────────────────────────────────────────────
┌─────┬────────────────────────┐
│uri │https://example.com/3 │
├─────┼───────────────────── │
│inner│ │
│└─uri │https://example.com/4 │
│ ─────┼───────────────────── │
│ inner│┌────────┐ │
│ ││header 3│ │
│ │├────────┘ │
│ │└─┬──────────┐ │
│ │ │subchild 3│ │
│ │ └──────────┘ │
└───────┴──────────────────────┘
─────┬──────────────────────────────────────────────
uri │https://example.com/5
─────┼──────────────────────────────────────────────
inner│──┬────────┐
│ │header 4│
│ ├────────┘
│ └─┬──────────┐
│ │subchild 4│
│ └──────────┘
─────┴──────────────────────────────────────────────
┌───────┐
│child 5│
└───────┘
─────┬──────────────────────────────────────────────
uri │https://example.com/6
─────┼──────────────────────────────────────────────
inner│┌─────────────────────────────────┐
││┌────────┐ │
│││header 6│ │
││├────────┘ │
││└─┬───────┐ │
││ │child 6│ │
││ ├──────┬┘ │
││ └─uri │https://example.com/7 │
││ ─────┼───────────────────── │
││ inner│┌──────────┐ │
││ ││subchild 6│ │
││ │└──────────┘ │
│└─────────┴───────────────────────┘
─────┴──────────────────────────────────────────────
{#FirstAnchor}┌──────────────────┐
│anchor self-link 1│
└──────────────────┘
────────────────────────────────────────────────────
{#SecondAnchor}silent anchor
└─subchild 7
────────────────────────────────────────────────────
{#ThirdAnchor}anchor self-link 2 after anchor link 2
└─subchild 8
─────┬──────────────────────────────────────────────
uri │https://example.com/8 after external link 8
─────┼─────────────────────
inner│external link 8
18 changes: 17 additions & 1 deletion test/test_text_uri.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ let b =
let open PrintBox in
vlist
[
link ~uri:"#SecondAnchor" @@ frame @@ text "Link to a within-document anchor";
link ~uri:"#SecondAnchor" @@ frame
@@ text "Link to a within-document anchor";
link ~uri:"https://example.com/1" @@ frame @@ text "child 1";
link ~uri:"https://example.com/2" @@ text "child 2";
frame
Expand Down Expand Up @@ -32,6 +33,21 @@ let b =
(hlist ~bars:false
[ anchor ~id:"SecondAnchor" empty; text "silent anchor" ])
[ text "subchild 7" ];
tree
(hlist ~bars:false
[
anchor ~id:"ThirdAnchor" @@ text "anchor self-link 2";
text " after anchor link 2";
])
[ text "subchild 8" ];
hlist ~bars:false
[
link ~uri:"https://example.com/8" @@ text "external link 8";
text " after external link 8";
];
]

let () = print_endline "Output with ANSI styling:"
let () = print_endline @@ PrintBox_text.to_string b
let () = print_endline "Output without ANSI styling:"
let () = print_endline @@ PrintBox_text.to_string_with ~style:false b

0 comments on commit 3b314dd

Please sign in to comment.