Skip to content

Commit 52c4e7d

Browse files
committed
TMP: Add an MDX manual using odoc .mld files
Signed-off-by: Nathan Rebours <[email protected]>
1 parent 9150bcd commit 52c4e7d

File tree

9 files changed

+207
-0
lines changed

9 files changed

+207
-0
lines changed

doc/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(documentation
2+
(package mdx))

doc/dune_stanza.mld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{0 Dune Stanza}
2+

doc/index.mld

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{0 MDX Manual}
2+
3+
Welcome to the MDX Manual!
4+
5+
MDX is a tool to help developpers and authors keep their documentation
6+
up-to-date.
7+
It ensures that the code examples you write compile and behave the way you
8+
expect them to by actually running them.
9+
10+
It is for example used to verify all of
11+
{{:https://dev.realworldocaml.org/}Realworlocaml}'s examples!
12+
13+
MDX is released on opam. You can install it in your switch by running:
14+
{@sh[
15+
opam install mdx
16+
]}
17+
18+
{1 Basic Usage}
19+
20+
You can use MDX with your Markdown or [.mli] documentation, where it will ensure
21+
the correctness of code respectively written in multi-line code blocks and
22+
verbatim code blocks.
23+
24+
To enable MDX, you need to add [(mdx)] stanzas to the right dune files. Before
25+
that you will need to enable MDX for your project by adding the following to
26+
your [dune-project]:
27+
28+
{@dune[
29+
(using mdx 0.2)
30+
]}
31+
32+
You can then add the following in the relevant dune file:
33+
{@dune[
34+
(mdx)
35+
]}
36+
That will enable MDX on all markdown files in the folder.
37+
The MDX stanza can be further configured. If you want to know more about it,
38+
visit the {{!page-mdx_dune_stanza}corresponding section of this manual} or the
39+
one in
40+
{{:https://dune.readthedocs.io/en/latest/dune-files.html#mdx-since-2-4}dune's manual}.
41+
42+
MDX supports various type of code blocks but the most common are OCaml toplevel
43+
blocks so we'll look at one of those for our example. In a markdown file, you
44+
would write something along those lines:
45+
46+
{@markdown[
47+
Let's look at how good OCaml is with integers and strings:
48+
```ocaml
49+
# 1 + 2;;
50+
- : int = 2
51+
# "a" ^ "bc";;
52+
- : string = "ab"
53+
```
54+
]}
55+
56+
The content of the toplevel blocks looks just like an interactive toplevel
57+
session. Phrases, i.e. the toplevel "input", start with a [#] and end with [;;].
58+
Each of them is followed by the toplevel evaluation, or "output" which you
59+
probably are already familiar with.
60+
61+
Now you probably have noticed that [1 + 2] is not equal to [3] nor ["a" ^ "bc"]
62+
to ["ab"]. Somebody must have updated the phrases but they then forgot to update
63+
the evaluation.
64+
65+
That's exactly what MDX is here for!
66+
67+
If MDX were enabled for this file and they ran [dune runtest], here's what they
68+
would have gotten:
69+
70+
{@sh[
71+
$ dune runtest
72+
File "README.md", line 1, characters 0-0:
73+
git (internal) (exit 1)
74+
(cd _build/default && /usr/bin/git --no-pager diff --no-index --color=always -u README.md .mdx/README.md.corrected)
75+
diff --git a/README.md b/.mdx/README.md.corrected
76+
index 181b86f..458ecec 100644
77+
--- a/README.md
78+
+++ b/.mdx/README.md.corrected
79+
@@ -1,13 +1,13 @@
80+
Let's look at how good OCaml is with integers and strings:
81+
```ocaml
82+
# 1 + 2;;
83+
-- : int = 2
84+
+- : int = 3
85+
# "a" ^ "bc";;
86+
-- : string = "ab"
87+
+- : string = "abc"
88+
```
89+
]}
90+
91+
The test run just failed and dune is showing the diff between what we have
92+
locally and what should be, according to MDX.
93+
This uses dune's promotion workflow so at this point you can either investigate
94+
it further if you're surprised by this diff or if you're happy with it, simply
95+
accept it by running:
96+
97+
{@sh[
98+
dune promote
99+
]}
100+
101+
Now the documentation is up-to-date and running [dune runtest] again should be
102+
successful!
103+
104+
{1 Table of Content}
105+
- {{!page-markdown_syntax}Markdown MDX Syntax}
106+
- {{!page-mli_syntax}.mli MDX Syntax}
107+
- {{!page-types_of_blocks}Types of Blocks}
108+
- {{!page-labels}Labels}
109+
- {{!page-dune_stanza}Dune stanza}
110+
- {{!page-preludes}Preludes}
111+
- {{!page-using_libs}Using Libraries in your code examples}

doc/labels.mld

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{0 Labels}
2+
3+
MDX blocks behaviour can be customised through the use of labels.
4+
5+
This section documents in detail what each existing label does and how to use
6+
it.
7+
8+
{1:env Env}
9+
10+
{2 Description}
11+
12+
This label allows you to assign an environment to an OCaml block. That means
13+
you benefit from whole the code that has been previously evaluated in that
14+
environment, be it from other code blocks or {{!page-preludes}preludes}.
15+
16+
{2 Syntax}
17+
18+
[env=<value>]
19+
20+
The environement label expects a single string value which corresponds to the
21+
name of the environment to use.
22+
23+
[.mli] example:
24+
{v
25+
(** Here is how to use this function:
26+
{@ocaml env=foo[
27+
# f 0;;
28+
- : int = 0
29+
]} *)
30+
v}
31+
32+
[.md] example:
33+
{@markdown[
34+
Here is how to use this function:
35+
<!-- $MDX env=foo -->
36+
```ocaml
37+
# f 0;;
38+
- : int = 0
39+
```
40+
]}
41+
42+
{2 Applies to}
43+
44+
- {{!page-types_of_blocks.ocaml_toplevel} OCaml Toplevel Blocks}
45+
- {{!page-types_of_blocks.ocaml} OCaml Blocks}
46+
47+
{2 Default}
48+
49+
When absent, the block will be evaluated in the default environment, e.g.
50+
the environment shared by all blocks without an [env] label.

doc/markdown_syntax.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

doc/mli_syntax.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

doc/preludes.mld

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{0 Preludes}
2+
3+
Preludes are fragments of code that can be evaluated ahead of running the
4+
code in OCaml and OCaml toplevel MDX blocks. They can be used to globally
5+
open modules, register toplevel printers, set some globals like
6+
[Printexc.record_backtrace], you name it!
7+
8+
They are especially useful if you want to hide or share this kind of setup
9+
phase, e.g. if you are writing polished documentation you want to publish
10+
or you are using MDX to add tests in your mli files directly.
11+
12+
TODO

doc/types_of_blocks.mld

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{0 Types of Blocks}
2+
3+
MDX searches for code blocks within your documentation to make sure they are up
4+
to date.
5+
It supports different type of block contents, each have their own behaviour.
6+
7+
If a code block does not belong to one of the categories described below, MDX
8+
will simply ignore it.
9+
10+
The type of a block can either be set explicitly using a label, otherwise MDX
11+
will try to infer the type of the block based on its language tag, labels and
12+
content.
13+
Setting the block type explicitly allows MDX to better report syntax errors
14+
or invalid labels.
15+
16+
{1:ocaml_toplevel OCaml Toplevel Blocks}
17+
18+
OCaml Toplevel Blocks are composed of a sequence of toplevel phrases, starting
19+
with a [#] and a space and ending with [;;] followed by the output of the
20+
toplevel evaluation of the phrase.
21+
22+
23+
{1:ocaml OCaml Blocks}
24+
25+
{1:file_include File Include Blocks}
26+
27+
{1:shell Shell Blocks}

doc/using_libs.mld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO

0 commit comments

Comments
 (0)