Skip to content

Commit

Permalink
Allow snippets to specify multiple markers
Browse files Browse the repository at this point in the history
Example:

    @@ snip [Code.scala](../scala/Code.scala) { #imports #functions }

Fixes #21
  • Loading branch information
jonas committed Sep 13, 2016
1 parent e2d7673 commit 285b5ee
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import org.pegdown.ast.DirectiveNode.Format._
import org.pegdown.plugins.ToHtmlSerializerPlugin
import org.pegdown.Printer

import scala.collection.JavaConverters._

/**
* Serialize directives, checking the name and format against registered directives.
*/
Expand Down Expand Up @@ -113,9 +115,9 @@ object RefDirective {
case class SnipDirective(page: Page) extends LeafBlockDirective("snip") {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
try {
val label = Option(node.attributes.identifier)
val labels = node.attributes.values("identifier").asScala
val file = new File(page.file.getParentFile, node.source)
val text = Snippet(file, label)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))
new VerbatimNode(text, lang).accept(visitor)
} catch {
Expand All @@ -142,7 +144,7 @@ object SnipDirective {
case class FiddleDirective(page: Page) extends LeafBlockDirective("fiddle") {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
try {
val label = Option(node.attributes.identifier)
val labels = node.attributes.values("identifier").asScala

val baseUrl = node.attributes.value("baseUrl", "https://embed.scalafiddle.io/embed")
val cssClass = node.attributes.value("cssClass", "fiddle")
Expand All @@ -152,7 +154,7 @@ case class FiddleDirective(page: Page) extends LeafBlockDirective("fiddle") {
val cssStyle = node.attributes.value("cssStyle", "overflow: hidden;")

val file = new File(page.file.getParentFile, node.source)
val text = Snippet(file, label)
val text = Snippet(file, labels)
val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file))

val fiddleSource = java.net.URLEncoder.encode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ object Snippet {

class SnippetException(message: String) extends RuntimeException(message)

def apply(file: File, label: Option[String]): String = label match {
case Some(label) => extract(file, label)
case None => extract(file, _ => true, _ => false, addFilteredLine).snippetLines.mkString("\n")
def apply(file: File, labels: Seq[String]): String = labels match {
case Seq() => extract(file, _ => true, _ => false, addFilteredLine).snippetLines.mkString("\n")
case _ => labels.map(label => extract(file, label)).mkString("\n")
}

def extract(file: File, label: String): String = {
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/sbt-test/paradox/snippets/expected/multiple.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<pre class="prettyprint"><code class="language-scala">import scala.concurrent.duration._

case class Measurement(method: Method, duration: Duration)</code></pre>
<pre class="prettyprint"><code class="language-scala">import scala.util.Try

def parseInt(s: String): Option[Int] = Try(s.toInt).toOption</code></pre>
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
@@ snip [multiple snippet](../../test/scala/Multiple.scala) { #multiple }

@@ snip [comma separated snippet](../../test/scala/Multiple.scala) { #parseint-imports #parseint-def }
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// #parseint-imports
import scala.util.Try
// #parseint-imports

object Multiple {
// #multiple
import scala.concurrent.duration._
Expand All @@ -14,4 +18,9 @@ object Multiple {
// #multiple
case class Measurement(method: Method, duration: Duration)
// #multiple
}

// #parseint-def

def parseInt(s: String): Option[Int] = Try(s.toInt).toOption
// #parseint-def
}

0 comments on commit 285b5ee

Please sign in to comment.