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

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

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

Parser.parseReader介绍

[英]Parse the specified reader into a tree of nodes. The caller is responsible for closing the reader.

Parser parser = Parser.builder().build(); 
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("file.md"), StandardCharsets.UTF_8)) { 
Node document = parser.parseReader(reader); 
// ... 
}

Note that if you have a file with a byte order mark (BOM), you need to skip it before handing the reader to this library. There's existing classes that do that, e.g. see BOMInputStream in Commons IO.

This method is thread-safe (a new parser state is used for each invocation).
[中]将指定的读取器解析为节点树。呼叫方负责关闭读卡器

Parser parser = Parser.builder().build(); 
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("file.md"), StandardCharsets.UTF_8)) { 
Node document = parser.parseReader(reader); 
// ... 
}

请注意,如果您有一个带有字节顺序标记(BOM)的文件,则需要在将读取器交给此库之前跳过它。现有的类可以做到这一点,例如,参见Commons IO中的BOMInputStream。
这个方法是线程安全的(每次调用都使用一个新的解析器状态)。

代码示例

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

相关文章