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);
}
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();
}
}
}
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();
}
}
}
}
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);
}
}
}
}
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;
}
Aggregations