11package com .structurizr .dsl ;
22
3+ import com .structurizr .export .mermaid .MermaidDiagramExporter ;
4+ import com .structurizr .export .plantuml .StructurizrPlantUMLExporter ;
35import com .structurizr .importer .diagrams .kroki .KrokiImporter ;
46import com .structurizr .importer .diagrams .mermaid .MermaidImporter ;
57import com .structurizr .importer .diagrams .plantuml .PlantUMLImporter ;
68import com .structurizr .util .ImageUtils ;
79import com .structurizr .util .Url ;
810import com .structurizr .view .ImageView ;
11+ import com .structurizr .view .ModelView ;
12+ import com .structurizr .view .View ;
913
1014import java .io .File ;
1115
1216final class ImageViewContentParser extends AbstractParser {
1317
14- private static final String PLANTUML_GRAMMAR = "plantuml <file|url>" ;
15- private static final String MERMAID_GRAMMAR = "mermaid <file|url>" ;
18+ private static final String PLANTUML_GRAMMAR = "plantuml <file|url|viewKey >" ;
19+ private static final String MERMAID_GRAMMAR = "mermaid <file|url|viewKey >" ;
1620 private static final String KROKI_GRAMMAR = "kroki <format> <file|url>" ;
1721 private static final String IMAGE_GRAMMAR = "image <file|url>" ;
1822
19- private static final int TITLE_INDEX = 1 ;
20- private static final int DESCRIPTION_INDEX = 1 ;
21-
2223 private static final int PLANTUML_SOURCE_INDEX = 1 ;
2324 private static final int MERMAID_SOURCE_INDEX = 1 ;
2425 private static final int KROKI_FORMAT_INDEX = 1 ;
@@ -32,7 +33,7 @@ final class ImageViewContentParser extends AbstractParser {
3233 }
3334
3435 void parsePlantUML (ImageViewDslContext context , File dslFile , Tokens tokens ) {
35- // plantuml <file|url>
36+ // plantuml <file|url|viewKey >
3637
3738 if (tokens .hasMoreThan (PLANTUML_SOURCE_INDEX )) {
3839 throw new RuntimeException ("Too many tokens, expected: " + PLANTUML_GRAMMAR );
@@ -44,16 +45,23 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {
4445 String source = tokens .get (PLANTUML_SOURCE_INDEX );
4546
4647 try {
47- if (Url .isUrl (source )) {
48- RemoteContent content = readFromUrl (source );
49- new PlantUMLImporter ().importDiagram (context .getView (), content .getContent ());
50- context .getView ().setTitle (source .substring (source .lastIndexOf ("/" )+1 ));
48+ View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
49+ if (viewWithKey instanceof ModelView ) {
50+ StructurizrPlantUMLExporter exporter = new StructurizrPlantUMLExporter ();
51+ String plantuml = exporter .export ((ModelView )viewWithKey ).getDefinition ();
52+ new PlantUMLImporter ().importDiagram (context .getView (), plantuml );
5153 } else {
52- if (!restricted ) {
53- File file = new File (dslFile .getParentFile (), source );
54- new PlantUMLImporter ().importDiagram (context .getView (), file );
54+ if (Url .isUrl (source )) {
55+ RemoteContent content = readFromUrl (source );
56+ new PlantUMLImporter ().importDiagram (context .getView (), content .getContent ());
57+ context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
5558 } else {
56- throw new RuntimeException ("PlantUML source must be specified as a URL when running in restricted mode" );
59+ if (!restricted ) {
60+ File file = new File (dslFile .getParentFile (), source );
61+ new PlantUMLImporter ().importDiagram (context .getView (), file );
62+ } else {
63+ throw new RuntimeException ("PlantUML source must be specified as a URL when running in restricted mode" );
64+ }
5765 }
5866 }
5967 } catch (Exception e ) {
@@ -66,7 +74,7 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {
6674 }
6775
6876 void parseMermaid (ImageViewDslContext context , File dslFile , Tokens tokens ) {
69- // mermaid <file|url>
77+ // mermaid <file|url|viewKey >
7078
7179 if (tokens .hasMoreThan (MERMAID_SOURCE_INDEX )) {
7280 throw new RuntimeException ("Too many tokens, expected: " + MERMAID_GRAMMAR );
@@ -78,16 +86,23 @@ void parseMermaid(ImageViewDslContext context, File dslFile, Tokens tokens) {
7886 String source = tokens .get (MERMAID_SOURCE_INDEX );
7987
8088 try {
81- if (Url .isUrl (source )) {
82- RemoteContent content = readFromUrl (source );
83- new MermaidImporter ().importDiagram (context .getView (), content .getContent ());
84- context .getView ().setTitle (source .substring (source .lastIndexOf ("/" )+1 ));
89+ View viewWithKey = context .getWorkspace ().getViews ().getViewWithKey (source );
90+ if (viewWithKey instanceof ModelView ) {
91+ MermaidDiagramExporter exporter = new MermaidDiagramExporter ();
92+ String mermaid = exporter .export ((ModelView )viewWithKey ).getDefinition ();
93+ new MermaidImporter ().importDiagram (context .getView (), mermaid );
8594 } else {
86- if (!restricted ) {
87- File file = new File (dslFile .getParentFile (), source );
88- new MermaidImporter ().importDiagram (context .getView (), file );
95+ if (Url .isUrl (source )) {
96+ RemoteContent content = readFromUrl (source );
97+ new MermaidImporter ().importDiagram (context .getView (), content .getContent ());
98+ context .getView ().setTitle (source .substring (source .lastIndexOf ("/" ) + 1 ));
8999 } else {
90- throw new RuntimeException ("Mermaid source must be specified as a URL when running in restricted mode" );
100+ if (!restricted ) {
101+ File file = new File (dslFile .getParentFile (), source );
102+ new MermaidImporter ().importDiagram (context .getView (), file );
103+ } else {
104+ throw new RuntimeException ("Mermaid source must be specified as a URL when running in restricted mode" );
105+ }
91106 }
92107 }
93108 } catch (Exception e ) {
0 commit comments