Search in sources :

Example 71 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class YamlFrontMatterBlockParser method tryContinue.

@Override
public BlockContinue tryContinue(ParserState state) {
    final BasedSequence line = state.getLine();
    if (inYAMLBlock) {
        if (REGEX_END.matcher(line).matches()) {
            if (currentKey != null) {
                block.appendChild(new YamlFrontMatterNode(currentKey, currentValues));
            }
            // add the last line
            addLine(state, line);
            return BlockContinue.finished();
        }
        Matcher matcher = REGEX_METADATA.matcher(line);
        if (matcher.matches()) {
            if (currentKey != null) {
                block.appendChild(new YamlFrontMatterNode(currentKey, currentValues));
            }
            inLiteral = false;
            currentKey = matcher.group(1);
            currentValues = new ArrayList<String>();
            if ("|".equals(matcher.group(2))) {
                inLiteral = true;
            } else if (!"".equals(matcher.group(2))) {
                currentValues.add(matcher.group(2));
            }
            return BlockContinue.atIndex(state.getIndex());
        } else {
            if (inLiteral) {
                matcher = REGEX_METADATA_LITERAL.matcher(line);
                if (matcher.matches()) {
                    if (currentValues.size() == 1) {
                        currentValues.set(0, currentValues.get(0) + "\n" + matcher.group(1).trim());
                    } else {
                        currentValues.add(matcher.group(1).trim());
                    }
                }
            } else {
                matcher = REGEX_METADATA_LIST.matcher(line);
                if (matcher.matches()) {
                    currentValues.add(matcher.group(1));
                }
            }
            return BlockContinue.atIndex(state.getIndex());
        }
    } else if (REGEX_BEGIN.matcher(line).matches()) {
        inYAMLBlock = true;
        return BlockContinue.atIndex(state.getIndex());
    }
    return BlockContinue.none();
}
Also used : Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) YamlFrontMatterNode(com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterNode)

Example 72 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class MacroBlockParser method closeBlock.

@Override
public void closeBlock(ParserState state) {
    // first line is macro open and possibly close
    if (oneLine) {
        List<BasedSequence> lines = new ArrayList<BasedSequence>();
        Macro macro = (Macro) block.getFirstChild();
        Node node = block.getLastChild();
        BasedSequence contentLine;
        if (node instanceof MacroClose) {
            contentLine = macro.getChars().baseSubSequence(macro.getEndOffset(), node.getStartOffset());
        } else {
            contentLine = macro.getChars().baseSubSequence(macro.getEndOffset(), macro.getEndOffset());
        }
        lines.add(contentLine);
        block.setContent(lines);
    } else {
        // last line is close, first line is open
        if (hadClose) {
            final List<BasedSequence> lines = content.getLines();
            block.setContent(lines);
        } else {
            final List<BasedSequence> lines = content.getLines();
            block.setContent(lines.subList(0, lines.size()));
        }
    }
    block.setCharsFromContent();
    content = null;
}
Also used : MacroClose(com.vladsch.flexmark.ext.xwiki.macros.MacroClose) Macro(com.vladsch.flexmark.ext.xwiki.macros.Macro) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Node(com.vladsch.flexmark.ast.Node) ArrayList(java.util.ArrayList)

Example 73 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class MacroInlineParser method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    if (inlineParser.peek(1) == '{') {
        BasedSequence input = inlineParser.getInput();
        int index = inlineParser.getIndex();
        Matcher matcher = inlineParser.matcher(parsing.MACRO_TAG);
        if (matcher != null) {
            BasedSequence macroOpen = input.subSequence(matcher.start(), matcher.end());
            // see what we have
            if (macroOpen.charAt(2) == '/') {
                // close
                BasedSequence macroName = input.subSequence(matcher.start(2), matcher.end(2));
                for (int i = openMacros.size(); i-- > 0; ) {
                    if (openMacros.get(i).getName().equals(macroName)) {
                        // this one is now closed, we close all intervening ones too
                        inlineParser.flushTextNode();
                        for (int j = openMacros.size(); j-- > i; ) {
                            inlineParser.moveNodes(openMacros.get(j), inlineParser.getBlock().getLastChild());
                        }
                        MacroClose macroClose = new MacroClose(macroOpen.subSequence(0, 3), macroName, macroOpen.endSequence(2));
                        inlineParser.getBlock().appendChild(macroClose);
                        inlineParser.moveNodes(openMacros.get(i), macroClose);
                        if (i == 0) {
                            openMacros.clear();
                        } else {
                            openMacros = openMacros.subList(0, i);
                        }
                        return true;
                    }
                }
            } else {
                // open, see if open/close
                BasedSequence macroName = input.subSequence(matcher.start(1), matcher.end(1));
                boolean isClosedTag = macroOpen.endCharAt(3) == '/';
                Macro macro = new Macro(macroOpen.subSequence(0, 2), macroName, macroOpen.endSequence(isClosedTag ? 3 : 2));
                macro.setCharsFromContent();
                inlineParser.flushTextNode();
                inlineParser.getBlock().appendChild(macro);
                if (!isClosedTag) {
                    openMacros.add(macro);
                }
                BasedSequence attributeText = macroOpen.baseSubSequence(macroName.getEndOffset(), macro.getClosingMarker().getStartOffset()).trim();
                if (!attributeText.isEmpty()) {
                    // have some attribute text
                    macro.setAttributeText(attributeText);
                    // parse attributes
                    Matcher attributeMatcher = parsing.MACRO_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);
                        }
                        MacroAttribute attribute = new MacroAttribute(attributeName, attributeSeparator, attributeOpen, attributeValue, attributeClose);
                        macro.appendChild(attribute);
                    }
                }
                return true;
            }
            // did not process, reset to where we started
            inlineParser.setIndex(index);
        }
    }
    return false;
}
Also used : MacroClose(com.vladsch.flexmark.ext.xwiki.macros.MacroClose) Matcher(java.util.regex.Matcher) Macro(com.vladsch.flexmark.ext.xwiki.macros.Macro) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) MacroAttribute(com.vladsch.flexmark.ext.xwiki.macros.MacroAttribute)

Example 74 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class CoreNodeFormatter method render.

private void render(Node node, NodeFormatterContext context, MarkdownWriter markdown) {
    BasedSequence chars = node.getChars();
    if (node instanceof Block) {
        BasedSequence contentChars = ((Block) node).getContentChars();
        if (chars.isNotNull()) {
            BasedSequence prefix = chars.prefixOf(contentChars);
            if (!prefix.isEmpty()) {
                markdown.append(prefix);
            }
        }
        context.renderChildren(node);
        if (chars.isNotNull()) {
            BasedSequence suffix = chars.suffixOf(contentChars);
            if (!suffix.isEmpty()) {
                markdown.append(suffix);
            }
        }
    } else {
        markdown.append(chars);
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 75 with BasedSequence

use of com.vladsch.flexmark.util.sequence.BasedSequence in project flexmark-java by vsch.

the class MacroBlock method getMacroContentChars.

public BasedSequence getMacroContentChars() {
    Node firstChild = getFirstChild();
    Node lastChild = getLastChild();
    Node firstContentNode = firstChild.getNext();
    Node lastContentNode = lastChild instanceof MacroClose ? lastChild.getPrevious() : lastChild;
    // noinspection UnnecessaryLocalVariable
    BasedSequence contentChars = Node.spanningChars(firstContentNode == null || firstContentNode instanceof MacroClose ? BasedSequence.NULL : firstContentNode.getChars(), lastContentNode == null || lastContentNode == firstChild ? BasedSequence.NULL : lastContentNode.getChars());
    return contentChars;
}
Also used : Node(com.vladsch.flexmark.ast.Node) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Aggregations

BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)91 Matcher (java.util.regex.Matcher)13 Node (com.vladsch.flexmark.ast.Node)6 ArrayList (java.util.ArrayList)5 MacroClose (com.vladsch.flexmark.ext.xwiki.macros.MacroClose)3 ReplacedTextMapper (com.vladsch.flexmark.util.sequence.ReplacedTextMapper)3 Text (com.vladsch.flexmark.ast.Text)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)2 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)2 Macro (com.vladsch.flexmark.ext.xwiki.macros.Macro)2 Pair (com.vladsch.flexmark.util.Pair)2 RepeatedCharSequence (com.vladsch.flexmark.util.sequence.RepeatedCharSequence)2 Block (com.vladsch.flexmark.ast.Block)1 BulletListItem (com.vladsch.flexmark.ast.BulletListItem)1 Link (com.vladsch.flexmark.ast.Link)1 ListItem (com.vladsch.flexmark.ast.ListItem)1 NodeIterator (com.vladsch.flexmark.ast.NodeIterator)1 OrderedListItem (com.vladsch.flexmark.ast.OrderedListItem)1 Parsing (com.vladsch.flexmark.ast.util.Parsing)1 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)1