Skip to content

Commit

Permalink
Added Asciidoctor console + preferences + improvements #147
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Oct 9, 2018
1 parent 744c4a7 commit 6d08b85
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,27 @@
AMAZONAPIGATEWAY(gateway,"",rectangle)
@enduml
----
----

[plantuml, sample-plantuml-diagram-u2, alt="Use case diagram 2"]
----
@startuml
!include <cloudinsight/tomcat>
!include <cloudinsight/kafka>
!include <cloudinsight/java>
!include <cloudinsight/cassandra>
title Cloudinsight sprites example
skinparam monochrome true
rectangle "<$tomcat>\nwebapp" as webapp
queue "<$kafka>" as kafka
rectangle "<$java>\ndaemon" as daemon
database "<$cassandra>" as cassandra
webapp -> kafka
kafka -> daemon
daemon --> cassandra
@enduml
----
3 changes: 2 additions & 1 deletion asciidoctor-editor-plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.ide,
org.eclipse.core.expressions,
de.jcup.asciidoctoreditor.libs;bundle-version="1.2.0",
de.jcup.asciidoctoreditor.css;bundle-version="1.0.0"
de.jcup.asciidoctoreditor.css;bundle-version="1.0.0",
org.eclipse.ui.console
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Import-Package: org.apache.commons.io,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.jcup.asciidoctoreditor;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import de.jcup.asciidoctoreditor.preferences.AsciiDoctorEditorPreferences;

public class AsciiDoctorConsole {

public static void error(String message) {
MessageConsole myConsole = output(message);

if (AsciiDoctorEditorPreferences.getInstance().isConsoleAlwaysShownOnOutput()){
getConsoleManager().showConsoleView(myConsole);
}
}

protected static MessageConsole output(String message) {
MessageConsole myConsole = findConsole("Asciidoctor");
MessageConsoleStream out = myConsole.newMessageStream();
out.println(message);
return myConsole;
}

public static MessageConsole findConsole(String name) {
IConsoleManager conMan = getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (name.equals(existing[i].getName())) {
return (MessageConsole) existing[i];
}
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, EclipseUtil.createImageDescriptor("icons/asciidoctor-editor.png", AsciiDoctorEditorActivator.PLUGIN_ID));
conMan.addConsoles(new IConsole[] { myConsole });

return myConsole;
}

protected static IConsoleManager getConsoleManager() {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
return conMan;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
Expand All @@ -22,8 +23,6 @@
import org.asciidoctor.extension.JavaExtensionRegistry;
import org.asciidoctor.extension.RubyExtensionRegistry;

import com.kenai.jffi.internal.StubLoader.OS;

import de.jcup.asciidoctoreditor.preferences.AsciiDoctorEditorPreferences;

/**
Expand All @@ -35,117 +34,115 @@
*/
public class InstalledAsciidoctor implements Asciidoctor {


/*
* -------------------------------------------------------------------------
*/
/*
* +++++++++++++++++++++++++++++++ used++ ++++++++++++++++++++++++++++++++++
*/
/*
* -------------------------------------------------------------------------
*/

@Override
public DocumentHeader readDocumentHeader(File filename) {
return AsciiDoctorOSGIWrapper.INSTANCE.getAsciidoctor().readDocumentHeader(filename);
}

@Override
public String convertFile(File filename, Map<String, Object> options) {

List<String> commands = new ArrayList<String>();
if (OSUtil.isWindows()){
if (OSUtil.isWindows()) {
commands.add("cmd.exe");
commands.add("/C");
}
commands.add("asciidoctor");
// for (String key : options.keySet()) {
// commands.add("-"+key);
// commands.add(""+options.get(key));
// }


String outDir = null;

@SuppressWarnings("unchecked")
Map<String,String>attributes =(Map<String, String>) options.get("attributes");
Map<String, String> attributes = (Map<String, String>) options.get("attributes");
for (String key : attributes.keySet()) {
Object value = attributes.get(key);
if (value==null){
if (value == null) {
continue;
}
String v= value.toString();
String v = value.toString();
String attrib = key;
if (v.isEmpty()){
if (v.isEmpty()) {
continue;
}
commands.add("-a");
String safeValue = toWindowsSafeVariant(value);
if (key.equals("outdir")){
outDir=safeValue;
if (key.equals("outdir")) {
outDir = safeValue;
}
attrib+="="+safeValue;
attrib += "=" + safeValue;
commands.add(attrib);
}

// commands.add("-b");
// commands.add("html5");
// commands.add("-s");
// commands.add("unsafe");

// commands.add("--base-dir");
// commands.add(toWindowsSafeVariant(options.get("base_dir")));

// commands.add("--source-dir");
// commands.add(""+options.get("base_dir"));
List<String> preferenceCLICommands = CLITextUtil.convertToList(AsciiDoctorEditorPreferences.getInstance().getArgumentsForInstalledAsciidoctor());

String argumentsForInstalledAsciidoctor = AsciiDoctorEditorPreferences.getInstance()
.getArgumentsForInstalledAsciidoctor();
List<String> preferenceCLICommands = CLITextUtil.convertToList(argumentsForInstalledAsciidoctor);
commands.addAll(preferenceCLICommands);
if (outDir!=null){
if (outDir != null) {
commands.add("-D");
commands.add(outDir);
}

// commands.add("--no-header-footer");
// commands.add("-r");
// commands.add("asciidoctor-diagram");


commands.add(toWindowsSafeVariant(filename.getAbsolutePath()));
StringBuilder sb = new StringBuilder();
for (String command: commands){
if ("cmd.exe".equals(command) || "/C".equals(command)){
continue;
}
sb.append(command);
sb.append(" ");
StringBuilder commandLine = new StringBuilder();
for (String command : commands) {
commandLine.append(command);
commandLine.append(" ");
}
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("installed ascidoctor >>>> executing:"+sb.toString());
AsciiDoctorConsole.output(">> rendering:"+filename.getName());
ProcessBuilder pb = new ProcessBuilder(commands);
pb.inheritIO();
try {
StringBuffer sbx=null;
Process process = pb.start();
process.waitFor(2, TimeUnit.MINUTES);
} catch (InterruptedException | IOException e) {
e.printStackTrace();
try (InputStream is = process.getErrorStream();) {
int c;
sbx = new StringBuffer();
while ((c = is.read()) != -1) {
sbx.append((char) c);
}
String line = sbx.toString();
if (line.isEmpty()){
AsciiDoctorConsole.output(line);
}else{
AsciiDoctorConsole.error(line);
}
}
boolean exitdone = process.waitFor(2, TimeUnit.MINUTES);
int exitCode =-1;
if (exitdone){
exitCode=process.exitValue();
}
if (EclipseDevelopmentSettings.DEBUG_LOGGING_ENABLED){
AsciiDoctorConsole.output("Called:" + commandLine.toString());
AsciiDoctorConsole.output("Exitcode:"+exitCode);
}
if (exitCode>0){
AsciiDoctorEclipseLogAdapter.INSTANCE.logWarn("Installed Asciidoctor rendering failed for '"+filename.getName()+"'\n\nCommandLine was:\n"+commandLine.toString()+"\n\nResulted in exitcode:"+exitCode+", \nLast output:"+sbx);
}
} catch (Exception e) {
AsciiDoctorEditorUtil.logError("Cannot execute installed asciidoctor\n\nCommandline was:\n"+commandLine.toString(), e);
}

return null;
}

private String toWindowsSafeVariant(Object obj){
String command = ""+obj;
boolean windowsPath = command.indexOf('\\')!=-1;
if (!windowsPath){
private String toWindowsSafeVariant(Object obj) {
String command = "" + obj;
boolean windowsPath = command.indexOf('\\') != -1;
if (!windowsPath) {
return command;
}
return "\""+command+"\"";
return "\"" + command + "\"";
}

/*
* -------------------------------------------------------------------------
*/
/*
* +++++++++++++++++++++++++++++++ unused ++++++++++++++++++++++++++++++++++
*/
/*
* -------------------------------------------------------------------------
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public enum AsciiDoctorEditorPreferenceConstants implements PreferenceIdentifiab

P_USE_INSTALLED_ASCIIDOCTOR_ENABLED("useInstalledAsciidoctorEnabled"),

P_INSTALLED_ASCIICDOCTOR_ARGUMENTS("installedAsciidoctorArguments")
P_INSTALLED_ASCIICDOCTOR_ARGUMENTS("installedAsciidoctorArguments"),

P_SHOW_ASCIIDOC_CONSOLE_ON_OUTPUT("showAsciidoctorConsoleOnOutput"),
;

private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void initializeDefaultPreferences() {
store.setDefault(P_USE_INSTALLED_ASCIIDOCTOR_ENABLED.getId(), false);
store.setDefault(P_INSTALLED_ASCIICDOCTOR_ARGUMENTS.getId(),
"-r asciidoctor-diagram\n--no-header-footer\n");
store.setDefault(P_SHOW_ASCIIDOC_CONSOLE_ON_OUTPUT.getId(), true);

/* +++++++++++++++++ */
/* + Editor Colors + */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
Expand Down Expand Up @@ -214,7 +213,7 @@ protected void createExternalPreviewParts(Composite composite) {

protected void createAsciidoctorGroup(Composite composite) {
GridData groupLayoutData = new GridData();
groupLayoutData.horizontalSpan = 2;
groupLayoutData.horizontalSpan = 1;
groupLayoutData.widthHint = 400;

Group group = new Group(composite, SWT.NONE);
Expand All @@ -226,25 +225,30 @@ protected void createAsciidoctorGroup(Composite composite) {
P_USE_INSTALLED_ASCIIDOCTOR_ENABLED.getId(), "Use installed asciidoctor",
group);
useInstalledAsciidoctor.getDescriptionControl(group).setToolTipText(
"When enabled the asciidoctor will be used instead of embedded variant.\n\n"
+ "Using the installed version enables you to use templates, newer asciidoctor features etc.\n\n");
"When enabled the installed asciidoctor will be used instead of embedded variant.\n\n"
+ "Using the installed version enables you to use templates, newer asciidoctor features etc. Just setup your behaviour in CLI arguments.\n\n");
addField(useInstalledAsciidoctor);

MultiLineStringFieldEditor cliArguments = new MultiLineStringFieldEditor(P_INSTALLED_ASCIICDOCTOR_ARGUMENTS.getId(),
"Additional\nCLI-Arguments", group);
cliArguments.getTextControl().setToolTipText("Setup arguments which shall be added to CLI call of installed asciidoctor instance.\n\nYou can use multiple lines.");
GridData data = new GridData();
data.verticalAlignment = SWT.CENTER;
data.verticalAlignment = SWT.BEGINNING;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
data.heightHint=75;
data.widthHint=250;
cliArguments.getTextControl().setLayoutData(data);

// cliArguments.getTextControl().setLayoutData(data);
addField(cliArguments);

createDependency(useInstalledAsciidoctor.getChangeControl(group),
cliArguments.getTextControl(group));

AccessibleBooleanFieldEditor consoleEnabled = new AccessibleBooleanFieldEditor(P_SHOW_ASCIIDOC_CONSOLE_ON_OUTPUT.getId(), "Show console when asciidoctor writes to standard error", group);
addField(consoleEnabled);


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,8 @@ public String getArgumentsForInstalledAsciidoctor(){
return getStringPreference(P_INSTALLED_ASCIICDOCTOR_ARGUMENTS);
}

public boolean isConsoleAlwaysShownOnOutput() {
return getBooleanPreference(P_SHOW_ASCIIDOC_CONSOLE_ON_OUTPUT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.regex.Pattern;

public class CLITextUtil {
private static final Pattern P = Pattern.compile("[\n\t]");
private static final Pattern P = Pattern.compile("[\n\r\t]");

public static List<String> convertToList(String string) {
List<String> list = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

public class CLITextUtilTest {


@Test
public void alpha_newline_omega_results_in_two_entries() {
List<String> commands = CLITextUtil.convertToList("alpha\nomega");
assertContains(commands, "alpha","omega");
}
@Test
public void alpha_cr_newline_omega_results_in_two_entries() {
List<String> commands = CLITextUtil.convertToList("alpha\r\nomega");
assertContains(commands, "alpha","omega");
}
@Test
public void newline_alpha_newline_omega_results_in_two_entries() {
List<String> commands = CLITextUtil.convertToList("\nalpha\nomega");
assertContains(commands, "alpha","omega");
Expand Down Expand Up @@ -81,7 +85,7 @@ public void emty_string_is_empty_list() {
}

private void assertContains(List<String> commands, String ...expected){
assertEquals(Arrays.asList(expected),commands);
assertEquals(Arrays.asList(expected).toString(),commands.toString());
}

}

0 comments on commit 6d08b85

Please sign in to comment.