Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #349 create HTML from IDE using an editor action #354

Merged
merged 3 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions FEATURES.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ It looks relative to the `pom.xml` for `target/generated-snippets` or relative t
Once it finds this folder, it sets the snippets attributes and enables the `+++operation::[]+++` block macro.

EXPERIMENTAL:
The user can choose _Create PDF from current file_ to convert the file in the current AsciiDoc editor to a PDF using AsciiDoctor PDF version 1.5.x. If creating the PDF succeeds, the PDF is opened in the system's PDF viewer.
To find out more how to configure the output and formatting, please visit https://asciidoctor.org/docs/asciidoctor-pdf/.

* The user can choose _Create PDF from current file_ to convert the file in the current AsciiDoc editor to a PDF using AsciiDoctor PDF version 1.5.0-beta.2. If creating the PDF succeeds, the PDF is opened in the system's PDF viewer. To find out more how to configure the output and formatting, please visit https://asciidoctor.org/docs/asciidoctor-pdf/.
* The user can also choose _Create HTML from current file_ to convert the file to HTML format. If creating HTML succeeds, the exported file is opened in the system's default browser.

=== Advanced

Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/asciidoc/intellij/AsciiDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
Expand Down Expand Up @@ -476,7 +477,8 @@ public String render(String text, String config, List<String> extensions, Notifi
}
}

public void convertToPdf(File file, String config, List<String> extensions) {
public void convertTo(File file, String config, List<String> extensions, FileType format) {

Notifier notifier = this::notifyAlways;
synchronized (AsciiDoc.class) {
CollectingLogHandler logHandler = new CollectingLogHandler();
Expand All @@ -489,11 +491,11 @@ public void convertToPdf(File file, String config, List<String> extensions) {
LocalFileSystem.getInstance().findFileByIoFile(new File(projectBasePath)),
LocalFileSystem.getInstance().findFileByIoFile(fileBaseDir));
try {
Asciidoctor asciidoctor = initWithExtensions(extensions, springRestDocsSnippets != null, "pdf");
Asciidoctor asciidoctor = initWithExtensions(extensions, springRestDocsSnippets != null, format.toString());
prependConfig.setConfig(config);
asciidoctor.registerLogHandler(logHandler);
try {
asciidoctor.convertFile(file, getDefaultOptions("pdf", springRestDocsSnippets));
asciidoctor.convertFile(file, getExportOptions(getDefaultOptions(format.toString(), springRestDocsSnippets), format));
} finally {
prependConfig.setConfig("");
asciidoctor.unregisterLogHandler(logHandler);
Expand Down Expand Up @@ -529,6 +531,13 @@ public void convertToPdf(File file, String config, List<String> extensions) {
}
}

public Map<String, Object> getExportOptions(Map<String, Object> options, FileType fileType) {
if (fileType == FileType.HTML) {
options.put(Options.HEADER_FOOTER, true);
}
return options;
}

private Map<String, Object> getDefaultOptions(String backend, VirtualFile springRestDocsSnippets) {
AttributesBuilder builder = AttributesBuilder.attributes()
.showTitle(true)
Expand Down Expand Up @@ -572,4 +581,20 @@ private Map<String, Object> getDefaultOptions(String backend, VirtualFile spring

return opts.asMap();
}

public enum FileType {
PDF("pdf"),
HTML("html5");

private final String formatType;

FileType(String formatType) {
this.formatType = formatType;
}

@Override
public String toString() {
return formatType;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.asciidoc.intellij.actions.asciidoc;

import com.intellij.ide.BrowserUtil;
import com.intellij.ide.projectView.ProjectView;
import com.intellij.ide.projectView.impl.ProjectViewPane;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.util.ProgressIndicatorBase;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.apache.commons.io.FileUtils;
import org.asciidoc.intellij.AsciiDoc;
import org.asciidoc.intellij.editor.AsciiDocPreviewEditor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

/**
* @author Balasubramanian Naagarajan(balabarath)
*/
public class CreateHtmlAction extends AsciiDocAction {

public static final String ID = "org.asciidoc.intellij.actions.asciidoc.CreateHtmlAction";

private Project project;

@Override
public boolean displayTextInToolbar() {
// this doesn't have an icon, therefore show text
return true;
}

@Override
public void actionPerformed(AnActionEvent event) {
project = event.getProject();
if (project == null) {
return;
}
Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor == null) {
return;
}

VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());
if (file == null) {
return;
}
if (file.getCanonicalPath() == null) {
return;
}

ApplicationManager.getApplication().runWriteAction(() ->
ProgressManager.getInstance().executeProcessUnderProgress(() -> {
ApplicationManager.getApplication().saveAll();
File fileBaseDir = new File("");
VirtualFile parent = file.getParent();
if (parent != null && parent.getCanonicalPath() != null) {
// parent will be null if we use Language Injection and Fragment Editor
fileBaseDir = new File(parent.getCanonicalPath());
}
Path tempImagesPath = AsciiDoc.tempImagesPath();
try {

AsciiDoc asciiDoc = new AsciiDoc(project.getBasePath(), fileBaseDir,
tempImagesPath, file.getName());
List<String> extensions = AsciiDoc.getExtensions(project);
String config = AsciiDoc.config(editor.getDocument(), project);

asciiDoc.convertTo(new File(file.getCanonicalPath()), config, extensions, AsciiDoc.FileType.HTML);
VirtualFile virtualFile = VirtualFileManager.getInstance()
.refreshAndFindFileByUrl(changeFileExtensionToHtml(file.getUrl()));
updateProjectView(virtualFile != null ? virtualFile : parent);
if (virtualFile != null) {
BrowserUtil.browse(virtualFile);
}
} finally {
if (tempImagesPath != null) {
try {
FileUtils.deleteDirectory(tempImagesPath.toFile());
} catch (IOException _ex) {
Logger.getInstance(AsciiDocPreviewEditor.class).warn("could not remove temp folder", _ex);
}
}
}
}, new ProgressIndicatorBase()));

}

private String changeFileExtensionToHtml(String filePath) {
return filePath.replaceAll("\\.(adoc|asciidoc|ad)$", ".html");
}

private void updateProjectView(VirtualFile virtualFile) {
//update project view
ProjectView projectView = ProjectView.getInstance(project);
projectView.changeView(ProjectViewPane.ID);
projectView.select(null, virtualFile, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void actionPerformed(AnActionEvent event) {
tempImagesPath, file.getName());
List<String> extensions = AsciiDoc.getExtensions(project);
String config = AsciiDoc.config(editor.getDocument(), project);
asciiDoc.convertToPdf(new File(file.getCanonicalPath()), config, extensions);
asciiDoc.convertTo(new File(file.getCanonicalPath()), config, extensions, AsciiDoc.FileType.PDF);
VirtualFile virtualFile = VirtualFileManager.getInstance()
.refreshAndFindFileByUrl(file.getUrl().replaceAll("\\.(adoc|asciidoc|ad)$", ".pdf"));
updateProjectView(virtualFile != null ? virtualFile : parent);
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
description="Create PDF from current file">
</action>

<action class="org.asciidoc.intellij.actions.asciidoc.CreateHtmlAction"
id="org.asciidoc.intellij.actions.asciidoc.CreateHtmlAction"
text="HTML"
description="Create HTML from current file">
</action>

<!--
<action id="asciidoc.convert.document6" class="org.asciidoc.intellij.actions.ConvertToAsciiDocAction"
text="Insert image" description="Convert this document to AsciiDoc" />
Expand Down
36 changes: 35 additions & 1 deletion src/test/java/org/asciidoc/intellij/AsciiDocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testShouldRenderPdf() throws IOException {
fw.close();

// when...
this.asciidoc.convertToPdf(asciidoc, "", new ArrayList<>());
this.asciidoc.convertTo(asciidoc, "", new ArrayList<>(), AsciiDoc.FileType.PDF);

// then...
Assert.assertTrue(pdf.exists());
Expand All @@ -84,6 +84,40 @@ public void testShouldRenderPdf() throws IOException {
}
}

public void testShouldRenderHtml() throws IOException {
// given...
File asciidoc = File.createTempFile("asciidocforhtml", ".adoc");
File html = new File(asciidoc.getAbsoluteFile().getAbsolutePath().replaceAll("\\.adoc$", ".html"));
try {
Assert.assertTrue("replacement should have worked", html.getName().endsWith(".html"));
if (html.exists()) {
fail("HTML already exists, but shouldn't before running AsciiDoc");
}
Writer fw = Files.newBufferedWriter(asciidoc.toPath(), UTF_8);
fw.write("Hello world.");
fw.close();

// when...
this.asciidoc.convertTo(asciidoc, "", new ArrayList<>(), AsciiDoc.FileType.HTML);

// then...
Assert.assertTrue(html.exists());

} finally {
// cleanup...
if (asciidoc.exists()) {
if (!asciidoc.delete()) {
log.warn("unable to delete source file");
}
}
if (html.exists()) {
if (!html.delete()) {
log.warn("unable to delete destination file");
}
}
}
}

public void testShouldRenderAttributesAsciidoc() {
String expectedContent = "should replace attribute placeholder with value";
AsciiDocApplicationSettings.getInstance().getAsciiDocPreviewSettings().getAttributes().put("attr", expectedContent);
Expand Down