Skip to content

Commit

Permalink
pass prepended config via plugin instead of adding it to the content (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Sep 2, 2019
1 parent 37453c7 commit db872f3
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 76 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.1 (work in progress)

- support .asciidoctorconfig for PDF creation (#325)

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

- fix 'unable to read file' when creating a PDF and working with extensions (#325)
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/org/asciidoc/intellij/AsciiDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.geronimo.gshell.io.SystemOutputHijacker;
import org.asciidoc.intellij.actions.asciidoc.AsciiDocAction;
import org.asciidoc.intellij.asciidoc.PrependConfig;
import org.asciidoc.intellij.editor.AsciiDocPreviewEditor;
import org.asciidoc.intellij.editor.javafx.JavaFxHtmlPanelProvider;
import org.asciidoc.intellij.settings.AsciiDocApplicationSettings;
Expand Down Expand Up @@ -61,7 +62,6 @@
import java.util.List;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.function.IntConsumer;
import java.util.logging.Logger;

/**
Expand All @@ -71,6 +71,8 @@ public class AsciiDoc {

private static Asciidoctor asciidoctor;

private static PrependConfig prependConfig;

private com.intellij.openapi.diagnostic.Logger log =
com.intellij.openapi.diagnostic.Logger.getInstance(AsciiDoc.class);

Expand Down Expand Up @@ -144,6 +146,8 @@ private void initWithExtensions(List<String> extensions) {
try {
asciidoctor = Asciidoctor.Factory.create();
asciidoctor.registerLogHandler(logHandler);
prependConfig = new PrependConfig();
asciidoctor.javaExtensionRegistry().preprocessor(prependConfig);
// disable JUL logging of captured messages
// https://github.com/asciidoctor/asciidoctorj/issues/669
Logger.getLogger("asciidoctor").setUseParentHandlers(false);
Expand Down Expand Up @@ -255,8 +259,6 @@ private void notify(ByteArrayOutputStream boasOut, ByteArrayOutputStream boasErr
}
StringBuilder message = new StringBuilder();
message.append("Error during rendering ").append(name).append("; ").append(logRecord.getSeverity().name()).append(" ");
// TODO: for the current file there will be line numbers, but no file name.
// before we log the line numbers of the current file, they would need to be adjusted be the offset prefix
if (logRecord.getCursor() != null && logRecord.getCursor().getFile() != null) {
message.append(logRecord.getCursor().getFile()).append(":").append(logRecord.getCursor().getLineNumber());
}
Expand Down Expand Up @@ -298,7 +300,7 @@ public static Path tempImagesPath() {
}

@NotNull
public static String prependConfig(Document document, Project project, IntConsumer offset) {
public static String config(Document document, Project project) {
VirtualFile currentFile = FileDocumentManager.getInstance().getFile(document);
StringBuilder tempContent = new StringBuilder();
if (currentFile != null) {
Expand Down Expand Up @@ -329,9 +331,6 @@ public static String prependConfig(Document document, Project project, IntConsum
}
}
}
int offsetLineNo = (int) tempContent.chars().filter(i -> i == '\n').count();
tempContent.append(document.getText());
offset.accept(offsetLineNo);
return tempContent.toString();
}

Expand Down Expand Up @@ -362,10 +361,14 @@ public interface Notifier {
}

public String render(String text, List<String> extensions) {
return render(text, extensions, this::notify);
return render(text, "", extensions, this::notify);
}

public String render(String text, String config, List<String> extensions) {
return render(text, config, extensions, this::notify);
}

public String render(String text, List<String> extensions, Notifier notifier) {
public String render(String text, String config, List<String> extensions, Notifier notifier) {
synchronized (AsciiDoc.class) {
CollectingLogHandler logHandler = new CollectingLogHandler();
ClassLoader old = Thread.currentThread().getContextClassLoader();
Expand All @@ -376,9 +379,11 @@ public String render(String text, List<String> extensions, Notifier notifier) {
try {
initWithExtensions(extensions);
asciidoctor.registerLogHandler(logHandler);
prependConfig.setConfig(config);
try {
return "<div id=\"content\">\n" + asciidoctor.convert(text, getDefaultOptions("html5")) + "\n</div>";
} finally {
prependConfig.setConfig("");
asciidoctor.unregisterLogHandler(logHandler);
}
} catch (Exception | ServiceConfigurationError ex) {
Expand Down Expand Up @@ -408,7 +413,7 @@ public String render(String text, List<String> extensions, Notifier notifier) {
}
}

public void renderPdf(File file, List<String> extensions) {
public void renderPdf(File file, String config, List<String> extensions) {
Notifier notifier = this::notifyAlways;
synchronized (AsciiDoc.class) {
CollectingLogHandler logHandler = new CollectingLogHandler();
Expand All @@ -419,10 +424,12 @@ public void renderPdf(File file, List<String> extensions) {
Thread.currentThread().setContextClassLoader(AsciiDocAction.class.getClassLoader());
try {
initWithExtensions(extensions);
prependConfig.setConfig(config);
asciidoctor.registerLogHandler(logHandler);
try {
asciidoctor.convertFile(file, getDefaultOptions("pdf"));
} finally {
prependConfig.setConfig("");
asciidoctor.unregisterLogHandler(logHandler);
}
} catch (Exception | ServiceConfigurationError ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ public void actionPerformed(AnActionEvent event) {
try {
AsciiDoc asciiDoc = new AsciiDoc(project.getBasePath(), fileBaseDir,
tempImagesPath, file.getName());
/* TODO: content from .asciidoctorconfig not included, needing more experiments and ideas
how to include this into a file on disk. Maybe convert it to an extension? */
List<String> extensions = AsciiDoc.getExtensions(project);
asciiDoc.renderPdf(new File(file.getCanonicalPath()), extensions);
String config = AsciiDoc.config(editor.getDocument(), project);
asciiDoc.renderPdf(new File(file.getCanonicalPath()), config, extensions);
VirtualFile virtualFile = VirtualFileManager.getInstance()
.refreshAndFindFileByUrl(file.getUrl().replaceAll("\\.(adoc|asciidoc|ad)$", ".pdf"));
updateProjectView(virtualFile != null ? virtualFile : parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
public class AsciiDocAnnotationResultType {

private final Document document;
private final int offsetLineNo;
private List<LogRecord> logRecords;

public AsciiDocAnnotationResultType(Document document, int offsetLineNo) {
public AsciiDocAnnotationResultType(Document document) {
this.document = document;
this.offsetLineNo = offsetLineNo;
}

public Document getDocument() {
Expand All @@ -26,10 +24,6 @@ public List<LogRecord> getLogRecords() {
return logRecords;
}

public int getOffsetLineNo() {
return offsetLineNo;
}

public static class Message {
private final HighlightSeverity severity;
private final Integer line;
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/asciidoc/intellij/annotator/AsciiDocInfoType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
public class AsciiDocInfoType {
private final PsiFile file;
private final Editor editor;
private final String contentWithConfig;
private final String content;
private final String config;
private final List<String> extensions;
private final int offsetLineNo;

public AsciiDocInfoType(PsiFile file, Editor editor, String contentWithConfig, List<String> extensions, int offsetLineNo) {
public AsciiDocInfoType(PsiFile file, Editor editor, String content, String config, List<String> extensions) {
this.file = file;
this.editor = editor;
this.contentWithConfig = contentWithConfig;
this.content = content;
this.config = config;
this.extensions = extensions;
this.offsetLineNo = offsetLineNo;
}

public PsiFile getFile() {
Expand All @@ -28,15 +28,15 @@ public Editor getEditor() {
return editor;
}

public String getContentWithConfig() {
return contentWithConfig;
public String getContent() {
return content;
}

public List<String> getExtensions() {
return extensions;
public String getConfig() {
return config;
}

public int getOffsetLineNo() {
return offsetLineNo;
public List<String> getExtensions() {
return extensions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Run Asciidoc and use the warnings and errors as annotations in the file.
Expand All @@ -41,10 +40,9 @@ public class ExternalAnnotator extends com.intellij.lang.annotation.ExternalAnno
@Nullable
@Override
public AsciiDocInfoType collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) {
AtomicInteger offsetLineNo = new AtomicInteger(0);
final String contentWithConfig = AsciiDoc.prependConfig(editor.getDocument(), file.getProject(), offsetLineNo::set);
final String config = AsciiDoc.config(editor.getDocument(), file.getProject());
List<String> extensions = AsciiDoc.getExtensions(file.getProject());
return new AsciiDocInfoType(file, editor, contentWithConfig, extensions, offsetLineNo.get());
return new AsciiDocInfoType(file, editor, editor.getDocument().getText(), config, extensions);
}

@Nullable
Expand All @@ -64,8 +62,7 @@ public AsciiDocAnnotationResultType doAnnotate(AsciiDocInfoType collectedInfo) {
}
}

AsciiDocAnnotationResultType asciidocAnnotationResultType = new AsciiDocAnnotationResultType(editor.getDocument(),
collectedInfo.getOffsetLineNo());
AsciiDocAnnotationResultType asciidocAnnotationResultType = new AsciiDocAnnotationResultType(editor.getDocument());

if (!AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().isShowAsciiDocWarningsAndErrorsInEditor()) {
asciidocAnnotationResultType.setLogRecords(Collections.emptyList());
Expand All @@ -76,7 +73,7 @@ public AsciiDocAnnotationResultType doAnnotate(AsciiDocInfoType collectedInfo) {
try {
AsciiDoc asciiDoc = new AsciiDoc(file.getProject().getBasePath(), fileBaseDir,
tempImagesPath, name);
asciiDoc.render(collectedInfo.getContentWithConfig(), collectedInfo.getExtensions(), (boasOut, boasErr, logRecords)
asciiDoc.render(collectedInfo.getContent(), collectedInfo.getConfig(), collectedInfo.getExtensions(), (boasOut, boasErr, logRecords)
-> asciidocAnnotationResultType.setLogRecords(logRecords));
} finally {
if (tempImagesPath != null) {
Expand Down Expand Up @@ -110,7 +107,7 @@ public void apply(@NotNull PsiFile file, AsciiDocAnnotationResultType annotation
// the line number used for creating the annotation (starting with 0)
int lineNumberForAnnotation = 0;
if (logRecord.getCursor() != null && logRecord.getCursor().getFile() == null && logRecord.getCursor().getLineNumber() >= 0) {
lineNumber = logRecord.getCursor().getLineNumber() - annotationResult.getOffsetLineNo();
lineNumber = logRecord.getCursor().getLineNumber();
lineNumberForAnnotation = lineNumber - 1;
if (lineNumberForAnnotation < 0) {
// logRecords created in the prepended .asciidoctorconfig elements - will be shown on line zero
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/asciidoc/intellij/asciidoc/PrependConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.asciidoc.intellij.asciidoc;

import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Preprocessor;
import org.asciidoctor.extension.PreprocessorReader;

import java.util.Arrays;

/**
* Prepend a configuration before a file.
* This will push back the configuration lines at the beginning.
* This will not change the line original numbers in the file.
* If messages are reported for the configuration lines, they will receive negative numbers.
*/
public class PrependConfig extends Preprocessor {
private String config = "";

@Override
public void process(Document document, PreprocessorReader reader) {
reader.restoreLines(Arrays.asList(config.split("\n")));
}

public void setConfig(String config) {
this.config = config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected static String getCssLines(@Nullable String inlineCss) {
return result.toString();
}

public abstract void scrollToLine(int line, int lineCount, int offsetLineNo);
public abstract void scrollToLine(int line, int lineCount);

public Editor getEditor() {
return editor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public class AsciiDocPreviewEditor extends UserDataHolderBase implements FileEdi
private transient String currentContent = null;

private transient int targetLineNo = 0;
private transient int offsetLineNo = 0;
private transient int currentLineNo = 0;

/**
Expand Down Expand Up @@ -130,22 +129,22 @@ public AsciiDoc call() {
});

private void render() {
final String contentWithConfig = AsciiDoc.prependConfig(document, project, o -> offsetLineNo = o);
final String config = AsciiDoc.config(document, project);
final String content = document.getText();
List<String> extensions = AsciiDoc.getExtensions(project);

lazyExecutor.execute(() -> {
try {
if (!contentWithConfig.equals(currentContent)) {
currentContent = contentWithConfig;

String markup = asciidoc.get().render(contentWithConfig, extensions);
if (!(config + content).equals(currentContent)) {
currentContent = config + content;
String markup = asciidoc.get().render(content, config, extensions);
if (markup != null) {
myPanel.setHtml(markup);
}
}
if (currentLineNo != targetLineNo) {
currentLineNo = targetLineNo;
myPanel.scrollToLine(targetLineNo, document.getLineCount(), offsetLineNo);
myPanel.scrollToLine(targetLineNo, document.getLineCount());
}
ApplicationManager.getApplication().invokeLater(myHtmlPanelWrapper::repaint);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -137,13 +136,13 @@ private boolean isDarcula() {
@NotNull
public String getHtml(@NotNull VirtualFile file, @NotNull Project project) {
Document document = FileDocumentManager.getInstance().getDocument(file);
AtomicInteger offsetLineNo = new AtomicInteger();
final String contentWithConfig = AsciiDoc.prependConfig(document, project, offsetLineNo::set);
Objects.requireNonNull(document);
final String config = AsciiDoc.config(document, project);
List<String> extensions = AsciiDoc.getExtensions(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(contentWithConfig, extensions);
String html = asciiDoc.render(document.getText(), config, extensions);
if (file.getParent() != null) {
// parent will be null if we use Language Injection and Fragment Editor
base = file.getParent().getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,12 @@ public void render() {
}

@Override
public void scrollToLine(final int line, final int lineCount, int offsetLineNo) {
public void scrollToLine(final int line, final int lineCount) {
this.lineCount = lineCount;
this.offset = offsetLineNo;
runInPlatformWhenAvailable(() -> {
JavaFxHtmlPanel.this.getWebViewGuaranteed().getEngine().executeScript(
"if ('__IntelliJTools' in window) " +
"__IntelliJTools.scrollToLine(" + line + ", " + lineCount + ", " + offsetLineNo + ");"
"__IntelliJTools.scrollToLine(" + line + ", " + lineCount + ");"
);
final Object result = JavaFxHtmlPanel.this.getWebViewGuaranteed().getEngine().executeScript(
"document.documentElement.scrollTop || document.body.scrollTop");
Expand Down
Loading

0 comments on commit db872f3

Please sign in to comment.