Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add post modifiers to support custom renderings like charts
Post modifiers enable users to fully customize the rendering of evaluated code blocks based on the static type and runtime values of variables in the code fence. One example use-case for post modifiers is rendering charts, but I'm sure we can come up with other examples as well.
- Loading branch information
Showing
30 changed files
with
739 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
mdoc-docs/src/main/resources/META-INF/services/mdoc.PostModifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mdoc.docs.EvilplotModifier |
1 change: 1 addition & 0 deletions
1
mdoc-docs/src/main/resources/META-INF/services/mdoc.StringModifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mdoc.docs.FileModifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mdoc.docs | ||
|
||
import com.cibo.evilplot.geometry.Drawable | ||
import java.nio.file.Files | ||
import mdoc._ | ||
import scala.meta.inputs.Position | ||
|
||
class EvilplotModifier extends PostModifier { | ||
val name = "evilplot" | ||
def process(ctx: PostModifierContext): String = { | ||
val out = ctx.outputFile.resolveSibling(_ => ctx.info) | ||
ctx.lastValue match { | ||
case d: Drawable => | ||
Files.createDirectories(out.toNIO.getParent) | ||
if (out.isFile) { | ||
Files.delete(out.toNIO) | ||
} | ||
d.write(out.toFile) | ||
s"![](${out.toNIO.getFileName})" | ||
case _ => | ||
val (pos, obtained) = ctx.variables.lastOption match { | ||
case Some(variable) => | ||
val prettyObtained = | ||
s"${variable.staticType} = ${variable.runtimeValue}" | ||
(variable.pos, prettyObtained) | ||
case None => | ||
(Position.Range(ctx.originalCode, 0, 0), "nothing") | ||
} | ||
ctx.reporter.error( | ||
pos, | ||
s"""type mismatch: | ||
expected: com.cibo.evilplot.geometry.Drawable | ||
obtained: $obtained""" | ||
) | ||
"" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mdoc.docs | ||
|
||
import java.nio.charset.StandardCharsets | ||
import mdoc.Reporter | ||
import mdoc.StringModifier | ||
import scala.meta.inputs.Input | ||
import scala.meta.inputs.Position | ||
import scala.meta.internal.io.FileIO | ||
import scala.meta.io.AbsolutePath | ||
import mdoc.internal.pos.PositionSyntax._ | ||
|
||
class FileModifier extends StringModifier { | ||
val name = "file" | ||
override def process( | ||
info: String, | ||
code: Input, | ||
reporter: Reporter | ||
): String = { | ||
val file = AbsolutePath(info) | ||
if (file.isFile) { | ||
val text = FileIO.slurp(file, StandardCharsets.UTF_8) | ||
s""" | ||
File: [${file.toNIO.getFileName}](https://github.com/olafurpg/mdoc/blob/master/$info) | ||
`````scala | ||
$text | ||
````` | ||
""" | ||
} else { | ||
val pos = Position.Range(code, 0 - info.length - 1, 0 - 1).toUnslicedPosition | ||
reporter.error(pos, s"no such file: $file") | ||
"" | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.