Skip to content

Commit

Permalink
resolves #1279 pass options to ditaa (#1283)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Jun 13, 2022
1 parent 2d33d8f commit b55fd35
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
28 changes: 26 additions & 2 deletions server/src/main/java/io/kroki/server/service/Ditaa.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,43 @@ public void convert(String sourceDecoded, String serviceName, FileFormat fileFor
vertx.executeBlocking(future -> {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
convert(fileFormat, new ByteArrayInputStream(sourceDecoded.getBytes()), outputStream);
convert(fileFormat, options, new ByteArrayInputStream(sourceDecoded.getBytes()), outputStream);
future.complete(outputStream.toByteArray());
} catch (IllegalStateException e) {
future.fail(e);
}
}, res -> handler.handle(res.map(o -> Buffer.buffer((byte[]) o))));
}

static void convert(FileFormat fileFormat, InputStream inputStream, OutputStream outputStream) {
static void convert(FileFormat fileFormat, JsonObject options, InputStream inputStream, OutputStream outputStream) {
List<String> args = new ArrayList<>();
if (fileFormat.equals(FileFormat.SVG)) {
args.add("--svg");
}
String noAntialias = options.getString("no-antialias");
if (noAntialias != null) {
args.add("--no-antialias");
}
String noSeparation = options.getString("no-separation");
if (noSeparation != null) {
args.add("--no-separation");
}
String roundCorners = options.getString("round-corners");
if (roundCorners != null) {
args.add("--round-corners");
}
String scale = options.getString("scale");
if (scale != null) {
args.add("--scale " + scale);
}
String noShadows = options.getString("no-shadows");
if (noShadows != null) {
args.add("--no-shadows");
}
String tabs = options.getString("tabs");
if (tabs != null) {
args.add("--tabs " + tabs);
}
CommandLineConverter.convert(args.toArray(new String[0]), inputStream, outputStream);
}
}
2 changes: 1 addition & 1 deletion server/src/main/java/io/kroki/server/service/Plantuml.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void convert(String sourceDecoded, String serviceName, FileFormat fileFor
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// REMIND: options are unsupported for now.
Ditaa.convert(fileFormat, new ByteArrayInputStream(ditaaContext.getSource().getBytes()), outputStream);
Ditaa.convert(fileFormat, options, new ByteArrayInputStream(ditaaContext.getSource().getBytes()), outputStream);
future.complete(outputStream.toByteArray());
} catch (IllegalStateException e) {
future.fail(e);
Expand Down
40 changes: 40 additions & 0 deletions server/src/test/java/io/kroki/server/service/DitaaServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.kroki.server.service;

import io.kroki.server.format.FileFormat;
import io.vertx.core.json.JsonObject;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

public class DitaaServiceTest {

@Test
public void should_call_ditaa_with_correct_arguments() throws Exception {
String input = "+---------+\n" +
"| cBLU |\n" +
"| |\n" +
"| +----+\n" +
"| |cPNK|\n" +
"| | |\n" +
"+----+----+";
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input.getBytes()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
Ditaa.convert(FileFormat.SVG, new JsonObject(), byteArrayInputStream, byteArrayOutputStream);
// shadows are enabled by default
assertThat(byteArrayOutputStream.toString()).contains("filter=\"url(#shadowBlur)\"");
}
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input.getBytes()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
JsonObject options = new JsonObject();
options.put("no-shadows", "");
Ditaa.convert(FileFormat.SVG, options, byteArrayInputStream, byteArrayOutputStream);
// disable shadows by setting the no-shadows option
assertThat(byteArrayOutputStream.toString()).doesNotContain("filter=\"url(#shadowBlur)\"");
}
}
}

0 comments on commit b55fd35

Please sign in to comment.