Skip to content

Commit 2aa52cf

Browse files
committed
Allow sclicheck to detect code blocks starting with >3 backticks & add dedicated handling for Markdown code blocks
1 parent 10fa7d5 commit 2aa52cf

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

modules/docs-tests/src/main/scala/sclicheck/sclicheck.scala

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import scala.io.StdIn.readLine
99
import scala.util.Random
1010
import scala.util.matching.Regex
1111

12-
val SnippetBlock = """ *```[^ ]+ title=([\w\d\.\-\/_]+) *""".r
13-
val CompileBlock = """ *``` *(\w+) +(compile|fail) *(?:title=([\w\d\.\-\/_]+))? *""".r
14-
val CodeBlockEnds = """ *``` *""".r
12+
val SnippetBlock = """ *(```[`]*)[^ ]+ title=([\w\d\.\-\/_]+) *""".r
13+
val CompileBlock = """ *(```[`]*) *(\w+) +(compile|fail) *(?:title=([\w\d\.\-\/_]+))? *""".r
14+
def compileBlockEnds(backticks: String): Regex = {
15+
val regexString = s""" *$backticks *"""
16+
regexString.r
17+
}
1518
val BashCommand = """ *```bash *(fail)? *""".r
1619
val CheckBlock = """ *\<\!-- Expected(-regex)?: *""".r
1720
val CheckBlockEnd = """ *\--> *""".r
@@ -67,7 +70,7 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
6770
inline def parseMultiline(
6871
lines: Seq[String],
6972
newCommand: Seq[String] => Commands,
70-
endMarker: Regex = CodeBlockEnds
73+
endMarker: Regex = compileBlockEnds("```")
7174
) =
7275
val codeLines = lines.takeWhile(l => !endMarker.matches(l))
7376
check(codeLines.size > 0, "Block cannot be empty!")
@@ -81,12 +84,17 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
8184
content match
8285
case Nil => currentCommands
8386

84-
case SnippetBlock(name) :: tail =>
85-
parseMultiline(tail, Commands.Write(name, _, context))
87+
case SnippetBlock(backticks, name) :: tail =>
88+
parseMultiline(tail, Commands.Write(name, _, context), compileBlockEnds(backticks))
8689

87-
case CompileBlock(name, status, fileName) :: tail =>
88-
val file = Option(fileName).getOrElse("snippet_" + Random.nextInt(1000) + "." + name)
89-
parseMultiline(tail, Commands.Compile(file, _, context, status == "fail"))
90+
case CompileBlock(backticks, name, status, fileName) :: tail =>
91+
val fileSuffix = if name == "markdown" then ".md" else s".$name"
92+
val file = Option(fileName).getOrElse("snippet_" + Random.nextInt(1000) + fileSuffix)
93+
parseMultiline(
94+
tail,
95+
Commands.Compile(file, _, context, status == "fail"),
96+
compileBlockEnds(backticks)
97+
)
9098

9199
case BashCommand(failGroup) :: tail =>
92100
parseMultiline(tail, Commands.Run(_, failGroup != null, context))

modules/docs-tests/src/test/scala/sclicheck/SclicheckTests.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ package sclicheck
33
class SclicheckTests extends munit.FunSuite:
44
test("Run regex") {
55
assert(
6-
Some(Seq("scala", "compile", null)) == clue(CompileBlock.unapplySeq("```scala compile"))
6+
Some(Seq("```", "scala", "compile", null)) == clue(
7+
CompileBlock.unapplySeq("```scala compile")
8+
)
9+
)
10+
assert(
11+
Some(Seq("```", "scala", "fail", null)) == clue(CompileBlock.unapplySeq("```scala fail"))
712
)
813
assert(
9-
Some(Seq("scala", "fail", null)) == clue(CompileBlock.unapplySeq("```scala fail"))
14+
Some(Seq("````", "markdown", "compile", null)) == clue(
15+
CompileBlock.unapplySeq("````markdown compile")
16+
)
1017
)
1118
assert(
12-
Some(Seq("scala", "fail", "a.sc")) == clue(
19+
Some(Seq("```", "scala", "fail", "a.sc")) == clue(
1320
CompileBlock.unapplySeq("```scala fail title=a.sc")
1421
)
1522
)

0 commit comments

Comments
 (0)