Class Parser

java.lang.Object
org.commonmark.parser.Parser

public class Parser extends Object
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");
 
  • Field Details

  • Constructor Details

  • Method Details

    • builder

      public static Parser.Builder builder()
      Create a new builder for configuring a Parser.
      Returns:
      a builder
    • parse

      public Node parse(String input)
      Parse the specified input text into a tree of nodes.

      This method is thread-safe (a new parser state is used for each invocation).

      Parameters:
      input - the text to parse - must not be null
      Returns:
      the root node
    • parseReader

      public Node parseReader(Reader input) throws IOException
      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).

      Parameters:
      input - the reader to parse - must not be null
      Returns:
      the root node
      Throws:
      IOException - when reading throws an exception
    • createDocumentParser

      private DocumentParser createDocumentParser()
    • postProcess

      private Node postProcess(Node document)