From 5adac1ce4c480a5a3353f3c77f1d9edb8d3260cc Mon Sep 17 00:00:00 2001 From: Felix van Oost Date: Fri, 11 Aug 2023 21:28:34 +0000 Subject: [PATCH] Add D2 layout engine option (#1570) * Support ELK layout engine * Add smoke test for D2 layout option * Add D2 layout option to documentation * Check for null 'layout' argument --- ci/tests/smoke.js | 1 + docs/modules/setup/pages/diagram-options.adoc | 6 ++++++ .../src/main/java/io/kroki/server/service/D2.java | 13 +++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ci/tests/smoke.js b/ci/tests/smoke.js index ea569d44e..6cfc47731 100755 --- a/ci/tests/smoke.js +++ b/ci/tests/smoke.js @@ -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'] } diff --git a/docs/modules/setup/pages/diagram-options.adoc b/docs/modules/setup/pages/diagram-options.adoc index a808e3911..8375bb7b3 100644 --- a/docs/modules/setup/pages/diagram-options.adoc +++ b/docs/modules/setup/pages/diagram-options.adoc @@ -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 ("") diff --git a/server/src/main/java/io/kroki/server/service/D2.java b/server/src/main/java/io/kroki/server/service/D2.java index 41703685a..c30769af8 100644 --- a/server/src/main/java/io/kroki/server/service/D2.java +++ b/server/src/main/java/io/kroki/server/service/D2.java @@ -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 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");