From e455f9f7cebd10cfabc3500b8deec336c1076414 Mon Sep 17 00:00:00 2001 From: Florin Iucha Date: Sun, 26 Apr 2020 22:18:54 -0400 Subject: [PATCH] [Core] Add support for embedding code in image insert block The embedded code can be extracted and run through a diagram generator such as ditaa or PlantUml. --- README.md | 4 ++++ build.gradle | 2 +- src/main/antlr/SamXParser.g4 | 8 +++++--- .../samx/visitors/EmbeddedCodeVisitor.java | 19 ++++++++++++++++++- .../samx/visitors/PrettyPrinterVisitor.java | 18 +++++++++++++++++- .../signbit/samx/visitors/XmlTextVisitor.java | 7 +++++++ .../samx/parser/PrettyPrinterTest.java | 6 ++++++ src/test/resources/insert/diagram.samx | 15 +++++++++++++++ 8 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/insert/diagram.samx diff --git a/README.md b/README.md index 4557c36..8f1153a 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ which is an asterisk. SAMx also supports a class attribute introduced by a dot. * SAMx grids support row and column spanning. +* SAMx image insert tags can contain the image definition in code blocks. +Those definitions can be extracted by the `extract_code` tool and rendered +to images that are later included in HTML or PDF output. + License for SAM --------------- diff --git a/build.gradle b/build.gradle index c57c261..250f215 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ plugins { } group 'net.signbit.samx' -version '0.4.5' +version '0.4.6' sourceCompatibility = 1.8 diff --git a/src/main/antlr/SamXParser.g4 b/src/main/antlr/SamXParser.g4 index e39ea80..f0236b6 100644 --- a/src/main/antlr/SamXParser.g4 +++ b/src/main/antlr/SamXParser.g4 @@ -214,6 +214,8 @@ unorderedList : (BULLET listElement) NEWLINE* ((BULLET listElement) | NEWLINE)* orderedList : (HASH listElement) NEWLINE* ((HASH listElement) | NEWLINE)* ; +codeBlockDef : CODE_MARKER language=text CLOSE_PAR metadata NEWLINE+ INDENT (externalCode? NEWLINE)+ DEDENT ; + block : NAME TYPESEP blockMetadata NEWLINE+ INDENT block+ DEDENT # TypedBlock | NAME TYPESEP metadata value=flow NEWLINE # Field @@ -227,13 +229,13 @@ block : | STT_INFRG name=NAME CLOSE_PAR metadata # InsertFragment | STT_DEFRG name=NAME CLOSE_PAR metadata NEWLINE+ INDENT block+ DEDENT # DefineFragment | STT_INCL reference=text CLOSE_PAR metadata { parseFile($reference.text); } # IncludeFile - | STT_IMAGE text CLOSE_PAR blockMetadata # InsertImage - | CODE_MARKER language=text CLOSE_PAR metadata NEWLINE+ INDENT (externalCode? NEWLINE)+ DEDENT # CodeBlock + | STT_IMAGE text CLOSE_PAR blockMetadata (NEWLINE INDENT NEWLINE? codeBlockDef DEDENT)? # InsertImage + | codeBlockDef # CodeBlock | STT_GRID blockMetadata NEWLINE+ INDENT (header=generalGridGroup? generalGridHeaderSep)? body=generalGridGroup (generalGridHeaderSep? footer=generalGridGroup)? - DEDENT # GeneralGrid + DEDENT # GeneralGrid | STT_PREC_GRID blockMetadata NEWLINE+ INDENT (preciseGridRow | NEWLINE) + DEDENT # PreciseGrid | NEWLINE # Empty ; diff --git a/src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java b/src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java index 103740d..4df715b 100644 --- a/src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java +++ b/src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java @@ -68,7 +68,7 @@ public StringBuilder visitTypedBlock(SamXParser.TypedBlockContext ctx) } @Override - public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx) + public StringBuilder visitCodeBlockDef(SamXParser.CodeBlockDefContext ctx) { AttributeVisitor attributeVisitor = new AttributeVisitor(); attributeVisitor.visit(ctx.metadata()); @@ -126,4 +126,21 @@ public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx) return null; } + + @Override + public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx) + { + visitCodeBlockDef(ctx.codeBlockDef()); + return null; + } + + @Override + public StringBuilder visitInsertImage(SamXParser.InsertImageContext ctx) + { + if (ctx.codeBlockDef() != null) + { + visitCodeBlockDef(ctx.codeBlockDef()); + } + return null; + } } diff --git a/src/main/java/net/signbit/samx/visitors/PrettyPrinterVisitor.java b/src/main/java/net/signbit/samx/visitors/PrettyPrinterVisitor.java index aa97176..417ee98 100644 --- a/src/main/java/net/signbit/samx/visitors/PrettyPrinterVisitor.java +++ b/src/main/java/net/signbit/samx/visitors/PrettyPrinterVisitor.java @@ -664,6 +664,12 @@ public StringBuilder visitUrl(SamXParser.UrlContext ctx) @Override public StringBuilder visitCodeBlock(SamXParser.CodeBlockContext ctx) + { + return visitCodeBlockDef(ctx.codeBlockDef()); + } + + @Override + public StringBuilder visitCodeBlockDef(SamXParser.CodeBlockDefContext ctx) { StringBuilder builder = new StringBuilder(); @@ -916,7 +922,17 @@ public StringBuilder visitInsertImage(SamXParser.InsertImageContext ctx) builder.append(visitText(ctx.text())); builder.append(')'); builder.append(visitBlockMetadata(ctx.blockMetadata())); - builder.append('\n'); + + if (ctx.codeBlockDef() != null) + { + indentLevel ++; + builder.append(visitCodeBlockDef(ctx.codeBlockDef())); + indentLevel --; + } + else + { + builder.append('\n'); + } return builder; } diff --git a/src/main/java/net/signbit/samx/visitors/XmlTextVisitor.java b/src/main/java/net/signbit/samx/visitors/XmlTextVisitor.java index 6c52a4c..7e804a7 100644 --- a/src/main/java/net/signbit/samx/visitors/XmlTextVisitor.java +++ b/src/main/java/net/signbit/samx/visitors/XmlTextVisitor.java @@ -796,6 +796,13 @@ public Object visitIdentifierAttr(SamXParser.IdentifierAttrContext ctx) @Override public Object visitCodeBlock(SamXParser.CodeBlockContext ctx) + { + visitCodeBlockDef(ctx.codeBlockDef()); + return null; + } + + @Override + public Object visitCodeBlockDef(SamXParser.CodeBlockDefContext ctx) { final int codeBlockIndent = VisitorUtils.getTokenIndent(ctx, tokenStream); diff --git a/src/test/java/net/signbit/samx/parser/PrettyPrinterTest.java b/src/test/java/net/signbit/samx/parser/PrettyPrinterTest.java index 7487b19..f728ebb 100644 --- a/src/test/java/net/signbit/samx/parser/PrettyPrinterTest.java +++ b/src/test/java/net/signbit/samx/parser/PrettyPrinterTest.java @@ -186,4 +186,10 @@ public void testGrids() testIsPretty("grids/multispan.samx"); } + + @Test + public void testDiagram() + { + testIsPretty("insert/diagram.samx"); + } } diff --git a/src/test/resources/insert/diagram.samx b/src/test/resources/insert/diagram.samx new file mode 100644 index 0000000..ca61acb --- /dev/null +++ b/src/test/resources/insert/diagram.samx @@ -0,0 +1,15 @@ +This document includes a diagram rendered using +{ditaa}(:http://ditaa.sourceforge.net/) + +>>>(image diagram.png)(#the-diagram) Pretty Diagram + ```(ditaa)(#diagram) + +--------+ +-------+ +-------+ + | | --+ ditaa +--> | | + | Text | +-------+ |diagram| + |Document| |!magic!| | | + | {d}| | | | | + +---+----+ +-------+ +-------+ + : ^ + | Lots of work | + +-------------------------+ +