org.commonmark.parser.Parser.parse()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(182)

本文整理了Java中org.commonmark.parser.Parser.parse()方法的一些代码示例,展示了Parser.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parser.parse()方法的具体详情如下:
包路径:org.commonmark.parser.Parser
类名称:Parser
方法名:parse

Parser.parse介绍

[英]Parse the specified input text into a tree of nodes.

This method is thread-safe (a new parser state is used for each invocation).
[中]将指定的输入文本解析为节点树。
这个方法是线程安全的(每次调用都使用一个新的解析器状态)。

代码示例

代码示例来源:origin: JpressProjects/jpress

/**
 * 渲染html
 *
 * @param markdown markdown格式文本
 * @return html文本
 */
public static String toHtml(String markdown) {
  if (StrUtils.isBlank(markdown)) return markdown;
  Node document = PARSER.parse(markdown);
  return RENDERER.render(document);
}

代码示例来源:origin: JpressProjects/jpress

/**
 * 获取元数据
 *
 * @param content content
 * @return Map
 */
public static Map<String, List<String>> getFrontMatter(String content) {
  YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
  Node document = PARSER.parse(content);
  document.accept(visitor);
  return visitor.getData();
}

代码示例来源:origin: ZHENFENG13/My-Blog

/**
 * 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;
}

代码示例来源:origin: jamesagnew/hapi-fhir

public Cell addMarkdown(String md) {
 try {
  Parser parser = Parser.builder().build();
  Node document = parser.parse(md);
  HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
  String html = renderer.render(document);  
  pieces.addAll(htmlToParagraphPieces(html));
 } catch (Exception e) {
  e.printStackTrace();
 }
 return this;
}
private List<Piece> htmlToParagraphPieces(String html) throws IOException, FHIRException {

代码示例来源:origin: nice-swa/my-site

/**
 * 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;
}

代码示例来源:origin: tomoya92/pybbs

public static String render(String content) {
 List<Extension> extensions = Arrays.asList(
   AutolinkExtension.create(),
   TablesExtension.create());
 Parser parser = Parser.builder()
   .extensions(extensions)
   .build();
 // 回车一次就可以实现换行
 HtmlRenderer renderer = HtmlRenderer.builder()
   .softbreak("<br/>")
   .attributeProviderFactory(context -> new MyAttributeProvider())
   .extensions(extensions)
   .build();
 Node document = parser.parse(content == null ? "" : content);
 return renderer.render(document);
}

代码示例来源:origin: aint/laverna-android

/**
 * A method which parses a text with markdown using a commonmark's parser.
 * @param text a text to parse.
 * @return a text after parsing.
 */
private String parseMarkdown(String text) {
  Node node = parser.parse(text);
  return renderer.render(node);
}

代码示例来源:origin: pingao777/markdown-preview-sync

public static String markdown2Html(String markdown) {
  Node document = PARSER.parse(markdown);
  String html = RENDERER.render(document);
  int index = html.indexOf(MARKER);
  if (index >= 0) {
    return html.substring(0, index) + MARKER_HTML + html.substring(index + MARKER.length());
  } else {
    return html;
  }
}

代码示例来源:origin: sdeleuze/spring-kotlin-deepdive

@Override
  public String apply(String input) {
    if (input == null || input.isEmpty()) {
      return "";
    }
    return renderer.render(parser.parse(input));
  }
}

代码示例来源:origin: org.symphonyoss.symphony/messageml

/**
 * Parse the Markdown message and entity JSON into a MessageML document.
 */
public MessageML parse(String message, JsonNode entities, JsonNode media) throws InvalidInputException {
 this.index = 0;
 message = message.replace((char) 160, (char) 32);
 String enriched = enrichMarkdown(message, entities, media);
 Node markdown = MARKDOWN_PARSER.parse(enriched);
 markdown.accept(this);
 return messageML;
}

代码示例来源:origin: noties/Markwon

/**
 * Returns parsed markdown with provided {@link SpannableConfiguration}
 *
 * @param configuration a {@link SpannableConfiguration}
 * @param markdown      raw markdown
 * @return parsed markdown
 * @see SpannableConfiguration
 * @since 1.0.0
 */
@NonNull
public static CharSequence markdown(@NonNull SpannableConfiguration configuration, @NonNull String markdown) {
  final Parser parser = createParser();
  final Node node = parser.parse(markdown);
  final SpannableRenderer renderer = new SpannableRenderer();
  return renderer.render(configuration, node);
}

代码示例来源:origin: org.openapitools/openapi-generator

/** 
 * Convert input markdown text to HTML.
 * Simple text is not wrapped in <p>...</p>.
 * @param markdown text with Markdown styles. If <code>null</code>, <code>""</code> is returned.
 * @return HTML rendering from the Markdown
 */
public String toHtml(String markdown) {
  if (markdown == null)
    return "";
  Node document = parser.parse(markdown);
  String html = renderer.render(document);
  html = unwrapped(html);
  return html;
}

代码示例来源:origin: googleapis/gapic-generator

@Override
public String reformat(String comment) {
 Node root = PARSER.parse(comment);
 GoVisitor visitor = new GoVisitor();
 try {
  root.accept(visitor);
  return visitor.toString();
 } catch (ErrorMarkdownVisitor.UnimplementedRenderException e) {
  LOGGER.log(
    Level.WARNING, "markdown contains elements we don't handle; copying doc verbatim", e);
  return comment;
 }
}

代码示例来源:origin: andresth/Kandroid

@NonNull
  @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    if (position < getCount() - (mShowAdd ? 1 : 0)) {
      convertView = mInflater.inflate(R.layout.listitem_comment, parent, false);
      convertView.setLongClickable(true);
      ((TextView) convertView.findViewById(R.id.username)).setText(Utils.fromHtml(String.format("<small>%s</small>", users == null ? mObjects.get(position).getUsername() : users.get(mObjects.get(position).getUserId()))));
      ((TextView) convertView.findViewById(R.id.date)).setText(Utils.fromHtml(String.format("<small>%tF</small>", mObjects.get(position).getDateModification())));
      ((TextView) convertView.findViewById(R.id.comment)).setText(Utils.fromHtml(mRenderer.render(mParser.parse(mObjects.get(position).getContent()))));
    } else {
      convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
      ((TextView) convertView.findViewById(android.R.id.text1)).setText(getString(R.string.taskview_fab_new_comment));
      ((TextView) convertView.findViewById(android.R.id.text1)).setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    }
    return convertView;
  }
}

代码示例来源:origin: ca.uhn.hapi.fhir/hapi-fhir-utilities

public Cell addMarkdown(String md) {
 try {
  Parser parser = Parser.builder().build();
  Node document = parser.parse(md);
  HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build();
  String html = renderer.render(document);  
  pieces.addAll(htmlToParagraphPieces(html));
 } catch (Exception e) {
  e.printStackTrace();
 }
 return this;
}
private List<Piece> htmlToParagraphPieces(String html) throws IOException, FHIRException {

代码示例来源:origin: 94fzb/zrlog

public static String renderMd(String md) {
  Parser parser = Parser.builder().build();
  Node document = parser.parse(md);
  HtmlRenderer renderer = HtmlRenderer.builder().build();
  return renderer.render(document);
}

代码示例来源:origin: org.opensingular/singular-form-wicket

private static String renderMarkdown(String message) {
    Parser parser = Parser.builder().build();
    Node node = parser.parse(message);
    return HtmlRenderer.builder().build().render(node);
  }
}

代码示例来源:origin: xiancloud/xian

/**
   * @param md the markdown string.
   * @return converted html
   */
  public static String mdToHtml(String md) {
    Parser parser = Parser.builder().build();
    Node document = parser.parse(md);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    return renderer.render(document);
  }
}

代码示例来源:origin: synchrony/smsn

@Test
public void commonMarkParsesSimpleMarkdown() {
  Parser parser = Parser.builder().build();
  Node document = parser.parse("This is *Sparta*");
  HtmlRenderer renderer = HtmlRenderer.builder().build();
  String doc = renderer.render(document);
  assertEquals("<p>This is <em>Sparta</em></p>\n", doc);
}

代码示例来源:origin: noties/Markwon

@Test
public void test() {
  final TestData data = TestDataReader.readTest(file);
  final SpannableConfiguration configuration = configuration(data.config());
  final SpannableBuilder builder = new SpannableBuilder();
  final SpannableMarkdownVisitor visitor = new SpannableMarkdownVisitor(configuration, builder);
  final Node node = Markwon.createParser().parse(data.input());
  node.accept(visitor);
  final SpannableStringBuilder stringBuilder = builder.spannableStringBuilder();
  final TestValidator validator = TestValidator.create(file);
  int index = 0;
  for (TestNode testNode : data.output()) {
    index = validator.validate(stringBuilder, index, testNode);
  }
  // assert that the whole thing is processed
  assertEquals("`" + stringBuilder + "`", stringBuilder.length(), index);
  final Object[] spans = stringBuilder.getSpans(0, stringBuilder.length(), Object.class);
  final int length = spans != null
      ? spans.length
      : 0;
  assertEquals(Arrays.toString(spans), validator.processedSpanNodesCount(), length);
}

相关文章