diff options
author | mitchell <unknown> | 2020-06-25 19:16:11 -0400 |
---|---|---|
committer | mitchell <unknown> | 2020-06-25 19:16:11 -0400 |
commit | 9b2cfc0e05f666c31bfa2b6fa60956f3943b3b07 (patch) | |
tree | 371c358e82eabe48080c679e2e9910bb9b83bc5c /lexlua/markdown.lua | |
parent | 2b019d9846520047c5b0233829c6007011060e57 (diff) | |
download | scintilla-mirror-9b2cfc0e05f666c31bfa2b6fa60956f3943b3b07.tar.gz |
lexlua: Updated Markdown lexer to handle code blocks and spans better.
Thanks to Alexander Misel.
Diffstat (limited to 'lexlua/markdown.lua')
-rw-r--r-- | lexlua/markdown.lua | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/lexlua/markdown.lua b/lexlua/markdown.lua index cac5c8322..fbedb2418 100644 --- a/lexlua/markdown.lua +++ b/lexlua/markdown.lua @@ -29,10 +29,18 @@ lex:add_rule('list', token('list', lexer.starts_line(S(' \t')^0 * (S('*+-') + R('09')^1 * '.')) * S(' \t'))) lex:add_style('list', lexer.STYLE_CONSTANT) +local hspace = S('\t\v\f\r ') +local blank_line = '\n' * hspace^0 * ('\n' + P(-1)) + local code_line = lexer.to_eol(lexer.starts_line(P(' ')^4 + '\t') * -P('<')) * lexer.newline^-1 -local code_block = lexer.range(lexer.starts_line('```'), '```') -local code_inline = lexer.range('``') + lexer.range('`', false, false) +local code_block = lexer.range( + lexer.starts_line('```'), '\n```' * hspace^0 * ('\n' + P(-1))) +local code_inline = lpeg.Cmt(lpeg.C(P('`')^1), function(input, index, bt) + -- `foo`, ``foo``, ``foo`bar``, `foo``bar` are all allowed. + local _, e = input:find('[^`]' .. bt .. '%f[^`]', index) + return (e or #input) + 1 +end) lex:add_rule('block_code', token('code', code_line + code_block + code_inline)) lex:add_style('code', lexer.STYLE_EMBEDDED .. ',eolfilled') @@ -80,8 +88,8 @@ local function flanked_range(s, not_inword) s * #(fl_char - lexer.punct) local right_fl = lpeg.B(lexer.punct) * s * #(punct_space - s) + lpeg.B(fl_char) * s - return left_fl * (lexer.any - (not_inword and s * #punct_space or s))^0 * - right_fl + return left_fl * (lexer.any - blank_line - + (not_inword and s * #punct_space or s))^0 * right_fl end lex:add_rule('strong', token('strong', flanked_range('**') + @@ -96,9 +104,9 @@ lex:add_style('em', 'italics') -- Embedded HTML. local html = lexer.load('html') -local start_rule = lexer.starts_line(S(' \t')^0) * #P('<') * - html:get_rule('element') -local end_rule = token(lexer.DEFAULT, P('\n')) -- TODO: lexer.WHITESPACE errors +local start_rule = lexer.starts_line(P(' ')^-3) * #P('<') * + html:get_rule('element') -- P(' ')^4 starts code_line +local end_rule = token(lexer.DEFAULT, blank_line) -- TODO: lexer.WHITESPACE errors lex:embed(html, start_rule, end_rule) return lex |