1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/*******************************************************************************
* Copyright (c) 2012, 2013 Stefan Seelmann 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:
* Stefan Seelmann - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.wikitext.markdown.core.block;
import java.util.regex.Pattern;
import org.eclipse.mylyn.internal.wikitext.markdown.core.util.LookAheadReader;
import org.eclipse.mylyn.internal.wikitext.markdown.core.util.ReadAheadBlock;
import org.eclipse.mylyn.wikitext.core.parser.Attributes;
/**
* Markdown underlined headings.
*
* @author Stefan Seelmann
*/
public class UnderlinedHeadingBlock extends NestableBlock implements ReadAheadBlock {
private static final Pattern h1pattern = Pattern.compile("=+\\s*"); //$NON-NLS-1$
private static final Pattern h2pattern = Pattern.compile("-+\\s*"); //$NON-NLS-1$
private int blockLineCount;
private int level;
public boolean canStart(String line, int lineOffset, LookAheadReader lookAheadReader) {
blockLineCount = 0;
level = 0;
String nextLine = lookAheadReader.lookAhead();
if (nextLine == null || nextLine.length() < lineOffset) {
return false;
}
String text = nextLine.substring(lineOffset);
if (h1pattern.matcher(text).matches()) {
level = 1;
return true;
} else if (h2pattern.matcher(text).matches()) {
level = 2;
return true;
} else {
return false;
}
}
@Override
public boolean canStart(String line, int lineOffset) {
String message = "Read-ahead required, call canStart(String, int, LookAheadReader) instead."; //$NON-NLS-1$
throw new UnsupportedOperationException(message);
}
@Override
protected int processLineContent(String line, int offset) {
if (blockLineCount == 0) {
builder.beginHeading(level, new Attributes());
markupLanguage.emitMarkupLine(getParser(), state, line, offset);
} else {
builder.endHeading();
setClosed(true);
}
blockLineCount++;
return -1;
}
}
|