Search in sources :

Example 36 with BasedSequence

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

the class MacroBlockParser method tryContinue.

@Override
public BlockContinue tryContinue(ParserState state) {
    if (hadClose) {
        return BlockContinue.none();
    }
    final BasedSequence line = state.getLine();
    final Matcher closeMatcher = parsing.MACRO_CLOSE.matcher(line);
    if (closeMatcher.find()) {
        // check if this close belongs to a nested child block macro
        if (macroName.equals(closeMatcher.group(1))) {
            final List<BlockParser> parsers = state.getActiveBlockParsers();
            boolean isChildClose = false;
            for (int i = parsers.size(); i-- > 0; ) {
                final BlockParser parser = parsers.get(i);
                if (parser == this)
                    break;
                if (parser instanceof MacroBlockParser) {
                    if (!((MacroBlockParser) parser).hadClose && ((MacroBlockParser) parser).macroName.equals(macroName)) {
                        isChildClose = true;
                    }
                }
            }
            if (!isChildClose) {
                hadClose = true;
                MacroClose macroClose = new MacroClose(line.subSequence(closeMatcher.start(), closeMatcher.start() + 3), line.subSequence(closeMatcher.start(1), closeMatcher.end(1)), line.subSequence(closeMatcher.end() - 2, closeMatcher.end()));
                macroClose.setCharsFromContent();
                block.appendChild(macroClose);
                return BlockContinue.atIndex(state.getLineEndIndex());
            }
        }
    }
    return BlockContinue.atIndex(state.getIndex());
}
Also used : MacroClose(com.vladsch.flexmark.ext.xwiki.macros.MacroClose) Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 37 with BasedSequence

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

the class ZzzzzzNodePostProcessor method processText.

private void processText(NodeTracker state, Text node) {
    BasedSequence original = node.getChars();
    boolean wrapInTextBase = !(node.getParent() instanceof TextBase);
    TextBase textBase = null;
    while (wrapInTextBase) {
        if (wrapInTextBase) {
            wrapInTextBase = false;
            textBase = new TextBase(original);
            node.insertBefore(textBase);
            textBase.appendChild(node);
            state.nodeAdded(textBase);
        }
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence)

Example 38 with BasedSequence

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

the class TaskListItemBlockPreProcessor method preProcess.

@Override
public void preProcess(ParserState state, Block block) {
    if (block instanceof BulletListItem || block instanceof OrderedListItem) {
        // we chop up the previous paragraph into definition terms and add the definition item to the last one
        // we add all these to the previous DefinitionList or add a new one if there isn't one
        final ListItem listItem = (ListItem) block;
        final BasedSequence markerSuffix = listItem.getMarkerSuffix();
        if (markerSuffix.matches("[ ]") || markerSuffix.matches("[x]") || markerSuffix.matches("[X]")) {
            TaskListItem taskListItem = new TaskListItem(listItem);
            taskListItem.setTight(listItem.isTight());
            listItem.insertBefore(taskListItem);
            listItem.unlink();
            state.blockAdded(taskListItem);
            state.blockRemoved(listItem);
        }
    }
}
Also used : BulletListItem(com.vladsch.flexmark.ast.BulletListItem) TaskListItem(com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) OrderedListItem(com.vladsch.flexmark.ast.OrderedListItem) ListItem(com.vladsch.flexmark.ast.ListItem) BulletListItem(com.vladsch.flexmark.ast.BulletListItem) TaskListItem(com.vladsch.flexmark.ext.gfm.tasklist.TaskListItem) OrderedListItem(com.vladsch.flexmark.ast.OrderedListItem)

Example 39 with BasedSequence

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

the class GfmUsersInlineParserExtension method parse.

@Override
public boolean parse(final InlineParser inlineParser) {
    BasedSequence[] matches = inlineParser.matchWithGroups(GITHUB_USER);
    if (matches != null) {
        BasedSequence input = inlineParser.getInput();
        inlineParser.flushTextNode();
        BasedSequence openMarker = matches[1];
        BasedSequence text = matches[2];
        GfmUser gitHubIssue = new GfmUser(openMarker, text);
        inlineParser.getBlock().appendChild(gitHubIssue);
        return true;
    }
    return false;
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) GfmUser(com.vladsch.flexmark.ext.gfm.users.GfmUser)

Example 40 with BasedSequence

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

the class SpecExampleBlockParser method closeBlock.

@Override
public void closeBlock(ParserState state) {
    // first line, if not blank, has the info string
    List<BasedSequence> lines = content.getLines();
    if (lines.size() > 0) {
        BasedSequence info = lines.get(0);
        int exampleKeyWordStart = myOptions.exampleBreak.length() + 1;
        int exampleKeyWordEnd = exampleKeyWordStart + EXAMPLE_KEYWORD.length();
        BasedSequence exampleKeyword = info.subSequence(exampleKeyWordStart, exampleKeyWordEnd);
        BasedSequence optionsChars = info.subSequence(exampleKeyWordEnd);
        Matcher options = OPTIONS_PATTERN.matcher(optionsChars.toString().replace('\u00A0', ' '));
        block.setOpeningMarker(info.subSequence(0, myOptions.exampleBreak.length()));
        block.setExampleKeyword(exampleKeyword);
        if (options.matches()) {
            BasedSequence coordOpeningMarker = BasedSequence.NULL;
            BasedSequence section = BasedSequence.NULL;
            BasedSequence numberSeparator = BasedSequence.NULL;
            BasedSequence number = BasedSequence.NULL;
            BasedSequence coordClosingMarker = BasedSequence.NULL;
            BasedSequence optionsKeyword = BasedSequence.NULL;
            BasedSequence optionsOpeningMarker = BasedSequence.NULL;
            BasedSequence optionsText = BasedSequence.NULL;
            BasedSequence optionsClosingMarker = BasedSequence.NULL;
            // @formatter:off
            if (options.group(GROUP_COORD_OPEN) != null && !options.group(GROUP_COORD_OPEN).trim().isEmpty()) {
                coordOpeningMarker = optionsChars.subSequence(options.start(GROUP_COORD_OPEN), options.end(GROUP_COORD_OPEN)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_SECTION) != null && !options.group(GROUP_SECTION).trim().isEmpty()) {
                section = optionsChars.subSequence(options.start(GROUP_SECTION), options.end(GROUP_SECTION)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_NUMBER_SEPARATOR) != null && !options.group(GROUP_NUMBER_SEPARATOR).trim().isEmpty()) {
                numberSeparator = optionsChars.subSequence(options.start(GROUP_NUMBER_SEPARATOR), options.end(GROUP_NUMBER_SEPARATOR)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_NUMBER) != null && !options.group(GROUP_NUMBER).trim().isEmpty()) {
                number = optionsChars.subSequence(options.start(GROUP_NUMBER), options.end(GROUP_NUMBER)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_COORD_CLOSE) != null && !options.group(GROUP_COORD_CLOSE).trim().isEmpty()) {
                coordClosingMarker = optionsChars.subSequence(options.start(GROUP_COORD_CLOSE), options.end(GROUP_COORD_CLOSE)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTION_KEYWORD) != null && !options.group(GROUP_OPTION_KEYWORD).trim().isEmpty()) {
                optionsKeyword = optionsChars.subSequence(options.start(GROUP_OPTION_KEYWORD), options.end(GROUP_OPTION_KEYWORD)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTIONS_OPEN) != null && !options.group(GROUP_OPTIONS_OPEN).trim().isEmpty()) {
                optionsOpeningMarker = optionsChars.subSequence(options.start(GROUP_OPTIONS_OPEN), options.end(GROUP_OPTIONS_OPEN)).trim(WHITESPACE_NBSP_CHARS);
            }
            if (options.group(GROUP_OPTIONS) != null) {
                optionsText = optionsChars.subSequence(options.start(GROUP_OPTIONS), options.end(GROUP_OPTIONS));
            }
            if (options.group(GROUP_OPTIONS_CLOSE) != null && !options.group(GROUP_OPTIONS_CLOSE).trim().isEmpty()) {
                optionsClosingMarker = optionsChars.subSequence(options.start(GROUP_OPTIONS_CLOSE), options.end(GROUP_OPTIONS_CLOSE)).trim(WHITESPACE_NBSP_CHARS);
            }
            // @formatter:on
            if (section.isNotNull() && optionsKeyword.isNull() && numberSeparator.isNull() && coordOpeningMarker.isNull() && section.matchChars("options")) {
                // move all from section to options
                int pos = section.indexOfAny(' ', '\t', '\u00A0');
                if (pos < 0) {
                    optionsKeyword = section;
                } else {
                    optionsKeyword = section.subSequence(0, pos);
                    optionsText = section.subSequence(pos + 1);
                }
                optionsClosingMarker = coordClosingMarker;
                section = BasedSequence.NULL;
                coordClosingMarker = BasedSequence.NULL;
            }
            if (optionsText.isNull()) {
                if (optionsClosingMarker.isNotNull()) {
                    optionsText = optionsClosingMarker.subSequence(0, 0);
                } else if (optionsOpeningMarker.isNotNull()) {
                    optionsText = optionsOpeningMarker.subSequence(1, 1);
                } else if (optionsKeyword.isNotNull()) {
                    optionsText = optionsKeyword.subSequence(optionsKeyword.length(), optionsKeyword.length());
                }
            }
            block.setCoordOpeningMarker(coordOpeningMarker);
            block.setSection(section);
            block.setNumberSeparator(numberSeparator);
            block.setNumber(number);
            block.setCoordClosingMarker(coordClosingMarker);
            block.setOptionsKeyword(optionsKeyword);
            block.setOptionsOpeningMarker(optionsOpeningMarker);
            block.setOptions(optionsText);
            block.setOptionsClosingMarker(optionsClosingMarker);
        }
        // if we create option nodes, we break up the options
        if (myOptions.optionNodes && block.getOptionsKeyword().isNotNull()) {
            Node optionsList = new SpecExampleOptionsList(block.getOptions());
            block.appendChild(optionsList);
            BasedSequence trimmedOptionsList = block.getOptions().trim(WHITESPACE_NBSP_CHARS);
            if (!trimmedOptionsList.isEmpty()) {
                BasedSequence[] list = trimmedOptionsList.split(',', 0, SPLIT_INCLUDE_DELIM_PARTS);
                for (BasedSequence item : list) {
                    BasedSequence option = item.trim(WHITESPACE_NBSP_CHARS);
                    if (!option.isEmpty()) {
                        if (option.matches(",")) {
                            Node optionNode = new SpecExampleOptionSeparator(option);
                            optionsList.appendChild(optionNode);
                        } else {
                            Node optionNode = new SpecExampleOption(option);
                            optionsList.appendChild(optionNode);
                        }
                    }
                }
            }
        }
        BasedSequence chars = content.getSpanningChars();
        BasedSequence spanningChars = chars.baseSubSequence(chars.getStartOffset(), lines.get(0).getEndOffset());
        if (lines.size() > 1) {
            // have more lines
            block.setContent(spanningChars, lines.subList(1, lines.size()));
            // need to find the parts
            boolean inSource = true;
            boolean inHtml = false;
            boolean inAst = false;
            int sectionStart = -1;
            BasedSequence prevLine = BasedSequence.NULL;
            BasedSequence lastLine = lines.get(lines.size() - 1);
            String typeBreak = myOptions.typeBreak;
            int typeBreakLength = typeBreak.length();
            for (BasedSequence line : lines.subList(1, lines.size())) {
                if (line.length() == typeBreakLength + line.countTrailing(BasedSequence.EOL_CHARS) && line.matchChars(typeBreak)) {
                    if (inSource) {
                        inSource = false;
                        if (sectionStart != -1) {
                            block.setSource(line.baseSubSequence(sectionStart, line.getStartOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setSource(line.subSequence(0, 0));
                        }
                        block.setHtmlSeparator(line);
                        inHtml = true;
                        sectionStart = -1;
                    } else if (inHtml) {
                        inHtml = false;
                        if (sectionStart != -1) {
                            block.setHtml(line.baseSubSequence(sectionStart, line.getStartOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setHtml(line.subSequence(0, 0));
                        }
                        block.setAstSeparator(line);
                        inAst = true;
                        sectionStart = -1;
                    } else {
                        if (sectionStart == -1) {
                            sectionStart = line.getStartOffset();
                        }
                    }
                } else {
                    if (sectionStart == -1) {
                        sectionStart = line.getStartOffset();
                    }
                }
                prevLine = line;
                if (line == lastLine) {
                    // done
                    if (inSource) {
                        if (sectionStart != -1) {
                            block.setSource(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setSource(line.subSequence(line.length(), line.length()));
                        }
                    } else if (inHtml) {
                        if (sectionStart != -1) {
                            block.setHtml(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setHtml(line.subSequence(line.length(), line.length()));
                        }
                    } else if (inAst) {
                        if (sectionStart != -1) {
                            block.setAst(line.baseSubSequence(sectionStart, line.getEndOffset() - prevLine.countTrailing(BasedSequence.EOL_CHARS)));
                        } else {
                            block.setAst(line.subSequence(line.length(), line.length()));
                        }
                    }
                    break;
                }
            }
            // here if we create section nodes
            if (block.getSource().isNotNull()) {
                Node node = new SpecExampleSource(block.getSource());
                block.appendChild(node);
            }
            if (block.getHtmlSeparator().isNotNull()) {
                Node node = new SpecExampleSeparator(block.getHtmlSeparator());
                block.appendChild(node);
                if (block.getHtml().isNotNull()) {
                    node = new SpecExampleHtml(block.getHtml());
                    block.appendChild(node);
                }
                if (block.getAstSeparator().isNotNull()) {
                    node = new SpecExampleSeparator(block.getAstSeparator());
                    block.appendChild(node);
                    if (block.getAst().isNotNull()) {
                        node = new SpecExampleAst(block.getAst());
                        block.appendChild(node);
                    }
                }
            }
        } else {
            Node node = new SpecExampleSource(block.getClosingMarker().subSequence(0, 0));
            block.appendChild(node);
            block.setContent(spanningChars, BasedSequence.EMPTY_LIST);
        }
    } else {
        Node node = new SpecExampleSource(block.getClosingMarker().subSequence(0, 0));
        block.appendChild(node);
        block.setContent(content);
    }
    block.setCharsFromContent();
    content = null;
}
Also used : Matcher(java.util.regex.Matcher) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Node(com.vladsch.flexmark.ast.Node)

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