Search in sources :

Example 1 with YamlFrontMatterNode

use of com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterNode 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)

Aggregations

YamlFrontMatterNode (com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterNode)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 Matcher (java.util.regex.Matcher)1