Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/create d2 diagrams extension #589

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"java.configuration.updateBuildConfiguration": "interactive",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx4G -Xms100m -Xlog:disable",
"maven.view": "hierarchical",
"[markdown]": {
"editor.defaultFormatter": null,
"editor.formatOnSave": false
}
}
41 changes: 41 additions & 0 deletions flexmark-ext-d2-diagrams/flexmark-ext-d2-diagrams.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FlexmarkExt.ModuleBuildProperties">
<option name="extensionName" value="D2 Diagrams" />
<option name="extensionPackage" value="com.vladsch.flexmark.ext.d2" />
<option name="extensionPackagePrefix" value="com.vladsch.flexmark.ext" />
<option name="delimiterProcessor" value="false" />
<option name="blockParser" value="true" />
<option name="blockPreProcessor" value="false" />
<option name="linkRefProcessor" value="false" />
<option name="paragraphPreProcessor" value="false" />
<option name="documentPostProcessor" value="false" />
<option name="customBlockNode" value="true" />
<option name="customNodeRepository" value="false" />
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="flexmark-util-ast" />
<orderEntry type="module" module-name="flexmark-util-builder" />
<orderEntry type="module" module-name="flexmark-util-data" />
<orderEntry type="module" module-name="flexmark-util-dependency" />
<orderEntry type="module" module-name="flexmark-util-html" />
<orderEntry type="module" module-name="flexmark-util-misc" />
<orderEntry type="module" module-name="flexmark-util-sequence" />
<orderEntry type="module" module-name="flexmark-util-visitor" />
<orderEntry type="module" module-name="flexmark" />
<orderEntry type="module" module-name="flexmark-test-util" scope="TEST" />
<orderEntry type="module" module-name="flexmark-core-test" scope="TEST" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="module" module-name="flexmark-core-test" scope="TEST" />
<orderEntry type="library" name="org.jetbrains:annotations" level="project" />
</component>
</module>
34 changes: 34 additions & 0 deletions flexmark-ext-d2-diagrams/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-java</artifactId>
<version>0.64.8</version>
</parent>

<artifactId>flexmark-ext-d2-diagrams</artifactId>
<name>flexmark-java extension d2 diagrams</name>
<description>flexmark-java extension for d2 diagrams</description>

<dependencies>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-util</artifactId>
</dependency>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark</artifactId>
</dependency>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-test-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vladsch.flexmark</groupId>
<artifactId>flexmark-core-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.vladsch.flexmark.ext.d2;

import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;

public class D2Block extends Block {
@NotNull
@Override
public BasedSequence[] getSegments() {
return EMPTY_SEGMENTS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.vladsch.flexmark.ext.d2;

import com.vladsch.flexmark.ext.d2.internal.*;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.MutableDataHolder;

import org.jetbrains.annotations.NotNull;

/**
* Extension for d2 diagrams
*/
public class D2Extension implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension {
private D2Extension() {
}

public static D2Extension create() {
return new D2Extension();
}

@Override
public void rendererOptions(@NotNull MutableDataHolder options) {

}

@Override
public void parserOptions(MutableDataHolder options) {

}

@Override
public void extend(Parser.Builder parserBuilder) {
parserBuilder.customBlockParserFactory(new D2BlockParser.Factory());
}

@Override
public void extend(@NotNull HtmlRenderer.Builder htmlRendererBuilder, @NotNull String rendererType) {
if (htmlRendererBuilder.isRendererType("HTML") || htmlRendererBuilder.isRendererType("JIRA")) {
htmlRendererBuilder.nodeRendererFactory(new D2NodeRenderer.Factory());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.vladsch.flexmark.ext.d2;

import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class D2Node extends Node {
private BasedSequence key;
//private List<BasedSequence> values;

@NotNull
@Override
public BasedSequence[] getSegments() {
return new BasedSequence[] { key };
}

public D2Node(BasedSequence key, List<BasedSequence> values) {
this.key = key;
//this.values = values;
for (BasedSequence value : values) {
appendChild(new D2Value(value));
}
}

public String getKey() {
return key.toString();
}

public BasedSequence getKeySequence() {
return key;
}

public void setKey(BasedSequence key) {
this.key = key;
}

public List<String> getValues() {
ArrayList<String> list = new ArrayList<>();
Node child = getFirstChild();
while (child != null) {
list.add(child.getChars().toString());
child = child.getNext();
}
return list;
}

public List<BasedSequence> getValuesSequences() {
ArrayList<BasedSequence> list = new ArrayList<>();
Node child = getFirstChild();
while (child != null) {
list.add(child.getChars());
child = child.getNext();
}
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.vladsch.flexmark.ext.d2;

import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;

public class D2Value extends Node {
@NotNull
@Override
public BasedSequence[] getSegments() {
return EMPTY_SEGMENTS;
}

public D2Value() {
}

public D2Value(BasedSequence chars) {
super(chars);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.vladsch.flexmark.ext.d2.internal;

import com.vladsch.flexmark.ext.d2.D2Block;
import com.vladsch.flexmark.ext.d2.D2Node;
import com.vladsch.flexmark.parser.InlineParser;
import com.vladsch.flexmark.parser.block.*;
import com.vladsch.flexmark.parser.core.DocumentBlockParser;
import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.ast.BlockContent;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class D2BlockParser extends AbstractBlockParser {
final private static Pattern REGEX_BEGIN = Pattern.compile("^`{3}d2(\\s.*)?");
final private static Pattern REGEX_END = Pattern.compile("^`{3}(\\s.*)?");

private boolean inD2Block;
private BasedSequence currentKey;
private List<BasedSequence> currentValues;
private D2Block block;
private BlockContent content;

public D2BlockParser() {
inD2Block = true;
currentKey = null;
currentValues = new ArrayList<>();
block = new D2Block();
content = new BlockContent();
}

@Override
public Block getBlock() {
return block;
}

@Override
public boolean isContainer() {
return false;
}

@Override
public void addLine(ParserState state, BasedSequence line) {
content.add(line, state.getIndent());
}

@Override
public void closeBlock(ParserState state) {
block.setContent(content.getLines().subList(0, content.getLineCount()));
block.setCharsFromContent();
content = null;
}

@Override
public BlockContinue tryContinue(ParserState state) {
final BasedSequence line = state.getLine();

if (inD2Block) {
if (REGEX_END.matcher(line).matches()) {
System.out.println("test");
D2Node child = new D2Node(line, currentValues);
child.setCharsFromContent();
block.appendChild(child);
return BlockContinue.finished();
} else {
return BlockContinue.atIndex(state.getIndex());
}
} else if (REGEX_BEGIN.matcher(line).matches()) {
inD2Block = true;
return BlockContinue.atIndex(state.getIndex());
}
return BlockContinue.none();
}

@Override
public void parseInlines(InlineParser inlineParser) {
}

public static class Factory implements CustomBlockParserFactory {
@Nullable
@Override
public Set<Class<?>> getAfterDependents() {
return null;
}

@Nullable
@Override
public Set<Class<?>> getBeforeDependents() {
return null;
}

@Override
public boolean affectsGlobalScope() {
return false;
}

@NotNull
@Override
public BlockParserFactory apply(@NotNull DataHolder options) {
return new BlockFactory(options);
}
}

private static class BlockFactory extends AbstractBlockParserFactory {
private BlockFactory(DataHolder options) {
super(options);
}

@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
CharSequence line = state.getLine();
BlockParser parentParser = matchedBlockParser.getBlockParser();
// check whether this line is the first line of whole document or not
if (parentParser instanceof DocumentBlockParser && parentParser.getBlock().getFirstChild() == null &&
REGEX_BEGIN.matcher(line).matches()) {
return BlockStart.of(new D2BlockParser()).atIndex(state.getNextNonSpaceIndex());
}

return BlockStart.none();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.vladsch.flexmark.ext.d2.internal;

import com.vladsch.flexmark.ext.d2.D2Node;
import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.NodeRenderer;
import com.vladsch.flexmark.html.renderer.NodeRendererContext;
import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
import com.vladsch.flexmark.util.data.DataHolder;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;

public class D2NodeRenderer implements NodeRenderer {
public D2NodeRenderer(DataHolder options) {

}

@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
set.add(new NodeRenderingHandler<>(D2Node.class, this::render));
return set;
}

private void render(D2Node node, NodeRendererContext context, HtmlWriter html) {
html.tag("p");
html.text("rendering d2 node");
html.tag("/p");
}

public static class Factory implements NodeRendererFactory {
@NotNull
@Override
public NodeRenderer apply(@NotNull DataHolder options) {
return new D2NodeRenderer(options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.vladsch.flexmark.ext.d2.internal;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.vladsch.flexmark.ext.d2;
Loading