@@ -13,7 +13,123 @@ Markdown sources are ignored by default unless passed explicitly as inputs.
13
13
You can enable including non-explicit ` .md ` inputs by passing the ` --enable-markdown ` option.
14
14
:::
15
15
16
- ## Plain ` scala ` snippets
16
+ ## Markdown inputs
17
+
18
+ ### On-disk markdown sources
19
+
20
+ You can pass local ` .md ` inputs by passing their path to Scala CLI (as you would for any other kind of input).
21
+
22
+ ``` bash ignore
23
+ scala-cli hello.md
24
+ ```
25
+
26
+ ` .md ` sources inside of directories are ignored by default, unless the ` --enable-markdown ` option is passed.
27
+
28
+ ``` bash ignore
29
+ scala-cli dir-with-markdown --enable-markdown
30
+ ```
31
+
32
+ ### Zipped archives
33
+
34
+ Scala CLI can run ` .md ` sources inside a ` .zip ` archive.
35
+ Same as with directories, ` .md ` sources inside zipped archives are ignored by default, unless
36
+ the ` --enable-markdown ` option is passed.
37
+
38
+ ``` bash ignore
39
+ scala-cli archive-with-markdown.zip --enable-markdown
40
+ ```
41
+
42
+ ### Remote inputs
43
+
44
+ ::: warning
45
+ Running unverified code from the Internet can be very handy for * trusted* sources, but it can also be really dangerous,
46
+ since Scala CLI does not provide any sandboxing at this moment.
47
+
48
+ Make sure that you trust the code that you are about to run.
49
+ :::
50
+
51
+ #### URLs
52
+
53
+ You can also pass a URL pointing to a ` .md ` file to run it with Scala CLI.
54
+
55
+ <ChainedSnippets >
56
+
57
+ ``` bash
58
+ scala-cli https://gist.githubusercontent.com/Gedochao/6415211eeb8ca4d8d6db123f83f0f839/raw/4c5ce7593e19f1390555221e0d076f4b02f4b4fd/example.md
59
+ ```
60
+
61
+ ``` text
62
+ Hello
63
+ ```
64
+
65
+ </ChainedSnippets >
66
+
67
+ #### Github Gist
68
+
69
+ Scala CLI accepts GitHub Gist URLs.
70
+ The gist is technically treated as a zipped archive (which it is downloaded as), so it is necessary to pass
71
+ the ` --enable-markdown ` option alongside the gist URL to run any contained Markdown sources.
72
+
73
+ <ChainedSnippets >
74
+
75
+ ``` bash
76
+ scala-cli https://gist.github.com/Gedochao/6415211eeb8ca4d8d6db123f83f0f839 --enable-markdown
77
+ ```
78
+
79
+ ``` text
80
+ Hello
81
+ ```
82
+
83
+ </ChainedSnippets >
84
+
85
+ You can find more information on running GitHub Gists in the [ gists cookbook] ( ../cookbooks/gists.md ) .
86
+
87
+ ### Piped Markdown code
88
+
89
+ Instead of passing paths to your Markdown sources, you can also pipe your code via standard input:
90
+
91
+ <ChainedSnippets >
92
+
93
+ ``` bash
94
+ echo ' # Example Snippet
95
+ ```scala
96
+ println("Hello")
97
+ ```' | scala-cli _.md
98
+ ```
99
+
100
+ ``` text
101
+ Hello
102
+ ```
103
+
104
+ </ChainedSnippets >
105
+
106
+ You can find more information on piped sources in the [ piping guide] ( ./piping.md ) .
107
+
108
+ ### Markdown code as a command line snippet
109
+
110
+ It is also possible to pass Markdown code as a snippet directly from the command line.
111
+
112
+ <ChainedSnippets >
113
+
114
+ ```` bash
115
+ scala-cli run --markdown-snippet ' # Markdown snippet
116
+ with a code block
117
+ ```scala
118
+ println("Hello")
119
+ ```'
120
+ ````
121
+
122
+ ``` text
123
+ Hello
124
+ ```
125
+
126
+ </ChainedSnippets >
127
+
128
+ You can find more information on command line snippets in the [ snippets guide] ( ./snippets.md ) .
129
+
130
+ ## Markdown code blocks
131
+
132
+ ### Plain ` scala ` snippets
17
133
18
134
```` markdown title=Example.md
19
135
# Example
@@ -85,7 +201,7 @@ Example1_md Example2_md
85
201
86
202
</ChainedSnippets >
87
203
88
- ## ` scala raw ` snippets
204
+ ### ` scala raw ` snippets
89
205
90
206
You can mark a ` scala ` code block with the ` raw ` keyword, indicating that this snippet should not be wrapped as a script
91
207
and should instead be treated as is. This is the equivalent of code in a ` .scala ` file. For a ` raw ` snippet to be
@@ -116,7 +232,7 @@ Hello from Markdown
116
232
117
233
</ChainedSnippets >
118
234
119
- ## ` scala test ` snippets
235
+ ### ` scala test ` snippets
120
236
121
237
It is possible to run tests from ` scala ` code blocks marked as ` test ` . This is similar to ` raw ` snippets in that the
122
238
code is not wrapped and is treated as is.
@@ -150,7 +266,7 @@ Test:
150
266
151
267
</ChainedSnippets >
152
268
153
- ## ` reset ` scope for ` scala ` snippets
269
+ ### ` reset ` scope for ` scala ` snippets
154
270
155
271
When multiple plain ` scala ` snippets are used in a single ` .md ` file, by default they are actually treated as a single
156
272
script. They share context and when run, are executed one after another, as if they were all in a single ` .sc ` file.
@@ -199,11 +315,94 @@ world
199
315
200
316
</ChainedSnippets >
201
317
318
+ ## ` using ` directives and markdown code blocks
319
+
320
+ It is possible to define ` using ` directives at the beginning of a ` scala ` code block inside a markdown input.
321
+ This is supported for all ` scala ` code block flavours.
322
+
323
+ ```` markdown title=UsingDirectives.md
324
+ # Using directives in `.md` inputs
325
+
326
+ ## `scala raw` example
327
+ ```scala raw
328
+ //> using lib "com.lihaoyi::pprint:0.8.0"
329
+ object Printer {
330
+ def printHello(): Unit = pprint.pprintln("Hello")
331
+ }
332
+ ```
333
+
334
+ ## Plain `scala` example
335
+ ```scala
336
+ //> using lib "com.lihaoyi::os-lib:0.8.1"
337
+ println(os.pwd)
338
+ ```
339
+
340
+ ## `scala test` example
341
+ ```scala test
342
+ //> using lib "org.scalameta::munit:0.7.29"
343
+
344
+ class Test extends munit.FunSuite {
345
+ test("foo") {
346
+ assert(true)
347
+ println("Hello from tests")
348
+ }
349
+ }
350
+ ```
351
+ ## Relying on directives from other snippets
352
+ Directives from other snippets apply to the whole context.
353
+ As a result, nothing really stops you from using a dependency
354
+ from an earlier code block.
355
+ ```scala
356
+ Printer.printHello()
357
+ pprint.pprintln("world")
358
+ ```
359
+ ````
360
+
361
+ ::: note
362
+ ` scala ` snippets inside of a Markdown input are not isolated. Each ` using ` directive applies to the whole project's
363
+ context. A directive defined in a later snippet within the same source may override another defined in an earlier one.
364
+
365
+ ```` markdown title="OverriddenDirective.md"
366
+ ## 1
367
+
368
+ ```scala
369
+ //> using scala "2.12.17"
370
+ println(util.Properties.versionNumberString)
371
+ ```
372
+
373
+ ## 2
374
+
375
+ ```scala
376
+ //> using scala "2.13.10"
377
+ println(util.Properties.versionNumberString)
378
+ ```
379
+ ````
380
+
381
+ In this example, the directive from the second ` scala ` snippet will override the previous one and Scala ` 2.13.10 ` will
382
+ be used for both.
383
+
384
+ <ChainedSnippets >
385
+
386
+ ``` bash ignore
387
+ scala-cli OverriddenDirective.md
388
+ ```
389
+
390
+ ``` text
391
+ Compiling project (Scala 2.13.10, JVM)
392
+ Compiled project (Scala 2.13.10, JVM)
393
+ 2.13.10
394
+ 2.13.10
395
+ ```
396
+
397
+ </ChainedSnippets >
398
+
399
+ :::
400
+
202
401
## Referring to code from Markdown
203
402
204
403
### Plain ` scala ` code blocks
205
404
206
- Referring to code from plain ` scala ` snippets in Markdown requires using their package name.
405
+ Referring to code from plain ` scala ` snippets in markdown requires using their package name.
207
406
Similarly to scripts, the package is inferred based on the relative path to the source file in your project.
208
407
209
408
You also have to point to the Scope under which the code is located.
0 commit comments