From 73dd536c63c6580fdbf695219f0134eff464b902 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Fri, 21 Apr 2017 10:43:03 +0200 Subject: [PATCH] Show group dropdown, default groups For snippets, the group now defaults to the type of snippet (e.g. java/scala). --- build.sbt | 1 + .../paradox/markdown/Directive.scala | 2 +- .../lightbend/paradox/markdown/Groups.scala | 4 +- .../markdown/InlineGroupDirectiveSpec.scala | 8 +- .../markdown/InlineWrapDirectiveSpec.scala | 8 +- .../paradox/markdown/SnipDirectiveSpec.scala | 81 +++++++++++++++++++ .../lightbend/paradox/markdown/example.scala | 23 ++++++ .../lightbend/paradox/markdown/example2.java | 25 ++++++ .../paradox/features/snippet-inclusion.md | 16 ++-- .../snippets/expected/configured-bases.html | 8 +- .../paradox/snippets/expected/multiple.html | 6 +- .../paradox/snippets/expected/nocode.html | 2 +- .../paradox/snippets/expected/reference.html | 4 +- .../paradox/snippets/expected/snippets.html | 8 +- .../paradox/snippets/expected/some-xml.html | 2 +- themes/generic/src/main/assets/js/page.js | 70 ++++++++++------ 16 files changed, 209 insertions(+), 59 deletions(-) create mode 100644 core/src/test/scala/com/lightbend/paradox/markdown/SnipDirectiveSpec.scala create mode 100644 core/src/test/scala/com/lightbend/paradox/markdown/example.scala create mode 100644 core/src/test/scala/com/lightbend/paradox/markdown/example2.java diff --git a/build.sbt b/build.sbt index 73cde108..947ea341 100644 --- a/build.sbt +++ b/build.sbt @@ -107,6 +107,7 @@ lazy val docs = (project in file("docs")) name := "paradox docs", paradoxTheme := Some(builtinParadoxTheme("generic")), paradoxProperties in Compile += ("empty" -> "") + //paradoxGroups := Map("Languages" -> Seq("Scala", "Java")) ) addCommandAlias("verify", ";test ;scripted ;docs/paradox") 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 506c9e1f..7c2f5698 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Directive.scala @@ -311,7 +311,7 @@ case class SnipDirective(page: Page, variables: Map[String, String]) } else new File(page.file.getParentFile, source) val text = Snippet(file, labels) val lang = Option(node.attributes.value("type")).getOrElse(Snippet.language(file)) - val group = Option(node.attributes.value("group")).getOrElse("") + val group = Option(node.attributes.value("group")).getOrElse(lang) new VerbatimGroupNode(text, lang, group).accept(visitor) } catch { case e: FileNotFoundException => diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Groups.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Groups.scala index 78124e94..6ed09a14 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Groups.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Groups.scala @@ -20,7 +20,9 @@ object Groups { def html(supergroups: Map[String, Seq[String]]) = { supergroups.map { case (_, groups) => - """" + """" }.mkString("\n") } } \ No newline at end of file diff --git a/core/src/test/scala/com/lightbend/paradox/markdown/InlineGroupDirectiveSpec.scala b/core/src/test/scala/com/lightbend/paradox/markdown/InlineGroupDirectiveSpec.scala index 4c60cf80..5eb97e69 100644 --- a/core/src/test/scala/com/lightbend/paradox/markdown/InlineGroupDirectiveSpec.scala +++ b/core/src/test/scala/com/lightbend/paradox/markdown/InlineGroupDirectiveSpec.scala @@ -1,12 +1,12 @@ /* - * Copyright © 2015 - 2016 Lightbend, Inc. - * + * Copyright © 2015 - 2017 Lightbend, Inc. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/core/src/test/scala/com/lightbend/paradox/markdown/InlineWrapDirectiveSpec.scala b/core/src/test/scala/com/lightbend/paradox/markdown/InlineWrapDirectiveSpec.scala index 88522f51..b2a1db36 100644 --- a/core/src/test/scala/com/lightbend/paradox/markdown/InlineWrapDirectiveSpec.scala +++ b/core/src/test/scala/com/lightbend/paradox/markdown/InlineWrapDirectiveSpec.scala @@ -1,12 +1,12 @@ /* - * Copyright © 2015 - 2016 Lightbend, Inc. - * + * Copyright © 2015 - 2017 Lightbend, Inc. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/core/src/test/scala/com/lightbend/paradox/markdown/SnipDirectiveSpec.scala b/core/src/test/scala/com/lightbend/paradox/markdown/SnipDirectiveSpec.scala new file mode 100644 index 00000000..6ce58dbf --- /dev/null +++ b/core/src/test/scala/com/lightbend/paradox/markdown/SnipDirectiveSpec.scala @@ -0,0 +1,81 @@ +/* + * Copyright © 2015 - 2017 Lightbend, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.lightbend.paradox.markdown + +import com.lightbend.paradox.tree.Tree.Location + +class SnipDirectiveSpec extends MarkdownBaseSpec { + + val testProperties = Map("version" -> "1.2.3") + + implicit val context: Location[Page] => Writer.Context = { loc => + writerContext(loc).copy(properties = testProperties) + } + + "The `snip` directive" should "render code snippets" in { + markdown("""@@snip[example.scala](core/src/test/scala/com/lightbend/paradox/markdown/example.scala) {#example }""") shouldEqual html(""" + |
+      |
+      |object example extends App {
+      |  println("Hello, World!")
+      |}
+      |
""") + } + + it should "render code snippets in definition lists" in { + markdown(""" + |Scala + |: @@snip[example.scala](core/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example } + | + |Java + |: @@snip[example2.java](core/src/test/scala/com/lightbend/paradox/markdown/example2.java) { #example2 } + |""") shouldEqual html(""" + |
+ |
Scala
+ |
+ |
+      |
+      |object example extends App {
+      |  println("Hello, World!")
+      |}
+      |
+ |
+ |
Java
+ |
+ |
+      |
+      |public class example2 {
+      |    public static void main(String[] args) {
+      |        System.out.println("Hello, World");
+      |    }
+      |}
+      |
+ |
+ |
""") + } + + it should "support a custom id and custom CSS classes at the same time" in { + markdown(""" + |@@@ div { #yeah .red .blue } + |Simple sentence here. + |@@@""") shouldEqual html(""" + |
+ |

Simple sentence here.

+ |
""") + } + +} diff --git a/core/src/test/scala/com/lightbend/paradox/markdown/example.scala b/core/src/test/scala/com/lightbend/paradox/markdown/example.scala new file mode 100644 index 00000000..943bcc64 --- /dev/null +++ b/core/src/test/scala/com/lightbend/paradox/markdown/example.scala @@ -0,0 +1,23 @@ +/* + * Copyright © 2015 - 2017 Lightbend, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.lightbend.paradox.markdown + +//#example +object example extends App { + println("Hello, World!") +} +//#example diff --git a/core/src/test/scala/com/lightbend/paradox/markdown/example2.java b/core/src/test/scala/com/lightbend/paradox/markdown/example2.java new file mode 100644 index 00000000..384c3cd3 --- /dev/null +++ b/core/src/test/scala/com/lightbend/paradox/markdown/example2.java @@ -0,0 +1,25 @@ +/* + * Copyright © 2015 - 2017 Lightbend, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.lightbend.paradox.markdown; + +//#example2 +public class example2 { + public static void main(String[] args) { + System.out.println("Hello, World"); + } +} +//#example2 diff --git a/docs/src/main/paradox/features/snippet-inclusion.md b/docs/src/main/paradox/features/snippet-inclusion.md index 03843ef4..45e456a9 100644 --- a/docs/src/main/paradox/features/snippet-inclusion.md +++ b/docs/src/main/paradox/features/snippet-inclusion.md @@ -70,13 +70,13 @@ $empty$ First-scala : @@snip [example-first.scala](../../resources/tab-switching/examples.scala) { #scala_first group=scala } $empty$ -Some separator with text describing the @span[Java variant] { .group-java } @span[Scala variant containing ***markdown*** and @ref:[Linking](linking.md)] { .group-scala } +Some separator with text describing the @java[Java variant] @scala[Scala variant containing ***markdown*** and @ref:[Linking](linking.md)] $empty$ Second-java -: @@snip [example-second.java](../../resources/tab-switching/examples.java) { #java_second group=java } +: @@snip [example-second.java](../../resources/tab-switching/examples.java) { #java_second } $empty$ Second-scala -: @@snip [example-second.scala](../../resources/tab-switching/examples.scala) { #scala_second group=scala } +: @@snip [example-second.scala](../../resources/tab-switching/examples.scala) { #scala_second } ``` @@@ @@ -88,13 +88,13 @@ First-java First-scala : @@snip [example-first.scala](../../resources/tab-switching/examples.scala) { #scala_first group=scala } -Some separator with text describing the @span[Java variant] { .group-java } @span[Scala variant containing ***markdown*** and @ref:[Linking](linking.md)] { .group-scala } +Some separator with text describing the @java[Java variant] @scala[Scala variant containing ***markdown*** and @ref:[Linking](linking.md)] -Second-java -: @@snip [example-second.java](../../resources/tab-switching/examples.java) { #java_second group=java } +Java +: @@snip [example-second.java](../../resources/tab-switching/examples.java) -Second-scala -: @@snip [example-second.scala](../../resources/tab-switching/examples.scala) { #scala_second group=scala } +Scala +: @@snip [example-second.scala](../../resources/tab-switching/examples.scala) ### snip.*.base_dir diff --git a/plugin/src/sbt-test/paradox/snippets/expected/configured-bases.html b/plugin/src/sbt-test/paradox/snippets/expected/configured-bases.html index 8ad3a6b3..c10ce2ce 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/configured-bases.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/configured-bases.html @@ -1,4 +1,4 @@ -
val foo = 42
-
val foo = 42
-
val foo = 42
-
val foo = 42
\ No newline at end of file +
val foo = 42
+
val foo = 42
+
val foo = 42
+
val foo = 42
\ No newline at end of file diff --git a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html index 717998d5..b0d2865e 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/multiple.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/multiple.html @@ -1,10 +1,10 @@ -
import scala.concurrent.duration._
+
import scala.concurrent.duration._
 
 case class Measurement(method: Method, duration: Duration)
-
import scala.util.Try
+
import scala.util.Try
 
 def parseInt(s: String): Option[Int] = Try(s.toInt).toOption
-
# HTTP Configuration
+
# HTTP Configuration
 http {
   port=80
   host=0.0.0.0
diff --git a/plugin/src/sbt-test/paradox/snippets/expected/nocode.html b/plugin/src/sbt-test/paradox/snippets/expected/nocode.html
index 6b838a24..5e9fb819 100644
--- a/plugin/src/sbt-test/paradox/snippets/expected/nocode.html
+++ b/plugin/src/sbt-test/paradox/snippets/expected/nocode.html
@@ -1,4 +1,4 @@
-
certpath: -Using checker7 ... [sun.security.provider.certpath.RevocationChecker]
+
certpath: -Using checker7 ... [sun.security.provider.certpath.RevocationChecker]
 certpath: connecting to OCSP service at: http://gtssl2-ocsp.geotrust.com
 certpath: OCSP response status: SUCCESSFUL
 certpath: OCSP response type: basic
diff --git a/plugin/src/sbt-test/paradox/snippets/expected/reference.html b/plugin/src/sbt-test/paradox/snippets/expected/reference.html
index bdaab0f8..c96851aa 100644
--- a/plugin/src/sbt-test/paradox/snippets/expected/reference.html
+++ b/plugin/src/sbt-test/paradox/snippets/expected/reference.html
@@ -1,9 +1,9 @@
-
# This should be included
+
# This should be included
 #and this as well
 
 snippets {
   # this snip is a snap
   snip = "snap"
 }
-
# this snip is a snap
+
# this snip is a snap
 snip = "snap"
diff --git a/plugin/src/sbt-test/paradox/snippets/expected/snippets.html b/plugin/src/sbt-test/paradox/snippets/expected/snippets.html index bc16abac..8a343915 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/snippets.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/snippets.html @@ -1,8 +1,8 @@ -
def indented = {
+
def indented = {
   1 + 2
 }
-
snippets {
+
snippets {
   test = 1
 }
-
val symbols = Seq('symbols, Symbol("@"), 'EOL)
-
val spacy = "Please do not remove ending spaces after these markers"
+
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/expected/some-xml.html b/plugin/src/sbt-test/paradox/snippets/expected/some-xml.html index aa75dc31..8118450f 100644 --- a/plugin/src/sbt-test/paradox/snippets/expected/some-xml.html +++ b/plugin/src/sbt-test/paradox/snippets/expected/some-xml.html @@ -1,3 +1,3 @@ -
<bar>
+
<bar>
     XML FTW
 </bar>
diff --git a/themes/generic/src/main/assets/js/page.js b/themes/generic/src/main/assets/js/page.js index 55cc24eb..a55dd0ea 100644 --- a/themes/generic/src/main/assets/js/page.js +++ b/themes/generic/src/main/assets/js/page.js @@ -3,7 +3,6 @@ $(function() { // Groups (like 'java' and 'scala') represent groups of 'switchable' content, either in tabs or in regular text. // The catalog of groups can be defined in the sbt parameters to initialize the group. - var groupClass = "group"; var groupCookie = "groupsPref"; var cookieTg = getCookie(groupCookie); var cookieTgList = []; @@ -65,16 +64,22 @@ $(function() { }); } if (!current) { - current = "group-" + groups.first().text(); + current = "group-" + groups.first().text().toLowerCase(); cookieTgList = addToList(cookieTgList, current); } groups.each(function() { - var group = "group-" + $(this).text(); - if(group != current) { + var group = "group-" + $(this).text().toLowerCase(); + if(group == current) { + switchToGroup(this.value); + } else { $("span." + group).hide() } }); + + $(this).on("change", function() { + switchToGroup(this.value); + }); }); $("dl").has("dd > pre").each(function() { @@ -125,35 +130,48 @@ $(function() { var currentContent = currentDt.next("dd").addClass("current").show(); currentDl.css("height", currentDt.height() + currentContent.height()); } else { - $("span ." + currentGroup).show(); - $("dl").has("dd > pre").each(function() { - var dl = $(this); - dl.find("dt").each(function() { - var dt = $(this); - var pre = dt.next("dd").find("pre"); - if(pre.hasClass(currentGroup)) { - dl.find(".current").removeClass("current").next("dd").removeClass("current").hide(); - dt.addClass("current"); - var currentContent = dt.next("dd").addClass("current").show(); - dl.css("height", dt.height() + currentContent.height()); - $("span." + currentGroup).show() - } else { - $("span." + groupOf(dt)).hide() - } - }); - }); + switchToGroup(currentGroup); } }); + function switchToGroup(group) { + // Cookie: + cookieTgList = addToList(cookieTgList, group); + setCookie(groupCookie, arrayToJson(cookieTgList)); + + // Dropdown switcher: + $("select") + .has("option[value=" + group +"]") + .val(group); + + // Inline snippets: + $("span ." + group).show(); + + // Tabbed snippets: + $("dl").has("dd > pre").each(function() { + var dl = $(this); + dl.find("dt").each(function() { + var dt = $(this); + var pre = dt.next("dd").find("pre"); + if(pre.hasClass(group)) { + dl.find(".current").removeClass("current").next("dd").removeClass("current").hide(); + dt.addClass("current"); + var currentContent = dt.next("dd").addClass("current").show(); + dl.css("height", dt.height() + currentContent.height()); + $("span." + group).show() + } else { + $("span." + groupOf(dt)).hide() + } + }); + }); + } + function groupOf(elem) { var currentClasses = elem.next("dd").find("pre").attr("class").split(' '); - var regex = new RegExp("^" + groupClass + "-.*"); + var regex = new RegExp("^group-.*"); for(var i = 0; i < currentClasses.length; i++) { if(regex.test(currentClasses[i])) { - var currentGroup = currentClasses[i]; - cookieTgList = addToList(cookieTgList, currentGroup); - setCookie(groupCookie, arrayToJson(cookieTgList)); - return currentGroup; + return currentClasses[i]; } } }