Skip to content

Commit

Permalink
Code snippets now contain a button copying the contents (#474)
Browse files Browse the repository at this point in the history
Code snippets now contain one button for showing snippet sources and one for copying the contents

This aligns the snippets with other doc tools that has a copy button up top right.
  • Loading branch information
johanandren authored Feb 19, 2021
1 parent 534d7e3 commit 30197a0
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ abstract class StyledVerbatimSerializer extends VerbatimSerializer {

node match {
case vgn: VerbatimGroupNode =>
printer.print("""<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button>""")
vgn.getSourceUrl.ifPresent(new Consumer[String] {
override def accept(sourceUrl: String): Unit =
printer.print(s"""<a class="icon go-to-source" href="$sourceUrl" target="_blank" title="Go to snippet source"></a>""")
override def accept(sourceUrl: String): Unit = {
printer.print(s"""<a class="snippet-button go-to-source" href="$sourceUrl" target="_blank" title="Go to snippet source">source</a>""")
}
})
case _ =>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript" src="js/page.js"></script>
<script type="text/javascript" src="js/warnOldVersion.js"></script>
<script type="text/javascript" src="js/groups.js"></script>
<script type="text/javascript" src="js/snippets.js"></script>
<link rel="stylesheet" type="text/css" href="lib/foundation/dist/foundation.min.css"/>
<link rel="stylesheet" type="text/css" href="css/page.css"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript" src="../js/page.js"></script>
<script type="text/javascript" src="../js/warnOldVersion.js"></script>
<script type="text/javascript" src="../js/groups.js"></script>
<script type="text/javascript" src="../js/snippets.js"></script>
<link rel="stylesheet" type="text/css" href="../lib/foundation/dist/foundation.min.css"/>
<link rel="stylesheet" type="text/css" href="../css/page.css"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript" src="../js/page.js"></script>
<script type="text/javascript" src="../js/warnOldVersion.js"></script>
<script type="text/javascript" src="../js/groups.js"></script>
<script type="text/javascript" src="../js/snippets.js"></script>
<link rel="stylesheet" type="text/css" href="../lib/foundation/dist/foundation.min.css"/>
<link rel="stylesheet" type="text/css" href="../css/page.css"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<pre class="prettyprint"><code class="language-scala">val snippet = 0</code></pre>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val snippet = 0</code></pre>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div>
<div>
<pre class="prettyprint"><code class="language-scala">object Hello extends App {
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">object Hello extends App {
def say(str: String): Unit = {
println(str)
}
Expand All @@ -10,7 +10,7 @@
</div>
</div>

<pre class="prettyprint"><code class="language-scala">object Hello extends App {
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">object Hello extends App {
def say(str: String): Unit = {
println(str)
}
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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val foo = 42</code></pre>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val foo = 42</code></pre>
4 changes: 2 additions & 2 deletions plugin/src/sbt-test/paradox/snippets/expected/group.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<pre class="prettyprint group-a"><code class="language-scala">private class SomethingElse</code></pre>
<pre class="prettyprint group-b"><code class="language-scala">import scala.util.Try</code></pre>
<pre class="prettyprint group-a"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">private class SomethingElse</code></pre>
<pre class="prettyprint group-b"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">import scala.util.Try</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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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,10 +1,10 @@
<pre class="prettyprint"><code class="language-conf"># This should be included
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">def indented = {
1 + 2
}</code></pre>
<pre class="prettyprint"><code class="language-conf">snippets {
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">val symbols = Seq(&#39;symbols, Symbol(&quot;@&quot;), &#39;EOL)</code></pre>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><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"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-xml">&lt;bar&gt;
XML FTW
&lt;/bar&gt;</code></pre>
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class IncludeDirectiveSpec extends MarkdownBaseSpec {
it should "include nested code snippets" in {
markdown("""@@include(tests/src/test/resources/include-code-snip.md)""") shouldEqual html("""
|<pre class="prettyprint">
|<code class="language-conf">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-conf">
|a = b</code>
|</pre>""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
"The `snip` directive" should "render code snippets" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example }""") shouldEqual html("""
|<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
Expand All @@ -50,7 +50,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
|<dt>Scala</dt>
|<dd>
|<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
Expand All @@ -59,7 +59,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
|<dt>Java</dt>
|<dd>
|<pre class="prettyprint">
|<code class="language-java">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-java">
|public class example2 {
| public static void main(String[] args) {
| System.out.println("Hello, World");
Expand All @@ -75,7 +75,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
|@@snip[example2.java](tests/src/test/scala/com/lightbend/paradox/markdown/example2.java){ #example2 .red .blue }
|""") shouldEqual html("""
|<pre class="prettyprint red blue">
|<code class="language-java">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-java">
|public class example2 {
| public static void main(String[] args) {
| System.out.println("Hello, World");
Expand All @@ -87,7 +87,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "trim indentation from snippets" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #indented-example }""") shouldEqual html("""
|<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|case object Dent
| case object DoubleDent</code>
|</pre>""")
Expand All @@ -96,7 +96,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "not truncate snippets" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #multi-indented-example }""") shouldEqual html("""
|<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object AnotherIndentedExample {
| def rendered(): Unit = {
| }
Expand All @@ -106,15 +106,15 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
|</pre>""")
}

it should "add link to source" in {
it should "add link to source and copy button" in {
implicit val context = writerContextWithProperties(
"github.base_url" -> "https://github.com/lightbend/paradox/tree/v0.2.1",
"github.root.base_dir" -> new File(".").getAbsoluteFile.getParent,
"snip.github_link" -> "true")

markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example }""") shouldEqual html(
"""<pre class="prettyprint">
|<a class="icon go-to-source" href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30" target="_blank" title="Go to snippet source"></a><code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30" target="_blank" title="Go to snippet source">source</a><code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
Expand All @@ -131,7 +131,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {

markdown("""@@snip[example.scala]($test$/example.scala) { #example }""") shouldEqual html(
"""<pre class="prettyprint">
|<a class="icon go-to-source" href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30" target="_blank" title="Go to snippet source"></a><code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/lightbend/paradox/tree/v0.2.1/tests/src/test/scala/com/lightbend/paradox/markdown/example.scala#L28-L30" target="_blank" title="Go to snippet source">source</a><code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
Expand All @@ -147,7 +147,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {

markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example }""") shouldEqual html(
"""<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object example extends App {
| println("Hello, World!")
|}</code>
Expand All @@ -157,7 +157,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "include labels when including the whole file" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala)""") shouldEqual html(
"""<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|/*
| * Copyright &copy; 2015 - 2019 Lightbend, Inc. &lt;http://www.lightbend.com&gt;
| *
Expand Down Expand Up @@ -247,7 +247,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "filter labels by default" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example-with-label }""") shouldEqual html(
"""<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object Constants {
|}</code>
|</pre>"""
Expand All @@ -257,7 +257,7 @@ class SnipDirectiveSpec extends MarkdownBaseSpec {
it should "allow including labels if specified" in {
markdown("""@@snip[example.scala](tests/src/test/scala/com/lightbend/paradox/markdown/example.scala) { #example-with-label filterLabels=false }""") shouldEqual html(
"""<pre class="prettyprint">
|<code class="language-scala">
|<button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><code class="language-scala">
|object Constants {
| val someString = " #foo "
|}</code>
Expand Down
24 changes: 19 additions & 5 deletions themes/generic/src/main/assets/css/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,29 @@ a.anchor .anchor-link:before {
content: "§";
}

a.go-to-source::before {
content: "📄";
.site-content .page-content .prettyprint .snippet-button {
float: right;
text-decoration: none;
font-size: 0.75rem;
font-weight: bold;
font-family: "Roboto", "Helvetica Neue", "Helvetica", Arial, sans-serif;
speak: none;
color: #4078c0;
border: 2px solid #e5e6e4;
padding: 4px 4px;
margin-left: 8px;
line-height: 1.2;
vertical-align: middle;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.prettyprint .icon {
float: right;
.site-content .page-content .prettyprint .snippet-button:hover {
background-color: #e5e6e4;
border: 2px solid #4078c0;
}

a.anchor .anchor-link, .icon {
a.anchor .anchor-link {
font-size: 18px;
font-family: "icons" !important;
font-style: normal !important;
Expand Down
6 changes: 6 additions & 0 deletions themes/generic/src/main/assets/js/snippets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$(function() {
$(".snippet-button.copy-snippet").click(function() {
var code = $(this).parent().find("code").text()
navigator.clipboard.writeText(code)
})
})
1 change: 1 addition & 0 deletions themes/generic/src/main/assets/page.st
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $endif$
<script type="text/javascript" src="$page.base$js/page.js"></script>
<script type="text/javascript" src="$page.base$js/warnOldVersion.js"></script>
<script type="text/javascript" src="$page.base$js/groups.js"></script>
<script type="text/javascript" src="$page.base$js/snippets.js"></script>
<link rel="stylesheet" type="text/css" href="$page.base$lib/foundation/dist/foundation.min.css"/>
<link rel="stylesheet" type="text/css" href="$page.base$css/page.css"/>

Expand Down

0 comments on commit 30197a0

Please sign in to comment.