Class MarkdownParser

All Implemented Interfaces:
Markup, TextMarkup, Parser

@Singleton @Named("markdown") public class MarkdownParser extends AbstractTextParser implements TextMarkup

Implementation of Parser for Markdown documents.

Defers effective parsing to the flexmark-java library, which generates HTML content then delegates parsing of this content to a slightly modified Doxia Xhtml5 parser. (before 1.8, the PegDown library was used)

Since:
1.3
  • Field Details

    • METADATA_SECTION_PATTERN

      private static final Pattern METADATA_SECTION_PATTERN
      Regex that identifies a multimarkdown-style metadata section at the start of the document In order to ensure that we have minimal risk of false positives when slurping metadata sections, the first key in the metadata section must be one of these standard keys or else the entire metadata section is ignored.
      See Also:
    • METADATA_ENTRY_PATTERN

      private static final Pattern METADATA_ENTRY_PATTERN
      Regex that captures the key and value of a multimarkdown-style metadata entry. Group 1 captures the key, group 2 captures the value. Multivalues are not supported in the syntax! Multiline values need to be normalized
      See Also:
    • parser

      @Inject private MarkdownParser.MarkdownHtmlParser parser
      The parser of the HTML produced by Flexmark, that we will use to convert this HTML to Sink events
    • FLEXMARK_PARSER

      private static final com.vladsch.flexmark.parser.Parser FLEXMARK_PARSER
      Flexmark's Markdown parser (one static instance fits all)
    • FLEXMARK_METADATA_PARSER

      private static final com.vladsch.flexmark.parser.Parser FLEXMARK_METADATA_PARSER
      Flexmark's Markdown Metadata parser
    • FLEXMARK_HTML_RENDERER

      private static final com.vladsch.flexmark.html.HtmlRenderer FLEXMARK_HTML_RENDERER
      Flexmark's HTML renderer (its output will be re-parsed and converted to Sink events)
  • Constructor Details

    • MarkdownParser

      public MarkdownParser()
  • Method Details

    • parse

      public void parse(Reader source, Sink sink, String reference) throws ParseException
      Parses the given source model and emits Doxia events into the given sink.
      Specified by:
      parse in interface Parser
      Parameters:
      source - not null reader that provides the source document. You could use newReader methods from ReaderFactory.
      sink - A sink that consumes the Doxia events.
      reference - a string identifying the source (for file based documents the source file path)
      Throws:
      ParseException - if the model could not be parsed.
    • processMetadataForHtml

      private boolean processMetadataForHtml(StringBuilder html, StringBuilder source)
    • normalizeMultilineValue

      static String normalizeMultilineValue(String value)
    • writeHtmlMetadata

      private boolean writeHtmlMetadata(StringBuilder html, Map<String,List<String>> data)
    • writeHtmlMetadata

      private boolean writeHtmlMetadata(StringBuilder html, String key, List<String> values)
    • toHtml

      String toHtml(Reader source) throws IOException
      uses flexmark-java library to parse content and generate HTML output.
      Parameters:
      source - the Markdown source
      Returns:
      HTML content generated by flexmark-java
      Throws:
      IOException - passed through