1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package org.zwobble.mammoth.html;
import org.zwobble.mammoth.util.MammothLists;
import java.util.List;
import java.util.Map;
import static org.zwobble.mammoth.util.MammothLists.list;
import static org.zwobble.mammoth.util.MammothMaps.map;
public class Html {
public static final HtmlNode FORCE_WRITE = HtmlForceWrite.FORCE_WRITE;
public static String write(List<HtmlNode> nodes) {
StringBuilder builder = new StringBuilder();
nodes.forEach(node -> HtmlWriter.write(node, builder));
return builder.toString();
}
public static HtmlNode text(String value) {
return new HtmlTextNode(value);
}
public static HtmlNode element(String tagName) {
return element(tagName, list());
}
public static HtmlNode element(String tagName, Map<String, String> attributes) {
return element(tagName, attributes, list());
}
public static HtmlNode element(String tagName, List<HtmlNode> children) {
return element(tagName, map(), children);
}
public static HtmlNode element(String tagName, Map<String, String> attributes, List<HtmlNode> children) {
return new HtmlElement(tagName, attributes, children, false);
}
public static HtmlNode collapsibleElement(String tagName, List<HtmlNode> children) {
return collapsibleElement(tagName, map(), children);
}
public static HtmlNode collapsibleElement(String tagName, Map<String, String> attributes, List<HtmlNode> children) {
return new HtmlElement(tagName, attributes, children, true);
}
public static HtmlNode selfClosingElement(String tagName) {
return selfClosingElement(tagName, map());
}
public static HtmlNode selfClosingElement(String tagName, Map<String, String> attributes) {
return new HtmlSelfClosingElement(tagName, attributes);
}
public static List<HtmlNode> stripEmpty(List<HtmlNode> nodes) {
return MammothLists.eagerFlatMap(nodes, node -> stripEmpty(node));
}
private static List<HtmlNode> stripEmpty(HtmlNode node) {
return node.accept(new HtmlNode.Mapper<List<HtmlNode>>() {
@Override
public List<HtmlNode> visit(HtmlElement element) {
List<HtmlNode> children = stripEmpty(element.getChildren());
if (children.isEmpty()) {
return list();
} else {
return list(new HtmlElement(
element.getTagName(),
element.getAttributes(),
children,
element.isCollapsible()));
}
}
@Override
public List<HtmlNode> visit(HtmlSelfClosingElement element) {
return list(element);
}
@Override
public List<HtmlNode> visit(HtmlTextNode node) {
if (node.getValue().isEmpty()) {
return list();
} else {
return list(node);
}
}
@Override
public List<HtmlNode> visit(HtmlForceWrite forceWrite) {
return list(forceWrite);
}
});
}
public static List<HtmlNode> collapse(List<HtmlNode> nodes) {
return nodes;
}
}
|