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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*******************************************************************************
* 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.Matcher;
import java.util.regex.Pattern;
import org.eclipse.mylyn.wikitext.core.parser.Attributes;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.markup.Block;
/**
* Markdown blockquote.
*
* @author Stefan Seelmann
*/
public class QuoteBlock extends NestableBlock {
private static final Pattern startPattern = Pattern.compile(">\\s?(.*)"); //$NON-NLS-1$
private static final Pattern linePattern = Pattern.compile("(?:>\\s?)?(.*)"); //$NON-NLS-1$
private int blockLineCount = 0;
private Block nestedBlock = null;
@Override
public boolean canStart(String line, int lineOffset) {
return startPattern.matcher(line.substring(lineOffset)).matches();
}
@Override
protected int processLineContent(String line, int offset) {
String text = line.substring(offset);
// start of block
if (blockLineCount == 0) {
builder.beginBlock(BlockType.QUOTE, new Attributes());
}
// empty line: end of block
if (markupLanguage.isEmptyLine(text)) {
setClosed(true);
return offset;
}
// extract the content
Matcher matcher = linePattern.matcher(text);
if (!matcher.matches()) {
setClosed(true);
return offset;
}
int contentStart = offset + matcher.start(1);
if (nestedBlock != null) {
if (!(nestedBlock instanceof QuoteBlock) && this.canStart(line, contentStart)) {
nestedBlock.setClosed(true);
nestedBlock = null;
processNextBlock(line, contentStart);
} else {
int processed = nestedBlock.processLine(line, contentStart);
if (nestedBlock.isClosed()) {
nestedBlock = null;
if (processed >= contentStart && processed < line.length()) {
processNextBlock(line, contentStart);
}
}
}
} else {
processNextBlock(line, contentStart);
}
blockLineCount++;
return -1;
}
private void processNextBlock(String line, int contentStart) {
// determine nested block, at least the paragraph block must match
for (Block block : getMarkupLanguage().getBlocks()) {
if (block.canStart(line, contentStart)) {
nestedBlock = clone(block);
break;
}
}
// delegate content processing to nested block
nestedBlock.processLine(line, contentStart);
if (nestedBlock.isClosed()) {
nestedBlock = null;
}
}
private Block clone(Block block) {
Block clonedBlock = block.clone();
clonedBlock.setParser(getParser());
clonedBlock.setState(getState());
return clonedBlock;
}
@Override
public void setClosed(boolean closed) {
if (nestedBlock != null) {
nestedBlock.setClosed(true);
nestedBlock = null;
}
if (closed && !isClosed()) {
builder.endBlock();
}
super.setClosed(closed);
}
}
|