Skip to content

Commit

Permalink
Merge pull request #71 from olafurpg/silent
Browse files Browse the repository at this point in the history
Implement mdoc:silent modifier
  • Loading branch information
olafurpg authored Sep 9, 2018
2 parents 2e97407 + 681c0e0 commit 0419af2
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
17 changes: 17 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Table of contents:
- [Command-line](#command-line)
- [Modifiers](#modifiers)
- [Default](#default)
- [Silent](#silent)
- [Fail](#fail)
- [Crash](#crash)
- [Passthrough](#passthrough)
Expand Down Expand Up @@ -165,6 +166,22 @@ x + y
```
````

### Silent

The `silent` modifier is identical to the default modifier except that it hides
the evaluated output. The input code fence renders unchanged.

````scala mdoc:mdoc
```scala mdoc:silent
val x = 1
val y = 2
x + y
```
```scala mdoc
x + y
```
````

### Fail

The `fail` modifier asserts that the code block will not compile
Expand Down
3 changes: 1 addition & 2 deletions mdoc/src/main/scala/mdoc/internal/markdown/BlockInput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class BlockInput(ctx: Context, baseInput: Input) {
val allModifiers =
Modifier.all.map(_.toString.toLowerCase()) ++
ctx.settings.stringModifiers.map(_.name)
val expected = allModifiers.mkString(", ")
val msg = s"Invalid mode '$mode'. Expected one of: $expected"
val msg = s"Invalid mode '$mode'"
val offset = "scala mdoc:".length
val start = block.getInfo.getStartOffset + offset
val end = block.getInfo.getEndOffset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Instrumenter(sections: List[SectionInput]) {
sb.print(s"; $$doc.binder($name, ${position(pos)})")
}
private def printStatement(stat: Stat, mod: Modifier, sb: PrintStream): Unit = mod match {
case Modifier.Default | Modifier.Passthrough =>
case Modifier.Default | Modifier.Passthrough | Modifier.Silent =>
val binders = stat match {
case Binders(names) =>
names.map(name => name -> name.pos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class MdocPostProcessor(implicit ctx: Context) extends DocumentPostProcessor {
case (section, ScalaBlockInput(block, _, mod)) =>
block.setInfo(CharSubSequence.of("scala"))
mod match {
case Modifier.Silent =>
case Modifier.Default | Modifier.Fail =>
val str = Renderer.renderEvaluatedSection(rendered, section, ctx.reporter)
val content: BasedSequence = CharSubSequence.of(str)
Expand Down
6 changes: 5 additions & 1 deletion mdoc/src/main/scala/mdoc/internal/markdown/Modifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ sealed trait Modifier {
def isPassthrough: Boolean = this == Passthrough
def isString: Boolean = this.isInstanceOf[Str]
def isCrash: Boolean = this == Crash
def isSilent: Boolean = this == Silent
}
object Modifier {
def all: List[Modifier] = List(Default, Passthrough, Fail, Crash)
def all: List[Modifier] = List(Default, Passthrough, Fail, Crash, Silent)
def apply(string: String): Option[Modifier] =
all.find(_.toString.equalsIgnoreCase(string))

Expand All @@ -35,6 +36,9 @@ object Modifier {
/** Expect a runtime exception from evaluating the block. */
case object Crash extends Modifier

/** Keep the input code fence unchanged, don't print out the evaluated output. */
case object Silent extends Modifier

/** Render stdout as raw markdown and remove code block. */
case object Passthrough extends Modifier

Expand Down
2 changes: 1 addition & 1 deletion mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ object Renderer {

case Modifier.Crash =>
throw new IllegalArgumentException(Modifier.Crash.toString)
case c: Modifier.Str =>
case c @ (Modifier.Str(_, _) | Modifier.Silent) =>
throw new IllegalArgumentException(c.toString)
}
}
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Table of contents:
- [Command-line](#command-line)
- [Modifiers](#modifiers)
- [Default](#default)
- [Silent](#silent)
- [Fail](#fail)
- [Crash](#crash)
- [Passthrough](#passthrough)
Expand Down Expand Up @@ -186,6 +187,40 @@ x + y
````


### Silent

The `silent` modifier is identical to the default modifier except that it hides
the evaluated output. The input code fence renders unchanged.


Before:

````
```scala mdoc:silent
val x = 1
val y = 2
x + y
```
```scala mdoc
x + y
```
````

After:

````
```scala
val x = 1
val y = 2
x + y
```
```scala
x + y
// res1: Int = 3
```
````

### Fail

The `fail` modifier asserts that the code block will not compile
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/test/scala/tests/markdown/ErrorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ErrorSuite extends BaseMarkdownSuite {
|```
""".stripMargin,
"""
|error: invalid-mod.md:2:15: error: Invalid mode 'foobaz'. Expected one of: default, passthrough, fail, crash
|error: invalid-mod.md:2:15: error: Invalid mode 'foobaz'
|```scala mdoc:foobaz
| ^^^^^^
""".stripMargin
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/SilentSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tests.markdown

class SilentSuite extends BaseMarkdownSuite {

check(
"basic",
"""
|```scala mdoc:silent
|val x = 4
|```
|```scala mdoc
|println(x)
|```
""".stripMargin,
"""|```scala
|val x = 4
|```
|
|```scala
|println(x)
|// 4
|```
""".stripMargin
)

checkError(
"error",
"""
|```scala mdoc:silent
|val x: String = 4
|```
""".stripMargin,
"""|error: error.md:3:17: error: type mismatch;
| found : Int(4)
| required: String
|val x: String = 4
| ^
|""".stripMargin
)

}

0 comments on commit 0419af2

Please sign in to comment.