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());
}
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);
}
}
}
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);
}
}
}
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;
}
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;
}
Aggregations