#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2014, by John MacFarlane.
# Copyright, 2015-2019, by Garen Torikian.
# Copyright, 2015, by Nick Wellnhofer.
# Copyright, 2016-2017, by Yuki Izumi.
# Copyright, 2020-2026, by Samuel Williams.

require "markly/markly"

require_relative "markly/flags"
require_relative "markly/node"
require_relative "markly/renderer/html"

require_relative "markly/version"

# Parses and renders CommonMark-compatible Markdown documents.
module Markly
	# Parse a Markdown string into a document node.
	#
	# @parameter text [String] The Markdown text to parse.
	# @parameter flags [Integer] The enabled parsing flags.
	# @parameter extensions [Array(Symbol) | Nil] The extensions to enable.
	# @returns [Markly::Node] The parsed document node.
	def self.parse(text, flags: DEFAULT, extensions: nil)
		parser = Parser.new(flags)
		
		extensions&.each do |extension|
			parser.enable(extension)
		end
		
		return parser.parse(text.encode(Encoding::UTF_8))
	end
	
	# Parse a Markdown string and render it as HTML.
	#
	# @parameter text [String] The Markdown text to render.
	# @parameter flags [Integer] The default parsing and rendering flags.
	# @parameter parse_flags [Integer] The enabled parsing flags.
	# @parameter render_flags [Integer] The enabled rendering flags.
	# @parameter extensions [Array(Symbol)] The extensions to enable.
	# @returns [String] The rendered HTML.
	def self.render_html(text, flags: DEFAULT, parse_flags: flags, render_flags: flags, extensions: [])
		root = self.parse(text, flags: parse_flags, extensions: extensions)
		
		return root.to_html(flags: render_flags, extensions: extensions)
	end
end
