Skip to content

Commit

Permalink
[Core] Add support for embedding code in image insert block
Browse files Browse the repository at this point in the history
The embedded code can be extracted and run through a diagram generator
such as ditaa or PlantUml.
  • Loading branch information
0x8000-0000 committed Apr 27, 2020
1 parent 7b086d3 commit e455f9f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'net.signbit.samx'
version '0.4.5'
version '0.4.6'

sourceCompatibility = 1.8

Expand Down
8 changes: 5 additions & 3 deletions src/main/antlr/SamXParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
;
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/net/signbit/samx/visitors/EmbeddedCodeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/signbit/samx/visitors/XmlTextVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/test/java/net/signbit/samx/parser/PrettyPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ public void testGrids()

testIsPretty("grids/multispan.samx");
}

@Test
public void testDiagram()
{
testIsPretty("insert/diagram.samx");
}
}
15 changes: 15 additions & 0 deletions src/test/resources/insert/diagram.samx
Original file line number Diff line number Diff line change
@@ -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 |
+-------------------------+

0 comments on commit e455f9f

Please sign in to comment.