Skip to content

Commit

Permalink
Show group dropdown, default groups
Browse files Browse the repository at this point in the history
For snippets, the group now defaults to the type of snippet (e.g. java/scala).
  • Loading branch information
raboof committed Apr 24, 2017
1 parent c326e73 commit 73dd536
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 59 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ object Groups {
def html(supergroups: Map[String, Seq[String]]) = {
supergroups.map {
case (_, groups) =>
"""<select class="supergroup" style="display: none">""" + groups.map(group => s"""<option class="group">$group</option>""").mkString + "</select>"
"""<select class="supergroup">""" +
groups.map(group => s"""<option class="group" value="group-${group.toLowerCase}">$group</option>""").mkString +
"</select>"
}.mkString("\n")
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright © 2015 - 2016 Lightbend, Inc. <http://www.lightbend.com>
*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright © 2015 - 2016 Lightbend, Inc. <http://www.lightbend.com>
*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* 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("""
|<pre class="prettyprint group-scala">
|<code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
|</pre>""")
}

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("""
|<dl>
|<dt>Scala</dt>
|<dd>
|<pre class="prettyprint group-scala">
|<code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
|</pre>
|</dd>
|<dt>Java</dt>
|<dd>
|<pre class="prettyprint group-java">
|<code class="language-java">
|public class example2 {
| public static void main(String[] args) {
| System.out.println("Hello, World");
| }
|}</code>
|</pre>
|</dd>
|</dl>""")
}

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("""
|<div id="yeah" class="red blue">
|<p>Simple sentence here.</p>
|</div>""")
}

}
23 changes: 23 additions & 0 deletions core/src/test/scala/com/lightbend/paradox/markdown/example.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* 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
25 changes: 25 additions & 0 deletions core/src/test/scala/com/lightbend/paradox/markdown/example2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2015 - 2017 Lightbend, Inc. <http://www.lightbend.com>
*
* 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
16 changes: 8 additions & 8 deletions docs/src/main/paradox/features/snippet-inclusion.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
```
@@@

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<pre class="prettyprint"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val foo = 42</code></pre>
6 changes: 3 additions & 3 deletions plugin/src/sbt-test/paradox/snippets/expected/multiple.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<pre class="prettyprint"><code class="language-scala">import scala.concurrent.duration._
<pre class="prettyprint group-scala"><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
<pre class="prettyprint group-scala"><code class="language-scala">import scala.util.Try

def parseInt(s: String): Option[Int] = Try(s.toInt).toOption</code></pre>
<pre class="prettyprint"><code class="language-conf"># HTTP Configuration
<pre class="prettyprint group-conf"><code class="language-conf"># HTTP Configuration
http {
port=80
host=0.0.0.0
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/sbt-test/paradox/snippets/expected/nocode.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<pre class="prettyprint"><code class="nocode">certpath: -Using checker7 ... [sun.security.provider.certpath.RevocationChecker]
<pre class="prettyprint group-text"><code class="nocode">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
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/sbt-test/paradox/snippets/expected/reference.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<pre class="prettyprint"><code class="language-conf"># This should be included
<pre class="prettyprint group-conf"><code class="language-conf"># This should be included
#and this as well

snippets {
# this snip is a snap
snip = &quot;snap&quot;
}</code></pre>
<pre class="prettyprint"><code class="language-conf"># this snip is a snap
<pre class="prettyprint group-conf"><code class="language-conf"># this snip is a snap
snip = &quot;snap&quot;</code></pre>
8 changes: 4 additions & 4 deletions plugin/src/sbt-test/paradox/snippets/expected/snippets.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<pre class="prettyprint"><code class="language-scala">def indented = {
<pre class="prettyprint group-scala"><code class="language-scala">def indented = {
1 + 2
}</code></pre>
<pre class="prettyprint"><code class="language-conf">snippets {
<pre class="prettyprint group-conf"><code class="language-conf">snippets {
test = 1
}</code></pre>
<pre class="prettyprint"><code class="language-scala">val symbols = Seq(&#39;symbols, Symbol(&quot;@&quot;), &#39;EOL)</code></pre>
<pre class="prettyprint"><code class="language-scala">val spacy = &quot;Please do not remove ending spaces after these markers&quot;</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val symbols = Seq(&#39;symbols, Symbol(&quot;@&quot;), &#39;EOL)</code></pre>
<pre class="prettyprint group-scala"><code class="language-scala">val spacy = &quot;Please do not remove ending spaces after these markers&quot;</code></pre>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<pre class="prettyprint"><code class="language-xml">&lt;bar&gt;
<pre class="prettyprint group-xml"><code class="language-xml">&lt;bar&gt;
XML FTW
&lt;/bar&gt;</code></pre>
70 changes: 44 additions & 26 deletions themes/generic/src/main/assets/js/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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];
}
}
}
Expand Down

0 comments on commit 73dd536

Please sign in to comment.