Created
November 2, 2021 19:32
-
-
Save alexpdp7/cb1aa275c3ae49542c70c625ad50b96f to your computer and use it in GitHub Desktop.
AsciiDoctor to JSON AST Java Converter hack
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
import java.io.File; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import org.asciidoctor.Asciidoctor; | |
import org.asciidoctor.Options; | |
import org.asciidoctor.ast.ContentNode; | |
import org.asciidoctor.ast.Cursor; | |
import org.asciidoctor.ast.List; | |
import org.asciidoctor.ast.ListItem; | |
import org.asciidoctor.ast.PhraseNode; | |
import org.asciidoctor.ast.StructuralNode; | |
import org.asciidoctor.converter.ConverterFor; | |
import org.asciidoctor.converter.StringConverter; | |
import com.google.gson.Gson; | |
@ConverterFor("ast") | |
public class Converter extends StringConverter { | |
public Converter(String backend, Map<String, Object> opts) { | |
super(backend, opts); | |
} | |
public String convert(ContentNode node, String transform, Map<Object, Object> opts) { | |
if(node instanceof List) { | |
return convertList((List) node, transform, opts); | |
} | |
if(node instanceof PhraseNode) { | |
return convertPhraseNode((PhraseNode) node, transform, opts); | |
} | |
if(node instanceof StructuralNode) { | |
return convertStructuralNode((StructuralNode) node, transform, opts); | |
} | |
throw new RuntimeException(node.getClass().toString()); | |
} | |
public String convertStructuralNode(StructuralNode structuralNode, String transform, Map<Object, Object> ops) { | |
Map<String, Object> json = new HashMap<>(); | |
json.put("content", getJsonContent(structuralNode)); | |
json.put("title", structuralNode.getTitle()); | |
json.put("position", getJson(structuralNode.getSourceLocation())); | |
return new Gson().toJson(json); | |
} | |
protected Object getJsonContent(StructuralNode structuralNode) { | |
if(structuralNode.getContentModel().equals("simple") || structuralNode.getContentModel().equals("verbatim")) { | |
return Map.of("text", structuralNode.getContent(), "position", getJson(structuralNode.getSourceLocation())); | |
} | |
else if(structuralNode.getContentModel().equals("compound")) { | |
return structuralNode.getBlocks().stream().map(sn -> convert(sn, null, null)).map(s -> new Gson().fromJson(s, Object.class)).collect(Collectors.toList()); | |
} | |
else { | |
Object content = structuralNode.getContent(); | |
if(content == null) { | |
return "null"; | |
} | |
return new Gson().fromJson(content.toString(), Object.class); | |
} | |
} | |
protected Map<String, String> getJson(Cursor sourceLocation) { | |
return Map.of("lineNumber", Integer.toString(sourceLocation.getLineNumber())); | |
} | |
public String convertList(List list, String transform, Map<Object, Object> ops) { | |
java.util.List<Map<String, String>> itemsJson = list.getItems().stream().map(ListItem.class::cast).map(this::convertListItem).collect(Collectors.toList()); | |
return new Gson().toJson(Map.ofEntries(Map.entry("items", itemsJson))); | |
} | |
public Map<String, String> convertListItem(ListItem listItem) { | |
return Map.ofEntries(Map.entry("text", listItem.getText())); | |
} | |
public String convertPhraseNode(PhraseNode phraseNode, String transform, Map<Object, Object> ops) { | |
return new Gson().toJson(Map.ofEntries(Map.entry("text", phraseNode.getText()))); | |
} | |
public static void main(String[] args) { | |
Asciidoctor asciidoctor = Asciidoctor.Factory.create(); | |
asciidoctor.javaConverterRegistry().register(Converter.class); | |
System.out.println(asciidoctor.convertFile(new File(args[0]), Options.builder().sourcemap(true).backend("ast").toFile(false).build())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment