Skip to content

Commit 4be8060

Browse files
committed
Add a copy to clipboard extension
1 parent 5ebe581 commit 4be8060

File tree

5 files changed

+161
-2
lines changed

5 files changed

+161
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Hibernate Infra - Asciidoctor extensions
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.infra.asciidoctor.extensions.copytoclipboard;
8+
9+
import java.io.BufferedReader;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.InputStreamReader;
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.stream.Collectors;
15+
16+
import org.asciidoctor.ast.Document;
17+
import org.asciidoctor.extension.DocinfoProcessor;
18+
import org.asciidoctor.extension.Location;
19+
import org.asciidoctor.extension.LocationType;
20+
21+
@Location(LocationType.FOOTER)
22+
public class CopyToClipboardProcessor extends DocinfoProcessor {
23+
@Override
24+
public String process(Document document) {
25+
try (
26+
InputStream is = this.getClass().getResourceAsStream( "/copy/clipboard.min.js" );
27+
InputStreamReader isr = new InputStreamReader( is, StandardCharsets.UTF_8 );
28+
BufferedReader reader = new BufferedReader( isr )
29+
) {
30+
String clipboardJs = reader.lines().collect( Collectors.joining( "\n" ) );
31+
return String.format(
32+
"<script>%s</script>\n<link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v6.7.2/css/all.css\" crossorigin=\"anonymous\">\n<style>%s</style>\n<script>%s</script>",
33+
clipboardJs,
34+
"pre.highlight .btn-copy{-webkit-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;opacity:0;padding:2px 6px;position:absolute;right:4px;top:.225rem;background-color:#fff0;border:none}.listingblock:hover .btn-copy,pre.highlight .btn-copy:focus{opacity:.5}pre.highlight{position:relative}.listingblock code[data-lang]::before{display:block;content:attr(data-lang) '|';right:1.5rem;-webkit-transition:opacity 0.3s ease-in-out;-o-transition:opacity 0.3s ease-in-out;transition:opacity 0.3s ease-in-out;opacity:0}.listingblock:hover code[data-lang]::before{opacity:.5}.tooltip-text{display:block;position:absolute;top:1.5rem;right:4px;background:#444;color:#fff;padding:6px 10px;border-radius:4px;white-space:nowrap;z-index:1;opacity:0;font-size:11px}.listingblock:hover .tooltip-text.show{opacity:1}",
35+
"const codes=document.querySelectorAll('pre.highlight > code');let index=0;codes.forEach((code)=>{code.setAttribute(\"id\",\"code\"+index);const block=document.createElement('div');block.className=\"tooltip\";const btn=document.createElement('button');btn.className=\"btn-copy fa-regular fa-copy\";btn.setAttribute(\"data-clipboard-action\",\"copy\");btn.setAttribute(\"data-clipboard-target\",\"#code\"+index);btn.setAttribute(\"title\",\"Copy to clipboard\");btn.setAttribute(\"float-right\",\"true\");code.before(btn);const tooltip=document.createElement('div');tooltip.className=\"tooltip-text\";tooltip.textContent=\"Copied!\";code.before(tooltip);index++});const clipboard=new ClipboardJS('.btn-copy');clipboard.on('success',function(e){e.clearSelection();e.trigger.className=e.trigger.className.replace(\"fa-copy\",\"fa-check\").replace(\"fa-regular \",\"fa \");e.trigger.setAttribute(\"title\",\"Copied!\");e.trigger.nextSibling.classList.toggle(\"show\");e.trigger.blur();setTimeout(function(){e.trigger.className=e.trigger.className.replace(\"fa-check\",\"fa-copy\").replace(\"fa \",\"fa-regular \");e.trigger.setAttribute(\"title\",\"Copy to clipboard\");e.trigger.nextSibling.classList.toggle(\"show\")},1500)})"
36+
);
37+
}
38+
catch (IOException e) {
39+
throw new RuntimeException( e );
40+
}
41+
}
42+
}

src/main/resources/copy/clipboard.min.js

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/resources/copy/copy.css

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* NOTE: These styles are minified and added as a simple string to CopyToClipboardProcessor.
4+
* To make changes:
5+
* - update this file
6+
* - minify the content
7+
* - add the minified content to the `CopyToClipboardProcessor`
8+
*
9+
*/
10+
pre.highlight .btn-copy {
11+
-webkit-transition: opacity 0.3s ease-in-out;
12+
-o-transition: opacity 0.3s ease-in-out;
13+
transition: opacity 0.3s ease-in-out;
14+
opacity: 0;
15+
padding: 2px 6px;
16+
position: absolute;
17+
right: 4px;
18+
top: .225rem;
19+
background-color: transparent;
20+
border: none;
21+
}
22+
23+
.listingblock:hover .btn-copy,
24+
pre.highlight .btn-copy:focus {
25+
opacity: 0.5;
26+
}
27+
28+
pre.highlight {
29+
position: relative;
30+
}
31+
32+
.listingblock code[data-lang]::before {
33+
display: block;
34+
content: attr(data-lang) '|';
35+
right: 1.5rem;
36+
-webkit-transition: opacity 0.3s ease-in-out;
37+
-o-transition: opacity 0.3s ease-in-out;
38+
transition: opacity 0.3s ease-in-out;
39+
opacity: 0;
40+
}
41+
42+
.listingblock:hover code[data-lang]::before {
43+
opacity: 0.5;
44+
}
45+
46+
.tooltip-text {
47+
display: block;
48+
position: absolute;
49+
top: 1.5rem;
50+
right: 4px;
51+
background: #444;
52+
color: #fff;
53+
padding: 6px 10px;
54+
border-radius: 4px;
55+
white-space: nowrap;
56+
z-index: 1;
57+
opacity: 0;
58+
font-size: 11px;
59+
}
60+
61+
.listingblock:hover .tooltip-text.show {
62+
opacity: 1;
63+
}

src/main/resources/copy/copy.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This is an adapted version of https://github.com/quarkusio/quarkusio.github.io/blob/317db07ca629dc331455692d2976ecece95319cd/assets/javascript/copy.js
3+
*
4+
* NOTE: These styles are minified and added as a simple string to CopyToClipboardProcessor.
5+
* To make changes:
6+
* - update this file
7+
* - minify the content
8+
* - add the minified content to the `CopyToClipboardProcessor`
9+
*
10+
*/
11+
const codes = document.querySelectorAll('pre.highlight > code');
12+
let index = 0;
13+
codes.forEach((code) => {
14+
15+
code.setAttribute("id", "code" + index);
16+
17+
const block = document.createElement('div');
18+
block.className = "tooltip";
19+
20+
const btn = document.createElement('button');
21+
btn.className = "btn-copy fa-regular fa-copy";
22+
btn.setAttribute("data-clipboard-action", "copy");
23+
btn.setAttribute("data-clipboard-target", "#code" + index);
24+
btn.setAttribute("title", "Copy to clipboard");
25+
btn.setAttribute("float-right", "true");
26+
code.before(btn);
27+
28+
const tooltip = document.createElement('div');
29+
tooltip.className = "tooltip-text";
30+
tooltip.textContent = "Copied!";
31+
code.before(tooltip);
32+
33+
index++;
34+
});
35+
36+
const clipboard = new ClipboardJS('.btn-copy');
37+
clipboard.on('success', function (e) {
38+
e.clearSelection();
39+
e.trigger.className = e.trigger.className.replace("fa-copy", "fa-check").replace("fa-regular ", "fa ");
40+
e.trigger.setAttribute("title", "Copied!");
41+
e.trigger.nextSibling.classList.toggle("show");
42+
e.trigger.blur();
43+
44+
setTimeout(function () {
45+
e.trigger.className = e.trigger.className.replace("fa-check", "fa-copy").replace("fa ", "fa-regular ");
46+
e.trigger.setAttribute("title", "Copy to clipboard");
47+
e.trigger.nextSibling.classList.toggle("show");
48+
}, 1500);
49+
});

src/test/java/org/hibernate/infra/asciidoctor/extensions/customnumbering/ExampleNumberingTest.java

-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import java.util.function.Function;
1919
import java.util.stream.Collectors;
2020

21-
import org.junit.Assert;
2221
import org.junit.BeforeClass;
2322
import org.junit.Test;
2423

2524
import org.asciidoctor.Asciidoctor;
2625
import org.asciidoctor.Options;
27-
import org.asciidoctor.OptionsBuilder;
2826
import org.asciidoctor.SafeMode;
2927
import org.jsoup.Jsoup;
3028
import org.jsoup.nodes.Document;

0 commit comments

Comments
 (0)