-
Notifications
You must be signed in to change notification settings - Fork 267
/
AsideBlockParser.java
205 lines (180 loc) · 8.28 KB
/
AsideBlockParser.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.vladsch.flexmark.ext.aside.internal;
import com.vladsch.flexmark.ast.ListItem;
import com.vladsch.flexmark.ast.util.Parsing;
import com.vladsch.flexmark.ext.aside.AsideBlock;
import com.vladsch.flexmark.ext.aside.AsideExtension;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.block.*;
import com.vladsch.flexmark.parser.core.*;
import com.vladsch.flexmark.util.ast.Block;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.misc.CharPredicate;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import com.vladsch.flexmark.util.sequence.mappers.SpecialLeadInHandler;
import com.vladsch.flexmark.util.sequence.mappers.SpecialLeadInStartsWithCharsHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class AsideBlockParser extends AbstractBlockParser {
final public static char MARKER_CHAR = '|';
final private AsideBlock block = new AsideBlock();
final private boolean allowLeadingSpace;
final private boolean continueToBlankLine;
final private boolean ignoreBlankLine;
final private boolean interruptsParagraph;
final private boolean interruptsItemParagraph;
final private boolean withLeadSpacesInterruptsItemParagraph;
private int lastWasBlankLine = 0;
public AsideBlockParser(DataHolder options, BasedSequence marker) {
block.setOpeningMarker(marker);
continueToBlankLine = AsideExtension.EXTEND_TO_BLANK_LINE.get(options);
allowLeadingSpace = AsideExtension.ALLOW_LEADING_SPACE.get(options);
ignoreBlankLine = AsideExtension.IGNORE_BLANK_LINE.get(options);
interruptsParagraph = AsideExtension.INTERRUPTS_PARAGRAPH.get(options);
interruptsItemParagraph = AsideExtension.INTERRUPTS_ITEM_PARAGRAPH.get(options);
withLeadSpacesInterruptsItemParagraph = AsideExtension.WITH_LEAD_SPACES_INTERRUPTS_ITEM_PARAGRAPH.get(options);
}
@Override
public boolean isContainer() {
return true;
}
@Override
public boolean isPropagatingLastBlankLine(BlockParser lastMatchedBlockParser) {
return false;
}
@Override
public boolean canContain(ParserState state, BlockParser blockParser, Block block) {
return true;
}
@Override
public AsideBlock getBlock() {
return block;
}
@Override
public void closeBlock(ParserState state) {
block.setCharsFromContent();
if (!Parser.BLANK_LINES_IN_AST.get(state.getProperties())) {
removeBlankLines();
}
}
@Override
public BlockContinue tryContinue(ParserState state) {
int nextNonSpace = state.getNextNonSpaceIndex();
boolean isMarker;
if (!state.isBlank() && ((isMarker = isMarker(state, nextNonSpace, false, false, allowLeadingSpace, interruptsParagraph, interruptsItemParagraph, withLeadSpacesInterruptsItemParagraph)) || (continueToBlankLine && lastWasBlankLine == 0))) {
int newColumn = state.getColumn() + state.getIndent();
lastWasBlankLine = 0;
if (isMarker) {
newColumn++;
// optional following space or tab
if (Parsing.isSpaceOrTab(state.getLine(), nextNonSpace + 1)) {
newColumn++;
}
}
return BlockContinue.atColumn(newColumn);
} else {
if (ignoreBlankLine && state.isBlank()) {
lastWasBlankLine++;
int newColumn = state.getColumn() + state.getIndent();
return BlockContinue.atColumn(newColumn);
}
return BlockContinue.none();
}
}
static boolean isMarker(
final ParserState state,
final int index,
final boolean inParagraph,
final boolean inParagraphListItem,
final boolean allowLeadingSpace,
final boolean interruptsParagraph,
final boolean interruptsItemParagraph,
final boolean withLeadSpacesInterruptsItemParagraph
) {
CharSequence line = state.getLine();
if ((!inParagraph || interruptsParagraph) && index < line.length() && line.charAt(index) == MARKER_CHAR) {
if ((allowLeadingSpace || state.getIndent() == 0) && (!inParagraphListItem || interruptsItemParagraph)) {
if (inParagraphListItem && !withLeadSpacesInterruptsItemParagraph) {
return state.getIndent() == 0;
} else {
return state.getIndent() < state.getParsing().CODE_BLOCK_INDENT;
}
}
}
return false;
}
static boolean endsWithMarker(BasedSequence line) {
int tailBlanks = line.countTrailing(CharPredicate.WHITESPACE_NBSP);
return tailBlanks + 1 < line.length() && line.charAt(line.length() - tailBlanks - 1) == MARKER_CHAR;
}
public static class Factory implements CustomBlockParserFactory {
@Nullable
@SuppressWarnings("UnnecessaryLocalVariable")
@Override
public Set<Class<?>> getAfterDependents() {
HashSet<Class<?>> set = new HashSet<>();
//set.add(BlockQuoteParser.Factory.class);
return set;
}
@Nullable
@Override
public Set<Class<?>> getBeforeDependents() {
return new HashSet<>(Arrays.asList(
//BlockQuoteParser.Factory.class,
HeadingParser.Factory.class,
FencedCodeBlockParser.Factory.class,
HtmlBlockParser.Factory.class,
ThematicBreakParser.Factory.class,
ListBlockParser.Factory.class,
IndentedCodeBlockParser.Factory.class
));
}
@Override
public @Nullable SpecialLeadInHandler getLeadInHandler(@NotNull DataHolder options) {
return AsideLeadInHandler.HANDLER;
}
@Override
public boolean affectsGlobalScope() {
return false;
}
@NotNull
@Override
public BlockParserFactory apply(@NotNull DataHolder options) {
return new BlockFactory(options);
}
}
static class AsideLeadInHandler {
final static SpecialLeadInHandler HANDLER = SpecialLeadInStartsWithCharsHandler.create('|');
}
private static class BlockFactory extends AbstractBlockParserFactory {
final private boolean allowLeadingSpace;
final private boolean interruptsParagraph;
final private boolean interruptsItemParagraph;
final private boolean withLeadSpacesInterruptsItemParagraph;
BlockFactory(DataHolder options) {
super(options);
allowLeadingSpace = AsideExtension.ALLOW_LEADING_SPACE.get(options);
interruptsParagraph = AsideExtension.INTERRUPTS_PARAGRAPH.get(options);
interruptsItemParagraph = AsideExtension.INTERRUPTS_ITEM_PARAGRAPH.get(options);
withLeadSpacesInterruptsItemParagraph = AsideExtension.WITH_LEAD_SPACES_INTERRUPTS_ITEM_PARAGRAPH.get(options);
}
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
int nextNonSpace = state.getNextNonSpaceIndex();
BlockParser matched = matchedBlockParser.getBlockParser();
boolean inParagraph = matched.isParagraphParser();
boolean inParagraphListItem = inParagraph && matched.getBlock().getParent() instanceof ListItem && matched.getBlock() == matched.getBlock().getParent().getFirstChild();
if (!endsWithMarker(state.getLine()) && isMarker(state, nextNonSpace, inParagraph, inParagraphListItem, allowLeadingSpace, interruptsParagraph, interruptsItemParagraph, withLeadSpacesInterruptsItemParagraph)) {
int newColumn = state.getColumn() + state.getIndent() + 1;
// optional following space or tab
if (Parsing.isSpaceOrTab(state.getLine(), nextNonSpace + 1)) {
newColumn++;
}
return BlockStart.of(new AsideBlockParser(state.getProperties(), state.getLine().subSequence(nextNonSpace, nextNonSpace + 1))).atColumn(newColumn);
} else {
return BlockStart.none();
}
}
}
}