/*=============================================================================#
# Copyright (c) 2015-2016 David Green and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# David Green - initial API and implementation in Mylyn
# Stephan Wahlbrink (WalWare.de) - revised API and implementation
#=============================================================================*/
package de.walware.docmlet.wikitext.internal.commonmark.core.blocks;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
import de.walware.docmlet.wikitext.internal.commonmark.core.CommonmarkLocator;
import de.walware.docmlet.wikitext.internal.commonmark.core.Line;
import de.walware.docmlet.wikitext.internal.commonmark.core.LineSequence;
import de.walware.docmlet.wikitext.internal.commonmark.core.ProcessingContext;
import de.walware.docmlet.wikitext.internal.commonmark.core.SourceBlock;
import de.walware.docmlet.wikitext.internal.commonmark.core.SourceBlockItem;
import de.walware.docmlet.wikitext.internal.commonmark.core.SourceBlocks.SourceBlockBuilder;
/**
* CM Thematic Break (= Horizontal Rule)
*/
public class ThematicBreakBlock extends SourceBlock {
private static final Pattern PATTERN= Pattern.compile(
"(?:\\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,}",
Pattern.DOTALL );
private final Matcher matcher= PATTERN.matcher("");
public ThematicBreakBlock() {
}
@Override
public boolean canStart(final LineSequence lineSequence, final SourceBlockItem<?> currentBlockItem) {
return canStart(lineSequence.getCurrentLine());
}
public boolean canStart(final Line currentLine) {
return (currentLine != null
&& !currentLine.isBlank() && currentLine.getIndent() < 4
&& currentLine.setupIndent(this.matcher).matches() );
}
@Override
public void createItem(final SourceBlockBuilder builder, final LineSequence lineSequence) {
final SourceBlockItem<ThematicBreakBlock> blockItem= new SourceBlockItem<>(this, builder);
lineSequence.advance();
}
@Override
public void initializeContext(final ProcessingContext context, final SourceBlockItem<?> blockItem) {
}
@Override
public void emit(final ProcessingContext context, final SourceBlockItem<?> blockItem,
final CommonmarkLocator locator, final DocumentBuilder builder) {
locator.setBlockBegin(blockItem);
builder.horizontalRule();
}
}