File tree Expand file tree Collapse file tree 3 files changed +63
-1
lines changed
main/scala/scala/build/internal/markdown
test/scala/scala/build/tests/markdown Expand file tree Collapse file tree 3 files changed +63
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package scala.build.internal.markdown
2
2
3
3
import scala .build .Position
4
4
import scala .build .errors .MarkdownUnclosedBackticksError
5
+ import scala .build .preprocessing .SheBang
5
6
6
7
/** Representation for an open code block in Markdown. (open meaning the closing backticks haven't
7
8
* yet been parsed or they aren't at all present)
@@ -37,9 +38,11 @@ case class MarkdownOpenFence(
37
38
): MarkdownCodeBlock = {
38
39
val start : Int = tickStartLine + 1
39
40
val bodyLines : Array [String ] = lines.slice(start, tickEndLine)
41
+ val body = bodyLines.mkString(" \n " )
42
+ val (bodyWithNoSheBang, _) = SheBang .ignoreSheBangLines(body)
40
43
MarkdownCodeBlock (
41
44
info.split(" \\ s+" ).toList, // strip info by whitespaces
42
- bodyLines.mkString( " \n " ) ,
45
+ bodyWithNoSheBang ,
43
46
start, // snippet has to begin in the new line
44
47
tickEndLine - 1 // ending backticks have to be placed below the snippet
45
48
)
Original file line number Diff line number Diff line change @@ -46,6 +46,28 @@ class MarkdownCodeBlockTests extends munit.FunSuite {
46
46
expect(actualResult == expectedResult)
47
47
}
48
48
49
+ test(" shebang line is ignored in plain scala code blocks" ) {
50
+ val code = """ println("Hello")""" .stripMargin
51
+ val markdown =
52
+ s """ # Some snippet
53
+ |
54
+ |```scala
55
+ |#!/usr/bin/env -S scala-cli shebang
56
+ | $code
57
+ |```
58
+ | """ .stripMargin
59
+ val expectedResult =
60
+ MarkdownCodeBlock (
61
+ info = PlainScalaInfo ,
62
+ body = " \n " + code,
63
+ startLine = 3 ,
64
+ endLine = 4
65
+ )
66
+ val Right (Seq (actualResult : MarkdownCodeBlock )) =
67
+ MarkdownCodeBlock .findCodeBlocks(os.sub / " Example.md" , markdown)
68
+ expect(actualResult == expectedResult)
69
+ }
70
+
49
71
test(" a raw Scala code block is extracted correctly from markdown" ) {
50
72
val code = """ object Main extends App {
51
73
| println("Hello")
@@ -69,6 +91,31 @@ class MarkdownCodeBlockTests extends munit.FunSuite {
69
91
expect(actualResult == expectedResult)
70
92
}
71
93
94
+ test(" shebang line is ignored in raw scala code blocks" ) {
95
+ val code =
96
+ """ object Main extends App {
97
+ | println("Hello")
98
+ |}""" .stripMargin
99
+ val markdown =
100
+ s """ # Some snippet
101
+ |
102
+ |```scala raw
103
+ |#!/usr/bin/env -S scala-cli shebang
104
+ | $code
105
+ |```
106
+ | """ .stripMargin
107
+ val expectedResult =
108
+ MarkdownCodeBlock (
109
+ info = RawScalaInfo ,
110
+ body = " \n " + code,
111
+ startLine = 3 ,
112
+ endLine = 6
113
+ )
114
+ val Right (Seq (actualResult : MarkdownCodeBlock )) =
115
+ MarkdownCodeBlock .findCodeBlocks(os.sub / " Example.md" , markdown)
116
+ expect(actualResult == expectedResult)
117
+ }
118
+
72
119
test(" a test Scala snippet is extracted correctly from markdown" ) {
73
120
val code =
74
121
""" //> using lib "org.scalameta::munit:0.7.29"
Original file line number Diff line number Diff line change @@ -337,6 +337,18 @@ world
337
337
338
338
</ChainedSnippets >
339
339
340
+ ## ` shebang ` header and Markdown code blocks
341
+ The ` shebang ` line in ` scala ` code blocks inside a markdown input are always ignored.
342
+ You can use them (i.e. to give an example of their usage), but they do not change how the code is handled.
343
+
344
+ ```` markdown
345
+ ## Self executable Scala script
346
+ ```scala
347
+ #!/usr/bin/env -S scala-cli shebang
348
+ println("Hello world")
349
+ ```
350
+ ````
351
+
340
352
## ` using ` directives and Markdown code blocks
341
353
342
354
It is possible to define ` using ` directives at the beginning of a ` scala ` code block inside a markdown input.
You can’t perform that action at this time.
0 commit comments