Skip to content

Commit

Permalink
Add D2 layout engine option (#1570)
Browse files Browse the repository at this point in the history
* Support ELK layout engine
* Add smoke test for D2 layout option
* Add D2 layout option to documentation
* Check for null 'layout' argument
  • Loading branch information
felixvanoost committed Aug 11, 2023
1 parent 22558e0 commit 5adac1c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions ci/tests/smoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const tests = [
{ engine: 'diagramsnet', file: 'diagramsnet-venn.xml', options: {}, outputFormat: ['svg', 'png'] },
{ engine: 'diagramsnet', file: 'diagramsnet-infography.xml', options: { id: 'foo' }, outputFormat: ['svg', 'png'] },
{ engine: 'd2', file: 'connections.d2', options: {}, outputFormat: ['svg'] },
{ engine: 'd2', file: 'connections.d2', options: { layout: 'elk' }, outputFormat: ['svg'] },
{ engine: 'd2', file: 'connections.d2', options: { sketch: 'true' }, outputFormat: ['svg'] },
{ engine: 'wireviz', file: 'wireviz.yaml', options: {}, outputFormat: ['svg', 'png'] },
{ engine: 'tikz', file: 'periodic-table.tex', options: {}, outputFormat: ['jpeg', 'pdf', 'png', 'svg'] }
Expand Down
6 changes: 6 additions & 0 deletions docs/modules/setup/pages/diagram-options.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ With flag set, option takes effect.

|See: https://d2lang.com/tour/themes/

|layout
|
* `dagre` (default)
* `elk`
|Use an alternate layout engine: https://d2lang.com/tour/layouts/

|sketch
|_flag_ +
empty string ("")
Expand Down
13 changes: 9 additions & 4 deletions server/src/main/java/io/kroki/server/service/D2.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,26 @@ public void convert(String sourceDecoded, String serviceName, FileFormat fileFor
private byte[] d2(byte[] source, JsonObject options) throws IOException, InterruptedException, IllegalStateException {
List<String> commands = new ArrayList<>();
commands.add(binPath);
String value = options.getString("theme");
if (value != null) {
String theme = options.getString("theme");
if (theme != null) {
int themeId = 0;
Integer builtinThemeId = builtinThemes.get(value.toLowerCase().replaceAll("\\s", "-"));
Integer builtinThemeId = builtinThemes.get(theme.toLowerCase().replaceAll("\\s", "-"));
if (builtinThemeId != null) {
themeId = builtinThemeId;
} else {
try {
themeId = Integer.parseInt(value, 10);
themeId = Integer.parseInt(theme, 10);
} catch (NumberFormatException e) {
// ignore, fallback to 0
}
}
commands.add("--theme=" + themeId);
}
String layout = options.getString("layout");
if (layout != null && layout.equals("elk")) {
// Only pass the layout argument if the ELK layout engine is requested (default is 'dagre')
commands.add("--layout=" + layout);
}
String sketch = options.getString("sketch");
if (sketch != null) {
commands.add("--sketch");
Expand Down

0 comments on commit 5adac1c

Please sign in to comment.