自定义 Vim HTML 语法

发布于 2024-10-21 05:16:02 字数 1085 浏览 9 评论 0原文

我有一个脚本,它读取 HTML 文件并用 Perl 设置的值替换出现的 ~%foo%~ 。像这样的东西:

<span class="~%classname%~">~%hi_mom%~</span>

在浏览器中会产生类似这样的东西:

<span class="classyclass">Hello World</span>

对,所以我想使用 Vim 语法突出显示来区分 HTML 中出现的 ~%foo%~ 。默认情况下,HTML 语法突出显示将使 HTML 元素的属性值为 Magenta,而我希望 ~%foo%~ 部分为 DarkMagenta。我走在正确的轨道上,因为如果我注释掉 tokenQuoted 行(或标记行),我会得到所需的结果,但匹配项和突出显示都未注释,标记突出显示会覆盖 tokenQuoted代码>突出显示。

syntax match token       containedin=ALLBUT,htmlString,htmlValue '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue        '\~%[^%]\+%\~'
" tokenQuoted assumes htmlString/htmlValue (:highlight String) is Magenta
highlight token          term=none ctermfg=White       guifg=White
highlight tokenQuoted    term=none ctermfg=DarkMagenta guifg=DarkMagenta

我正在处理的文件是在默认的 html.vim 来源之后获取的 .vimrc 中的 autocmd *.html ~/.vim/syntax/html.vim

I have a script that reads an HTML file and replaces occurrences of ~%foo%~ with a value set by Perl. Something like this:

<span class="~%classname%~">~%hi_mom%~</span>

Would produce something like this in the browser:

<span class="classyclass">Hello World</span>

Right so I want to use Vim syntax highlighting to distinguish the occurrences ~%foo%~ in the HTML. By default, the HTML syntax highlighting will make an HTML element's attribute values Magenta and I want the ~%foo%~ portion to be DarkMagenta. I'm on the right track because if I comment out the tokenQuoted lines (or token lines) I get the desired results but with both matches and highlights uncommented the token highlighting overrides the tokenQuoted highlighting.

syntax match token       containedin=ALLBUT,htmlString,htmlValue '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue        '\~%[^%]\+%\~'
" tokenQuoted assumes htmlString/htmlValue (:highlight String) is Magenta
highlight token          term=none ctermfg=White       guifg=White
highlight tokenQuoted    term=none ctermfg=DarkMagenta guifg=DarkMagenta

The file I'm working in is sourced after the default html.vim is sourced via
autocmd *.html ~/.vim/syntax/html.vim in .vimrc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最佳男配角 2024-10-28 05:16:02

问题在于 token 匹配未排除在 tokenQuoted 匹配中。要获得所需的结果,即突出显示与非引号标记不同的引号标记,请在语法文件中使用以下内容。

syntax match token       containedin=ALLBUT,htmlString,htmlValue,tokenQuoted '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue        '\~%[^%]\+%\~'
highlight token          term=none ctermfg=White       guifg=White
highlight tokenQuoted    term=none ctermfg=DarkMagenta guifg=DarkMagenta

或者,如果使用语法区域而不是匹配更有意义,请将上面的语法匹配行替换为以下内容。

syntax region token       contained start=+\~%+ end=+%\~+ containedin=ALLBUT,htmlString,tokenQuoted
syntax region tokenQuoted contained start=+\~%+ end=+%\~+ containedin=htmlString   

我想我还应该提到,当我测试这个时,我刚刚创建了文件 ~/.vim/syntax/html.vim 并添加了上述内容。无需向我的 .vimrc 文件添加任何内容。

The problem is that token match is not being excluded from being contained in the tokenQuoted match. To get the desired results, i.e. highlighting quoted tokens different from non quoted tokens, use the following in your syntax file.

syntax match token       containedin=ALLBUT,htmlString,htmlValue,tokenQuoted '\~%[^%]\+%\~'
syntax match tokenQuoted containedin=htmlString,htmlValue        '\~%[^%]\+%\~'
highlight token          term=none ctermfg=White       guifg=White
highlight tokenQuoted    term=none ctermfg=DarkMagenta guifg=DarkMagenta

Or if it makes sense to use a syntax region rather than a match, replace the syntax match lines above with the following.

syntax region token       contained start=+\~%+ end=+%\~+ containedin=ALLBUT,htmlString,tokenQuoted
syntax region tokenQuoted contained start=+\~%+ end=+%\~+ containedin=htmlString   

I guess I should also mention that when I was testing this I just created the file ~/.vim/syntax/html.vim and added the above content. There was no need to add anything to my .vimrc file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文