Search in sources :

Example 1 with AttributeNode

use of com.vladsch.flexmark.ext.attributes.AttributeNode in project flexmark-java by vsch.

the class AttributesInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    if (inlineParser.peek(1) != '{') {
        int index = inlineParser.getIndex();
        BasedSequence input = inlineParser.getInput();
        Matcher matcher = inlineParser.matcher(parsing.ATTRIBUTES_TAG);
        if (matcher != null) {
            BasedSequence attributesOpen = input.subSequence(matcher.start(), matcher.end());
            // see what we have
            // open, see if open/close
            BasedSequence attributesText = input.subSequence(matcher.start(1), matcher.end(1));
            AttributesNode attributes = new AttributesNode(attributesOpen.subSequence(0, 1), attributesText, attributesOpen.endSequence(1));
            attributes.setCharsFromContent();
            int leadingSpaces = attributesText.countLeading(" \t");
            // give it to the text node
            if (leadingSpaces > 0) {
                inlineParser.appendText(attributesText, 0, leadingSpaces);
                attributesText = attributesText.subSequence(leadingSpaces);
            }
            inlineParser.flushTextNode();
            inlineParser.getBlock().appendChild(attributes);
            BasedSequence attributeText = attributesText.trim();
            if (!attributeText.isEmpty()) {
                // have some attribute text
                // parse attributes
                Matcher attributeMatcher = parsing.ATTRIBUTE.matcher(attributeText);
                while (attributeMatcher.find()) {
                    BasedSequence attributeName = attributeText.subSequence(attributeMatcher.start(1), attributeMatcher.end(1));
                    BasedSequence attributeSeparator = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.end(1), attributeMatcher.start(2)).trim();
                    BasedSequence attributeValue = attributeMatcher.groupCount() == 1 || attributeMatcher.start(2) == -1 ? BasedSequence.NULL : attributeText.subSequence(attributeMatcher.start(2), attributeMatcher.end(2));
                    boolean isQuoted = attributeValue.length() >= 2 && (attributeValue.charAt(0) == '"' && attributeValue.endCharAt(1) == '"' || attributeValue.charAt(0) == '\'' && attributeValue.endCharAt(1) == '\'');
                    BasedSequence attributeOpen = !isQuoted ? BasedSequence.NULL : attributeValue.subSequence(0, 1);
                    BasedSequence attributeClose = !isQuoted ? BasedSequence.NULL : attributeValue.endSequence(1, 0);
                    if (isQuoted) {
                        attributeValue = attributeValue.midSequence(1, -1);
                    }
                    AttributeNode attribute;
                    if (attributeSeparator.isNull() && attributeSeparator.isNull() && attributeValue.isNull() && AttributeNode.isImplicitName(attributeName)) {
                        attribute = new AttributeNode(attributeName.subSequence(0, 1), attributeSeparator, attributeOpen, attributeName.subSequence(1), attributeClose);
                    } else {
                        attribute = new AttributeNode(attributeName, attributeSeparator, attributeOpen, attributeValue, attributeClose);
                    }
                    attributes.appendChild(attribute);
                }
                return true;
            }
            // did not process, reset to where we started
            inlineParser.setIndex(index);
        }
    }
    return false;
}
Also used : AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode)

Example 2 with AttributeNode

use of com.vladsch.flexmark.ext.attributes.AttributeNode in project flexmark-java by vsch.

the class AttributesNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    if (node instanceof AttributesNode) {
        AttributesNode attributesNode = (AttributesNode) node;
        // apply to sibling unless the sibling is a Text node then apply it to the parent
        Node previous = attributesNode.getPrevious();
        Node next = attributesNode.getNext();
        // and to trim start of next sibling if we are first
        if (previous == null) {
            // we are first, trim start of next sibling
            if (next != null && !(next instanceof AttributesNode)) {
                if (next.getChars().isBlank()) {
                    // remove this node it is all blank
                    Node tmp = next;
                    next = next.getNext();
                    tmp.unlink();
                    state.nodeRemoved(tmp);
                } else {
                    next.setChars(next.getChars().trimStart());
                }
            }
        }
        if (next == null) {
            // we are last, trim end of prev sibling
            if (previous != null && !(previous instanceof AttributesNode)) {
                if (previous.getChars().isBlank()) {
                    // remove this node it is all blank
                    Node tmp = previous;
                    previous = previous.getPrevious();
                    tmp.unlink();
                    state.nodeRemoved(tmp);
                } else {
                    previous.setChars(previous.getChars().trimEnd());
                }
            }
        }
        Node attributeOwner = getAttributeOwner(state, attributesNode);
        nodeAttributeRepository.put(attributeOwner, attributesNode);
        // set the heading id for this node so the correct id will be used
        if (attributeOwner instanceof AnchorRefTarget) {
            for (Node attributeNode : attributesNode.getReversedChildren()) {
                if (attributeNode instanceof AttributeNode) {
                    if (((AttributeNode) attributeNode).isId()) {
                        ((AnchorRefTarget) attributeOwner).setAnchorRefId(((AttributeNode) attributeNode).getValue().toString());
                        break;
                    }
                }
            }
        }
    }
}
Also used : AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode)

Example 3 with AttributeNode

use of com.vladsch.flexmark.ext.attributes.AttributeNode in project flexmark-java by vsch.

the class EnumeratedReferenceNodePostProcessor method process.

@Override
public void process(NodeTracker state, Node node) {
    if (node instanceof AttributesNode) {
        AttributesNode attributesNode = (AttributesNode) node;
        for (Node attributeNode : attributesNode.getChildren()) {
            if (attributeNode instanceof AttributeNode) {
                if (((AttributeNode) attributeNode).isId()) {
                    final String text = ((AttributeNode) attributeNode).getValue().toString();
                    enumeratedReferences.add(text);
                }
            }
        }
    }
}
Also used : AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) Node(com.vladsch.flexmark.ast.Node) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) AttributeNode(com.vladsch.flexmark.ext.attributes.AttributeNode) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode)

Aggregations

AttributeNode (com.vladsch.flexmark.ext.attributes.AttributeNode)3 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)3 Node (com.vladsch.flexmark.ast.Node)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 Matcher (java.util.regex.Matcher)1