1
0
Fork 0

.config/vis: update personal markdown lexer fork

main
urosm 2024-08-03 15:50:53 +02:00
parent 6bdbaea817
commit 517bc58b8f
1 changed files with 37 additions and 49 deletions

View File

@ -1,5 +1,5 @@
-- Adapted from Markdown LPeg lexer by Mitchell.
-- See <https://git.sr.ht/~martanne/vis>.
-- Adapted from Markdown LPeg lexer by Mitchell to simplify and lex some pandoc
-- specifics.
local lexer = lexer
local P, S, B = lpeg.P, lpeg.S, lpeg.B
@ -14,81 +14,69 @@ local bl = nl * nl
local nws = lexer.any - lexer.space
-- Heading.
lex:add_rule('heading_delimiter',
lex:tag('delimiter' .. md,
lexer.starts_line(P('#')^-6 * P(' '))))
lex:add_rule('heading',
lex:tag(lexer.HEADING .. md,
(
B(bl * P('# ')) +
B(bl * P('## ')) +
B(bl * P('### ')) +
B(bl * P('#### ')) +
B(bl * P('##### ')) +
B(bl * P('###### '))
) *
lexer.to_eol(nws) *
lex:tag(lexer.COMMENT, lexer.starts_line(P('#')^-6)) *
P(' ') *
lex:tag(lexer.HEADING, lexer.to_eol(nws) *
#bl))
-- Blockquote.
lex:add_rule('blockquote_delimiter',
lex:tag('delimiter' .. md,
lex:add_rule('blockquote',
lex:tag(lexer.COMMENT,
lexer.starts_line(P('> ')^1)))
-- Horizontal rule.
lex:add_rule('hr',
lex:tag('hr' .. md, B(bl) * S('*-_')^3 * #bl))
lex:tag(lexer.COMMENT, B(bl) * S('*-_')^3 * #bl))
-- Native divs.
lex:add_rule("native_div_delimiter",
lex:tag('delimiter' .. md,
lexer.starts_line(lexer.to_eol(':::'))))
-- Native div.
lex:add_rule("native_div",
lex:tag(lexer.COMMENT, lexer.starts_line(lexer.to_eol(P(':')^3))))
-- Code block.
local code_line = lexer.starts_line(
(B(' ') + B('\t')) * lexer.to_eol(), true)
local code_block = lexer.range(lexer.starts_line('```', true), lexer.starts_line(P('```'))) + lexer.range(lexer.starts_line('~~~', true), lexer.starts_line(P('~~~')))
local code_inline = lpeg.Cmt(
lpeg.C(P('`')^1), function(input, index, bt)
local code_block = lexer.range(lexer.starts_line('```', true), lexer.starts_line(P('```'))) +
lexer.range(lexer.starts_line('~~~', true), lexer.starts_line(P('~~~')))
local code_inline = lpeg.Cmt(lpeg.C(P('`')^1), function(input, index, bt)
local _, e = input:find('[^`]' .. bt .. '%f[^`]', index)
return (e or #input) + 1
end)
lex:add_rule('block_code', lex:tag(lexer.CODE .. md, code_line + code_block))
lex:add_rule('block_code', lex:tag(lexer.CODE, code_line + code_block))
-- Escape.
lex:add_rule('escape', lex:tag(lexer.DEFAULT, P('\\') * 1))
-- Bracket.
lex:add_rule('brackets',
lex:tag('delimiter' .. md, S('[]')))
lex:tag(lexer.COMMENT, S('[]')))
-- Native span.
lex:add_rule('native_span',
B(']') * lex:tag(lexer.COMMENT, lexer.range('{', '}')))
-- Footnote.
lex:add_rule('footnote_key',
lex:tag(lexer.REFERENCE .. md,
(P('^') * #P('[')) +
(B('[') * P('^') * (lexer.any - lexer.space - S('^[]'))^1 * #P(']'))))
lex:tag(lexer.REFERENCE, P('^')) * #P('[') +
B('[') * lex:tag(lexer.REFERENCE, P('^') * (lexer.any - lexer.space - S('^[]'))^1) * #P(']'))
-- @todo footnote reference
-- Cite.
lex:add_rule('cite_key',
lex:tag(lexer.REFERENCE .. md,
B(lexer.space + P('[')) * P('-')^-1 * P('@') * (lexer.alnum + P('_')) * (lexer.alnum + S(':.#$%&-+?<>~/'))^0 +
B(lexer.space + P('[')) * P('-')^-1 * P('@') * lexer.range('{', '}')))
B(lexer.space + P('[')) * lex:tag(lexer.REFERENCE,
P('-')^-1 *
(P('@') * (lexer.alnum + P('_')) * (lexer.alnum + S(':.#$%&-+?<>~/'))^0 +
P('@') * lexer.range('{', '}'))))
-- Link.
lex:add_rule('link_text',
lex:tag(lexer.LINK .. md,
B('[') * (lexer.any - P(']'))^1 * #P(P(']') * lexer.range('(', ')'))))
B('[') * lex:tag(lexer.LINK, (lexer.any - P(']'))^1) * #P(']' * lexer.range('(', ')')))
lex:add_rule('link_target',
lex:tag('delimiter' .. md, B(']') * lexer.range('(', ')')))
lex:add_rule('link_ref',
lex:tag('delimiter' .. md, B(']') * P(':') * #lexer.space))
B(']') * lex:tag(lexer.COMMENT, lexer.range('(', ')')))
-- @todo link reference
-- Image
lex:add_rule('image_bang', lex:tag(lexer.REFERENCE .. md, P('!') * #P('[')))
-- Attribute.
lex:add_rule('attribute',
lex:tag('delimiter' .. md, B(S('])')) * lexer.range('{', '}')))
lex:add_rule('image_bang', lex:tag(lexer.REFERENCE, P('!')) * #P('['))
local punct_space = lexer.punct + lexer.space
@ -121,8 +109,8 @@ lex:embed(html, html_start_rule, html_end_rule)
-- Embedded YAML.
local yaml = lexer.load('yaml')
local yaml_start_rule = #lexer.starts_line(P('---'), false) * yaml:get_rule('doc_bounds')
local yaml_end_rule = #lexer.starts_line(P('...'), false) * yaml:get_rule('doc_bounds')
local yaml_start_rule = yaml:get_rule('doc_bounds') - B(bl) * P(P('---') + '...') * #bl
local yaml_end_rule = yaml:get_rule('doc_bounds') - B(bl) * P(P('---') + '...') * #bl
lex:embed(yaml, yaml_start_rule, yaml_end_rule)
return lex