-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathliterate.fsx
287 lines (208 loc) · 10.5 KB
/
literate.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
(**
---
category: Documentation
categoryindex: 1
index: 3
---
*)
(*** condition: prepare ***)
#nowarn "211"
#I "../src/FSharp.Formatting/bin/Release/netstandard2.0"
#r "FSharp.Formatting.Common.dll"
#r "FSharp.Formatting.Markdown.dll"
#r "FSharp.Formatting.CodeFormat.dll"
#r "FSharp.Formatting.Literate.dll"
(*** condition: fsx ***)
#if FSX
#r "nuget: FSharp.Formatting,{{fsdocs-package-version}}"
#endif // FSX
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: FSharp.Formatting,{{fsdocs-package-version}}"
#endif // IPYNB
(**
[](https://mybinder.org/v2/gh/fsprojects/fsharp.formatting/gh-pages?filepath={{fsdocs-source-basename}}.ipynb) 
[]({{root}}/{{fsdocs-source-basename}}.fsx) 
[]({{root}}/{{fsdocs-source-basename}}.ipynb)
Literate Scripts
================
The following example shows most of the features that can be used in a literate
F# script file with `.fsx` extension. Most of the features should be quite self-explanatory:
(**
# First-level heading
Some more documentation using `Markdown`.
*)
let helloWorld() = printfn "Hello world!"
(**
## Second-level heading
With some more documentation
*)
let numbers = [ 0 .. 99 ]
(*** include-value: numbers ***)
List.sum numbers
(*** include-it ***)
The F# script files is processed as follows:
- A multi-line comment starting with `(**` and ending with `*)` is
turned into text and is processed using the F# Markdown processor
(which supports standard Markdown commands).
- A single-line comment starting with `(***` and ending with `***)`
is treated as a special command. The command can consist of
`key`, `key: value` or `key=value` pairs.
| Literate Command | Description |
|:-----------------------|:----------------------------|
| `(** ... *)` | Markdown |
| `(*** condition: prepare ***)` | Utilise a code snippet when analyzing for tooltips or executing for outputs |
| `(*** condition: ipynb ***)` | Include a code snippet when making a .ipynb notebook |
| `(*** condition: tex ***)` | Include a code snippet when making a .tex output |
| `(*** condition: html ***)` | Include a code snippet when making HTML output |
| `(*** hide ***)` | Hide the subsequent snippet |
| `(*** raw ***)` | The subsequent code is treated as raw text |
### Naming and including snippets
The command `define` defines a named snippet (such as `final-sample`) and removes the command together with
the following F# code block from the main document. The snippet can then
be referred to in 'include'. This makes it
possible to write documents without the ordering requirements of the
F# language.
| Literate Command | Description |
|:-----------------------|:----------------------------|
| `(*** define: snippet-name ***)` | Define a named snippet |
| `(*** include: snippet-name ***)` | Include the code of the named snippet |
### Naming and including outputs
| Literate Command | Description |
|:-----------------------|:----------------------------|
| `(*** define-output: output-name ***)` | Define a name for the outputs of the preceding snippet |
| `(*** include-output ***)` | The console output of the preceding snippet |
| `(*** include-output: output-name ***)` | The console output of the snippet (named with define-output) |
| `(*** include-fsi-output ***)` | The F# Interactive output of the preceding snippet |
| `(*** include-fsi-output: output-name ***)` | The F# Interactive output of the snippet (named with define-output) |
| `(*** include-fsi-merged-output ***)` | The merge of console output and F# Interactive output of the preceding snippet |
| `(*** include-fsi-merged-output: output-name ***)` | The merge of console output and F# Interactive output of the snippet (named with define-output) |
| `(*** include-it ***)` | The formatted result of the preceding snippet |
| `(*** include-it: output-name ***) ` | The formatted result of the snippet (named with define-output) |
| `(*** include-it-raw ***)` | The unformatted result of the preceding snippet |
| `(*** include-it-raw: output-name ***)` | The unformatted result of the snippet (named with define-output) |
| `(*** include-value: value-name ***)` | The formatted value, an F# identifier name |
#### Hiding code snippets
The command `hide` specifies that the following F# code block (until the next comment or command) should be
omitted from the output.
#### Evaluating and formatting results
The commands to evaluate and format results are explained in [evaluation](evaluation.html).
You must build your documentation with evaluation turned on using `--eval`.
#### Substitutions
Substitutions are applied to content, see [content](content.html).
### Literate Markdown Documents
For files with `.md` extension, the entire file is a Markdown document, which may
contain F# code snippets (but also other code snippets). As usual, snippets are
indented with four spaces. In addition, the snippets can be annotated with special
commands. Some of them are demonstrated in the following example:
[lang=text]
# First-level heading
[hide]
let print s = printfn "%s" s
Some more documentation using `Markdown`.
[module=Hello]
let helloWorld() = print "Hello world!"
## Second-level heading
With some more documentation
[lang=csharp]
Console.WriteLine("Hello world!");
When processing the document, all F# snippets are copied to a separate file that
is type-checked using the F# compiler (to obtain colours and tool tips).
The commands are written on the first line of the named snippet, wrapped in `[...]`:
- The `hide` command specifies that the F# snippet should not be included in the
final document. This can be used to include code that is needed to type-check
the code, but is not visible to the reader.
- The `module=Foo` command can be used to specify F# `module` where the snippet
is placed. Use this command if you need multiple versions of the same snippet
or if you need to separate code from different snippets.
- The `lang=foo` command specifies the language of the named snippet. If the language
is other than `fsharp`, the snippet is copied to the output as `<pre>` HTML
tag without any processing.
*)
(**
### LaTeX in Literate Scripts and Markdown Documents
Literate Scripts may contain LaTeX sections in Markdown using these forms:
1. Single line latex starting with `$$`.
2. A block delimited by `\begin{equation}...\end{equation}` or `\begin{align}...\end{align}`.
3. An indented paragraph starting with `$$$`. This is F#-literate-specific and corresponds to
`\begin{equation}...\end{equation}`.
For example
[lang=text]
$$\frac{x}{y}$$
\begin{equation}
\frac{d}{dx} \left. \left( x \left( \left. \frac{d}{dy} x y \; \right|_{y=3} \right) \right) \right|_{x=2}
\end{equation}
Becomes
$$\frac{x}{y}$$
\begin{equation}
\frac{d}{dx} \left. \left( x \left( \left. \frac{d}{dy} x y \; \right|_{y=3} \right) \right) \right|_{x=2}
\end{equation}
The LaTeX will also be used in HTML and iPython notebook outputs.
### Making literate scripts work for different outputs
Literate scripts and markdown can by turned into LaTex, Python Notebooks and F# scripts.
A header may be needed to get the code to load, a typical example is this:
```text
(*** condition: prepare ***)
#nowarn "211"
#I "../src/FSharp.Formatting/bin/Release/netstandard2.0"
#r "FSharp.Formatting.Common.dll"
#r "FSharp.Formatting.Markdown.dll"
#r "FSharp.Formatting.CodeFormat.dll"
#r "FSharp.Formatting.Literate.dll"
(*** condition: fsx ***)
#if FSX
#r "nuget: FSharp.Formatting,{{fsdocs-package-version}}"
#endif // FSX
(*** condition: ipynb ***)
#if IPYNB
#r "nuget: FSharp.Formatting,{{fsdocs-package-version}}"
#endif // IPYNB
```
### Processing literate files programatically
To process file Use the two static methods to turn single documents into HTML
as follows using functionality from the `cref:T:FSharp.Formatting.Literate.Literate` type:
*)
open System.IO
open FSharp.Formatting.Literate
let source = __SOURCE_DIRECTORY__
let template = Path.Combine(source, "template.html")
let script = Path.Combine(source, "../docs/script.fsx")
Literate.ConvertScriptFile(script, template)
let doc = Path.Combine(source, "../docs/document.md")
Literate.ConvertMarkdownFile(doc, template)
(**
The following sample also uses optional parameter `parameters` to specify additional
keywords that will be replaced in the template file (this matches the `template-project.html`
file which is included as a sample in the package):
*)
// Load the template & specify project information
let projTemplate = source + "template-project.html"
let projInfo =
[ "fsdocs-authors", "Tomas Petricek"
"fsdocs-source-link", "https://github.com/fsprojects/FSharp.Formatting"
"fsdocs-collection-name", "F# Formatting" ]
(**
The methods used above (`cref:M:FSharp.Formatting.Literate.Literate.ConvertScriptFile`, `cref:M:FSharp.Formatting.Literate.Literate.ConvertMarkdownFile`)
produce HTML output by default, but they can be also used to produce LaTeX output. This is done
by setting the output kind. The following
example shows how to call the methods to generate LaTeX documents:
*)
let templateTex = Path.Combine(source, "template.tex")
let scriptTex = Path.Combine(source, "../docs/script.fsx")
Literate.ConvertScriptFile(scriptTex, templateTex, outputKind = OutputKind.Latex)
let docTex = Path.Combine(source, "../docs/document.md")
Literate.ConvertMarkdownFile(docTex, templateTex, outputKind = OutputKind.Latex)
(**
The methods used above (`ConvertScriptFile`, `ConvertMarkdownFile`)
can also produce iPython Notebook output. This is done
by setting the named parameter `format` to `OutputKind.Pynb`:
*)
// Process script file, Markdown document and a directory
let scriptPynb = Path.Combine(source, "../docs/script.fsx")
Literate.ConvertScriptFile(scriptPynb, outputKind = OutputKind.Pynb)
let docPynb = Path.Combine(source, "../docs/document.md")
Literate.ConvertMarkdownFile(docPynb, outputKind = OutputKind.Pynb)
(**
All of the three methods discussed in the previous two sections take a number of optional
parameters that can be used to tweak how the formatting works
*)