Skip to content

Commit

Permalink
Ignore shebang headers in scala code blocks in Markdown inputs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao authored Dec 6, 2022
1 parent 210657a commit 6d7f946
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scala.build.internal.markdown

import scala.build.Position
import scala.build.errors.MarkdownUnclosedBackticksError
import scala.build.preprocessing.SheBang

/** Representation for an open code block in Markdown. (open meaning the closing backticks haven't
* yet been parsed or they aren't at all present)
Expand Down Expand Up @@ -37,9 +38,11 @@ case class MarkdownOpenFence(
): MarkdownCodeBlock = {
val start: Int = tickStartLine + 1
val bodyLines: Array[String] = lines.slice(start, tickEndLine)
val body = bodyLines.mkString("\n")
val (bodyWithNoSheBang, _) = SheBang.ignoreSheBangLines(body)
MarkdownCodeBlock(
info.split("\\s+").toList, // strip info by whitespaces
bodyLines.mkString("\n"),
bodyWithNoSheBang,
start, // snippet has to begin in the new line
tickEndLine - 1 // ending backticks have to be placed below the snippet
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ class MarkdownCodeBlockTests extends munit.FunSuite {
expect(actualResult == expectedResult)
}

test("shebang line is ignored in plain scala code blocks") {
val code = """println("Hello")""".stripMargin
val markdown =
s"""# Some snippet
|
|```scala
|#!/usr/bin/env -S scala-cli shebang
|$code
|```
|""".stripMargin
val expectedResult =
MarkdownCodeBlock(
info = PlainScalaInfo,
body = "\n" + code,
startLine = 3,
endLine = 4
)
val Right(Seq(actualResult: MarkdownCodeBlock)) =
MarkdownCodeBlock.findCodeBlocks(os.sub / "Example.md", markdown)
expect(actualResult == expectedResult)
}

test("a raw Scala code block is extracted correctly from markdown") {
val code = """object Main extends App {
| println("Hello")
Expand All @@ -69,6 +91,31 @@ class MarkdownCodeBlockTests extends munit.FunSuite {
expect(actualResult == expectedResult)
}

test("shebang line is ignored in raw scala code blocks") {
val code =
"""object Main extends App {
| println("Hello")
|}""".stripMargin
val markdown =
s"""# Some snippet
|
|```scala raw
|#!/usr/bin/env -S scala-cli shebang
|$code
|```
|""".stripMargin
val expectedResult =
MarkdownCodeBlock(
info = RawScalaInfo,
body = "\n" + code,
startLine = 3,
endLine = 6
)
val Right(Seq(actualResult: MarkdownCodeBlock)) =
MarkdownCodeBlock.findCodeBlocks(os.sub / "Example.md", markdown)
expect(actualResult == expectedResult)
}

test("a test Scala snippet is extracted correctly from markdown") {
val code =
"""//> using lib "org.scalameta::munit:0.7.29"
Expand Down
12 changes: 12 additions & 0 deletions website/docs/guides/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ world

</ChainedSnippets>

## `shebang` header and Markdown code blocks
The `shebang` line in `scala` code blocks inside a markdown input are always ignored.
You can use them (i.e. to give an example of their usage), but they do not change how the code is handled.

````markdown
## Self executable Scala script
```scala
#!/usr/bin/env -S scala-cli shebang
println("Hello world")
```
````

## `using` directives and Markdown code blocks

It is possible to define `using` directives at the beginning of a `scala` code block inside a markdown input.
Expand Down

0 comments on commit 6d7f946

Please sign in to comment.