Search in sources :

Example 6 with TextNode

use of org.jsoup.nodes.TextNode in project mylyn.docs by eclipse.

the class RemoveEmptySpansProcessor method process.

@Override
public void process(Document document) {
    Element body = document.body();
    boolean modifiedOne = false;
    do {
        modifiedOne = false;
        // remove empty spans, and eliminate tags that only contain whitespace
        for (Element element : body.getAllElements()) {
            if (Html.isSpanElement(element)) {
                // remove span with no children
                List<Node> childNodes = element.childNodes();
                if (childNodes.isEmpty()) {
                    element.remove();
                    modifiedOne = true;
                } else {
                    // a span with a single text child that is only whitespace is removed (text is retained)
                    if (childNodes.size() == 1) {
                        Node node = childNodes.get(0);
                        if (node instanceof TextNode) {
                            TextNode textNode = (TextNode) node;
                            String text = textNode.text();
                            if (text.trim().length() == 0) {
                                textNode.remove();
                                element.before(textNode);
                                element.remove();
                                modifiedOne = true;
                            }
                            normalizeTextNodes((Element) textNode.parent());
                        }
                    }
                }
            }
            // a br within a span that is a first or last child is moved out
            Element parent = element.parent();
            if (element.tagName().equalsIgnoreCase("br") && Html.isSpanElement(parent)) {
                // $NON-NLS-1$
                List<Node> childNodes = parent.childNodes();
                if (childNodes.get(0) == element) {
                    element.remove();
                    parent.before(element);
                    modifiedOne = true;
                } else if (childNodes.get(childNodes.size() - 1) == element) {
                    element.remove();
                    parent.after(element);
                    modifiedOne = true;
                }
            }
        }
    } while (modifiedOne);
}
Also used : Element(org.jsoup.nodes.Element) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) TextNode(org.jsoup.nodes.TextNode)

Example 7 with TextNode

use of org.jsoup.nodes.TextNode in project mylyn.docs by eclipse.

the class WhitespaceCleanupProcessor method removeWhitespaceBefore.

private void removeWhitespaceBefore(Element element) {
    Node previousSibling = element.previousSibling();
    if (previousSibling instanceof TextNode) {
        TextNode textNode = (TextNode) previousSibling;
        String text = textNode.getWholeText();
        int startOfTrailingWhitespace = lastIndexOfNonWhitespace(text) + 1;
        if (startOfTrailingWhitespace <= 0) {
            textNode.remove();
        } else if (startOfTrailingWhitespace < text.length()) {
            textNode.splitText(startOfTrailingWhitespace);
            textNode.nextSibling().remove();
        }
    }
}
Also used : Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) TextNode(org.jsoup.nodes.TextNode)

Example 8 with TextNode

use of org.jsoup.nodes.TextNode in project mylyn.docs by eclipse.

the class WhitespaceCleanupProcessor method moveLeadingOrTrailingSpaceOutOfElements.

private void moveLeadingOrTrailingSpaceOutOfElements(Element body) {
    Set<Node> affectedParents = new HashSet<Node>();
    for (Element element : body.getAllElements()) {
        if (!Html.isWhitespacePreserve(element)) {
            normalizeTextNodes(element);
            List<Node> children = element.childNodes();
            if (!children.isEmpty()) {
                Node firstChild = children.get(0);
                if (firstChild instanceof TextNode) {
                    TextNode textNode = (TextNode) firstChild;
                    String text = textNode.getWholeText();
                    int nonWhitespaceIndex = firstIndexOfNonWhitespace(text);
                    if (nonWhitespaceIndex > 0) {
                        affectedParents.add(textNode.parent());
                        // split
                        textNode.splitText(nonWhitespaceIndex);
                        // move outside
                        textNode.remove();
                        computeBeforeTarget(element).before(textNode);
                        affectedParents.add(textNode.parent());
                    } else if (nonWhitespaceIndex == -1) {
                        // move outside
                        textNode.remove();
                        computeAfterTarget(element).after(textNode);
                        affectedParents.add(textNode.parent());
                    }
                }
                normalizeEmptySpaceBetweenNodes(element);
                children = element.childNodes();
                if (!children.isEmpty()) {
                    Node lastChild = children.get(children.size() - 1);
                    if (lastChild instanceof TextNode) {
                        TextNode textNode = (TextNode) lastChild;
                        String text = textNode.getWholeText();
                        int lastNonWhitespaceIndex = lastIndexOfNonWhitespace(text);
                        if (lastNonWhitespaceIndex < 0) {
                            // move outside
                            textNode.remove();
                            computeAfterTarget(element).after(textNode);
                            affectedParents.add(textNode.parent());
                        } else if (lastNonWhitespaceIndex < (text.length() - 1)) {
                            affectedParents.add(textNode.parent());
                            // split
                            textNode.splitText(lastNonWhitespaceIndex + 1);
                            // move outside
                            textNode = (TextNode) textNode.nextSibling();
                            textNode.remove();
                            computeAfterTarget(element).after(textNode);
                            affectedParents.add(textNode.parent());
                        }
                    }
                }
            }
            if (!affectedParents.isEmpty()) {
                for (Node parent : affectedParents) {
                    if (parent instanceof Element) {
                        normalizeTextNodes((Element) parent);
                    }
                }
                affectedParents.clear();
            }
        }
    }
}
Also used : Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) HashSet(java.util.HashSet)

Example 9 with TextNode

use of org.jsoup.nodes.TextNode in project mylyn.docs by eclipse.

the class WhitespaceCleanupProcessor method normalizeEmptySpaceBetweenNodes.

private void normalizeEmptySpaceBetweenNodes(Element parent) {
    List<Node> children = parent.childNodes();
    if (!children.isEmpty()) {
        children = new ArrayList<>(children);
        for (Node child : children) {
            Node previousSibling = child.previousSibling();
            Node nextSibling = child.nextSibling();
            if (child instanceof TextNode && previousSibling instanceof Element && nextSibling instanceof Element) {
                TextNode textNode = (TextNode) child;
                Element prevElement = (Element) previousSibling;
                Element nextElement = (Element) nextSibling;
                normalizeTextBetweenNodes(textNode, prevElement, nextElement);
            }
        }
    }
}
Also used : Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode)

Example 10 with TextNode

use of org.jsoup.nodes.TextNode in project McWiki by skylerdev.

the class CommandWiki method chatJson.

private JSONArray chatJson(Elements main) {
    JSONArray json = new JSONArray();
    for (Element mainchild : main) {
        JSONArray line = new JSONArray();
        line.add("");
        if (mainchild.is("p")) {
            List<Node> inner = mainchild.childNodes();
            for (Node n : inner) {
                if (n instanceof Element) {
                    Element e = (Element) n;
                    if (e.is("a")) {
                        String linkto = e.attr("href");
                        MCJson a = new MCJson(e.text(), link);
                        if (linkto.startsWith("/")) {
                            a.setClick("run_command", "/wiki " + linkto.substring(1));
                            a.setHover("show_text", "Click to show this article.");
                        } else {
                            a.setClick("open_url", linkto);
                            a.setHover("show_text", "External Link");
                        }
                        line.add(a);
                    } else if (e.is("b")) {
                        line.add(new MCJson(e.text(), bold));
                    } else if (e.is("i")) {
                        line.add(new MCJson(e.text(), italic));
                    }
                }
                if (n instanceof TextNode) {
                    line.add(new MCJson(((TextNode) n).text()));
                }
            }
            line.add("\n");
            json.add(line);
        }
    }
    return json;
}
Also used : Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Node(org.jsoup.nodes.Node) JSONArray(org.json.simple.JSONArray) TextNode(org.jsoup.nodes.TextNode)

Aggregations

TextNode (org.jsoup.nodes.TextNode)52 Element (org.jsoup.nodes.Element)41 Node (org.jsoup.nodes.Node)37 Document (org.jsoup.nodes.Document)19 ArrayList (java.util.ArrayList)16 Elements (org.jsoup.select.Elements)14 IOException (java.io.IOException)6 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)6 JSONException (org.json.JSONException)6 Copy (de.geeksfactory.opacclient.objects.Copy)5 DetailedItem (de.geeksfactory.opacclient.objects.DetailedItem)5 HashMap (java.util.HashMap)5 NameValuePair (org.apache.http.NameValuePair)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 Test (org.junit.jupiter.api.Test)5 NotReachableException (de.geeksfactory.opacclient.networking.NotReachableException)4 Detail (de.geeksfactory.opacclient.objects.Detail)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URI (java.net.URI)4 Matcher (java.util.regex.Matcher)4