Search in sources :

Example 6 with Text

use of com.vladsch.flexmark.ast.Text in project flexmark-java by vsch.

the class TextNodeConverter method mergeTextNodes.

// insert and clear list
public static void mergeTextNodes(Node parent) {
    Node prevNode = null;
    Node child = parent.getFirstChild();
    while (child != null) {
        Node nextChild = child.getNext();
        if (prevNode instanceof Text && child instanceof Text && prevNode.getChars().isContinuedBy(child.getChars())) {
            // merge them
            child.setChars(prevNode.getChars().spliceAtEnd(child.getChars()));
            prevNode.unlink();
        }
        prevNode = child;
        child = nextChild;
    }
}
Also used : Node(com.vladsch.flexmark.ast.Node) Text(com.vladsch.flexmark.ast.Text)

Example 7 with Text

use of com.vladsch.flexmark.ast.Text in project flexmark-java by vsch.

the class TextNodeMergingList method mergeList.

private void mergeList() {
    if (!isMerged) {
        // go through and see if some can be combined
        ArrayList<Node> mergedList = null;
        Node lastText = null;
        for (Node child : list) {
            if (child instanceof Text) {
                if (!child.getChars().isEmpty()) {
                    if (lastText == null) {
                        lastText = child;
                    } else if (lastText.getChars().isContinuedBy(child.getChars())) {
                        // merge their text
                        lastText.setChars(lastText.getChars().spliceAtEnd(child.getChars()));
                    } else {
                        if (mergedList == null)
                            mergedList = new ArrayList<Node>();
                        mergedList.add(lastText);
                        lastText = child;
                    }
                }
            } else {
                if (mergedList == null)
                    mergedList = new ArrayList<Node>();
                if (lastText != null) {
                    mergedList.add(lastText);
                    lastText = null;
                }
                mergedList.add(child);
            }
        }
        if (lastText != null) {
            if (mergedList == null) {
                list.clear();
                list.add(lastText);
            } else {
                mergedList.add(lastText);
            }
        }
        if (mergedList != null) {
            list = mergedList;
        }
    }
}
Also used : Node(com.vladsch.flexmark.ast.Node) ArrayList(java.util.ArrayList) Text(com.vladsch.flexmark.ast.Text)

Example 8 with Text

use of com.vladsch.flexmark.ast.Text in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method renderURL.

private void renderURL(final BasedSequence urlSrc, final DocxRendererContext docx, final String linkUrl, final Attributes attributes, final Runnable runnable) {
    P p = docx.getP();
    String linkTitle = attributes != null && attributes.contains(Attribute.TITLE_ATTR) ? attributes.getValue(Attribute.TITLE_ATTR) : "";
    final P.Hyperlink hyperlink = docx.getFactory().createPHyperlink();
    JAXBElement<P.Hyperlink> wrappedHyperlink = docx.getFactory().createPHyperlink(hyperlink);
    p.getContent().add(wrappedHyperlink);
    String highlightMissing = "";
    String linkTooltipText = linkTitle;
    String linkUrlText = linkUrl;
    if (linkUrl.startsWith("#")) {
        // local, we use anchor
        // see if we need to adjust it
        final String nodeId = linkUrl.substring(1);
        if (docx.getNodeFromId(nodeId) == null) {
            highlightMissing = docx.getRenderingOptions().localHyperlinkMissingHighlight;
            linkTooltipText = String.format(docx.getRenderingOptions().localHyperlinkMissingFormat, nodeId);
            if (docx.getDocxRendererOptions().errorsToStdErr) {
                // output details of error
                final Pair<Integer, Integer> atIndex = urlSrc.getBaseSequence().getLineColumnAtIndex(urlSrc.getStartOffset());
                String sourceFile = docx.getDocxRendererOptions().errorSourceFile;
                if (sourceFile.isEmpty()) {
                    sourceFile = "on line ";
                } else {
                    sourceFile = String.format("in %s:", sourceFile);
                }
                System.err.println(String.format("    WARN: Invalid anchor target '%s' %s%d:%d", nodeId, sourceFile, atIndex.getFirst() + 1, atIndex.getSecond() + 2));
            }
        }
        final String anchor = docx.getValidBookmarkName(nodeId) + options.localHyperlinkSuffix;
        hyperlink.setAnchor(anchor);
        linkUrlText = String.format("#%s", anchor);
    } else {
        Relationship rel = docx.getHyperlinkRelationship(linkUrl);
        // Create object for hyperlink (wrapped in JAXBElement)
        hyperlink.setId(rel.getId());
    }
    if (linkTooltipText != null && !linkTooltipText.isEmpty()) {
        hyperlink.setTooltip(linkTooltipText);
    }
    docx.setRunFormatProvider(new RunFormatProviderBase<Node>(docx, docx.getDocxRendererOptions().HYPERLINK_STYLE, options.noCharacterStyles, highlightMissing));
    docx.setRunContainer(new RunContainer() {

        @Override
        public void addR(final R r) {
            hyperlink.getContent().add(r);
        }

        @Override
        public R getLastR() {
            final List<Object> content = hyperlink.getContent();
            if (content == null || content.size() == 0)
                return null;
            final Object o = content.get(content.size() - 1);
            return o instanceof R ? (R) o : null;
        }
    });
    if (linkTitle != null && !linkTitle.isEmpty()) {
        // Create object for instrText (wrapped in JAXBElement)
        // Create object for r
        R r = docx.getFactory().createR();
        hyperlink.getContent().add(r);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = docx.getFactory().createRFldChar(fldchar);
        r.getContent().add(fldcharWrapped);
        fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
        // Create object for r
        R r2 = docx.getFactory().createR();
        hyperlink.getContent().add(r2);
        // Create object for instrText (wrapped in JAXBElement)
        org.docx4j.wml.Text text = docx.getFactory().createText();
        JAXBElement<org.docx4j.wml.Text> textWrapped = docx.getFactory().createRInstrText(text);
        r2.getContent().add(textWrapped);
        text.setValue(String.format(" HYPERLINK \"%s\" \\o \"%s\" ", linkUrlText, linkTitle));
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
        // Create object for r
        R r3 = docx.getFactory().createR();
        hyperlink.getContent().add(r3);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar2 = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped2 = docx.getFactory().createRFldChar(fldchar2);
        r3.getContent().add(fldcharWrapped2);
        fldchar2.setFldCharType(org.docx4j.wml.STFldCharType.SEPARATE);
    }
    runnable.run();
    if (linkTitle != null && !linkTitle.isEmpty()) {
        // Create object for r
        R r3 = docx.getFactory().createR();
        hyperlink.getContent().add(r3);
        // Create object for fldChar (wrapped in JAXBElement)
        FldChar fldchar2 = docx.getFactory().createFldChar();
        JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped2 = docx.getFactory().createRFldChar(fldchar2);
        r3.getContent().add(fldcharWrapped2);
        fldchar2.setFldCharType(STFldCharType.END);
    }
}
Also used : AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) EnumeratedReferenceText(com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceText) Text(com.vladsch.flexmark.ast.Text) BigInteger(java.math.BigInteger) Relationship(org.docx4j.relationships.Relationship) org.docx4j.wml(org.docx4j.wml)

Aggregations

Text (com.vladsch.flexmark.ast.Text)8 Node (com.vladsch.flexmark.ast.Node)3 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)2 Link (com.vladsch.flexmark.ast.Link)1 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)1 EnumeratedReferenceText (com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceText)1 YouTubeLink (com.vladsch.flexmark.ext.youtube.embedded.YouTubeLink)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Relationship (org.docx4j.relationships.Relationship)1 org.docx4j.wml (org.docx4j.wml)1