Markdown typeface: match closer to GFM specification, also add tests

This commit is contained in:
Tinmarino
2023-03-17 16:08:27 -03:00
parent f5399ffdfa
commit 8640988c5c
3 changed files with 102 additions and 30 deletions
+7 -7
View File
@@ -408,6 +408,13 @@ function! vimwiki#u#hi_typeface(dic) abort
let nested = vimwiki#u#get_syntax_dic().nested let nested = vimwiki#u#get_syntax_dic().nested
" Bold Italic
if has_key(a:dic, 'bold_italic')
for bi in a:dic['bold_italic']
call vimwiki#u#hi_tag(bi[0], bi[1], 'VimwikiBoldItalic', nested . ',VimwikiBoldItalicUnderline')
endfor
endif
" Italic " Italic
for i in a:dic['italic'] for i in a:dic['italic']
" -- Italic 1 " -- Italic 1
@@ -436,13 +443,6 @@ function! vimwiki#u#hi_typeface(dic) abort
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiUnderlineItalicBold', nested, 2) call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiUnderlineItalicBold', nested, 2)
endfor endfor
" Bold Italic
if has_key(a:dic, 'bold_italic')
for bi in a:dic['bold_italic']
call vimwiki#u#hi_tag(bi[0], bi[1], 'VimwikiBoldItalic', nested . ',VimwikiBoldItalicUnderline')
endfor
endif
" Underline " Underline
for u in a:dic['underline'] for u in a:dic['underline']
" -- Underline 1 " -- Underline 1
+36 -8
View File
@@ -753,19 +753,19 @@ function! s:get_markdown_syntaxlocal() abort
\ 'symH': {'type': type(0), 'default': 0}, \ 'symH': {'type': type(0), 'default': 0},
\ 'typeface': {'type': type({}), 'default': { \ 'typeface': {'type': type({}), 'default': {
\ 'bold': vimwiki#u#hi_expand_regex([ \ 'bold': vimwiki#u#hi_expand_regex([
\ ['\%(\\\@<!_\)\@<!__\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!__\%(\\\@<!_\)\@!'], \ [s:expand_delimiter('__', 1), s:expand_delimiter('__', 1)],
\ ['\%(\\\@<!\*\)\@<!\*\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\*\%(\\\@<!\*\)\@!'], \ [s:expand_delimiter('\*\*', 1), s:expand_delimiter('\*\*', 1)],
\ ]), \ ]),
\ 'italic': vimwiki#u#hi_expand_regex([ \ 'italic': vimwiki#u#hi_expand_regex([
\ ['\%(\\\@<!\*\)\@<!\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\%(\\\@<!\*\)\@!'], \ [s:expand_delimiter('_', 0), s:expand_delimiter('_', 0)],
\ ['\%(\\\@<!_\)\@<!_\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!_\%(\\\@<!_\)\@!'], \ [s:expand_delimiter('\*', 0), s:expand_delimiter('\*', 0)],
\ [s:expand_delimiter('\*_', 1), s:expand_delimiter('_\*', 1)],
\ [s:expand_delimiter('_\*', 1), s:expand_delimiter('\*_', 1)],
\ ]), \ ]),
\ 'underline': vimwiki#u#hi_expand_regex([]), \ 'underline': vimwiki#u#hi_expand_regex([]),
\ 'bold_italic': vimwiki#u#hi_expand_regex([ \ 'bold_italic': vimwiki#u#hi_expand_regex([
\ ['\*_', '_\*'], \ [s:expand_delimiter('\*\*\*', 1), s:expand_delimiter('\*\*\*', 1)],
\ ['_\*', '\*_'], \ [s:expand_delimiter('___', 1), s:expand_delimiter('___', 1)],
\ ['\%(\\\@<!\*\)\@<!\*\*\*\%(\\\@<!\*\)\@!', '\%(\\\@<!\*\)\@<!\*\*\*\%(\\\@<!\*\)\@!'],
\ ['\%(\\\@<!_\)\@<!___\%(\\\@<!_\)\@!', '\%(\\\@<!_\)\@<!___\%(\\\@<!_\)\@!']
\ ]), \ ]),
\ 'code': [ \ 'code': [
\ ['\%(^\|[^`\\]\)\@<=`\%($\|[^`]\)\@=', \ ['\%(^\|[^`\\]\)\@<=`\%($\|[^`]\)\@=',
@@ -1344,6 +1344,34 @@ function! s:normalize_syntax_settings(syntax) abort
endfunction endfunction
function! s:expand_delimiter(delim, b_can_mult) abort
" Helper: From a delimiter to the lookhead defensive version
" See: also vimwiki#u#hi_expand_regex
" TODO: get some cache to avoid recrafting the same prefix always
" Clause: if nothing, return nothing
if len(a:delim) == 0
return '\%(\)'
endif
" let c_start = a:delim[0] ==# '\' ? a:delim[0:1] : a:delim[0]
" let c_end = (len(a:delim) > 1 && a:delim[-2:-2] ==# '\') ? a:delim[-2:-1] : a:delim[-1:]
" Hardcode for markdown
let c_start = '[_*]'
let c_end = '[_*]'
let rx_mult = a:b_can_mult ? '\+' : ''
let rx_start = '\%(^\|\%(\\\@<!' . c_start . '\)\@<!\)'
let rx_middle = '\%(\%(' . a:delim . '\)' . rx_mult . '\)'
let rx_end = '\%($\|\%(\\\@<!' . c_end . '\)\@!\)'
let res = '\%(' . rx_start . rx_middle . rx_end . '\)'
echom res
return res
endfunction
" ---------------------------------------------------------- " ----------------------------------------------------------
" 4. Command (exported) {{{1 " 4. Command (exported) {{{1
" ---------------------------------------------------------- " ----------------------------------------------------------
+59 -15
View File
@@ -768,11 +768,9 @@ Execute (Set Markdown):
call SetSyntax('markdown') call SetSyntax('markdown')
Execute (Assert Syntax): Execute (Assert Syntax):
CommentLine 'TODO Change that, currently ItalicBold and shoul dbe only Italic' CommentLine 'Double italic (2)'
AssertEqual 0, 0 AssertEqual 'VimwikiItalic', SyntaxAt(1, 4)
# CommentLine 'Double italic (2)' AssertEqual '', SyntaxAt(2, 4)
# AssertEqual 'VimwikiItalic', SyntaxAt(1, 4)
# AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-473): Given vimwiki (Typeface: https://github.github.com/gfm/#example-473):
@@ -783,11 +781,9 @@ Execute (Set Markdown):
call SetSyntax('markdown') call SetSyntax('markdown')
Execute (Assert Syntax): Execute (Assert Syntax):
CommentLine 'TODO, my lookaheads in typeface vars are too strict' CommentLine 'Double bold with ****'
AssertEqual 0, 0 AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# CommentLine 'Double bold' AssertEqual '', SyntaxAt(2, 1)
# AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-474): Given vimwiki (Typeface: https://github.github.com/gfm/#example-474):
@@ -798,17 +794,53 @@ Execute (Set Markdown):
call SetSyntax('markdown') call SetSyntax('markdown')
Execute (Assert Syntax): Execute (Assert Syntax):
CommentLine 'TODO, my lookaheads in typeface vars are too strict' CommentLine 'Double bold with ____'
AssertEqual 0, 0 AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# CommentLine 'Double bold (2)' AssertEqual '', SyntaxAt(2, 1)
# AssertEqual 'VimwikiBold', SyntaxAt(1, 6)
# AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-475):
******foo******
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Triple bold with ******'
AssertEqual 'VimwikiBold', SyntaxAt(1, 8)
AssertEqual '', SyntaxAt(2, 1)
# Rule 14 {{{1 # Rule 14 {{{1
# An interpretation <em><strong>...</strong></em> is always preferred to <strong><em>...</em></strong>. # An interpretation <em><strong>...</strong></em> is always preferred to <strong><em>...</em></strong>.
Given vimwiki (Typeface: https://github.github.com/gfm/#example-476):
***foo***
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Bold Italic normal, with 3 *'
AssertEqual 'VimwikiBoldItalic', SyntaxAt(1, 5)
AssertEqual '', SyntaxAt(2, 1)
Given vimwiki (Typeface: https://github.github.com/gfm/#example-477):
_____foo_____
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'Bold Italic with 5 _'
CommentLine 'TODO Passed'
# AssertEqual 'VimwikiBoldItalic', SyntaxAt(1, 7)
# AssertEqual '', SyntaxAt(2, 1)
# Rule 15 {{{1 # Rule 15 {{{1
# When two potential emphasis or strong emphasis spans overlap, so that the second begins before the first ends and ends after the first ends, the first takes precedence. Thus, for example, *foo _bar* baz_ is parsed as <em>foo _bar</em> baz_ rather than *foo <em>bar* baz</em>. # When two potential emphasis or strong emphasis spans overlap, so that the second begins before the first ends and ends after the first ends, the first takes precedence. Thus, for example, *foo _bar* baz_ is parsed as <em>foo _bar</em> baz_ rather than *foo <em>bar* baz</em>.
@@ -835,5 +867,17 @@ Execute (Assert Syntax):
# Rule 17 {{{1 # Rule 17 {{{1
# Inline code spans, links, images, and HTML tags group more tightly than emphasis. So, when there is a choice between an interpretation that contains one of these elements and one that does not, the former always wins. Thus, for example, *[foo*](bar) is parsed as *<a href="bar">foo*</a> rather than as <em>[foo</em>](bar). # Inline code spans, links, images, and HTML tags group more tightly than emphasis. So, when there is a choice between an interpretation that contains one of these elements and one that does not, the former always wins. Thus, for example, *[foo*](bar) is parsed as *<a href="bar">foo*</a> rather than as <em>[foo</em>](bar).
Given vimwiki (Typeface: https://github.github.com/gfm/#example-485):
*a `*` b*
*a `a a*a a` b*
12345678901234
Execute (Set Markdown):
call SetSyntax('markdown')
Execute (Assert Syntax):
CommentLine 'TODO with a * as unique code, a nice trap'
AssertEqual 0, 0
# vim: foldmethod=marker foldlevel=30 sw=2 # vim: foldmethod=marker foldlevel=30 sw=2