2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2020-03-07 16:53:54 +01:00
|
|
|
local syntax = require "core.syntax"
|
2022-03-04 19:29:29 +01:00
|
|
|
local style = require "core.style"
|
|
|
|
local core = require "core"
|
2020-03-07 16:53:54 +01:00
|
|
|
|
2022-03-04 19:29:29 +01:00
|
|
|
local initial_color = style.syntax["keyword2"]
|
2021-09-16 23:29:44 +02:00
|
|
|
|
2022-03-04 19:29:29 +01:00
|
|
|
-- Add 3 type of font styles for use on markdown files
|
|
|
|
for _, attr in pairs({"bold", "italic", "bold_italic"}) do
|
|
|
|
local attributes = {}
|
|
|
|
if attr ~= "bold_italic" then
|
|
|
|
attributes[attr] = true
|
|
|
|
else
|
|
|
|
attributes["bold"] = true
|
|
|
|
attributes["italic"] = true
|
|
|
|
end
|
|
|
|
-- no way to copy user custom font with additional attributes :(
|
|
|
|
style.syntax_fonts["markdown_"..attr] = renderer.font.load(
|
|
|
|
DATADIR .. "/fonts/JetBrainsMono-Regular.ttf",
|
|
|
|
style.code_font:get_size(),
|
|
|
|
attributes
|
|
|
|
)
|
|
|
|
-- also add a color for it
|
|
|
|
style.syntax["markdown_"..attr] = style.syntax["keyword2"]
|
|
|
|
end
|
|
|
|
|
|
|
|
local in_squares_match = "^%[%]"
|
|
|
|
local in_parenthesis_match = "^%(%)"
|
2021-09-16 23:29:44 +02:00
|
|
|
|
2020-03-07 16:53:54 +01:00
|
|
|
syntax.add {
|
2021-11-21 03:46:43 +01:00
|
|
|
name = "Markdown",
|
2020-03-07 16:53:54 +01:00
|
|
|
files = { "%.md$", "%.markdown$" },
|
2022-03-04 19:29:29 +01:00
|
|
|
block_comment = { "<!--", "-->" },
|
2022-03-30 03:48:34 +02:00
|
|
|
space_handling = false, -- turn off this feature to handle it our selfs
|
2020-03-07 16:53:54 +01:00
|
|
|
patterns = {
|
2022-03-30 03:48:34 +02:00
|
|
|
---- Place patterns that require spaces at start to optimize matching speed
|
|
|
|
---- and apply the %s+ optimization immediately afterwards
|
|
|
|
-- bullets
|
|
|
|
{ pattern = "^%s*%*%s", type = "number" },
|
|
|
|
{ pattern = "^%s*%-%s", type = "number" },
|
|
|
|
{ pattern = "^%s*%+%s", type = "number" },
|
|
|
|
-- numbered bullet
|
|
|
|
{ pattern = "^%s*[0-9]+[%.%)]%s", type = "number" },
|
|
|
|
-- blockquote
|
|
|
|
{ pattern = "^%s*>+%s", type = "string" },
|
|
|
|
-- alternative bold italic formats
|
|
|
|
{ pattern = { "%s___", "___%f[%s]" }, type = "markdown_bold_italic" },
|
|
|
|
{ pattern = { "%s__", "__%f[%s]" }, type = "markdown_bold" },
|
|
|
|
{ pattern = { "%s_[%S]", "_%f[%s]" }, type = "markdown_italic" },
|
|
|
|
-- reference links
|
|
|
|
{
|
|
|
|
pattern = "^%s*%[%^()["..in_squares_match.."]+()%]: ",
|
|
|
|
type = { "function", "number", "function" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = "^%s*%[%^?()["..in_squares_match.."]+()%]:%s+.+\n",
|
|
|
|
type = { "function", "number", "function" }
|
|
|
|
},
|
|
|
|
-- optimization
|
|
|
|
{ pattern = "%s+", type = "normal" },
|
|
|
|
|
2022-03-04 19:29:29 +01:00
|
|
|
---- HTML rules imported and adapted from language_html
|
|
|
|
---- to not conflict with markdown rules
|
|
|
|
-- Inline JS and CSS
|
|
|
|
{
|
|
|
|
pattern = {
|
|
|
|
"<%s*[sS][cC][rR][iI][pP][tT]%s+[tT][yY][pP][eE]%s*=%s*" ..
|
|
|
|
"['\"]%a+/[jJ][aA][vV][aA][sS][cC][rR][iI][pP][tT]['\"]%s*>",
|
|
|
|
"<%s*/[sS][cC][rR][iI][pP][tT]>"
|
|
|
|
},
|
|
|
|
syntax = ".js",
|
|
|
|
type = "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = {
|
|
|
|
"<%s*[sS][cC][rR][iI][pP][tT]%s*>",
|
|
|
|
"<%s*/%s*[sS][cC][rR][iI][pP][tT]>"
|
|
|
|
},
|
|
|
|
syntax = ".js",
|
|
|
|
type = "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = {
|
|
|
|
"<%s*[sS][tT][yY][lL][eE][^>]*>",
|
|
|
|
"<%s*/%s*[sS][tT][yY][lL][eE]%s*>"
|
|
|
|
},
|
|
|
|
syntax = ".css",
|
|
|
|
type = "function"
|
|
|
|
},
|
|
|
|
-- Comments
|
|
|
|
{ pattern = { "<!%-%-", "%-%->" }, type = "comment" },
|
|
|
|
-- Tags
|
|
|
|
{ pattern = "%f[^<]![%a_][%w_]*", type = "keyword2" },
|
|
|
|
{ pattern = "%f[^<][%a_][%w_]*", type = "function" },
|
|
|
|
{ pattern = "%f[^<]/[%a_][%w_]*", type = "function" },
|
|
|
|
-- Attributes
|
|
|
|
{
|
|
|
|
pattern = "[a-z%-]+%s*()=%s*()\".-\"",
|
|
|
|
type = { "keyword", "operator", "string" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = "[a-z%-]+%s*()=%s*()'.-'",
|
|
|
|
type = { "keyword", "operator", "string" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = "[a-z%-]+%s*()=%s*()%-?%d[%d%.]*",
|
|
|
|
type = { "keyword", "operator", "number" }
|
|
|
|
},
|
|
|
|
-- Entities
|
|
|
|
{ pattern = "&#?[a-zA-Z0-9]+;", type = "keyword2" },
|
|
|
|
|
|
|
|
---- Markdown rules
|
|
|
|
-- code blocks
|
2021-09-16 23:29:44 +02:00
|
|
|
{ pattern = { "```c++", "```" }, type = "string", syntax = ".cpp" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```cpp", "```" }, type = "string", syntax = ".cpp" },
|
2021-09-16 23:29:44 +02:00
|
|
|
{ pattern = { "```python", "```" }, type = "string", syntax = ".py" },
|
|
|
|
{ pattern = { "```ruby", "```" }, type = "string", syntax = ".rb" },
|
|
|
|
{ pattern = { "```perl", "```" }, type = "string", syntax = ".pl" },
|
|
|
|
{ pattern = { "```php", "```" }, type = "string", syntax = ".php" },
|
2021-09-16 23:43:17 +02:00
|
|
|
{ pattern = { "```javascript", "```" }, type = "string", syntax = ".js" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```json", "```" }, type = "string", syntax = ".js" },
|
2021-09-16 23:29:44 +02:00
|
|
|
{ pattern = { "```html", "```" }, type = "string", syntax = ".html" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```ini", "```" }, type = "string", syntax = ".ini" },
|
2021-09-16 23:29:44 +02:00
|
|
|
{ pattern = { "```xml", "```" }, type = "string", syntax = ".xml" },
|
|
|
|
{ pattern = { "```css", "```" }, type = "string", syntax = ".css" },
|
|
|
|
{ pattern = { "```lua", "```" }, type = "string", syntax = ".lua" },
|
|
|
|
{ pattern = { "```bash", "```" }, type = "string", syntax = ".sh" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```sh", "```" }, type = "string", syntax = ".sh" },
|
2021-09-16 23:29:44 +02:00
|
|
|
{ pattern = { "```java", "```" }, type = "string", syntax = ".java" },
|
|
|
|
{ pattern = { "```c#", "```" }, type = "string", syntax = ".cs" },
|
|
|
|
{ pattern = { "```cmake", "```" }, type = "string", syntax = ".cmake" },
|
|
|
|
{ pattern = { "```d", "```" }, type = "string", syntax = ".d" },
|
|
|
|
{ pattern = { "```glsl", "```" }, type = "string", syntax = ".glsl" },
|
2021-12-03 15:50:23 +01:00
|
|
|
{ pattern = { "```c", "```" }, type = "string", syntax = ".c" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```julia", "```" }, type = "string", syntax = ".jl" },
|
|
|
|
{ pattern = { "```rust", "```" }, type = "string", syntax = ".rs" },
|
|
|
|
{ pattern = { "```dart", "```" }, type = "string", syntax = ".dart" },
|
2021-12-03 15:50:23 +01:00
|
|
|
{ pattern = { "```v", "```" }, type = "string", syntax = ".v" },
|
2022-03-04 19:29:29 +01:00
|
|
|
{ pattern = { "```toml", "```" }, type = "string", syntax = ".toml" },
|
|
|
|
{ pattern = { "```yaml", "```" }, type = "string", syntax = ".yaml" },
|
|
|
|
{ pattern = { "```nim", "```" }, type = "string", syntax = ".nim" },
|
|
|
|
{ pattern = { "```typescript", "```" }, type = "string", syntax = ".ts" },
|
|
|
|
{ pattern = { "```rescript", "```" }, type = "string", syntax = ".res" },
|
|
|
|
{ pattern = { "```moon", "```" }, type = "string", syntax = ".moon" },
|
|
|
|
{ pattern = { "```go", "```" }, type = "string", syntax = ".go" },
|
|
|
|
{ pattern = { "```lobster", "```" }, type = "string", syntax = ".lobster" },
|
|
|
|
{ pattern = { "```liquid", "```" }, type = "string", syntax = ".liquid" },
|
|
|
|
{ pattern = { "```", "```" }, type = "string" },
|
|
|
|
{ pattern = { "``", "``" }, type = "string" },
|
2022-03-13 09:55:41 +01:00
|
|
|
{ pattern = { "%f[\\`]%`[%S]", "`" }, type = "string" },
|
2022-03-04 19:29:29 +01:00
|
|
|
-- strike
|
|
|
|
{ pattern = { "~~", "~~" }, type = "keyword2" },
|
|
|
|
-- highlight
|
|
|
|
{ pattern = { "==", "==" }, type = "literal" },
|
|
|
|
-- lines
|
2022-03-13 09:55:41 +01:00
|
|
|
{ pattern = "^%-%-%-+\n", type = "comment" },
|
|
|
|
{ pattern = "^%*%*%*+\n", type = "comment" },
|
|
|
|
{ pattern = "^___+\n", type = "comment" },
|
2022-03-04 19:29:29 +01:00
|
|
|
-- bold and italic
|
2022-03-13 09:55:41 +01:00
|
|
|
{ pattern = { "%*%*%*%S", "%*%*%*" }, type = "markdown_bold_italic" },
|
|
|
|
{ pattern = { "%*%*%S", "%*%*" }, type = "markdown_bold" },
|
2022-03-04 19:29:29 +01:00
|
|
|
-- handle edge case where asterisk can be at end of line and not close
|
|
|
|
{
|
2022-03-13 09:55:41 +01:00
|
|
|
pattern = { "%f[\\%*]%*[%S]", "%*%f[^%*]" },
|
2022-03-04 19:29:29 +01:00
|
|
|
type = "markdown_italic"
|
|
|
|
},
|
2022-03-13 09:55:41 +01:00
|
|
|
-- alternative bold italic formats
|
|
|
|
{ pattern = "^___[%s%p%w]+___%s" , type = "markdown_bold_italic" },
|
|
|
|
{ pattern = "^__[%s%p%w]+__%s" , type = "markdown_bold" },
|
|
|
|
{ pattern = "^_[%s%p%w]+_%s" , type = "markdown_italic" },
|
|
|
|
-- heading with custom id
|
|
|
|
{
|
|
|
|
pattern = "^#+%s[%w%s%p]+(){()#[%w%-]+()}",
|
|
|
|
type = { "keyword", "function", "string", "function" }
|
|
|
|
},
|
2022-03-04 19:29:29 +01:00
|
|
|
-- headings
|
2022-03-13 09:55:41 +01:00
|
|
|
{ pattern = "^#+%s.+\n", type = "keyword" },
|
2022-03-04 19:29:29 +01:00
|
|
|
-- superscript and subscript
|
|
|
|
{
|
|
|
|
pattern = "%^()%d+()%^",
|
|
|
|
type = { "function", "number", "function" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = "%~()%d+()%~",
|
|
|
|
type = { "function", "number", "function" }
|
|
|
|
},
|
|
|
|
-- definitions
|
2022-03-13 09:55:41 +01:00
|
|
|
{ pattern = "^:%s.+", type = "function" },
|
2022-03-04 19:29:29 +01:00
|
|
|
-- emoji
|
|
|
|
{ pattern = ":[a-zA-Z0-9_%-]+:", type = "literal" },
|
2022-03-13 09:55:41 +01:00
|
|
|
-- images and link
|
2022-03-04 19:29:29 +01:00
|
|
|
{
|
2022-03-13 09:55:41 +01:00
|
|
|
pattern = "!?%[!?%[()["..in_squares_match.."]+()%]%(()["..in_parenthesis_match.."]+()%)%]%(()["..in_parenthesis_match.."]+()%)",
|
|
|
|
type = { "function", "string", "function", "number", "function", "number", "function" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
pattern = "!?%[!?%[?()["..in_squares_match.."]+()%]?%]%(()["..in_parenthesis_match.."]+()%)",
|
|
|
|
type = { "function", "string", "function", "number", "function" }
|
2022-03-04 19:29:29 +01:00
|
|
|
},
|
|
|
|
-- reference links
|
|
|
|
{
|
|
|
|
pattern = "%[()["..in_squares_match.."]+()%] *()%[()["..in_squares_match.."]+()%]",
|
|
|
|
type = { "function", "string", "function", "function", "number", "function" }
|
|
|
|
},
|
|
|
|
{
|
2022-03-13 09:55:41 +01:00
|
|
|
pattern = "!?%[%^?()["..in_squares_match.."]+()%]",
|
2022-03-04 19:29:29 +01:00
|
|
|
type = { "function", "number", "function" }
|
|
|
|
},
|
|
|
|
-- url's and email
|
|
|
|
{
|
|
|
|
pattern = "<[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+%.[a-zA-Z0-9-.]+>",
|
|
|
|
type = "function"
|
|
|
|
},
|
|
|
|
{ pattern = "<https?://%S+>", type = "function" },
|
2022-03-30 03:48:34 +02:00
|
|
|
{ pattern = "https?://%S+", type = "function" },
|
|
|
|
-- optimize consecutive dashes used in tables
|
|
|
|
{ pattern = "%-+", type = "normal" },
|
2020-03-07 16:53:54 +01:00
|
|
|
},
|
|
|
|
symbols = { },
|
|
|
|
}
|
2022-03-04 19:29:29 +01:00
|
|
|
|
2022-03-06 00:13:24 +01:00
|
|
|
-- Adjust the color on theme changes
|
2022-03-04 19:29:29 +01:00
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
|
|
|
if initial_color ~= style.syntax["keyword2"] then
|
|
|
|
for _, attr in pairs({"bold", "italic", "bold_italic"}) do
|
|
|
|
style.syntax["markdown_"..attr] = style.syntax["keyword2"]
|
|
|
|
end
|
|
|
|
initial_color = style.syntax["keyword2"]
|
|
|
|
end
|
|
|
|
coroutine.yield(1)
|
|
|
|
end
|
|
|
|
end)
|