本文整理汇总了Java中org.commonmark.parser.Parser类的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Parser类属于org.commonmark.parser包,在下文中一共展示了Parser类的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: generateContents
import org.commonmark.parser.Parser; //导入依赖的package包/类
public String generateContents(Page page) throws IOException {
List<Extension> extensions = Collections.singletonList(TablesExtension.create());
Parser parser = Parser
.builder()
.extensions(extensions)
.build();
HtmlRenderer renderer = HtmlRenderer
.builder()
.attributeProvider((node, attributes) -> {
if (node instanceof TableBlock) {
attributes.put("class", "table");
}
})
.extensions(extensions)
.build();
Node document = parser.parseReader(new FileReader(page.getFile()));
return renderer.render(document);
}
示例4: 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));
}
示例5: 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));
}
示例6: 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)));
}
示例7: 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()));
}
示例8: 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;
}
示例9: 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;
}
示例10: 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)));
}
}
示例11: createParser
import org.commonmark.parser.Parser; //导入依赖的package包/类
/**
* Helper method to obtain a Parser with registered strike-through & table extensions
* & task lists (added in 1.0.1)
*
* @return a Parser instance that is supported by this library
* @since 1.0.0
*/
@NonNull
public static Parser createParser() {
return new Parser.Builder()
.extensions(Arrays.asList(
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListExtension.create()
))
.build();
}
示例12: test
import org.commonmark.parser.Parser; //导入依赖的package包/类
@Test
public void test() {
String testInputDocument = "Foo\n"
+ "# Generated Section - Pages\n"
+ "Bar\n"
+ "## Generated Section - Categories\n";
String testOutputDocument = "Foo\n"
+ "# Pages\n"
+ "* [Foo](foo.md)\n"
+ "* [Bar](bar.md)\n"
+ "\nBar\n"
+ "## Categories\n"
+ "* [Goo](goo/index.md)\n"
+ "* [Gar](gar/index.md)\n";
Node testDocument = Parser.builder().build().parse(testInputDocument);
Node expectedDocument = Parser.builder().build().parse(testOutputDocument);
List<String> siblingPages = Arrays.asList(new String[]{"foo.md", "bar.md"});
List<String> subCategories = Arrays.asList(new String[]{"goo", "gar"});
NavigationLinkInjector navigationLinkInjector = new NavigationLinkInjector(
Paths.get("/example/example.md"), Paths.get("/example"),
siblingPages, subCategories);
testDocument.accept(navigationLinkInjector);
HtmlRenderer renderer = HtmlRenderer.builder().build();
Assert.assertEquals(renderer.render(expectedDocument), renderer.render(testDocument));
}
示例13: setData
import org.commonmark.parser.Parser; //导入依赖的package包/类
public void setData(javafx.scene.Scene parent, String markdown) {
parentScene = parent;
// this really should be fast enough that I don't need to make it
// asynchronous.... the notes aren't going to be that big.
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html = renderer.render(parser.parse(markdown));
webview.getEngine().loadContent(html);
}
示例14: 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);
}
示例15: htmlRendererTest
import org.commonmark.parser.Parser; //导入依赖的package包/类
@Test
public void htmlRendererTest() throws Exception {
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();
String renderedString = renderer.render(parser.parse(spec));
assertNotNull(renderedString);
}