Class MarkdownScanner

All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class MarkdownScanner extends PushbackReader
This class is the reader for the markdown parser. It contains additional methods to support this task.

This reader applies a filter operation to map the line endings \r, \n, and \r\n to \n. Thus all major types of line endings are treated the same.

  • Constructor Details

    • MarkdownScanner

      public MarkdownScanner(Reader in, int size)
      This is the constructor for MarkdownScanner.
      Parameters:
      in - the input reader
      size - the push-back buffer size
  • Method Details

    • expect

      public boolean expect(int... s) throws IOException
      This method checks that the lookahead of several characters are identical to the expected character. Then these are digested. Otherwise the characters are pushed back into the input.
      Parameters:
      s - the expected characters
      Returns:
      true iff n characters have been found
      Throws:
      IOException - in case of an I/O error
    • expectAfterSpace

      public boolean expectAfterSpace(char cc) throws IOException
      This method skips spaces and checks for an expected character. If the character is found then it is skipped as well. Otherwise the character found is pushed back to the input stream.
      Parameters:
      cc - the expected character
      Returns:
      true iff the expected character has been found
      Throws:
      IOException - in case of an I/O error
    • expectLineWith

      public boolean expectLineWith(int cc) throws IOException
      This method checks that the line contains only whitespace and at least 2 instances of the given character.
      Parameters:
      cc - the additional terminating character
      Returns:
      the next undigested character
      Throws:
      IOException - in case of an I/O error
    • expectNumberPeriod

      public String expectNumberPeriod(int cc) throws IOException
      The method expectNumberPeriod skips spaces and checks for at most 9 digits followed by a period and an optional space. If the characters are found then they are skipped as well. Otherwise the characters found are pushed back to the input stream.
      Parameters:
      cc - the digit already read
      Returns:
      the number found or null
      Throws:
      IOException - in case of an I/O error
    • expectParens

      public String[] expectParens() throws IOException
      This method parses to the closing parenthesis.
      Returns:
      a pair of the items contained
      Throws:
      IOException - in case of an I/O error
    • expectString

      public String expectString() throws IOException
      This method expects a string enclosed in single or double quotes.
      Returns:
      the string found or null on failure
      Throws:
      IOException - in case of an I/O error
    • expectUrl

      public String expectUrl(int endChar, boolean absolute) throws IOException
      This method parses a URL and returns it. If no proper URL is found then the input stream is left unchanged and null is returned.
      Parameters:
      endChar - the additional end character – except whitespace
      absolute - indicate that only absolute URLs are allowed
      Returns:
      the URL found or null on failure
      Throws:
      IOException - in case of an I/O error
    • lookahead

      public int lookahead() throws IOException
      This method looks at the next character without actually removing it from the input.
      Returns:
      the next character
      Throws:
      IOException - in case of an I/O error
    • read

      public int read() throws IOException
      Overrides:
      read in class PushbackReader
      Throws:
      IOException
      See Also:
    • readBrackets

      public String readBrackets() throws IOException
      This method reads text in brackets. It is assumed that the opening bracket has already been digested.
      Returns:
      the contents found in the brackets or null
      Throws:
      IOException - in case of an I/O error
    • readLine

      public String readLine() throws IOException
      This method eats up all characters till the next newline.
      Returns:
      the line read
      Throws:
      IOException - in case of an I/O error
    • readLineTo

      public int readLineTo(int c, StringBuilder buffer, char c1, char c2) throws IOException
      This method collects characters in a target buffer until a newline or one of two terminating characters is found.
      Parameters:
      c - the character already read
      buffer - the target buffer
      c1 - the first terminating character
      c2 - the second terminating character
      Returns:
      the character which caused the slurping to be terminated
      Throws:
      IOException - in case of an I/O error
    • readLineTo

      public StringBuilder readLineTo(StringBuilder buffer) throws IOException
      This method eats up all characters till the next newline.
      Parameters:
      buffer - the target buffer
      Returns:
      the target buffer
      Throws:
      IOException - in case of an I/O error
    • readLineTo

      public int readLineTo(StringBuilder buffer, char cc) throws IOException
      This method eats up all characters till the next newline or the terminating character is encountered.
      Parameters:
      buffer - the target buffer
      cc - the additional terminating character
      Returns:
      the next undigested character
      Throws:
      IOException - in case of an I/O error
    • readNonSpace

      public int readNonSpace() throws IOException
      This method eats up all whitespace characters.
      Returns:
      the next undigested character
      Throws:
      IOException - in case of an I/O error
    • readToWhitespace

      public StringBuilder readToWhitespace(StringBuilder buffer) throws IOException
      This method reads characters to the next whitespace into a target buffer.
      Parameters:
      buffer - the target buffer
      Returns:
      the buffer
      Throws:
      IOException - in case of an I/O error
    • scanSectionDepth

      public int scanSectionDepth(int max) throws IOException
      This method digests all hash marks found and returns their number.
      Parameters:
      max - the limit for the return value
      Returns:
      the number of hashes up to the limit
      Throws:
      IOException - in case of an I/O error
    • skipLine

      public void skipLine() throws IOException
      This method skips to EOL.
      Throws:
      IOException - in case of an I/O error
    • skipSpace

      public int skipSpace(StringBuilder buffer) throws IOException
      This method reads ahead and stores all whitespace characters in the provided buffer.
      Parameters:
      buffer - the target buffer
      Returns:
      the first non-whitespace character
      Throws:
      IOException - in case of an I/O error
    • skipWhiteSpace

      public int skipWhiteSpace(int indent) throws IOException
      This method reads ahead and skips all whitespace characters.
      Parameters:
      indent - the previous indentation
      Returns:
      the new indentation level
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(CharSequence s) throws IOException
      This method pushes back a string to the input stream.
      Parameters:
      s - the string
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(int c) throws IOException
      This method pushes back a character to the input stream.
      Parameters:
      c - the character code
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(int c, CharSequence s) throws IOException
      This method pushes back a string to the input stream.
      Parameters:
      c - the character code
      s - the string
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(int c, CharSequence s, CharSequence s2) throws IOException
      This method pushes back a string to the input stream.
      Parameters:
      c - the character code
      s - the string
      s2 - the second string
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(int c, CharSequence s, CharSequence s2, int c2) throws IOException
      This method pushes back a string to the input stream.
      Parameters:
      c - the character code
      s - the string
      s2 - the second string
      c2 - the terminating character
      Throws:
      IOException - in case of an I/O error
    • unget

      public void unget(int c, CharSequence s, int c2) throws IOException
      This method pushes back a string to the input stream.
      Parameters:
      c - the character code
      s - the string
      c2 - the terminating character
      Throws:
      IOException - in case of an I/O error