本文整理汇总了Java中org.commonmark.parser.Parser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java Parser.parse方法的具体用法?Java Parser.parse怎么用?Java Parser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.commonmark.parser.Parser
的用法示例。
在下文中一共展示了Parser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: render
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
public String render(String text, String format) {
if (text == null || text.length() == 0) {
return text;
}
try {
Parser parser = Parser.builder().build();
Node document = parser.parse(text);
Renderer renderer;
if (format.equals("html")) {
renderer = HtmlRenderer.builder().build();
} else {
renderer = TextContentRenderer.builder().build();
}
return renderer.render(document);
} catch (Exception e) {
logger.error("A problem occurred while rendering an email: {}", e);
return text;
}
}
示例2: render
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
protected static void render(@NonNull TextView textView, String markdown, int width) {
List<Extension> extensions = Arrays.asList(
StrikethroughExtension.create(),
AutolinkExtension.create(),
TablesExtension.create(),
InsExtension.create(),
EmojiExtension.create(),
MentionExtension.create(),
YamlFrontMatterExtension.create());
Parser parser = Parser.builder()
.extensions(extensions)
.build();
try {
Node node = parser.parse(markdown);
String rendered = HtmlRenderer
.builder()
.extensions(extensions)
.build()
.render(node);
HtmlHelper.htmlIntoTextView(textView, rendered, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
} catch (Exception ignored) {
HtmlHelper.htmlIntoTextView(textView, markdown, (width - (textView.getPaddingStart() + textView.getPaddingEnd())));
}
}
示例3: MockBodyPost
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
public MockBodyPost(String bodyMarkdown) {
super("","",null,null,
1, 1, "", bodyMarkdown, null, null, null, "", "");
SOUser asker = new SOUser("testUser",-1,1,"registered");
SOUser answerer = new SOUser("testUser",-1,1,"unregistered");
setAnswerCreationDate(Instant.now());
setQuestionCreationDate(Instant.EPOCH);
setAsker(asker);
setAnswerer(answerer);
Parser parser = Parser.builder().build();
Node document = parser.parse(bodyMarkdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
setBody(renderer.render(document));
}
示例4: ioReaderTest
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void ioReaderTest() throws IOException {
Parser parser = Parser.builder().build();
InputStream input1 = SpecReader.getSpecInputStream();
Node document1;
try (InputStreamReader reader = new InputStreamReader(input1)) {
document1 = parser.parseReader(reader);
}
String spec = SpecReader.readSpec();
Node document2 = parser.parse(spec);
HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
assertEquals(renderer.render(document2), renderer.render(document1));
}
示例5: enabledBlockTypes
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void enabledBlockTypes() {
String given = "# heading 1\n\nnot a heading";
Parser parser = Parser.builder().build(); // all core parsers by default
Node document = parser.parse(given);
assertThat(document.getFirstChild(), instanceOf(Heading.class));
Set<Class<? extends Block>> headersOnly = new HashSet<>();
headersOnly.add(Heading.class);
parser = Parser.builder().enabledBlockTypes(headersOnly).build();
document = parser.parse(given);
assertThat(document.getFirstChild(), instanceOf(Heading.class));
Set<Class<? extends Block>> noCoreTypes = new HashSet<>();
parser = Parser.builder().enabledBlockTypes(noCoreTypes).build();
document = parser.parse(given);
assertThat(document.getFirstChild(), not(instanceOf(Heading.class)));
}
示例6: indentation
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void indentation() {
String given = " - 1 space\n - 3 spaces\n - 5 spaces\n\t - tab + space";
Parser parser = Parser.builder().build();
Node document = parser.parse(given);
assertThat(document.getFirstChild(), instanceOf(BulletList.class));
Node list = document.getFirstChild(); // first level list
assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
assertEquals("1 space", firstText(list.getFirstChild()));
list = list.getFirstChild().getLastChild(); // second level list
assertEquals("expect one child", list.getFirstChild(), list.getLastChild());
assertEquals("3 spaces", firstText(list.getFirstChild()));
list = list.getFirstChild().getLastChild(); // third level list
assertEquals("5 spaces", firstText(list.getFirstChild()));
assertEquals("tab + space", firstText(list.getFirstChild().getNext()));
}
示例7: markdownToHtml
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
/**
* Converts a markdown buffer to a Html buffer
*
* @param buffer
* @return String
*/
private String markdownToHtml(String buffer) {
Parser parser = Parser.builder().build();
Node document = parser.parse(buffer);
HtmlRenderer renderer = HtmlRenderer.builder().build();
String asHtml = renderer.render(document);
return asHtml;
}
示例8: mdToHtml
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
/**
* markdown转换为html
*
* @param markdown
* @return
*/
public static String mdToHtml(String markdown) {
if (StringUtils.isBlank(markdown)) {
return "";
}
java.util.List<Extension> extensions = Arrays.asList(TablesExtension.create());
Parser parser = Parser.builder().extensions(extensions).build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().extensions(extensions).build();
String content = renderer.render(document);
content = Commons.emoji(content);
return content;
}
示例9: stripMdText
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
public static void stripMdText(@NonNull TextView textView, String markdown) {
if (!InputHelper.isEmpty(markdown)) {
Parser parser = Parser.builder().build();
Node node = parser.parse(markdown);
textView.setText(stripHtml(HtmlRenderer.builder().build().render(node)));
}
}
示例10: parseTest
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void parseTest() throws Exception {
Parser parser = new Parser.Builder().build();
Node document = parser.parse(spec);
assertNotNull(document);
}
示例11: parseWithExtensionsTest
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
private void parseWithExtensionsTest(Extension extension) throws Exception {
Parser parser = Parser.builder()
.extensions(Collections.singletonList(extension))
.build();
Node document = parser.parse(spec);
assertNotNull(document);
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(Collections.singletonList(extension))
.build();
String renderedString = renderer.render(document);
assertNotNull(renderedString);
}
示例12: customBlockParserFactory
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void customBlockParserFactory() {
Parser parser = Parser.builder().customBlockParserFactory(new DashBlockParserFactory()).build();
// The dashes would normally be a ThematicBreak
Node document = parser.parse("hey\n\n---\n");
assertThat(document.getFirstChild(), instanceOf(Paragraph.class));
assertEquals("hey", ((Text) document.getFirstChild().getFirstChild()).getLiteral());
assertThat(document.getLastChild(), instanceOf(DashBlock.class));
}
示例13: parseAndRender
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void parseAndRender() {
Parser parser = Parser.builder().build();
Node document = parser.parse("This is *Sparta*");
HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
assertEquals("<p>This is <em>Sparta</em></p>\n", renderer.render(document));
}
示例14: addAttributes
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void addAttributes() {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder()
.attributeProviderFactory(new AttributeProviderFactory() {
public AttributeProvider create(AttributeProviderContext context) {
return new ImageAttributeProvider();
}
})
.build();
Node document = parser.parse("![text](/url.png)");
assertEquals("<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n",
renderer.render(document));
}
示例15: customizeRendering
import org.commonmark.parser.Parser; //导入方法依赖的package包/类
@Test
public void customizeRendering() {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder()
.nodeRendererFactory(new HtmlNodeRendererFactory() {
public NodeRenderer create(HtmlNodeRendererContext context) {
return new IndentedCodeBlockNodeRenderer(context);
}
})
.build();
Node document = parser.parse("Example:\n\n code");
assertEquals("<p>Example:</p>\n<pre>code\n</pre>\n", renderer.render(document));
}