Permalink
Browse files

Added filetype detection

  • Loading branch information...
1 parent 09eb970 commit a9ffad7a78e47f97c5f3adabe8ee87e01f3e4b27 @mitsuhiko committed Sep 2, 2013
Showing with 94 additions and 0 deletions.
  1. +23 −0 README
  2. +4 −0 ftplugin/htmljinja.vim
  3. +67 −0 plugin/htmljinja.vim
View
@@ -1,2 +1,25 @@
+Jinja for Vim
+-------------
+
Jinja for Vim. This is the updated version unlike the
one on vimscripts.
+
+Installation
+------------
+
+If you don't have a preferred installation method, I recommend installing
+pathogen.vim, and then simply copy and paste:
+
+ cd ~/.vim/bundle
+ git clone git://github.com/mitsuhiko/vim-jinja.git
+
+Configuration
+-------------
+
+ g:htmljinja_disable_detection (Default: 0)
+ If set the plugin will not try to autodetect HTML files that
+ contain Jinja code.
+
+ g:htmljinja_disable_html_upgrade (Default: 0)
+ If set the plugin will not try to check on each write of an HTML
+ file it it should upgrade to htmljinja
@@ -0,0 +1,4 @@
+if exists("b:did_ftplugin")
+ finish
+endif
+runtime! ftplugin/html.vim
View
@@ -0,0 +1,67 @@
+" There are two ways in which vim can detect Jinja:
+" option a: vim detected HTML in which case we will scan the first
+" 50 lines to see if it looks like Jinja and then switch
+" over to htmljinja.
+" option b: vim detected htmldjango in which case we check the first
+" 50 lines to look for Django-ism's. If we don't find any
+" we switch to htmljinja.
+"
+" If we already did the detection we don't do anything so that users
+" can still switch manually to a different syntax.
+"
+" Additionally whenever the buffer finished writing and the file type
+" is html we also try to upgrade to htmljinja. This can be separately
+" disabled with g:htmljinja_disable_html_upgrade
+
+if exists("g:htmljinja_disable_detection") && g:htmljinja_disable_detection
+ finish
+endif
+
+fun! s:TryDetectJinja()
+ if exists("b:did_jinja_autodetect")
+ return
+ endif
+ let b:did_jinja_autodetect=1
+
+ let n = 1
+ while n < 50 && n < line("$")
+ if getline(n) =~ '{%\s*\(extends\|block\|macro\|set\|if\|for\|include\|trans\)\>'
+ setlocal filetype=htmljinja
+ return
+ endif
+ let n = n + 1
+ endwhile
+endfun
+
+fun! s:ConsiderSwitchingToJinja()
+ if exists("b:did_jinja_autodetect")
+ return
+ endif
+ let b:did_jinja_autodetect=1
+
+ let n = 1
+ while n < 50 && n < line("$")
+ let line = getline(n)
+ " Bail on django specific tags
+ if line =~ '{%\s*\(load\|autoescape \(on\|off\)\|cycle\|empty\)\>'
+ return
+ " Bail on django filter syntax
+ elseif line =~ '\({%\|{{\).*|[a-zA-Z0-9]\+:'
+ return
+ endif
+ let n = n + 1
+ endwhile
+ setlocal filetype=htmljinja
+endfun
+
+fun! s:ConsiderSwitchingToJinjaAgain()
+ unlet b:did_jinja_autodetect
+ call s:ConsiderSwitchingToJinja()
+endfun
+
+autocmd FileType htmldjango call s:ConsiderSwitchingToJinja()
+autocmd FileType html call s:TryDetectJinja()
+
+if !exists("g:htmljinja_disable_html_upgrade") || !g:htmljinja_disable_html_upgrade
+ autocmd BufWritePost *.html,*.htm,*.shtml,*.stm call s:ConsiderSwitchingToJinjaAgain()
+endif

0 comments on commit a9ffad7

Please sign in to comment.