org.commonmark.parser.Parser类的使用及代码示例

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

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

Parser介绍

[英]Parses input text to a tree of nodes.

Start with the #builder method, configure the parser and build it. Example:

Parser parser = Parser.builder().build(); 
Node document = parser.parse("input text");

[中]将输入文本解析为节点树。
从#builder方法开始,配置解析器并构建它。例子:

Parser parser = Parser.builder().build(); 
Node document = parser.parse("input text");

代码示例

代码示例来源: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: 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: synchrony/smsn

private Document parseToMarkdownDocument(final InputStream input) throws IOException {
  Parser parser = Parser.builder().build();
  Node document = parser.parseReader(new InputStreamReader(input));
  return (Document) document;
}

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

/**
 * A method which returns Parser which parses input text to a tree of nodes.
 * @return a Parser object.
 */
private Parser getParser() {
  return Parser.builder()
      .extensions(getExtensions())
      .build();
}

代码示例来源:origin: g00glen00b/spring-samples

@Override
  public HtmlResource process(Resource markdownResource) throws IOException {
    try (InputStreamReader reader = new InputStreamReader(markdownResource.getInputStream())) {
      Node document = parser.parseReader(reader);
      return new HtmlResource(markdownResource, htmlRenderer.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: com.xmlcalabash/xmlcalabash

public void load(URI id, String media, InputStream content, long len) throws IOException {
    Reader rdr = new InputStreamReader(content);
    Parser parser = Parser.builder().build();
    Node document = parser.parseReader(rdr);
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    // We rely on the fact that the CommonMark parser returns well-formed markup consisting
    // of the paragraphs and other bits that would occur inside a <body> element and
    // that it returns them with no namespace declarations.
    String markup = "<body xmlns='http://www.w3.org/1999/xhtml'>" + renderer.render(document) + "</body>";
    XdmNode parsed = runtime.parse(new InputSource(new StringReader(markup)));
    // Let's craft a baseURI for the document...
    String path = id.getPath();
    int lastSlash = path.lastIndexOf("/");
    String base = path.substring(0, lastSlash+1);
    String filename = path.substring(lastSlash+1);
    int lastDot = filename.lastIndexOf(".");
    if (lastDot > 0) {
      base = base + filename.substring(0, lastDot) + ".html";
    } else {
      base = base + filename + ".html";
    }
    URI baseURI = id.resolve(base);
    TreeWriter tree = new TreeWriter(runtime);
    tree.startDocument(baseURI);
    tree.addSubtree(parsed);
    tree.endDocument();
    result.write(tree.getResult());
  }
});

代码示例来源:origin: g00glen00b/spring-samples

@PostConstruct
public void initialize() {
  List<Extension> extensions = Collections.singletonList(TablesExtension.create());
  parser = Parser.builder().extensions(extensions).build();
  htmlRenderer = HtmlRenderer.builder().extensions(extensions).build();
}

代码示例来源:origin: iNPUTmice/caas

private static HashMap<String, String> load(Path serverFolder) throws IOException {
  HashMap<String, String> helpForThisSoftware = new HashMap<>();
  Files.walk(serverFolder, 1).filter(Files::isRegularFile).forEach(filePath -> {
    Node document;
    try {
      document = PARSER.parseReader(Files.newBufferedReader(filePath));
      String htmlText = RENDERER.render(document);
      String testFileName = filePath.getFileName().toString();
      int length = testFileName.length();
      if (!testFileName.substring(length - 3).equals(".md")) {
        throw new IllegalStateException("All help files should have md extension");
      }
      String testName = testFileName.substring(0, (testFileName.length() - 3));
      helpForThisSoftware.put(testName, htmlText);
    } catch (IOException e) {
      e.printStackTrace();
    }
  });
  LOGGER.info(
      "Read help file for " + serverFolder.getFileName().toString() +
          " which contained help for " + helpForThisSoftware.size() + " tests"
  );
  return helpForThisSoftware;
}

代码示例来源: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: 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: 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: 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: 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: sdeleuze/spring-kotlin-deepdive

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

代码示例来源: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.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: gsvigruha/cosyan

private static void markdownToHtml(String resourcesDir) throws IOException {
 File docRoot = new File(resourcesDir + File.separator + "doc");
 Collection<File> files = FileUtils.listFiles(docRoot, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).stream()
   .sorted((f1, f2) -> f1.getName().compareTo(f2.getName())).collect(Collectors.toList());
 JSONArray items = new JSONArray();
 String webRoot = "web" + File.separator + "app" + File.separator + "help";
 for (File markdown : files) {
  Parser parser = Parser.builder().build();
  Node document = parser.parse(FileUtils.readFileToString(markdown, Charset.defaultCharset()));
  HtmlRenderer renderer = HtmlRenderer.builder().build();
  String suffix = markdown.getAbsolutePath().substring(docRoot.getAbsolutePath().length() + 1, markdown.getAbsolutePath().length() - 3);
  File html = new File(webRoot + File.separator + suffix + ".html");
  FileUtils.writeStringToFile(html, renderer.render(document), Charset.defaultCharset());
  JSONObject object = new JSONObject();
  object.put("url", suffix);
  object.put("title", ((Text) document.getFirstChild().getFirstChild()).getLiteral());
  items.put(object);
 }
 FileUtils.writeStringToFile(new File(webRoot + File.separator + "list"), items.toString(), Charset.defaultCharset());
}

代码示例来源: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.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);
  }
}

相关文章