From 285b5ee80da14f50da53e2bffb0600cae78284ac Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 13 Sep 2016 13:36:57 -0400 Subject: [PATCH] Allow snippets to specify multiple markers Example: @@ snip [Code.scala](../scala/Code.scala) { #imports #functions } Fixes #21 --- .../com/lightbend/paradox/markdown/Directive.scala | 10 ++++++---- .../com/lightbend/paradox/markdown/Snippet.scala | 6 +++--- .../sbt-test/paradox/snippets/expected/multiple.html | 3 +++ .../paradox/snippets/src/main/paradox/multiple.md | 2 ++ .../paradox/snippets/src/test/scala/Multiple.scala | 11 ++++++++++- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala index dc84122b..c38d5155 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala @@ -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. */ @@ -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 { @@ -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") @@ -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( diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala index 2d4d36f6..2e9517c8 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala @@ -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 = { diff --git a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html index 5b7a6526..4ab50547 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html @@ -1,3 +1,6 @@
import scala.concurrent.duration._
 
 case class Measurement(method: Method, duration: Duration)
+
import scala.util.Try
+
+def parseInt(s: String): Option[Int] = Try(s.toInt).toOption
diff --git a/plugin/src/sbt-test/paradox/snippets/src/main/paradox/multiple.md b/plugin/src/sbt-test/paradox/snippets/src/main/paradox/multiple.md index c8b7cb3d..4783b248 100644 --- a/plugin/src/sbt-test/paradox/snippets/src/main/paradox/multiple.md +++ b/plugin/src/sbt-test/paradox/snippets/src/main/paradox/multiple.md @@ -1 +1,3 @@ @@ snip [multiple snippet](../../test/scala/Multiple.scala) { #multiple } + +@@ snip [comma separated snippet](../../test/scala/Multiple.scala) { #parseint-imports #parseint-def } diff --git a/plugin/src/sbt-test/paradox/snippets/src/test/scala/Multiple.scala b/plugin/src/sbt-test/paradox/snippets/src/test/scala/Multiple.scala index 9e2272f8..f91c22e3 100644 --- a/plugin/src/sbt-test/paradox/snippets/src/test/scala/Multiple.scala +++ b/plugin/src/sbt-test/paradox/snippets/src/test/scala/Multiple.scala @@ -1,3 +1,7 @@ +// #parseint-imports +import scala.util.Try +// #parseint-imports + object Multiple { // #multiple import scala.concurrent.duration._ @@ -14,4 +18,9 @@ object Multiple { // #multiple case class Measurement(method: Method, duration: Duration) // #multiple -} \ No newline at end of file + + // #parseint-def + + def parseInt(s: String): Option[Int] = Try(s.toInt).toOption + // #parseint-def +}