/
PluginParser.java
49 lines (41 loc) · 1.44 KB
/
PluginParser.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
package org.pegdown;
import org.parboiled.BaseParser;
import org.parboiled.Rule;
import org.parboiled.support.StringBuilderVar;
import org.pegdown.plugins.BlockPluginParser;
import org.pegdown.plugins.InlinePluginParser;
public class PluginParser extends Parser implements InlinePluginParser, BlockPluginParser {
public PluginParser() {
super(ALL, 1000l, DefaultParseRunnerProvider);
}
@Override
public Rule[] blockPluginRules() {
return new Rule[] {BlockPlugin()};
}
@Override
public Rule[] inlinePluginRules() {
return new Rule[] {InlinePlugin()};
}
public Rule InlinePlugin() {
StringBuilderVar text = new StringBuilderVar();
return NodeSequence(
Ch('%'),
OneOrMore(TestNot(Ch('%')), BaseParser.ANY, text.append(matchedChar())),
push(new InlinePluginNode(text.getString())),
Ch('%')
);
}
public Rule BlockPlugin() {
StringBuilderVar text = new StringBuilderVar();
return NodeSequence(
BlockPluginMarker(),
OneOrMore(TestNot(Newline(), BlockPluginMarker()), BaseParser.ANY, text.append(matchedChar())),
Newline(),
push(new BlockPluginNode(text.appended('\n').getString())),
BlockPluginMarker()
);
}
public Rule BlockPluginMarker() {
return Sequence(NOrMore('%', 3), Newline());
}
}