All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softicar.platform.common.ui.wiki.matchers.WikiParserListMatcher Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.ui.wiki.matchers;

import com.softicar.platform.common.string.scanning.ISimpleTextMatcher;
import com.softicar.platform.common.ui.wiki.SimpleWikiParserEngine;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WikiParserListMatcher implements ISimpleTextMatcher {

	private static final Pattern LIST_PREFIX_PATTERN = Pattern.compile("  [ ]*[\\*-]\\s+");
	private final SimpleWikiParserEngine engine;

	public WikiParserListMatcher(SimpleWikiParserEngine engine) {

		this.engine = engine;
	}

	@Override
	public int getMatchingLength(String text) {

		Matcher matcher = LIST_PREFIX_PATTERN.matcher(text);
		if (matcher.lookingAt()) {
			return matcher.group().length();
		} else if (engine.isInList() && text.startsWith("\n")) {
			return 1;
		} else {
			return 0;
		}
	}

	@Override
	public void consumeMatchingText(String text) {

		if (text.equals("\n")) {
			engine.endListItem();
		} else {
			while (!Objects.equals(engine.getCurrentList(), text)) {
				String top = engine.getCurrentList();
				if (top == null || top.length() < text.length()) {
					engine.beginList(text);
				} else if (top.length() >= text.length()) {
					engine.endCurrentList();
				}
			}

			engine.beginListItem();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy