Skip to content

Commit

Permalink
[asciidoctor-java] integrate code blocks with carbon.now - using fram…
Browse files Browse the repository at this point in the history
…e for a better portability for now
  • Loading branch information
rmannibucau committed Dec 13, 2023
1 parent 5a3205a commit 85f6fe3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
17 changes: 17 additions & 0 deletions asciidoc-java/src/main/demo/demo-code.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
= Demo Code
:asciidoctor-css:
:data-uri:

[source,carbonNowBaseUrl="auto",alt="user record"]
----
public record User(
String id,
String name) {
}
----
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ private Map<String, String> readAttributes(final Reader reader) {
while ((line = reader.nextLine()) != null && !line.isBlank()) {
final var matcher = ATTRIBUTE_DEFINITION.matcher(line);
if (matcher.matches()) {
var value = matcher.groupCount() == 3 ? matcher.group("value").strip() : "";
var value = matcher.groupCount() == 3 ? ofNullable(matcher.group("value")).orElse("").strip() : "";
while (value.endsWith("\\")) {
value = value.substring(0, value.length() - 1).strip() +
ofNullable(reader.nextLine()).map(String::strip).map(it -> ' ' + it).orElse("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

Expand Down Expand Up @@ -85,41 +86,52 @@ public SimpleHtmlRenderer(final Configuration configuration) {
public void visit(final Document document) {
final boolean contentOnly = Boolean.parseBoolean(configuration.getAttributes().getOrDefault("noheader", "false"));
if (!contentOnly) {
final var attributes = document.header().attributes();

builder.append("<!DOCTYPE html>\n");
builder.append("<html");
if (attr("nolang", document.header().attributes()) == null) {
final var lang = attr("lang", document.header().attributes());
if (attr("nolang", attributes) == null) {
final var lang = attr("lang", attributes);
builder.append(" lang=\"").append(lang == null ? "en" : lang).append('"');
}
builder.append(">\n");
builder.append("<head>\n");

final var encoding = attr("encoding", document.header().attributes());
final var encoding = attr("encoding", attributes);
builder.append(" <meta charset=\"").append(encoding == null ? "UTF-8" : encoding).append("\">\n");

builder.append(" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n");

final var appName = attr("app-name", document.header().attributes());
final var appName = attr("app-name", attributes);
if (appName != null) {
builder.append(" <meta name=\"application-name\" content=\"").append(appName).append("\">\n");
}
final var description = attr("description", document.header().attributes());
final var description = attr("description", attributes);
if (description != null) {
builder.append(" <meta name=\"description\" content=\"").append(description).append("\">\n");
}
final var keywords = attr("keywords", document.header().attributes());
final var keywords = attr("keywords", attributes);
if (keywords != null) {
builder.append(" <meta name=\"keywords\" content=\"").append(keywords).append("\">\n");
}
final var author = attr("author", document.header().attributes());
final var author = attr("author", attributes);
if (author != null) {
builder.append(" <meta name=\"author\" content=\"").append(author).append("\">\n");
}
final var copyright = attr("copyright", document.header().attributes());
final var copyright = attr("copyright", attributes);
if (copyright != null) {
builder.append(" <meta name=\"copyright\" content=\"").append(copyright).append("\">\n");
}

if (attributes.containsKey("asciidoctor-css")) {
builder.append(" <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/asciidoctor.js/1.5.9/css/asciidoctor.min.css\" integrity=\"sha512-lb4ZuGfCVoGO2zu/TMakNlBgRA6mPXZ0RamTYgluFxULAwOoNnBIZaNjsdfhnlKlIbENaQbEAYEWxtzjkB8wsQ==\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\" />\n");
}
Stream.of(attributes.getOrDefault("custom-css", "").split(","))
.map(String::strip)
.filter(Predicate.not(String::isBlank))
.map(i -> " " + i + '\n')
.forEach(builder::append);

// todo: favicon, highlighter, toc support etc...
builder.append("</head>\n");

Expand All @@ -132,6 +144,13 @@ public void visit(final Document document) {
builder.append(" </div>\n");

builder.append("</body>\n");

Stream.of(document.header().attributes().getOrDefault("custom-js", "").split(","))
.map(String::strip)
.filter(Predicate.not(String::isBlank))
.map(i -> " " + i + '\n')
.forEach(builder::append);

builder.append("</html>\n");
}
}
Expand Down Expand Up @@ -327,10 +346,15 @@ public void visitText(final Text element) {
public void visitCode(final Code element) {
final var carbonNowBaseUrl = element.options().get("carbonNowBaseUrl");
if (carbonNowBaseUrl != null) { // consider the code block as an image
visitImage(new Macro("image", (carbonNowBaseUrl.isBlank() || "auto".equals(carbonNowBaseUrl) ?
// todo: this needs to be tuned
final var frame = " <iframe\n" +
" src=\"" + (carbonNowBaseUrl.isBlank() || "auto".equals(carbonNowBaseUrl) ?
// todo: this needs to be tuned/tunable
"https://carbon.now.sh/embed?bg=rgba%28171%2C184%2C195%2C100%29&t=vscode&wt=none&l=text%2Fx-java&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=48px&ph=32px&ln=true&fl=1&fm=Droid+Sans+Mono&fs=13px&lh=133%25&si=false&es=2x&wm=false&code=" :
carbonNowBaseUrl) + URLEncoder.encode(element.value(), UTF_8), element.options(), false));
carbonNowBaseUrl) + URLEncoder.encode(element.value(), UTF_8) + "\"\n" +
" style=\"width: 1024px; height: 473px; border:0; transform: scale(1); overflow:hidden;\"\n" +
" sandbox=\"allow-scripts allow-same-origin\">\n" +
" </iframe>";
visitPassthroughBlock(new PassthroughBlock(frame, element.options()));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ public record UserId(String name) {}
assertEquals("""
<div>
<h1>Test</h1>
<img src="https://carbon.now.sh/embed?bg=rgba%28171%2C184%2C195%2C100%29&t=vscode&wt=none&l=text%2Fx-java&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=48px&ph=32px&ln=true&fl=1&fm=Droid+Sans+Mono&fs=13px&lh=133%25&si=false&es=2x&wm=false&code=public+record+UserId%28String+name%29+%7B%7D%0A" alt="image">
<iframe
src="https://carbon.now.sh/embed?bg=rgba%28171%2C184%2C195%2C100%29&t=vscode&wt=none&l=text%2Fx-java&width=680&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=48px&ph=32px&ln=true&fl=1&fm=Droid+Sans+Mono&fs=13px&lh=133%25&si=false&es=2x&wm=false&code=public+record+UserId%28String+name%29+%7B%7D%0A"
style="width: 1024px; height: 473px; border:0; transform: scale(1); overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
</div>
""", renderer.result());
}
Expand Down

0 comments on commit 85f6fe3

Please sign in to comment.