Created
November 4, 2021 14:35
-
-
Save alexpdp7/228d15152634af8bb503fdb0725b9138 to your computer and use it in GitHub Desktop.
Parsing AsciiDoc using asciidoctor-intellij-plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.pdp7; | |
import java.util.Arrays; | |
import org.asciidoc.intellij.AsciiDocLanguage; | |
import org.asciidoc.intellij.parser.AsciiDocParserDefinition; | |
import org.asciidoc.intellij.psi.AsciiDocASTFactory; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.intellij.lang.ASTNode; | |
import com.intellij.lang.LanguageASTFactory; | |
import com.intellij.openapi.util.TextRange; | |
import com.intellij.psi.PsiFile; | |
import com.intellij.testFramework.ParsingTestCase; | |
public class AsciiDocParser { | |
public static void main(String[] args) throws Exception { | |
TestAdapter testAdapter = new TestAdapter(); | |
testAdapter.setUp(); | |
PsiFile psiFile = testAdapter.doParseFile("foo", "= Hi\n\nHow are *you*?"); | |
ASTNode astNode = psiFile.getNode(); | |
GsonBuilder gson = new GsonBuilder(); | |
System.out.println(gson.create().toJson(toJsonElement(astNode))); | |
} | |
private static JsonElement toJsonElement(ASTNode astNode) { | |
JsonObject jsonObject = new JsonObject(); | |
jsonObject.addProperty("type", astNode.getElementType().toString()); | |
jsonObject.addProperty("text", astNode.getText()); | |
TextRange textRange = astNode.getTextRange(); | |
jsonObject.addProperty("startOffset", textRange.getStartOffset()); | |
jsonObject.addProperty("endOffset", textRange.getEndOffset()); | |
JsonArray children = new JsonArray(); | |
Arrays.stream(astNode.getChildren(null)).map(n -> toJsonElement(n)).forEach(e -> children.add(e)); | |
jsonObject.add("children", children); | |
return jsonObject; | |
} | |
public static class TestAdapter extends ParsingTestCase { | |
public TestAdapter() { | |
super("parser", "adoc", true, new AsciiDocParserDefinition()); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
addExplicitExtension(LanguageASTFactory.INSTANCE, AsciiDocLanguage.INSTANCE, new AsciiDocASTFactory()); | |
} | |
@Override | |
protected String getTestDataPath() { | |
return "foo"; | |
} | |
public PsiFile doParseFile(String name, String text) { | |
return parseFile(name, text); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "FILE", | |
"text": "= Hi\n\nHow are *you*?", | |
"startOffset": 0, | |
"endOffset": 20, | |
"children": [ | |
{ | |
"type": "ASCIIDOC_SECTION", | |
"text": "= Hi\n\nHow are *you*?", | |
"startOffset": 0, | |
"endOffset": 20, | |
"children": [ | |
{ | |
"type": "AsciiDoc:HEADING", | |
"text": "= Hi", | |
"startOffset": 0, | |
"endOffset": 4, | |
"children": [ | |
{ | |
"type": "AsciiDoc:HEADING_TOKEN", | |
"text": "= Hi", | |
"startOffset": 0, | |
"endOffset": 4, | |
"children": [] | |
} | |
] | |
}, | |
{ | |
"type": "WHITE_SPACE", | |
"text": "\n", | |
"startOffset": 4, | |
"endOffset": 5, | |
"children": [] | |
}, | |
{ | |
"type": "WHITE_SPACE", | |
"text": "\n", | |
"startOffset": 5, | |
"endOffset": 6, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:TEXT", | |
"text": "How", | |
"startOffset": 6, | |
"endOffset": 9, | |
"children": [] | |
}, | |
{ | |
"type": "WHITE_SPACE", | |
"text": " ", | |
"startOffset": 9, | |
"endOffset": 10, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:TEXT", | |
"text": "are", | |
"startOffset": 10, | |
"endOffset": 13, | |
"children": [] | |
}, | |
{ | |
"type": "WHITE_SPACE", | |
"text": " ", | |
"startOffset": 13, | |
"endOffset": 14, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:BOLD_START", | |
"text": "*", | |
"startOffset": 14, | |
"endOffset": 15, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:BOLD", | |
"text": "you", | |
"startOffset": 15, | |
"endOffset": 18, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:BOLD_END", | |
"text": "*", | |
"startOffset": 18, | |
"endOffset": 19, | |
"children": [] | |
}, | |
{ | |
"type": "AsciiDoc:TEXT", | |
"text": "?", | |
"startOffset": 19, | |
"endOffset": 20, | |
"children": [] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment