SuperNode wrapFirstSubItemInPara(SuperNode item) { Node firstItemFirstChild = item.getChildren().get(0); if (firstItemFirstChild.getChildren().size() == 1) { Node firstGrandChild = firstItemFirstChild.getChildren().get(0); if (firstGrandChild instanceof ListItemNode) { wrapFirstItemInPara((SuperNode)firstGrandChild); } } return item; }
Node withIndicesShifted(Node node, int delta) { if (delta != 0) { ((AbstractNode) node).shiftIndices(delta); for (Node subNode : node.getChildren()) { withIndicesShifted(subNode, delta); } } return node; }
protected void fixIndices(Node node, int[] ixMap) { ((AbstractNode) node).mapIndices(ixMap); for (Node subNode : node.getChildren()) { fixIndices(subNode, ixMap); } }
boolean wrapFirstItemInPara(SuperNode item) { Node firstItemFirstChild = item.getChildren().get(0); ParaNode paraNode = new ParaNode(firstItemFirstChild.getChildren()); paraNode.setStartIndex(firstItemFirstChild.getStartIndex()); paraNode.setEndIndex(firstItemFirstChild.getEndIndex()); // vsch: wrap the para in RootNode so that it is identical to the rest of the list items if they are loose, otherwise it creates differences in html serialization of task items RootNode rootNode = new RootNode(); rootNode.setStartIndex(paraNode.getStartIndex()); rootNode.setEndIndex(paraNode.getEndIndex()); rootNode.getChildren().add(paraNode); item.getChildren().set(0, rootNode); return true; }
public Rule TableRow() { Var<Boolean> leadingPipe = new Var<Boolean>(Boolean.FALSE); return NodeSequence( push(new TableRowNode()), Optional('|', leadingPipe.set(Boolean.TRUE)), OneOrMore(TableCell(), addAsChild()), leadingPipe.get() || ((Node) peek()).getChildren().size() > 1 || getContext().getInputBuffer().charAt(matchEnd() - 1) == '|', Sp(), Newline() ); }
public void visit(ListItemNode node) { if (node instanceof TaskListNode) { // vsch: #185 handle GitHub style task list items, these are a bit messy because the <input> checkbox needs to be // included inside the optional <p></p> first grand-child of the list item, first child is always RootNode // because the list item text is recursively parsed. Node firstChild = node.getChildren().get(0).getChildren().get(0); boolean firstIsPara = firstChild instanceof ParaNode; int indent = node.getChildren().size() > 1 ? 2 : 0; boolean startWasNewLine = printer.endsWithNewLine(); printer.println().print("<li class=\"task-list-item\">").indent(indent); if (firstIsPara) { printer.println().print("<p>"); printer.print("<input type=\"checkbox\" class=\"task-list-item-checkbox\"" + (((TaskListNode) node).isDone() ? " checked=\"checked\"" : "") + " disabled=\"disabled\"></input>"); visitChildren((SuperNode) firstChild); // render the other children, the p tag is taken care of here visitChildrenSkipFirst(node); printer.print("</p>"); } else { printer.print("<input type=\"checkbox\" class=\"task-list-item-checkbox\"" + (((TaskListNode) node).isDone() ? " checked=\"checked\"" : "") + " disabled=\"disabled\"></input>"); visitChildren(node); } printer.indent(-indent).printchkln(indent != 0).print("</li>") .printchkln(startWasNewLine); } else { printConditionallyIndentedTag(node, "li"); } }
public Rule DefinitionList() { return NodeSequence( // test for successful definition list match before actually building it to reduce backtracking TestNot(Spacechar()), Test( OneOrMore(TestNot(BlankLine()), TestNot(DefListBullet()), OneOrMore(NotNewline(), ANY), Newline()), ZeroOrMore(BlankLine()), DefListBullet() ), push(new DefinitionListNode()), OneOrMore( push(new SuperNode()), OneOrMore(DefListTerm(), addAsChild()), OneOrMore(Definition(), addAsChild()), ((SuperNode)peek(1)).getChildren().addAll(popAsNode().getChildren()), Optional(BlankLine()) ) ); }
public Rule BlockQuote() { StringBuilderVar inner = new StringBuilderVar(); StringBuilderVar optional = new StringBuilderVar(); return NodeSequence( OneOrMore( CrossedOut(Sequence('>', Optional(' ')), inner), Line(inner), ZeroOrMore( TestNot('>'), TestNot(BlankLine()), Line(inner) ), // ZeroOrMore(BlankLine()), inner.append(match()) Optional(Sequence(OneOrMore(BlankLine()), optional.append(match()), Test('>')), inner.append(optional.getString()) && optional.clearContents()) ), // vsch: the block quotes won't parse into Para because they will not be followed by a blank line, // unless the last line of the block quote is an empty block-quote line: ">". We append one here to // take care of that possibility, now that we don't include blank lines after a block quote we add one extra inner.append("\n\n"), // trigger a recursive parsing run on the inner source we just built // and attach the root of the inner parses AST push(new BlockQuoteNode(withIndicesShifted(parseInternal(inner), (Integer) peek()).getChildren())) ); }
private int numberOfLinkNodeChildren(Node cell) { int linkNodeCount = 0; List<Node> children = cell.getChildren(); for (Node child : children) { if (ExpLinkNode.class.isAssignableFrom(child.getClass()) || RefLinkNode.class.isAssignableFrom(child.getClass())) { linkNodeCount ++; } } return linkNodeCount; }
private int numberOfLinkNodeChildren(Node cell) { int linkNodeCount = 0; List<Node> children = cell.getChildren(); for (Node child : children) { if (ExpLinkNode.class.isAssignableFrom(child.getClass()) || RefLinkNode.class.isAssignableFrom(child.getClass())) { linkNodeCount ++; } } return linkNodeCount; }
protected <T extends Node> void visitChildren(T node) { for (Node child : node.getChildren()) { child.accept(this); } }
protected <T extends Node> void visitChildren(T node) { for (Node child : node.getChildren()) { child.accept(this); } }
@SuppressWarnings("unchecked") protected <T extends Node> void forEachChild( T node, FindPredicate<T> cb ) { final java.util.List<Node> children = node.getChildren(); for (int index = 0 ; index < children.size() ; ++index) { final Node child = children.get(index); if( !cb.f( (T)child, node, index ) ) { return ; } } }
protected <T extends Node> void forEachChild(T node, FindPredicate<T> cb ) { final List<Node> children = node.getChildren(); for (int index = 0 ; index < children.size() ; ++index) { final Node child = children.get(index); if( !cb.f( (T)child, node, index ) ) { return ; } } }
public void processChildren(int level, Node node) { for (Node child : node.getChildren()) { process(level + 1, child); } }
private boolean firstChildIsInstanceOf(Node node, Class<?> clazz) { return node.getChildren().size() > 0 && clazz.isAssignableFrom(firstChildOf(node).getClass()); }
Node xwithIndicesShifted(Node node, int delta) { ((AbstractNode) node).shiftIndices(delta); for (Node subNode : node.getChildren()) { xwithIndicesShifted(subNode, delta); } return node; } }
public void traverse(Node node) { if (node instanceof ReferenceNode) { declareReference((ReferenceNode) node); } for (Node child : node.getChildren()) { traverse(child); } }
private void declareReference(ReferenceNode node) { TextNode text = (TextNode) node.getChildren().get(0).getChildren().get(0); String title = node.getTitle(); String url = node.getUrl(); String key = text.getText(); Ref ref = new Ref(key, url, title); references.put(key, ref); }
public List<Element> collectChildren(int level, Node node) { Collector<Element> collector = new Collector<Element>(); iTextContext().pushElementConsumer(collector); for (Node child : node.getChildren()) { process(level + 1, child); } iTextContext().popElementConsumer(); return collector.getCollected(); }