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