Skip to content

Commit

Permalink
while JavaFX preview forces PNG diagram for readability, browser and …
Browse files Browse the repository at this point in the history
…PDF should use diagram in the format specified in the source (#325)
  • Loading branch information
ahus1 committed Sep 7, 2019
1 parent 72691ce commit c741690
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ This document provides a high-level view of the changes introduced by release.
[[releasenotes]]
== Release notes

=== 0.30.4 (work in progress)

- while JavaFX preview forces PNG diagram for readability, browser and PDF should use diagram in the format specified in the source (#325)

=== 0.30.3 (preview, available from Github releases)

- support operation block macro in https://docs.spring.io/spring-restdocs/docs/current/reference/html5/[spring-restdocs] and auto-detect the snippets folder (#312)
Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ patchPluginXml {
pluginDescription "${file('src/main/resources/META-INF/description.html').getText('UTF-8')}"
}

runIde {
jvmArgs = ['-Xmx1024m']
}

publishPlugin {
username 'ahus1'
token System.getenv('PLUGIN_REPO_TOKEN')
Expand Down
56 changes: 38 additions & 18 deletions src/main/java/org/asciidoc/intellij/AsciiDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
Expand All @@ -72,7 +73,20 @@
*/
public class AsciiDoc {

private static Asciidoctor asciidoctor;
private static class MaxHashMap extends LinkedHashMap<String, Asciidoctor> {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Asciidoctor> eldest) {
// cache up to three instances (for example: javafx, pdf, spring-restdocs)
if (this.size() > 3) {
eldest.getValue().shutdown();
return true;
} else {
return false;
}
}
}

private static MaxHashMap instances = new MaxHashMap();

private static PrependConfig prependConfig;

Expand All @@ -83,8 +97,6 @@ public class AsciiDoc {
SystemOutputHijacker.install();
}

private static String hash = "";

/**
* Base directory to look up includes.
*/
Expand All @@ -104,7 +116,7 @@ public AsciiDoc(String projectBasePath, File fileBaseDir, Path imagesPath, Strin
this.name = name;
}

private void initWithExtensions(List<String> extensions, boolean springRestDocs) {
private Asciidoctor initWithExtensions(List<String> extensions, boolean springRestDocs, String format) {
synchronized (AsciiDoc.class) {
boolean extensionsEnabled;
AsciiDocApplicationSettings asciiDocApplicationSettings = AsciiDocApplicationSettings.getInstance();
Expand All @@ -120,13 +132,14 @@ private void initWithExtensions(List<String> extensions, boolean springRestDocs)
md = calcMd(projectBasePath, Collections.emptyList());
}
if (springRestDocs) {
md = md + "restdoc";
md = md + ".restdoc";
}
if (!md.equals(hash)) {
if (asciidoctor != null) {
asciidoctor.shutdown();
asciidoctor = null;
}
if (format.equals("javafx")) {
// special plantuml-png-patch.rb only loaded here
md = md + "." + format;
}
Asciidoctor asciidoctor = instances.get(md);
if (asciidoctor == null) {
ByteArrayOutputStream boasOut = new ByteArrayOutputStream();
ByteArrayOutputStream boasErr = new ByteArrayOutputStream();
SystemOutputHijacker.register(new PrintStream(boasOut), new PrintStream(boasErr));
Expand Down Expand Up @@ -165,11 +178,13 @@ private void initWithExtensions(List<String> extensions, boolean springRestDocs)
}
asciidoctor.rubyExtensionRegistry().loadClass(is).treeprocessor("SourceLineTreeProcessor");

is = this.getClass().getResourceAsStream("/plantuml-png-patch.rb");
if (is == null) {
throw new RuntimeException("unable to load script plantuml-png-patch.rb");
if (format.equals("javafx")) {
is = this.getClass().getResourceAsStream("/plantuml-png-patch.rb");
if (is == null) {
throw new RuntimeException("unable to load script plantuml-png-patch.rb");
}
asciidoctor.rubyExtensionRegistry().loadClass(is);
}
asciidoctor.rubyExtensionRegistry().loadClass(is);

if (springRestDocs) {
is = this.getClass().getResourceAsStream("/springrestdoc-operation-blockmacro.rb");
Expand All @@ -184,7 +199,7 @@ private void initWithExtensions(List<String> extensions, boolean springRestDocs)
asciidoctor.rubyExtensionRegistry().requireLibrary(extension);
}
}
hash = md;
instances.put(md, asciidoctor);
} finally {
if (oldEncoding != null) {
System.setProperty("file.encoding", oldEncoding);
Expand All @@ -196,6 +211,7 @@ private void initWithExtensions(List<String> extensions, boolean springRestDocs)
notify(boasOut, boasErr, Collections.emptyList());
}
}
return asciidoctor;
}
}

Expand Down Expand Up @@ -260,7 +276,7 @@ private void notify(ByteArrayOutputStream boasOut, ByteArrayOutputStream boasErr
!AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().isShowAsciiDocWarningsAndErrorsInEditor());
}

private void notifyAlways(ByteArrayOutputStream boasOut, ByteArrayOutputStream boasErr, List<LogRecord> logRecords) {
public void notifyAlways(ByteArrayOutputStream boasOut, ByteArrayOutputStream boasErr, List<LogRecord> logRecords) {
notify(boasOut, boasErr, logRecords, true);
}

Expand Down Expand Up @@ -386,6 +402,10 @@ public String render(String text, String config, List<String> extensions) {
}

public String render(String text, String config, List<String> extensions, Notifier notifier) {
return render(text, config, extensions, notifier, "javafx");
}

public String render(String text, String config, List<String> extensions, Notifier notifier, String format) {
synchronized (AsciiDoc.class) {
CollectingLogHandler logHandler = new CollectingLogHandler();
ClassLoader old = Thread.currentThread().getContextClassLoader();
Expand All @@ -398,7 +418,7 @@ public String render(String text, String config, List<String> extensions, Notifi
LocalFileSystem.getInstance().findFileByIoFile(fileBaseDir)
);
try {
initWithExtensions(extensions, springRestDocsSnippets != null);
Asciidoctor asciidoctor = initWithExtensions(extensions, springRestDocsSnippets != null, format);
asciidoctor.registerLogHandler(logHandler);
prependConfig.setConfig(config);
try {
Expand Down Expand Up @@ -447,7 +467,7 @@ public void renderPdf(File file, String config, List<String> extensions) {
LocalFileSystem.getInstance().findFileByIoFile(new File(projectBasePath)),
LocalFileSystem.getInstance().findFileByIoFile(fileBaseDir));
try {
initWithExtensions(extensions, springRestDocsSnippets != null);
Asciidoctor asciidoctor = initWithExtensions(extensions, springRestDocsSnippets != null, "pdf");
prependConfig.setConfig(config);
asciidoctor.registerLogHandler(logHandler);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public String getHtml(@NotNull VirtualFile file, @NotNull Project project) {
Objects.requireNonNull(file.getParent().getCanonicalPath(), "we will have files, these will always have a parent directory");
AsciiDoc asciiDoc = new AsciiDoc(project.getBasePath(), new File(file.getParent().getCanonicalPath()),
imagesPath, file.getName());
String html = asciiDoc.render(document.getText(), config, extensions);
String html = asciiDoc.render(document.getText(), config, extensions, asciiDoc::notifyAlways, "html");
if (file.getParent() != null) {
// parent will be null if we use Language Injection and Fragment Editor
base = file.getParent().getPath();
Expand Down

0 comments on commit c741690

Please sign in to comment.