Skip to content

Commit fcc9a12

Browse files
committed
Document recent features & changes affecting working markdown inputs
- `.md` sources in zipped archives - `.md` sources in GitHub gists - remote `.md` sources - piping Markdown code - Markdown snippets
1 parent 21e1cf4 commit fcc9a12

File tree

4 files changed

+261
-8
lines changed

4 files changed

+261
-8
lines changed

website/docs/commands/basics.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ in which case it defaults to one of the sub-commands based on context:
2222
- if any inputs were passed, it defaults to the `run` sub-command
2323
- and so, `scala-cli a.scala` runs your `a.scala` file
2424
- additionally, when no inputs were passed, it defaults to the `run` sub-command in the following scenarios:
25-
- if a snippet was passed with `--execute-script`, `--execute-scala` or `--execute-java`
25+
- if a snippet was passed with `-e`, `--execute-script`, `--execute-scala`, `--execute-java` or `--execute-markdown`
2626
- if a main class was passed with the `--main-class` option alongside an extra `--classpath`
2727
- otherwise if no inputs were passed, it defaults to the `repl` sub-command
2828

@@ -207,6 +207,14 @@ You can also pipe code to `scala-cli` for execution:
207207
echo 'class Hello { public static void main(String args[]) { System.out.println("Hello"); } }' | scala-cli _.java
208208
# Hello
209209
```
210+
- Markdown code (experimental)
211+
```bash
212+
echo '# Example Snippet
213+
```scala
214+
println("Hello")
215+
```' | scala-cli _.md
216+
# Hello
217+
```
210218

211219
More details in the [Piping guide](../guides/piping.md).
212220

website/docs/cookbooks/gists.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Sharing and testing code with GitHub gists
33
sidebar_position: 6
44
---
55

6+
import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7+
68
## Running code from gists
79

810
`scala-cli` lets you run Scala code straight from GitHub gists, without the need to manually download them first.
@@ -78,4 +80,30 @@ scala-cli https://gist.github.com/lwronski/7ee12fa4b8b8bac3211841273df82080
7880
1,2,3,4
7981
-->
8082

81-
it will print `1,2,3,4` to the standard output.
83+
it will print `1,2,3,4` to the standard output.
84+
85+
## Gists and Markdown code
86+
87+
:::note
88+
This feature is a work in progress and should currently be treated as experimental.
89+
Markdown sources are ignored by default unless passed explicitly as inputs.
90+
You can enable including non-explicit `.md` inputs by passing the `--enable-markdown` option.
91+
:::
92+
93+
It is possible to run markdown sources from a GitHub gist.
94+
The gist is technically treated as a zipped archive (which it is downloaded as), so it is necessary to pass
95+
the `--enable-markdown` option alongside the gist URL to run any contained Markdown sources.
96+
97+
<ChainedSnippets>
98+
99+
```bash
100+
scala-cli https://gist.github.com/Gedochao/6415211eeb8ca4d8d6db123f83f0f839 --enable-markdown
101+
```
102+
103+
```text
104+
Hello
105+
```
106+
107+
</ChainedSnippets>
108+
109+
You can find more information on working with Markdown in the [Markdown guide](../guides/markdown.md).

website/docs/guides/markdown.md

Lines changed: 204 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,123 @@ Markdown sources are ignored by default unless passed explicitly as inputs.
1313
You can enable including non-explicit `.md` inputs by passing the `--enable-markdown` option.
1414
:::
1515

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
17133

18134
````markdown title=Example.md
19135
# Example
@@ -85,7 +201,7 @@ Example1_md Example2_md
85201

86202
</ChainedSnippets>
87203

88-
## `scala raw` snippets
204+
### `scala raw` snippets
89205

90206
You can mark a `scala` code block with the `raw` keyword, indicating that this snippet should not be wrapped as a script
91207
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
116232

117233
</ChainedSnippets>
118234

119-
## `scala test` snippets
235+
### `scala test` snippets
120236

121237
It is possible to run tests from `scala` code blocks marked as `test`. This is similar to `raw` snippets in that the
122238
code is not wrapped and is treated as is.
@@ -150,7 +266,7 @@ Test:
150266

151267
</ChainedSnippets>
152268

153-
## `reset` scope for `scala` snippets
269+
### `reset` scope for `scala` snippets
154270

155271
When multiple plain `scala` snippets are used in a single `.md` file, by default they are actually treated as a single
156272
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
199315

200316
</ChainedSnippets>
201317

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+
202401
## Referring to code from Markdown
203402

204403
### Plain `scala` code blocks
205404

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.
207406
Similarly to scripts, the package is inferred based on the relative path to the source file in your project.
208407

209408
You also have to point to the Scope under which the code is located.

website/docs/guides/piping.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ The available options are as follows:
2929

3030
- for standard Scala code use `_`, `_.scala` or `-.scala`;
3131
- for Scala scripts use `-`, `_.sc` or `-.sc`;
32-
- for Java code use `_.java` or `-.java`.
32+
- for Java code use `_.java` or `-.java`;
33+
- for Markdown code use `_.md` or `-.md`.
3334

3435
## Examples
3536

@@ -75,6 +76,23 @@ Hello
7576

7677
</ChainedSnippets>
7778

79+
- Markdown code (experimental)
80+
81+
<ChainedSnippets>
82+
83+
```bash
84+
echo '# Example Snippet
85+
```scala
86+
println("Hello")
87+
```' | scala-cli _.md
88+
```
89+
90+
```text
91+
Hello
92+
```
93+
94+
</ChainedSnippets>
95+
7896
## Mixing piped sources with on-disk ones
7997

8098
It is also possible to pipe some code via standard input, while the rest of your code is on-disk.

0 commit comments

Comments
 (0)