From 3e2786a004eddf968a5fa7d5a92dff5b1b5f7734 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 24 Feb 2016 08:16:16 +0000 Subject: Create internal package --- src/main/java/org/zwobble/mammoth/html/Html.java | 179 ----------------------- 1 file changed, 179 deletions(-) delete mode 100644 src/main/java/org/zwobble/mammoth/html/Html.java (limited to 'src/main/java/org/zwobble/mammoth/html/Html.java') diff --git a/src/main/java/org/zwobble/mammoth/html/Html.java b/src/main/java/org/zwobble/mammoth/html/Html.java deleted file mode 100644 index e1bcef7..0000000 --- a/src/main/java/org/zwobble/mammoth/html/Html.java +++ /dev/null @@ -1,179 +0,0 @@ -package org.zwobble.mammoth.html; - -import org.zwobble.mammoth.util.MammothLists; -import org.zwobble.mammoth.util.MammothOptionals; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static org.zwobble.mammoth.util.Casts.tryCast; -import static org.zwobble.mammoth.util.MammothIterables.tryGetLast; -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 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 attributes) { - return element(tagName, attributes, list()); - } - - public static HtmlNode element(String tagName, List children) { - return element(tagName, map(), children); - } - - public static HtmlNode element(String tagName, Map attributes, List children) { - return new HtmlElement(list(tagName), attributes, children, false); - } - - public static HtmlNode collapsibleElement(String tagName) { - return collapsibleElement(list(tagName)); - } - - public static HtmlNode collapsibleElement(List tagNames) { - return collapsibleElement(tagNames, map(), list()); - } - - public static HtmlNode collapsibleElement(String tagName, List children) { - return collapsibleElement(tagName, map(), children); - } - - public static HtmlNode collapsibleElement(String tagName, Map attributes, List children) { - return collapsibleElement(list(tagName), attributes, children); - } - - public static HtmlNode collapsibleElement(List tagNames, Map attributes, List children) { - return new HtmlElement(tagNames, attributes, children, true); - } - - public static HtmlNode selfClosingElement(String tagName) { - return selfClosingElement(tagName, map()); - } - - public static HtmlNode selfClosingElement(String tagName, Map attributes) { - return new HtmlSelfClosingElement(tagName, attributes); - } - - public static List stripEmpty(List nodes) { - return MammothLists.eagerFlatMap(nodes, node -> stripEmpty(node)); - } - - private static List stripEmpty(HtmlNode node) { - return node.accept(new HtmlNode.Mapper>() { - @Override - public List visit(HtmlElement element) { - List children = stripEmpty(element.getChildren()); - if (children.isEmpty()) { - return list(); - } else { - return list(new HtmlElement( - element.getTagNames(), - element.getAttributes(), - children, - element.isCollapsible())); - } - } - - @Override - public List visit(HtmlSelfClosingElement element) { - return list(element); - } - - @Override - public List visit(HtmlTextNode node) { - if (node.getValue().isEmpty()) { - return list(); - } else { - return list(node); - } - } - - @Override - public List visit(HtmlForceWrite forceWrite) { - return list(forceWrite); - } - }); - } - - public static List collapse(List nodes) { - List collapsed = new ArrayList<>(); - - for (HtmlNode node : nodes) { - collapsingAdd(collapsed, node); - } - - return collapsed; - } - - private static void collapsingAdd(List collapsed, HtmlNode node) { - HtmlNode collapsedNode = collapse(node); - if (!tryCollapse(collapsed, collapsedNode)) { - collapsed.add(collapsedNode); - } - } - - private static HtmlNode collapse(HtmlNode node) { - return node.accept(new HtmlNode.Mapper() { - @Override - public HtmlNode visit(HtmlElement element) { - return new HtmlElement( - element.getTagNames(), - element.getAttributes(), - collapse(element.getChildren()), - element.isCollapsible()); - } - - @Override - public HtmlNode visit(HtmlSelfClosingElement element) { - return element; - } - - @Override - public HtmlNode visit(HtmlTextNode node) { - return node; - } - - @Override - public HtmlNode visit(HtmlForceWrite forceWrite) { - return forceWrite; - } - }); - } - - private static boolean tryCollapse(List collapsed, HtmlNode node) { - return MammothOptionals.map( - tryGetLast(collapsed).flatMap(last -> tryCast(HtmlElement.class, last)), - tryCast(HtmlElement.class, node), - (last, next) -> { - if (next.isCollapsible() && isMatch(last, next)) { - for (HtmlNode child : next.getChildren()) { - collapsingAdd(last.getChildren(), child); - } - return true; - } else { - return false; - } - } - ).orElse(false); - } - - private static boolean isMatch(HtmlElement first, HtmlElement second) { - return second.getTagNames().contains(first.getTagName()) && - first.getAttributes().equals(second.getAttributes()); - } -} -- cgit v1.2.3