Refactor: Syntax highlighting typeface: match -> region: (Issues #847, #640)

- Less code, Easyer to maintain, to add a markup language
- Faster to load, and to highlight
- Support multiline tags: #847
- Support nested tags
- Support intraword tags: this<b>bold</b>type #640
This commit is contained in:
Tinmarino
2020-08-02 03:51:25 -04:00
parent 5408d74b35
commit 1b16720b7b
9 changed files with 248 additions and 210 deletions

View File

@ -9,6 +9,36 @@ if exists('g:loaded_vimwiki_html_auto') || &compatible
endif
let g:loaded_vimwiki_html_auto = 1
" FIXME: Magics: Why not use the current syntax highlight
" This is due to historical copy paste and lazyness of markdown user
" text: **strong** or __strong__
let s:rxBold = '\%(^\|\s\|[[:punct:]]\)\@<='.
\'\(\*\|_\)\{2\}'.
\'\%([^*_`[:space:]][^*_`]*[^*_`[:space:]]\|[^*_`[:space:]]\)'.
\'\1\{2\}'.
\'\%([[:punct:]]\|\s\|$\)\@='
" text: _emphasis_ or *emphasis*
let s:rxItalic = '\%(^\|\s\|[[:punct:]]\)\@<='.
\'\(\*\|_\)'.
\'\%([^*_`[:space:]][^*_`]*[^*_`[:space:]]\|[^*_`[:space:]]\)'.
\'\1'.
\'\%([[:punct:]]\|\s\|$\)\@='
" Fixme: and those ? are they converted ?
" text: *_bold italic_* or _*italic bold*_
let rxBoldItalic = '\%(^\|\s\|[[:punct:]]\)\@<='.
\'\(\*\)\{3\}'.
\'\%([^*`[:space:]][^*`]*[^*`[:space:]]\|[^*`[:space:]]\)'.
\'\1\{3\}'.
\'\%([[:punct:]]\|\s\|$\)\@='
let rxItalicBold = '\%(^\|\s\|[[:punct:]]\)\@<='.
\'\(_\)\{3\}'.
\'\%([^_`[:space:]][^_`]*[^_`[:space:]]\|[^_`[:space:]]\)'.
\'\1\{3\}'.
\'\%([[:punct:]]\|\s\|$\)\@='
function! s:root_path(subdir) abort
return repeat('../', len(split(a:subdir, '[/\\]')))
@ -609,8 +639,9 @@ endfunction
function! s:process_tags_typefaces(line, header_ids) abort
let line = a:line
let line = s:make_tag(line, vimwiki#vars#get_syntaxlocal('rxItalic'), 's:tag_em')
let line = s:make_tag(line, vimwiki#vars#get_syntaxlocal('rxBold'), 's:tag_strong', a:header_ids)
" Convert line tag by tag
let line = s:make_tag(line, s:rxItalic, 's:tag_em')
let line = s:make_tag(line, s:rxBold, 's:tag_strong', a:header_ids)
let line = s:make_tag(line, vimwiki#vars#get_global('rxTodo'), 's:tag_todo')
let line = s:make_tag(line, vimwiki#vars#get_syntaxlocal('rxDelText'), 's:tag_strike')
let line = s:make_tag(line, vimwiki#vars#get_syntaxlocal('rxSuperScript'), 's:tag_super')
@ -978,7 +1009,7 @@ function! s:process_tag_list(line, lists) abort
" text.
" XXX necessary? in *bold* text, no space must follow the first *
if !in_list
let pos = match(a:line, '^\s*'.vimwiki#vars#get_syntaxlocal('rxBold'))
let pos = match(a:line, '^\s*' . s:rxBold)
if pos != -1
return [0, []]
endif

View File

@ -215,3 +215,79 @@ function! vimwiki#u#ft_is_vw() abort
return 0
endif
endfunction
" Helper: Create highlight region between two tags
" :param: tag <string> example '<b>'
" :param: syntax_group <string> example: VimwikiBold
" :param: contains <string> coma separated and prefixed, default VimwikiHTMLTag
" :param: (1) <boolean> is contained
function! vimwiki#u#hi_tag(tag_pre, tag_post, syntax_group, contains, ...) abort
let opt_is_contained = a:0 > 0 ? 'contained ' : ''
let opt_contains = ''
if a:contains !=# ''
let opt_contains = 'contains=' . a:contains . ' '
endif
let cmd = 'syn region ' . a:syntax_group . ' matchgroup=VimwikiHTMLDelimiter ' .
\ opt_is_contained .
\ 'start="' . a:tag_pre . '" ' .
\ 'end="' . a:tag_post . '" ' .
\ 'keepend ' .
\ opt_contains .
\ b:vimwiki_syntax_concealends
"echom cmd
exe cmd
endfunction
" Highight typeface: see $VIMRUNTIME/syntax/html.vim
" -- Basically allow nesting with multiple definition contained
" :param: dic <dic:list:list> must contain: bold, italic and underline, even if underline is often void,
" -- see here for underline not defined: https://stackoverflow.com/questions/3003476
function! vimwiki#u#hi_typeface(dic) abort
" Italic must go before, otherwise single * takes precedence over ** and ** is considered as
" a void italic.
" Note that the last syntax defined take precedence so that user can change at runtime
" (:h :syn-define)
for i in a:dic['italic']
" -- Italic 1
call vimwiki#u#hi_tag(i[0], i[1], 'VimwikiItalic ', 'VimwikiItalicBold,VimwikiItalicUnderline')
" -- Bold 2
call vimwiki#u#hi_tag(i[0], i[1], 'VimwikiBoldItalic', 'VimwikiBoldItalicUnderline', 1)
" -- Bold 3
call vimwiki#u#hi_tag(i[0], i[1], 'VimwikiBoldUnderlineItalic', '', 2)
" -- Underline 2
call vimwiki#u#hi_tag(i[0], i[1], 'VimwikiUnderlineItalic', 'VimwikiUnderlineItalicBold', 1)
" -- Underline 3
call vimwiki#u#hi_tag(i[0], i[1], 'VimwikiUnderlineBoldItalic', '', 2)
endfor
for b in a:dic['bold']
" -- Bold 1
call vimwiki#u#hi_tag(b[0],b[1], 'VimwikiBold', 'VimwikiBoldUnderline,VimwikiBoldItalic')
" -- Italic 2
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiItalicBold', 'VimwikiItalicBoldUnderline', 1)
" -- Italic 3
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiItalicUnderlineBold', '', 2)
" -- Underline 2
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiUnderlineBold', 'VimwikiUnderlineBoldItalic', 1)
" -- Underline 3
call vimwiki#u#hi_tag(b[0], b[1], 'VimwikiUnderlineItalicBold', '', 2)
endfor
" markdown
if has_key(a:dic, 'bold_italic')
for bi in a:dic['bold_italic']
call vimwiki#u#hi_tag(bi[0], bi[1], 'VimwikiBoldItalic', 'VimwikiBoldItalicUnderline')
endfor
endif
for u in a:dic['underline']
" -- Underline 1
call vimwiki#u#hi_tag(u[0], u[1], 'VimwikiUnderline', 'VimwikiUnderlineBold,VimwikiUnderlineItalic')
" -- Bold 2
call vimwiki#u#hi_tag(u[0], u[1], 'VimwikiBoldUnderline', 'VimwikiBoldUnderlineItalic', 1)
" -- Bold 3
call vimwiki#u#hi_tag(u[0], u[1], 'VimwikiBoldItalicUnderline', '', 2)
" -- Italic 2
call vimwiki#u#hi_tag(u[0], u[1], 'VimwikiItalicUnderline', 'VimwikiItalicUnderlineBold', 1)
" -- Italic 3
call vimwiki#u#hi_tag(u[0], u[1], 'VimwikiItalicBoldUnderline', '', 2)
endfor
endfunction