-- lx format
local format = require("core.format")

local adornment = "([=%-~%^\"'`:%.+#*_])%1%1[=%-~%^\"'`:%.+#*_]*"

format.register({
	name = "reStructuredText Document",
	shortname = "RST",
	extensions = {
		"rst",
--		"rest",
	},
	comment = "..",
	author = "liqube",

	line_patterns = {
		{ pattern = "%s*%S.-\n", type = "heading", next_line = "%s*" .. adornment .. "%s*\n" },
		{ pattern = "%s*" .. adornment .. "%s*\n", type = "operator", consume_line = true },
	},

	patterns = {
		{ pattern = "\\.", type = "normal" },
		{ pattern = "(:[%w_.:%-]+:)(`)([^`\n]-)(`)", type = { "keyword", "operator", "literal", "operator" } },

		-- Substitutions, targets, footnotes, citations, directives, comments
		{ pattern = "%s*(%.%.%s+)(|[^|\n]+|)(%s+)([%w_.:%-]+)(::)", type = { "operator", "label", "normal", "keyword", "operator" }, line_start = true },
		{ pattern = "%s*(%.%.%s+_+)([^:\n]-)(:)", type = { "operator", "label", "operator" }, line_start = true },
		{ pattern = "%s*(%.%.%s+)(%[[^\n%]]+%])", type = { "operator", "label" }, line_start = true },
		{ pattern = "%s*(%.%.%s+)([%w_][%w_.:%-]*)(::)", type = { "operator", "keyword", "operator" }, line_start = true },
		{ pattern = "%s*%.%.%f[%s].-\n", type = "comment", line_start = true },

		-- Lists and field/directive-option markers
		{ pattern = "%s*([%*+%-])%s+", type = { "operator" }, line_start = true },
		{ pattern = "%s*(%d+[%.%)])%s+", type = { "number" }, line_start = true },
		{ pattern = "%s*(%(%d+%))%s+", type = { "number" }, line_start = true },
		{ pattern = "%s*(#[%.%)])%s+", type = { "number" }, line_start = true },
		{ pattern = "%s*(:)([^:\n]+)(:)", type = { "operator", "keyword", "operator" }, line_start = true },

		-- Inline markup
		{ pattern = "(``)([^`\n]-)(``)", type = { "operator", "literal", "operator" } },
		{ pattern = "(`)([^`\n]-)(%s+<)([^>\n]-)(>)(`_+)", type = { "operator", "bold", "operator", "function", "operator", "operator" } },
		{ pattern = "(`)([^`\n]-)(`_+)", type = { "operator", "label", "operator" } },
		{ pattern = "(`)([^`\n]-)(`)", type = { "operator", "string", "operator" } },
		{ pattern = "(%*%*)([^%*\n]-)(%*%*)", type = { "operator", "bold", "operator" } },
		{ pattern = "(%*)([^%*\n]-)(%*)", type = { "operator", "italic", "operator" } },
		{ pattern = "(%[)([^\n%]]+)(%]_+)", type = { "operator", "label", "operator" } },
		{ pattern = "(|)([%w_.:%-]+)(|)", type = { "operator", "label", "operator" } },
		{ pattern = "::%s*\n", type = "operator" },
		{ pattern = "https?://%S+", type = "function" },
	},

})
