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

Markdown backend: complete implementation #35

Merged
merged 46 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
093e74e
Markdown backend: complete initial impl. Verified: frames and optiona…
lukstafi Jan 20, 2024
014a2da
Fix limitations of the summary field
lukstafi Jan 21, 2024
49446c7
Cleanup; auto-regenerate `test_md.expected.md`
lukstafi Jan 21, 2024
162ff2f
More configurability: `Config`, preformatted, frames
lukstafi Jan 21, 2024
6c9b91a
Fixed handling of indented code (preformatted with leading whitespace)
lukstafi Jan 21, 2024
05d5ca2
Fix missing tab setting; ensure breaking lines
lukstafi Jan 21, 2024
8795d98
Fix stylized-framed unfolded empty header trees
lukstafi Jan 21, 2024
3dfe5f0
Fix breaking into lines; table tests.
lukstafi Jan 21, 2024
6da1506
PrintBox_md tweaks: simulate preformatting with nbsp, more newlines w…
lukstafi Jan 21, 2024
f6d7410
`PrintBox_md`: Docu-comments
lukstafi Jan 22, 2024
41d382b
printbox-md: fix config -- missing printbox-html dependency
lukstafi Jan 22, 2024
b01eece
Backward compatibility fix `concat_map`
lukstafi Jan 22, 2024
703a711
Fix: escaping characters `<`, `>`
lukstafi Jan 23, 2024
8000604
Packages setup fixes
lukstafi Jan 23, 2024
b863a2d
Fix `printbox-md` dependencies
lukstafi Jan 23, 2024
3054ee5
printbox-md: support of `vlist`
lukstafi Jan 23, 2024
8c25cc6
Support for native Markdown tables, allowing framed cells
lukstafi Jan 24, 2024
8d63a26
Fix mismatch between `multiline_heuristic` and `line_of_length_exn`
lukstafi Jan 24, 2024
bc6ed56
Greatly improve `pp_print_nbsp` -> `pp_print_escaped`:
lukstafi Jan 24, 2024
235fd23
Style `white-space: pre` for verbatim line breaks
lukstafi Jan 24, 2024
1915c54
Remove markdown experiments
lukstafi Jan 24, 2024
e253bc6
`pp_string_escaped`: Fix counting spaces
lukstafi Jan 24, 2024
dcdf99e
Fix interleaving to prevent whitespace collapse, change approach for …
lukstafi Jan 24, 2024
261f5cb
Markdown export of `hlist` boxes; make `line_of_length` style-sensitive
lukstafi Jan 25, 2024
689ba38
Fewer redundant `white-space: pre`; fix spacing of `Stylized` hlists
lukstafi Jan 25, 2024
1586150
Use `[]` for frames in Markdown tables; more tests
lukstafi Jan 25, 2024
71601b6
Removing style 1: `white-space: pre` does not work with GitHub preview
lukstafi Jan 25, 2024
1883545
Get rid of `&nbsp;`, use `·` instead
lukstafi Jan 25, 2024
862eccf
Ensure empty line after `</div>` to return to Markdown
lukstafi Jan 25, 2024
4330d78
Separate positions not accepting blocks vs not accepting markdown
lukstafi Jan 26, 2024
b96443b
Missing `no_block` on hlists
lukstafi Jan 26, 2024
9fd1686
Experimentally using <div> to see if GitHub rendering picks up styles
lukstafi Jan 26, 2024
65e7e17
Revert "Experimentally using <div>" -- it's styles in general, not <div>
lukstafi Jan 26, 2024
9c10b74
`printbox-html`: Non-flushing formatting output
lukstafi Jan 26, 2024
628513f
`printbox-md`: remove styles
lukstafi Jan 26, 2024
5584258
Non-Markdown preformatted style
lukstafi Jan 26, 2024
939ad87
`printbox-md`: no style escape sequences when falling back on `printb…
lukstafi Jan 26, 2024
fab8407
Use Markdown's `"<br>" = " "`
lukstafi Jan 26, 2024
8e8b2fc
Fixes #34: add line breaks for multiline text
lukstafi Jan 26, 2024
3837a15
Multiline text in HTML fixed now
lukstafi Jan 26, 2024
a599d46
Bug fix: handle backticks in text and code
lukstafi Jan 26, 2024
407646f
Fixed native Markdown export bug
lukstafi Jan 26, 2024
9f3380b
`printbox-md`: README-generating tutorial
lukstafi Jan 26, 2024
249de66
README source link; note on GitHubPreview missing styles
lukstafi Jan 26, 2024
e1b96d3
Account for text styling when shaping Markdown tables
lukstafi Jan 26, 2024
f9ace94
Fix: forgot about the impact of removing the bold style
lukstafi Jan 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
run: opam pin -n .

- name: Depext
run: opam depext -yt printbox printbox-html printbox-text
run: opam depext -yt printbox printbox-html printbox-md printbox-text

- name: Deps
run: opam install -d . --deps-only
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
dune-cache: true
allow-prerelease-opam: true
- run: opam pin -n .
- run: opam depext -yt printbox printbox-html printbox-text
- run: opam depext -yt printbox printbox-html printbox-md printbox-text
- run: opam install -t . --deps-only
- run: opam exec -- dune build @all
- run: opam exec -- dune runtest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ an alternative solution where trees are printed in HTML using the
# print_endline PrintBox_html.(to_string
~config:Config.(tree_summary true default)
B.(tree (text "0")[text "1"; tree (text "ω") [text "ω²"]]));;
<div><details><summary><span class="">0</span></summary><ul><li><span class="">1</span></li><li><details><summary><span class="">ω</span></summary><ul><li><span class="">ω²</span></li></ul></details></li></ul></details></div>
<div><details><summary><span class="">0</span></summary><ul><li><div class="">1</div></li><li><details><summary><span class="">ω</span></summary><ul><li><div class="">ω²</div></li></ul></details></li></ul></details></div>

- : unit = ()
```
12 changes: 12 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ Printbox allows to print nested boxes, lists, arrays, tables in several formats"
(odoc :with-test)
(tyxml (>= 4.3))
(mdx (and (>= 1.4) :with-test))))

(package
(name printbox-md)
(synopsis "Printbox Markdown rendering")
(description "
Adds Markdown output handling to the printbox package, with fallback to text and simplified HTML.
Printbox allows to print nested boxes, lists, arrays, tables in several formats")
(depends (printbox (= :version))
(printbox-text (and (= :version)))
(printbox-html (and (= :version)))
(odoc :with-test)
(mdx (and (>= 1.4) :with-test))))
37 changes: 37 additions & 0 deletions printbox-md.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "0.8"
synopsis: "Printbox Markdown rendering"
description: """

Adds Markdown output handling to the printbox package, with fallback to text and simplified HTML.
Printbox allows to print nested boxes, lists, arrays, tables in several formats"""
maintainer: ["c-cube"]
authors: ["Simon Cruanes" "Guillaume Bury"]
license: "BSD-2-Clause"
homepage: "https://github.com/c-cube/printbox"
bug-reports: "https://github.com/c-cube/printbox/issues"
depends: [
"dune" {>= "3.0"}
"printbox" {= version}
"printbox-text" {= version}
"printbox-html" {= version}
"odoc" {with-test}
"mdx" {>= "1.4" & with-test}
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/c-cube/printbox.git"
34 changes: 29 additions & 5 deletions src/printbox-html/PrintBox_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,33 @@ end

let to_html_rec ~config (b: B.t) =
let open Config in
let text_to_html ?(border=false) ~l ~style () =
let h_text_to_html ?(border=false) ~l ~style () =
let a, bold = attrs_of_style style in
let l = List.map H.txt l in
let l = if bold then List.map (fun x->H.b [x]) l else l in
let a_border = if border then [H.a_style "border:thin solid"] else [] in
H.span ~a:(H.a_class config.cls_text :: a_border @ (a @ config.a_text)) l in
let v_text_to_html ?(border=false) ~l ~style () =
let a, bold = attrs_of_style style in
let a_border = if border then [H.a_style "border:thin solid"] else [] in
if style.B.Style.preformatted then
H.pre ~a:(H.a_class config.cls_text :: a_border @ (a @ config.a_text))
[H.txt @@ String.concat "\n" l]
else
(* TODO: remove possible trailing '\r' *)
let l = List.map H.txt @@ List.concat @@ List.map (String.split_on_char '\n') l in
let len = List.length l in
let l =
List.concat @@ List.mapi
(fun i x-> (if bold then H.b [x] else x) :: if i < len - 1 then [H.br ()] else []) l in
H.div ~a:(H.a_class config.cls_text :: a_border @ (a @ config.a_text)) l in
let loop :
'tags. (B.t -> ([< Html_types.flow5 > `Pre `Span `Div `Ul `Table `P] as 'tags) html) -> B.t -> 'tags html =
fun fix b ->
match B.view b with
| B.Empty -> (H.div [] :> [< Html_types.flow5 > `Pre `Span `Div `P `Table `Ul ] html)
| B.Text {l; style} when style.B.Style.preformatted -> H.pre [text_to_html ~l ~style ()]
| B.Text {l; style} -> text_to_html ~l ~style ()
(* | B.Text {l; style} when style.B.Style.preformatted -> H.pre [h_text_to_html ~l ~style ()] *)
| B.Text {l; style} -> v_text_to_html ~l ~style ()
| B.Pad (_, b) -> fix b
| B.Frame b ->
H.div ~a:[H.a_style "border:thin solid"] [ fix b ]
Expand Down Expand Up @@ -137,12 +151,12 @@ let to_html_rec ~config (b: B.t) =
let l = Array.to_list l in
(match B.view b with
| B.Text {l=tl; style} ->
H.details (H.summary [text_to_html ~l:tl ~style ()])
H.details (H.summary [h_text_to_html ~l:tl ~style ()])
[ H.ul (List.map (fun x -> H.li [to_html_rec x]) l) ]
| B.Frame b ->
(match B.view b with
| (B.Text {l=tl; style}) ->
H.details (H.summary [text_to_html ~border:true ~l:tl ~style ()])
H.details (H.summary [h_text_to_html ~border:true ~l:tl ~style ()])
[ H.ul (List.map (fun x -> H.li [to_html_rec x]) l) ]
| _ ->
H.div
Expand All @@ -159,6 +173,7 @@ let to_html_rec ~config (b: B.t) =
| _ -> loop to_html_rec b
and to_html_nondet_rec b =
match B.view b with
| B.Text {l; style} -> v_text_to_html ~l ~style ()
| B.Link {uri; inner} ->
H.div [H.a ~a:[H.a_href uri] [to_html_nondet_rec inner]]
| _ -> loop to_html_nondet_rec b
Expand All @@ -170,6 +185,15 @@ let to_html ?(config=Config.default) b = H.div [to_html_rec ~config b]
let to_string ?config b =
Format.asprintf "@[%a@]@." (H.pp_elt ()) (to_html ?config b)

let to_string_indent ?config b =
Format.asprintf "@[%a@]@." (H.pp_elt ~indent:true ()) (to_html ?config b)

let pp ?(flush=true) ?config ?indent () pp b =
if flush then
Format.fprintf pp "@[%a@]@." (H.pp_elt ?indent ()) (to_html ?config b)
else
Format.fprintf pp "@[%a@]" (H.pp_elt ?indent ()) (to_html ?config b)

let to_string_doc ?config b =
let meta_str = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">" in
let footer_str =
Expand Down
5 changes: 5 additions & 0 deletions src/printbox-html/PrintBox_html.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ end
val to_html : ?config:Config.t -> PrintBox.t -> [`Div] html
(** HTML for one box *)

val pp :
?flush:bool -> ?config:Config.t -> ?indent:bool -> unit -> Format.formatter -> PrintBox.t -> unit

val to_string : ?config:Config.t -> PrintBox.t -> string

val to_string_indent : ?config:Config.t -> PrintBox.t -> string

val to_string_doc : ?config:Config.t -> PrintBox.t -> string
(** Same as {!to_string}, but adds the prelude and some footer *)