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

blockquotes: use html form instead of verbatim block #7

Merged
merged 1 commit into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let rec add_to_buffer buf = function
| TripleSurround (s1, t1, s2, t2, s3) ->
Printf.bprintf buf "%s%a%s%a%s" s1 add_to_buffer t1 s2 add_to_buffer t2 s3
| Surround (s1, t1, s2) -> Printf.bprintf buf "%s%a%s" s1 add_to_buffer t1 s2
| BlockSurround (s1, t1, s2) -> Printf.bprintf buf "\n\b%s%a%s\n" s1 add_to_buffer t1 s2
| BlockSurround (s1, t1, s2) -> Printf.bprintf buf "\n%s%a%s\n" s1 add_to_buffer t1 s2
| GeneralBlock t -> Printf.bprintf buf "\n%a\n" add_to_buffer t
| Text s -> Buffer.add_string buf s
| Raw s -> Buffer.add_string buf s
Expand Down Expand Up @@ -150,9 +150,19 @@ let rec inline ({ il_desc; il_attributes = _ } : Omd.inline) =
Surround ("{%html: ", text img, "%}")


let rec block min_head_lvl ({ bl_desc; bl_attributes = _attr } : Omd.block) =
type ctx =
{ min_head_lvl : int
; in_html : bool
}

let rec block ctx ({ bl_desc; bl_attributes = _attr } : Omd.block) =
match bl_desc with
| Blockquote q -> BlockSurround ("{v ", concat_map (block min_head_lvl) q, "v}")
| Blockquote q ->
let html =
BlockSurround
("<blockquote>", concat_map (block { ctx with in_html = true }) q, "</blockquote>")
in
if ctx.in_html then html else Surround ("{%html: ", html, "%}")
| Paragraph md -> GeneralBlock (inline md)
| List (ty, sp, bl) ->
let sign =
Expand All @@ -164,7 +174,7 @@ let rec block min_head_lvl ({ bl_desc; bl_attributes = _attr } : Omd.block) =
let block' (t : Omd.block) =
match t.bl_desc, sp with
| Paragraph t, Tight -> concat (inline t) nl
| _ -> block min_head_lvl t
| _ -> block ctx t
in
let nl = if sp = Tight then Null else nl in
Surround (sign, concat nl (concat_map block' t), "")
Expand All @@ -176,7 +186,7 @@ let rec block min_head_lvl ({ bl_desc; bl_attributes = _attr } : Omd.block) =
| Heading (level, text) ->
BlockSurround
( "{"
^ (match level + min_head_lvl with
^ (match level + ctx.min_head_lvl with
| 1 -> "0"
| 2 -> "1"
| 3 -> "2"
Expand All @@ -189,7 +199,9 @@ let rec block min_head_lvl ({ bl_desc; bl_attributes = _attr } : Omd.block) =
| Definition_list _ -> Null


let of_doc ?(min_head_lvl = 0) doc = concat_map (block min_head_lvl) doc
let of_doc ?(min_head_lvl = 0) doc =
concat_map (block { min_head_lvl; in_html = false }) doc


let to_string t =
let buf = Buffer.create 1024 in
Expand Down
8 changes: 4 additions & 4 deletions tests/readme-min-header-3.expected
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{3 Convert md files into odoc mld files}
{3 Convert md files into odoc mld files}

[md2mld] converts a Markdown-format file into the [mld] format used by {{: https://github.com/ocaml/odoc} odoc} to render HTML documentation or OCaml libraries. You can use this script to automatically embed a [README.md] file into API documentation for an OCaml library.

You can use it manually as follows

{[
{[
$ md2mld filename.md > outfile.mld
]}

In [dune] you can use it to generate an mld file with

{[
{[
(rule (with-stdout-to outfile.mld (run md2mld filename.md)))
]}

You can see the documentation generated from the latest tagged version of this README at {{: http://mseri.github.io/md2mld/md2mld/index.html} mseri.github.io/md2mld/md2mld/index.html}.

{3 Known issues}
{3 Known issues}

Until the new odoc {{: https://github.com/ocaml/odoc/issues/141} fixing #141} is released, the minimal header allowed in the [md] file will be the level 3 one [###].
You can work around this by using the [-min-header 3] flag during the invocation of [md2mld].
8 changes: 4 additions & 4 deletions tests/readme.expected
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{0 Convert md files into odoc mld files}
{0 Convert md files into odoc mld files}

[md2mld] converts a Markdown-format file into the [mld] format used by {{: https://github.com/ocaml/odoc} odoc} to render HTML documentation or OCaml libraries. You can use this script to automatically embed a [README.md] file into API documentation for an OCaml library.

You can use it manually as follows

{[
{[
$ md2mld filename.md > outfile.mld
]}

In [dune] you can use it to generate an mld file with

{[
{[
(rule (with-stdout-to outfile.mld (run md2mld filename.md)))
]}

You can see the documentation generated from the latest tagged version of this README at {{: http://mseri.github.io/md2mld/md2mld/index.html} mseri.github.io/md2mld/md2mld/index.html}.

{0 Known issues}
{0 Known issues}

Until the new odoc {{: https://github.com/ocaml/odoc/issues/141} fixing #141} is released, the minimal header allowed in the [md] file will be the level 3 one [###].
You can work around this by using the [-min-header 3] flag during the invocation of [md2mld].
20 changes: 10 additions & 10 deletions tests/test.expected
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{0 this is a markdown test document}
{0 this is a markdown test document}

This is {e important} or {b very important}, let's try the [code] fences.

{[
{[
let x = "bla"
]}

Expand All @@ -18,21 +18,21 @@ And this is a unordered list
- Two
- Three

{1 Another header}
{1 Another header}

Blah blah blah

{v
{%html:
<blockquote>
This is some quote

{v
<blockquote>
This is a nested quote
v}
v}

</blockquote>
</blockquote>
%}
And this is a link {{: https://ocaml.org} Hello}.

{[
{[
another code block?
]}

Expand Down