Markdown parser implemented in Prolog. Compatible with SWI-Prolog as the output tree is for direct use by html/1. The specification for the parser was taken from (Gruber's Markdown).

Example usage

Parse into a structure usable by html/1.

:- use_module(library(md/md_parse)).

?- md_parse_string("# Hello #", Blocks).
Blocks = [h1("Hello")].

Convert into an HTML string:

:- use_module(library(md/md_parse)).

?- md_html_string("# Hello #", Html).
Html = "<h1>Hello</h1>".

Deviations from the Gruber's Markdown


Example document (about 800 lines) is parsed in 80ms on 2.4GHz Q6600.


Requires SWI-Prolog 7.x.


API documentation

See for the top-level module documentation.


Bug reports/feature requests

Please send bug reports/feature request through the GitHub project page.


The MIT License. See the LICENSE file.

Prolog files

pack.plShow source
prolog/md/  -- Block-level parser for MarkdownShow source
md_blocks/3Parses given Markdown into a structure accepted by html//1.Source
prolog/md/  -- Markdown slash-escaped sequencesShow source
md_escaped_code/1List of possibly escaped symbols.Source
md_escaped_code/3Recognizes single code that could have been escaped.Source
md_escaped_string/3Recognizes string with escapes inside it.Source
prolog/md/  -- Markdown header parser.Show source
md_header/3Recognizes either setext or atx-styled headings.Source
prolog/md/  -- Parser for Markdown horizontal rulersShow source
md_hr/2Recognizes an horizontal ruler.Source
md_lookahead_hr/2Looks ahead an horizontal ruler.Source
prolog/md/  -- Line-based parsing primitives.Show source
discard_to_line_end/2Discards zero or more symbol codes untill the first line end or eos is reached.Source
empty_line/2Recognizes a single empty line.Source
empty_lines/2List of consequtive empty lines.Source
indent/2Recognizes normal indent which is a tab or 4 spaces.Source
ln/2Recognizes line ending.Source
ln_or_eos/2Recognizes either a line end or eos.Source
lookahead/3Looks ahead a single symbol code.Source
lookahead_ln/2Looks ahead a line end.Source
lookahead_ln_or_eos/2Looks ahead a line end or end-of-stream.Source
merge_lines/2Merges list of lines into a flat code list.Source
non_empty_line/3Single non-empty line ending with newline or end-of-stream.Source
prolog/md/  -- Markdown reference link parserShow source
md_link/3Retrieves recorded link from the last invocation of md_links/2.Source
md_links/2Same as md_links/3 but stores links in threadlocal predicate which is cleared on each invocation of this predicate.Source
md_links/3Markdown reference link definition parser.Source
prolog/md/  -- List item parserShow source
md_bullet_list_item/4Recognizes a single bulleted list item.Source
md_ordered_list_item/4Recognizes a single ordered list item.Source
prolog/md/md_parse.plShow source
md_html_codes/2Converts Markdown into HTML string.Source
md_html_file/2Same as md_html_codes/2 but reads input from file.Source
md_html_stream/2Same as md_html_codes/2 but reads input from stream.Source
md_html_string/2Same as md_html_codes/2 but takes input as string.Source
md_parse_codes/2Parses Markdown into a structure suitable in use with html//1.Source
md_parse_file/2Same as md_parse_codes/2 but reads input from file.Source
md_parse_stream/2Same as md_parse_codes/2 but reads input from stream.Source
md_parse_string/2Same as md_parse_codes/2 but takes a string instead.Source
prolog/md/  -- Span-level Markdown parserShow source
md_span_codes/2Turns the list of codes into a structure acceptable by SWI-Prolog's html//1 predicate.Source
md_span_string/2Same as md_span_codes/2 but uses a string ans input.Source
prolog/md/  -- Parser for span-level stylesShow source
md_span_decorate/4Recognizes style formatting in the middle of span text.Source
prolog/md/  -- Markdown span-level link parsingShow source
md_span_link/3Recognizes different types of links from the stream of symbol codes.Source
prolog/md/  -- Code list whitespace trimmingShow source
trim/2Trims whitespace from both sides of the list of codes.Source
trim_left/2Trims whitespaces from the beginning of the list of codes.Source
trim_right/2Trims whitespace from the end of the list of codes.Source
tests/block.plShow source
tests/file.plShow source
tests/header.plShow source
tests/html.plShow source
tests/link.plShow source
tests/list_item.plShow source
tests/span.plShow source
tests/span_link.plShow source
tests/tests.plShow source