Skip to content

Commit

Permalink
Fix: escaping characters <, >
Browse files Browse the repository at this point in the history
  • Loading branch information
lukstafi committed Jan 23, 2024
1 parent b01eece commit 703a711
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
22 changes: 17 additions & 5 deletions src/printbox-md/PrintBox_md.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,23 @@ let break_lines l =
else Some s)
lines
let pp_string_nbsp ~tab_width ~code_block ~code_quote ~infix out s =
let pp_string_nbsp ~tab_width ~code_block ~code_quote ~html ~infix out s =
(* TODO: benchmark if writing to a buffer first would be more efficient. *)
let open Format in
if code_block then pp_print_string out s
else
let print_sp nbsp =
pp_print_string out (if nbsp then "&nbsp;" else " ") in
let print_char =
if html then
fun c ->
pp_print_string out @@
match c with '<' -> "&lt;" | '>' -> "&gt;" | '&' -> "&amp;" | c -> String.make 1 c
else
fun c ->
pp_print_string out @@
(* TODO: consider extending this list, but maybe we shouldn't be too eager? *)
match c with '<' -> "\\<" | '>' -> "\\>" | '*' -> "\\*" | c -> String.make 1 c in
let len = String.length s in
let check i = i < len && (s.[i] = ' ' || s.[i] = '\t') in
let i = ref 0 in
Expand All @@ -113,10 +124,11 @@ let pp_string_nbsp ~tab_width ~code_block ~code_quote ~infix out s =
k := !i;
if code_quote then (
i := len;
(* TODO: escape the backtick `. *)
pp_print_string out @@ String.sub s !k (len - !k);)
else (
while !i < len && (not @@ check !i) do incr i done;
if !k < len then pp_print_string out @@ String.sub s !k (!i - !k);
while !i < len && (not @@ check !i) do print_char s.[!i]; incr i done;
(* if !k < len then pp_print_string out @@ String.sub s !k (!i - !k); *)
)
done
Expand All @@ -134,7 +146,6 @@ let rec multiline_heuristic b =
Array.length children > 0 || multiline_heuristic header
| B.Link {inner; _} -> multiline_heuristic inner
let pp c out b =
let open Format in
(* We cannot use Format for indentation, because we need to insert ">" at the right places. *)
Expand All @@ -146,7 +157,8 @@ let pp c out b =
let multiline = List.length l > 1 in
let sty_pre, sty_post, code_block, code_quote, inline =
style_format c ~in_span ~multiline style in
let preformat = pp_string_nbsp ~tab_width:c.Config.tab_width ~code_block ~code_quote in
let preformat =
pp_string_nbsp ~tab_width:c.Config.tab_width ~code_block ~code_quote ~html:in_span in
pp_print_string out sty_pre;
if not inline && String.length sty_pre > 0 then fprintf out "@,%s" prefix;
if code_block then fprintf out "```@,%s" prefix;
Expand Down
2 changes: 2 additions & 0 deletions src/printbox-md/playground.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@ Trying this <pre>one-line code</pre> is not inline.
multiline<br>
&nbsp;&nbsp;code line 1<br>
&nbsp; code line 2</span>

What happens with <returns> say & also `<returns>` html-ish syntax? &lt; and &gt; -- hmm &amp; only if it's &something; recognized?
26 changes: 21 additions & 5 deletions test/test_md.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Test default:
> - > subchild 3
-
- > header 4
- subchild 4
- \<returns\>
- `<nothing>`
- & \*subchild\* 4
- > `header 5`
> - ```
> subchild 5
Expand Down Expand Up @@ -54,7 +56,9 @@ Test uniform unfolded:
</div>
-
- <span style="border:thin solid">header 4</span>
- subchild 4
- \<returns\>
- <span style="font-family: monospace">\<nothing\></span>
- & \*subchild\* 4
- <div style="border:thin solid">

<span style="font-family: monospace">header 5</span>
Expand Down Expand Up @@ -130,7 +134,11 @@ Test foldable:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>

- `<nothing>`
</details>
- & \*subchild\* 4
</details>
</details>
- > <details><summary><span style="font-family: monospace">header 5</span></summary>
Expand Down Expand Up @@ -185,7 +193,11 @@ Test uniform tab=2, text tables:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>

- <span style="font-family: monospace">\<nothing\></span>
</details>
- & \*subchild\* 4
</details>
</details>
- <div style="border:thin solid">
Expand Down Expand Up @@ -247,7 +259,11 @@ Test single quote tab=2, text tables:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>

- <span style="font-family: monospace">\<nothing\></span>
</details>
- & \*subchild\* 4
</details>
</details>
- <div style="border:thin solid">
Expand Down
26 changes: 21 additions & 5 deletions test/test_md.expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Test default:
> - > subchild 3
-
- > header 4
- subchild 4
- \<returns\>
- `<nothing>`
- & \*subchild\* 4
- > `header 5`
> - ```
> subchild 5
Expand Down Expand Up @@ -54,7 +56,9 @@ Test uniform unfolded:
</div>
-
- <span style="border:thin solid">header 4</span>
- subchild 4
- \<returns\>
- <span style="font-family: monospace">\<nothing\></span>
- & \*subchild\* 4
- <div style="border:thin solid">

<span style="font-family: monospace">header 5</span>
Expand Down Expand Up @@ -130,7 +134,11 @@ Test foldable:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>
- `<nothing>`
</details>
- & \*subchild\* 4
</details>
</details>
- > <details><summary><span style="font-family: monospace">header 5</span></summary>
Expand Down Expand Up @@ -185,7 +193,11 @@ Test uniform tab=2, text tables:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>
- <span style="font-family: monospace">\<nothing\></span>
</details>
- & \*subchild\* 4
</details>
</details>
- <div style="border:thin solid">
Expand Down Expand Up @@ -247,7 +259,11 @@ Test single quote tab=2, text tables:

- <details><summary><span style="border:thin solid">header 4</span></summary>

- subchild 4
- <details><summary>&lt;returns&gt;</summary>
- <span style="font-family: monospace">\<nothing\></span>
</details>
- & \*subchild\* 4
</details>
</details>
- <div style="border:thin solid">
Expand Down
4 changes: 3 additions & 1 deletion test/test_md.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ let b =
tree (frame @@ text "header 3") [frame @@ text "subchild 3"]
];
tree empty [
tree (frame @@ text "header 4") [text "subchild 4"]
tree (frame @@ text "header 4") [
tree (text "<returns>") [text_with_style Style.preformatted "<nothing>"];
text "& *subchild* 4"]
];
frame @@ tree (text_with_style Style.preformatted "header 5")
[lines_with_style Style.preformatted
Expand Down

0 comments on commit 703a711

Please sign in to comment.