Class MarkdownElement

java.lang.Object
org.tealeaf.javamarkdown.MarkdownElement
Direct Known Subclasses:
InlineElement, Structure

public abstract class MarkdownElement extends Object
Represents a markdown item, or component, that contains its own method of printing itself.
Since:
0.0.8
Author:
Thomas Kwashnak
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract String
    Formats the element to a string
    asString(boolean newLineBefore)
    Formats the element to a string, including required new-lines as needed
    protected abstract <T> T
    checkType(T item)
    Checks the type of a passed object.
    abstract boolean
    Whether a new line should be added after inserting the element into a document.
    abstract boolean
    Whether the element requires at least one \n before printing itself in a document
    <T extends Appendable>
    T
    toAppendable(T appendable)
    Outputs the MarkdownElement into an appendable class
    Prints the compiled string representation of the element
    <T extends Writer>
    T
    toWriter(T writer)
    Writes the formatted item to a Writer

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MarkdownElement

      public MarkdownElement()
  • Method Details

    • toWriter

      public <T extends Writer> T toWriter(T writer) throws IOException
      Writes the formatted item to a Writer
      Type Parameters:
      T - The base type of the writer used. This type must extend Writer
      Parameters:
      writer - Writer to write contents to
      Returns:
      The writer passed in the parameters
      Throws:
      IOException - If any exceptions were thrown during writing
      Since:
      0.0.8
    • toAppendable

      public <T extends Appendable> T toAppendable(T appendable) throws IOException
      Outputs the MarkdownElement into an appendable class
      Type Parameters:
      T - The type of appendable. This type must implement the Appendable interface
      Parameters:
      appendable - The appendable object to write this element to
      Returns:
      The appendable object passed in as the parameter
      Throws:
      IOException - If any exceptions were thrown during appending
      Since:
      0.0.21
    • asString

      public abstract String asString()
      Formats the element to a string
      Returns:
      String representation of the formatted item
      Since:
      0.0.8
    • toString

      public String toString()

      Prints the compiled string representation of the element

      Uses the asString() to compile

      Overrides:
      toString in class Object
      Returns:
      String representation of the element
      Since:
      0.0.8
      See Also:
    • requiresNewlineBefore

      public abstract boolean requiresNewlineBefore()

      Whether the element requires at least one \n before printing itself in a document

      In instances such as in-line elements (like Bold), this would return false because bolds can be inserted directly in-line. However, in instances such as NumberedList, it would be required that there is a new line to prevent the following

           Some sentence - item 1
           - item 2
           - item 3
       

      and instead print the following

           Some sentence
           - item 1
           - item 2
       

      This method is natively used in MarkdownWriter.appendMarkdownElement(MarkdownElement) to handle adding newlines when necessary

      Returns:
      true if element requires a new line beforehand, false otherwise
      Since:
      0.0.8
    • requiresNewlineAfter

      public abstract boolean requiresNewlineAfter()

      Whether a new line should be added after inserting the element into a document. This is particularly useful for structures that need to have a newline after it gets rendered

      For example, if some text was printed immediately after a list, it would render as:

           - item 1
           - item 2
           - item 3some text
       

      Thus, by requiring a new line after the element is rendered, we get the following output instead:

           - item 1
           - item 2
           - item 3
           some text
       

      This method is natively used in MarkdownWriter.appendMarkdownElement(MarkdownElement) to handle adding newlines when necessary

      Returns:
      true if element requires a new line after, false otherwise
      Since:
      0.0.8
    • checkType

      protected abstract <T> T checkType(T item) throws IllegalContentsException

      Checks the type of a passed object. This method is specific to each element and throws an IllegalContentsException if the passed object cannot be put in as a content of that element. This prevents instances such as nested tables, where a table is put inside another table. If such feature is necessary, using the toString() of any markdown element will force it to be accepted in any spot

      Type Parameters:
      T - The type of the item
      Parameters:
      item - The item to check it's class
      Returns:
      That item if it is not an instance of any illegal types
      Throws:
      IllegalContentsException - if the object passed is an illegal type for the element
      Since:
      0.0.11
    • asString

      public String asString(boolean newLineBefore)

      Formats the element to a string, including required new-lines as needed

      Parameters:
      newLineBefore - Whether or not there is a new line directly before printing this element
      Returns:
      The string representation of this element, adapting to whether or not there is a newline directly before
      Since:
      0.0.21