Class MarkdownSink

    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • buffer

        private java.lang.StringBuilder buffer
        A buffer that holds the current text when headerFlag or bufferFlag set to true. The content of this buffer is already escaped.
      • authors

        private java.util.Collection<java.lang.String> authors
        author.
      • title

        private java.lang.String title
        title.
      • date

        private java.lang.String date
        date.
      • linkName

        private java.lang.String linkName
        linkName.
      • tableHeaderCellFlag

        private boolean tableHeaderCellFlag
        tableHeaderCellFlag, set to true for table rows containing at least one table header cell
      • cellCount

        private int cellCount
        number of cells in a table.
      • cellJustif

        private java.util.List<java.lang.Integer> cellJustif
        justification of table cells per column.
      • isFirstTableRow

        private boolean isFirstTableRow
        is header row
      • writer

        private final java.io.PrintWriter writer
        The writer to use.
      • inlineStack

        protected java.util.Queue<java.util.Queue<java.lang.String>> inlineStack
        Keep track of end markup for inline events.
      • elementContextStack

        protected java.util.Queue<MarkdownSink.ElementContext> elementContextStack
        The context of the surrounding elements as stack (LIFO)
      • figureSrc

        private java.lang.String figureSrc
    • Constructor Detail

      • MarkdownSink

        protected MarkdownSink​(java.io.Writer writer)
        Constructor, initialize the Writer and the variables.
        Parameters:
        writer - not null writer to write the result. Should be an UTF-8 Writer.
    • Method Detail

      • ensureBeginningOfLine

        private void ensureBeginningOfLine()
        Ensures that the writer is currently at the beginning of a new line. Optionally writes a line separator to ensure that.
      • ensureBlankLine

        private void ensureBlankLine()
        Ensures that the writer is either at the beginning or preceded by a blank line. Optionally writes a blank line to ensure that.
      • startBlock

        private void startBlock​(boolean requireBlankLine)
      • endBlock

        private void endBlock​(boolean requireBlankLine)
      • getContainerLinePrefixes

        private java.lang.String getContainerLinePrefixes()
      • getBuffer

        protected java.lang.StringBuilder getBuffer()
        Returns the buffer that holds the current text.
        Returns:
        A StringBuffer.
      • resetBuffer

        protected void resetBuffer()
        Reset the StringBuilder.
      • head

        public void head​(SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Starts the head element.

        This contains information about the current document, (eg its title) that is not considered document content. The head element is optional but if it exists, it has to be unique within a sequence of Sink events that produces one output document, and it has to come before the Sink.body(SinkEventAttributes) element.

        The canonical sequence of events for the head element is:

           sink.head();
        
           sink.title();
           sink.text("Title");
           sink.title_();
        
           sink.author();
           sink.text("Author");
           sink.author_();
        
           sink.date();
           sink.text("Date");
           sink.date_();
        
           sink.head_();
         

        but none of the enclosed events is required. However, if they exist they have to occur in the order shown, and the title() and date() events have to be unique (author() events may occur any number of times).

        Supported attributes are:

        PROFILE, LANG.
        Specified by:
        head in interface Sink
        Overrides:
        head in class SinkAdapter
        Parameters:
        attributes - A set of SinkEventAttributes, may be null.
      • sectionTitle

        public void sectionTitle​(int level,
                                 SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Start a new section title at the given level.

        This element is optional, but if it exists, it has to be contained, and be the first element, within a corresponding section element of the same level.

        NOTE: It is strongly recommended not to make section titles implicit anchors. Neither Parsers nor Sinks should insert any content that is not explicitly present in the original source document, as this would lead to undefined behaviour for multi-format processing chains. However, while Parsers must never emit anchors for section titles, some specialized Sinks may implement such a feature if the resulting output documents are not going to be further processed (and this is properly documented).

        Supported attributes are the base attributes plus ALIGN.

        Specified by:
        sectionTitle in interface Sink
        Overrides:
        sectionTitle in class SinkAdapter
        Parameters:
        level - the section title level (must be a value between 1 and 6).
        attributes - A set of SinkEventAttributes, may be null.
      • sectionTitle_

        public void sectionTitle_​(int level)
        Description copied from class: SinkAdapter
        Ends a section title at the given level.
        Specified by:
        sectionTitle_ in interface Sink
        Overrides:
        sectionTitle_ in class SinkAdapter
        Parameters:
        level - the section title level (must be a value between 1 and 6).
      • list_

        public void list_()
        Description copied from class: SinkAdapter
        Ends an unordered list element.
        Specified by:
        list_ in interface Sink
        Overrides:
        list_ in class SinkAdapter
      • listItem_

        public void listItem_()
        Description copied from class: SinkAdapter
        Ends a list item element within an unordered list.
        Specified by:
        listItem_ in interface Sink
        Overrides:
        listItem_ in class SinkAdapter
      • verbatim

        public void verbatim​(SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Starts a verbatim block, ie a block where whitespace has semantic relevance.

        Text in a verbatim block must only be wrapped at the linebreaks in the source, and spaces should not be collapsed. It should be displayed in a fixed-width font to retain the formatting but the overall size may be chosen by the implementation.

        Most Sink events may be emitted within a verbatim block, the only elements explicitly forbidden are font-changing events and figures. Also, verbatim blocks may not be nested.

        Supported attributes are the base attributes plus:

        DECORATION (values: "source"), ALIGN, WIDTH.
        Specified by:
        verbatim in interface Sink
        Overrides:
        verbatim in class SinkAdapter
        Parameters:
        attributes - A set of SinkEventAttributes, may be null.
      • table

        public void table​(SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Starts a table.

        The canonical sequence of events for the table element is:

           sink.table();
        
           sink.tableRows(justify, true);
        
           sink.tableRow();
           sink.tableCell();
           sink.text("cell 1,1");
           sink.tableCell_();
           sink.tableCell();
           sink.text("cell 1,2");
           sink.tableCell_();
           sink.tableRow_();
        
           sink.tableRows_();
        
           sink.tableCaption();
           sink.text("Table caption");
           sink.tableCaption_();
        
           sink.table_();
        
         

        where the tableCaption element is optional.

        However, NOTE that the order of tableCaption and Sink.tableRows(int[],boolean) events is arbitrary, ie a parser may emit the tableCaption before or after the tableRows. Implementing sinks should be prepared to handle both possibilities.

        Supported attributes are the base attributes plus:

        ALIGN, BGCOLOR, BORDER, CELLPADDING, CELLSPACING, FRAME, RULES, SUMMARY, WIDTH.
        Specified by:
        table in interface Sink
        Overrides:
        table in class SinkAdapter
        Parameters:
        attributes - A set of SinkEventAttributes, may be null.
      • tableRows

        public void tableRows​(int[] justification,
                              boolean grid)
        Description copied from class: SinkAdapter
        Starts an element that contains rows of table data.
        Specified by:
        tableRows in interface Sink
        Overrides:
        tableRows in class SinkAdapter
        Parameters:
        justification - the default justification of columns. This can be overridden by individual table rows or table cells. If null a left alignment is assumed by default. If this array has less elements than there are columns in the table then the value of the last array element will be taken as default for the remaining table cells.
        grid - true to provide a grid, false otherwise.
        See Also:
        Sink.table(SinkEventAttributes), Sink.JUSTIFY_CENTER, Sink.JUSTIFY_LEFT, Sink.JUSTIFY_RIGHT
      • writeEmptyTableHeader

        private void writeEmptyTableHeader()
      • writeTableDelimiterRow

        private void writeTableDelimiterRow()
        Emit the delimiter row which determines the alignment
      • endTableCell

        private void endTableCell()
        Ends a table cell.
      • figure

        public void figure​(SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Starts a basic image embedding element.

        The canonical sequence of events for the figure element is:

           sink.figure();
        
           sink.figureGraphics("figure.png");
        
           sink.figureCaption();
           sink.text("Figure caption",);
           sink.figureCaption_();
        
           sink.figure_();
         

        where the figureCaption element is optional.

        However, NOTE that the order of figureCaption and figureGraphics events is arbitrary, ie a parser may emit the figureCaption before or after the figureGraphics. Implementing sinks should be prepared to handle both possibilities.

        NOTE also that the figureGraphics() event does not have to be embedded inside figure(), in particular for in-line images the figureGraphics() should be used stand-alone (in HTML language, figureGraphics() produces a <img> tag, while figure() opens a paragraph- or <div>- like environment).

        Supported attributes are the base attributes.

        Specified by:
        figure in interface Sink
        Overrides:
        figure in class SinkAdapter
        Parameters:
        attributes - A set of SinkEventAttributes, may be null.
      • writeImage

        private void writeImage​(java.lang.String alt,
                                java.lang.String src)
      • anchor

        public void anchor​(java.lang.String name,
                           SinkEventAttributes attributes)
        Starts an element which defines an anchor.

        The name parameter has to be a valid SGML NAME token. According to the HTML 4.01 specification section 6.2 SGML basic types:

        ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

        Supported attributes are the base attributes. If NAME is specified in the SinkEventAttributes, it will be overwritten by the name parameter.

        Specified by:
        anchor in interface Sink
        Overrides:
        anchor in class SinkAdapter
        Parameters:
        name - the name of the anchor. This has to be a valid SGML NAME token.
        attributes - A set of SinkEventAttributes, may be null.
      • link

        public void link​(java.lang.String name,
                         SinkEventAttributes attributes)
        Starts a link.

        The name parameter has to be a valid URI according to RFC 3986, i.e. for internal links (links to an anchor within the same source document), name should start with the character "#". This also implies that all unsafe characters are already encoded.

        Supported attributes are the base attributes plus:

        CHARSET, COORDS, HREF, HREFLANG, REL, REV, SHAPE, TARGET, TYPE.

        If HREF is specified in the SinkEventAttributes, it will be overwritten by the name parameter.

        Specified by:
        link in interface Sink
        Overrides:
        link in class SinkAdapter
        Parameters:
        name - the name of the link.
        attributes - A set of SinkEventAttributes, may be null.
        See Also:
        URI.toASCIIString()
      • inline

        public void inline​(SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Starts an inline element.

        The inline method is similar to Sink.text(String,SinkEventAttributes), but allows you to wrap arbitrary elements in addition to text.

        Supported attributes are the base attributes plus

        SEMANTICS (values "emphasis", "strong", "small", "line-through", "citation", "quote", "definition", "abbreviation", "italic", "bold", "monospaced", "variable", "sample", "keyboard", "superscript", "subscript", "annotation", "highlight", "ruby", "rubyBase", "rubyText", "rubyTextContainer", "rubyParentheses", "bidirectionalIsolation", "bidirectionalOverride", "phrase", "insert", "delete").
        Specified by:
        inline in interface Sink
        Overrides:
        inline in class SinkAdapter
        Parameters:
        attributes - A set of SinkEventAttributes, may be null.
      • text

        public void text​(java.lang.String text,
                         SinkEventAttributes attributes)
        Description copied from class: SinkAdapter
        Adds a text.

        The text parameter should contain only real content, ie any ignorable/collapsable whitespace/EOLs or other pretty-printing should be removed/normalized by a parser.

        If text contains any variants of line terminators, they should be normalized to the System EOL by an implementing Sink.

        Supported attributes are the base attributes plus

        SEMANTICS (values "emphasis", "strong", "small", "line-through", "citation", "quote", "definition", "abbreviation", "italic", "bold", "monospaced", "variable", "sample", "keyboard", "superscript", "subscript", "annotation", "highlight", "ruby", "rubyBase", "rubyText", "rubyTextContainer", "rubyParentheses", "bidirectionalIsolation", "bidirectionalOverride", "phrase", "insert", "delete").

        The following attributes are deprecated:

        VALIGN (values "sub", "sup"), DECORATION (values "underline", "overline", "line-through"), STYLE (values "italic", "bold", "monospaced").
        Specified by:
        text in interface Sink
        Overrides:
        text in class SinkAdapter
        Parameters:
        text - The text to write.
        attributes - A set of SinkEventAttributes, may be null.
      • rawText

        public void rawText​(java.lang.String text)
        Description copied from class: SinkAdapter
        Adding a raw text, ie a text without any special formatting operations.
        Specified by:
        rawText in interface Sink
        Overrides:
        rawText in class SinkAdapter
        Parameters:
        text - The text to write.
      • comment

        public void comment​(java.lang.String comment)
        Description copied from class: SinkAdapter
        Add a comment.
        Specified by:
        comment in interface Sink
        Overrides:
        comment in class SinkAdapter
        Parameters:
        comment - The comment to write.
      • unknown

        public void unknown​(java.lang.String name,
                            java.lang.Object[] requiredParams,
                            SinkEventAttributes attributes)
        Add an unknown event. This may be used by parsers to notify a general Sink about an event that doesn't fit into any event defined by the Sink API. Depending on the parameters, a Sink may decide whether or not to process the event, emit it as raw text, as a comment, log it, etc. Unknown events just log a warning message but are ignored otherwise.
        Specified by:
        unknown in interface Sink
        Overrides:
        unknown in class SinkAdapter
        Parameters:
        name - The name of the event.
        requiredParams - An optional array of required parameters to the event. May be null.
        attributes - A set of SinkEventAttributes, may be null.
        See Also:
        Sink.unknown(String,Object[],SinkEventAttributes)
      • requiresBuffering

        private boolean requiresBuffering()
        Returns:
        true if any of the parent contexts require buffering
      • writeUnescaped

        protected void writeUnescaped​(java.lang.String text)
      • flush

        public void flush()
        Description copied from class: SinkAdapter
        Flush the writer or the stream, if needed. Flushing a previously-flushed Sink has no effect.
        Specified by:
        flush in interface Sink
        Overrides:
        flush in class SinkAdapter
      • close

        public void close()
        Description copied from class: SinkAdapter
        Close the writer or the stream, if needed. Closing a previously-closed Sink has no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface Sink
        Overrides:
        close in class SinkAdapter
      • escapeMarkdown

        private static java.lang.String escapeMarkdown​(java.lang.String text)
        First use XML escaping (leveraging the predefined entities, for browsers) afterwards escape special characters in a text with a leading backslash (for markdown parsers)
         \, `, *, _, {, }, [, ], (, ), #, +, -, ., !
         
        Parameters:
        text - the String to escape, may be null
        Returns:
        the text escaped, "" if null String input
        See Also:
        Backslash Escapes
      • escapeForTableCell

        private static java.lang.String escapeForTableCell​(java.lang.String text)
        Escapes the pipe character according to GFM Table Extension in addition to the regular markdown escaping.
        Parameters:
        text -
        Returns:
        the escaped text