Remove attrs object from format

This commit is contained in:
Dimitry Ivanov 2018-08-24 23:25:48 +03:00
parent 7881420cbd
commit 97c994d866
8 changed files with 84 additions and 65 deletions

@ -18,16 +18,38 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import ix.Ix;
import ix.IxFunction;
import ix.IxPredicate;
import static ru.noties.markwon.renderer.visitor.TestSpan.BLOCK_QUOTE;
import static ru.noties.markwon.renderer.visitor.TestSpan.BULLET_LIST;
import static ru.noties.markwon.renderer.visitor.TestSpan.CODE;
import static ru.noties.markwon.renderer.visitor.TestSpan.CODE_BLOCK;
import static ru.noties.markwon.renderer.visitor.TestSpan.EMPHASIS;
import static ru.noties.markwon.renderer.visitor.TestSpan.HEADING;
import static ru.noties.markwon.renderer.visitor.TestSpan.IMAGE;
import static ru.noties.markwon.renderer.visitor.TestSpan.LINK;
import static ru.noties.markwon.renderer.visitor.TestSpan.ORDERED_LIST;
import static ru.noties.markwon.renderer.visitor.TestSpan.PARAGRAPH;
import static ru.noties.markwon.renderer.visitor.TestSpan.STRIKE_THROUGH;
import static ru.noties.markwon.renderer.visitor.TestSpan.STRONG_EMPHASIS;
import static ru.noties.markwon.renderer.visitor.TestSpan.SUB_SCRIPT;
import static ru.noties.markwon.renderer.visitor.TestSpan.SUPER_SCRIPT;
import static ru.noties.markwon.renderer.visitor.TestSpan.TABLE_ROW;
import static ru.noties.markwon.renderer.visitor.TestSpan.TASK_LIST;
import static ru.noties.markwon.renderer.visitor.TestSpan.THEMATIC_BREAK;
import static ru.noties.markwon.renderer.visitor.TestSpan.UNDERLINE;
abstract class TestDataReader {
private static final String FOLDER = "tests/";
@ -79,9 +101,40 @@ abstract class TestDataReader {
static class Reader {
private static final String ATTRS = "attrs";
private static final String TEXT = "text";
private static final Set<String> TAGS;
static {
TAGS = new HashSet<>(Arrays.asList(
STRONG_EMPHASIS,
EMPHASIS,
BLOCK_QUOTE,
CODE,
CODE_BLOCK,
ORDERED_LIST,
BULLET_LIST,
THEMATIC_BREAK,
HEADING,
STRIKE_THROUGH,
TASK_LIST,
TABLE_ROW,
PARAGRAPH,
IMAGE,
LINK,
SUPER_SCRIPT,
SUB_SCRIPT,
UNDERLINE,
HEADING + "1",
HEADING + "2",
HEADING + "3",
HEADING + "4",
HEADING + "5",
HEADING + "6",
TEXT
));
}
private final String file;
Reader(@NonNull String file) {
@ -160,8 +213,7 @@ abstract class TestDataReader {
// it can additionally contain "attrs" key which is the attributes
// b:
// - text: "bold"
// attrs:
// href: "my-href"
// href: "my-href"
final int size = array.size();
@ -172,16 +224,25 @@ abstract class TestDataReader {
final JsonObject object = array.get(i).getAsJsonObject();
String name = null;
Map<String, String> attributes = null;
Map<String, String> attributes = new HashMap<>(0);
for (String key : object.keySet()) {
if (ATTRS.equals(key)) {
attributes = attributes(object.get(key));
} else if (name == null) {
name = key;
if (TAGS.contains(key)) {
if (name == null) {
name = key;
} else {
throw new RuntimeException("Unexpected key in object: " + object);
}
} else {
// we allow only 2 keys: span and/or attributes and no more
throw new RuntimeException("Unexpected key in object: " + object);
// fill attribute map with it
final String value;
final JsonElement valueElement = object.get(key);
if (valueElement.isJsonNull()) {
value = null;
} else {
value = valueElement.getAsString();
}
attributes.put(key, value);
}
}
@ -189,10 +250,6 @@ abstract class TestDataReader {
throw new RuntimeException("Object is missing tag name: " + object);
}
if (attributes == null) {
attributes = Collections.emptyMap();
}
final JsonElement element = object.get(name);
if (TEXT.equals(name)) {
@ -263,33 +320,5 @@ abstract class TestDataReader {
return new TestConfig(map);
}
@NonNull
private static Map<String, String> attributes(@NonNull JsonElement element) {
final JsonObject object = element.isJsonObject()
? element.getAsJsonObject()
: null;
final Map<String, String> attributes;
if (object != null) {
attributes = new HashMap<>(object.size());
for (String key : object.keySet()) {
final String value;
final JsonElement valueElement = object.get(key);
if (valueElement.isJsonNull()) {
value = null;
} else {
value = valueElement.getAsString();
}
attributes.put(key, value);
}
} else {
attributes = Collections.emptyMap();
}
return attributes;
}
}
}

@ -13,8 +13,7 @@ config:
output:
- text: "Here is some "
- a: "link"
attrs:
href: "https://my.href"
href: "https://my.href"
- text: " "
- b:
- text: "bold "

@ -66,8 +66,7 @@ output:
- h2: "link"
- text: "\n"
- a: "a"
attrs:
href: "a://href"
href: "a://href"
- text: "\n"
- h2: "unordered-list"
- text: "\n"
@ -78,18 +77,15 @@ output:
- h2: "ordered-list"
- text: "\n"
- ol: "ol1"
attrs:
start: 1
start: 1
- text: "\n"
- ol: "ol2"
attrs:
start: 2
start: 2
- text: "\n"
- h2: "image"
- text: "\n"
- img: "img"
attrs:
src: "img://src"
src: "img://src"
- text: "\n"
- h2: "blockquote"
- text: "\n"

@ -2,5 +2,4 @@ input: "[link](#href)"
output:
- a: "link"
attrs:
href: "#href"
href: "#href"

@ -2,7 +2,6 @@ input: "![image](#href)"
output:
- img: "image"
attrs:
src: "#href"
imageSize: null
replacementTextIsLink: false
src: "#href"
imageSize: null
replacementTextIsLink: false

@ -2,5 +2,4 @@ input: "1. ol"
output:
- ol: "ol"
attrs:
start: 1
start: 1

@ -2,6 +2,5 @@ input: "- [ ] task-list"
output:
- task-list: "task-list"
attrs:
blockIdent: 1
done: false
blockIdent: 1
done: false

@ -2,5 +2,4 @@ input: "* ul"
output:
- ul: "ul"
attrs:
level: 0
level: 0