在尝试将自定义ParserPlugin实现到我正在编写的库时,我遇到了pegdown v1.4.2的问题(Maven项目,JDK 8):
CustomPlugin:
public class CustomHeadersParserPlugin extends Parser implements BlockPluginParser {
public CustomHeadersParserPlugin() {super(HtmlMdProc.MDP_SETTINGS, HtmlMdProc.PROCESSING_TIME_LIMIT, DefaultParseRunnerProvider);
}
public CustomHeadersParserPlugin(Integer options, Long maxParsingTimeInMillis) {
super(options, maxParsingTimeInMillis, DefaultParseRunnerProvider);
}
public CustomHeadersParserPlugin(Integer options, Long maxParsingTimeInMillis, ParseRunnerProvider parseRunnerProvider) {
super(options, maxParsingTimeInMillis, parseRunnerProvider);
}
public CustomHeadersParserPlugin(Integer options, Long maxParsingTimeInMillis, ParseRunnerProvider parseRunnerProvider, PegDownPlugins plugins) {
super(options, maxParsingTimeInMillis, parseRunnerProvider, plugins);
}
//************* CUSTOM RULES ***************
...
Pegdown用法:
public class HtmlMdProc {
public static final int MDP_SETTINGS = Extensions.HARDWRAPS | Extensions.AUTOLINKS | Extensions.TABLES | Extensions.FENCED_CODE_BLOCKS;
public static final long PROCESSING_TIME_LIMIT = 5000l;
...
public HtmlMdProc markdown() {
PegDownPlugins pdp = PegDownPlugins.builder().withPlugin(CustomHeadersParserPlugin.class).build();
PegDownProcessor mdp = new PegDownProcessor(MDP_SETTINGS, PROCESSING_TIME_LIMIT, pdp);
RootNode rn = mdp.parseMarkdown(text.toCharArray());
String result = new CustomMarkdownToHtmlSerializer().toHtml(rn);
if (result != null)
this.text = result;
else
logger.debug("Could not process markdown in {} seconds", PROCESSING_TIME_LIMIT / 1000);
return this;
}
测试:
@Test
public void testmarkdownWithoutCode() {
String before = "Simple new line\nTest\n\nTest\nVot";
String expected = "<p>Simple new line<br />Test</p><p>Test<br />Vot</p>".replaceAll("\r", "");
HtmlMdProc textProc = new HtmlMdProc(before);
String result = textProc.markdown().text();
assertEquals(expected, result);
}
测试执行:
java.lang.RuntimeException: Error creating extended parser class: null
at org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.objectweb.asm.ClassReader.<init>(Unknown Source)
at org.parboiled.transform.AsmUtils.createClassReader(AsmUtils.java:56)
at org.parboiled.transform.ClassNodeInitializer.process(ClassNodeInitializer.java:62)
at org.parboiled.transform.ParserTransformer.extendParserClass(ParserTransformer.java:44)
at org.parboiled.transform.ParserTransformer.transformParser(ParserTransformer.java:38)
at org.parboiled.Parboiled.createParser(Parboiled.java:54)
at org.pegdown.plugins.PegDownPlugins$Builder.withPlugin(PegDownPlugins.java:113)
at com.myorg.html.services.HtmlMdProc.markdown(HtmlMdProc.java:317)
at com.myorg.html.services.HtmlMdProcTest.testmarkdownWithoutCode(HtmlMdProcTest.java:262)
我在讨论Here时发现了问题,但我太新手不能单独处理Maven插件和思考。