From e2d76734026a2bbe7197d1847202b288b1dc4596 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 13 Sep 2016 10:17:55 -0400 Subject: [PATCH 1/3] Make space handling after snippet markers more lenient Also adds tests for symbols after snippet markers. --- .../scala/com/lightbend/paradox/markdown/Snippet.scala | 9 +++++---- .../src/sbt-test/paradox/snippets/expected/snippets.html | 2 ++ .../paradox/snippets/src/main/paradox/snippets.md | 4 ++++ .../paradox/snippets/src/test/scala/Snippets.scala | 8 ++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) 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 6c06ebae..2d4d36f6 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Snippet.scala @@ -32,9 +32,10 @@ object Snippet { def extract(file: File, label: String): String = { if (!verifyLabel(label)) throw new SnippetException(s"Label [$label] for [$file] contains illegal characters. " + "Only [a-zA-Z0-9_-] are allowed.") - // A label can be followed by an end of line or a space followed by a single sequence of contiguous - // (no whitespace) non-word characters (anything not in the group [a-zA-Z0-9_] - val labelPattern = ("""#\Q""" + label + """\E( [^w \t]*)?$""").r + // A label can be followed by an end of line or one or more spaces followed by an + // optional single sequence of contiguous (no whitespace) non-word characters + // (anything not in the group [a-zA-Z0-9_]) + val labelPattern = ("""#\Q""" + label + """\E( +[^w \t]*)?$""").r val hasLabel = (s: String) => labelPattern.findFirstIn(s).nonEmpty val extractionState = extract(file, hasLabel, hasLabel, addFilteredLine) val snippetLines = extractionState.snippetLines @@ -59,7 +60,7 @@ object Snippet { private case class ExtractionState(inBlock: Boolean, snippetLines: Seq[String]) - private val anyLabelRegex = """#[a-zA-Z_0-9\-]+( [^w \t]*)?$""".r + private val anyLabelRegex = """#[a-zA-Z_0-9\-]+( +[^w \t]*)?$""".r private def addFilteredLine(line: String, lines: Seq[String]): Seq[String] = anyLabelRegex.findFirstIn(line).map(_ => lines).getOrElse(lines :+ line) private def verifyLabel(label: String): Boolean = anyLabelRegex.findFirstIn(s"#$label").nonEmpty diff --git a/plugin/src/sbt-test/paradox/snippets/expected/snippets.html b/plugin/src/sbt-test/paradox/snippets/expected/snippets.html index 5bef638a..bc16abac 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/snippets.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/snippets.html @@ -4,3 +4,5 @@
snippets {
   test = 1
 }
+
val symbols = Seq('symbols, Symbol("@"), 'EOL)
+
val spacy = "Please do not remove ending spaces after these markers"
diff --git a/plugin/src/sbt-test/paradox/snippets/src/main/paradox/snippets.md b/plugin/src/sbt-test/paradox/snippets/src/main/paradox/snippets.md index e1fb4e00..7a641cb2 100644 --- a/plugin/src/sbt-test/paradox/snippets/src/main/paradox/snippets.md +++ b/plugin/src/sbt-test/paradox/snippets/src/main/paradox/snippets.md @@ -1,3 +1,7 @@ @@ snip [indented snippet](../../test/scala/Snippets.scala) { #indented } @@ snip [typed snippet](../../test/scala/Snippets.scala) { #config type=conf } + +@@ snip [symbols @ EOL snippet](../../test/scala/Snippets.scala) { identifier=symbols-at-eol } + +@@ snip [lenient space snippet](../../test/scala/Snippets.scala) { #space-after-marker } diff --git a/plugin/src/sbt-test/paradox/snippets/src/test/scala/Snippets.scala b/plugin/src/sbt-test/paradox/snippets/src/test/scala/Snippets.scala index e4167ad5..43be36a7 100644 --- a/plugin/src/sbt-test/paradox/snippets/src/test/scala/Snippets.scala +++ b/plugin/src/sbt-test/paradox/snippets/src/test/scala/Snippets.scala @@ -5,6 +5,14 @@ object Snippets { } // #indented + //#symbols-at-eol ¯\_(ツ)_/¯ + val symbols = Seq('symbols, Symbol("@"), 'EOL) + //#symbols-at-eol ¯\_(ツ)_/¯ + + //#space-after-marker + val spacy = "Please do not remove ending spaces after these markers" + //#space-after-marker + val config = """ #config snippets { From 285b5ee80da14f50da53e2bffb0600cae78284ac Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 13 Sep 2016 13:36:57 -0400 Subject: [PATCH 2/3] 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 +} From 4056a7ce6ed65c884ef73a27e5808109f4033ee8 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Tue, 13 Sep 2016 15:12:06 -0400 Subject: [PATCH 3/3] Add aggregation test case for embedded config --- .../paradox/snippets/expected/multiple.html | 12 +++++++++++ .../snippets/src/main/paradox/multiple.md | 4 +++- .../snippets/src/test/scala/Multiple.scala | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html index 4ab50547..717998d5 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html @@ -4,3 +4,15 @@
import scala.util.Try
 
 def parseInt(s: String): Option[Int] = Try(s.toInt).toOption
+
# HTTP Configuration
+http {
+  port=80
+  host=0.0.0.0
+}
+
+# Database Configuration
+db {
+  url=jdbc:mysql://mydb/mytable
+  user=dev
+  pass=secret
+}
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 4783b248..435cc512 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,3 +1,5 @@ @@ snip [multiple snippet](../../test/scala/Multiple.scala) { #multiple } -@@ snip [comma separated snippet](../../test/scala/Multiple.scala) { #parseint-imports #parseint-def } +@@ snip [multi-label snippet](../../test/scala/Multiple.scala) { #parseint-imports #parseint-def } + +@@ snip [multi-label conf snippet](../../test/scala/Multiple.scala) { #http-config #db-config type=conf } 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 f91c22e3..95cea788 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 @@ -23,4 +23,25 @@ object Multiple { def parseInt(s: String): Option[Int] = Try(s.toInt).toOption // #parseint-def + + val config = """ + #http-config + # HTTP Configuration + http { + port=80 + host=0.0.0.0 + } + + #http-config + http.port=${?HTTP_PORT} + + #db-config + # Database Configuration + db { + url=jdbc:mysql://mydb/mytable + user=dev + pass=secret + } + #db-config + """ }