Drastic proposal: rename eruby to rhtml to make way for an HTML free eruby filetype

Tim Pope vim-ruby-devel at tpope.info
Fri Mar 9 14:49:06 EST 2007

On Fri, Mar 09, 2007 at 04:39:20PM +0000, Hugh Sasse wrote:
> > The subject says it all.  The most common use of eruby is filling out
> > HTML templates, but this is by no means the only use.  I think it
> No, see the book "Code Generation in Action" for example.  
> http://www.codegeneration.net/tiki-index.php?page=GenerationBooks

I'd say HTML templates are still the most common, due to Rails alone,
but okay, you're actually helping make my case.

> The difficulty I see is that when you mix syntaxes you have to pull
> in the right syntax groups, and there isn't a *simple* way to do thig
> in vim.  With lots of help from Doug and others I have, for example
> in my .vimrc:
> <snip>

These look to be the opposite of ERB: other syntax embedded in Ruby
rather than Ruby embedded in other syntax.

> Is the extension .erb already taken?

The extension isn't the limiting factor, the filetype is.  Having erb
for plain text and eruby for HTML is confusing.  However, it might be
a good idea to use erb rather than eruby for the new file type, so
that we end up with erb and rhtml.

> Drat! :-)  I've answered the wrong question.  Hope soemthing in here
> is of use...  In principle I'm in favour of encouraging this use of
> eruby, mainly because I keep forgetting about it, and doing code generation
> the hard way.  Hence all that sfuff in my vimrc.

If you want to use ERB with C, attached is a proof of concept for you
to try.  Drop it in ~/.vim/syntax/erb.vim.  Edit somefile.c.erb and
and :set ft=erb.  This syntax file looks at the filename to determine
what to embed.  So foo.c.erb will embed in C, foo.html.erb will embed
in HTML, and foo.sql.erb will embed in SQL.  In fact, this script
could be renamed to eruby.vim and it would more or less be a drop-in
replacement, rendering my original suggestion moot.  But all the magic
going on in the background is probably a bad idea.  If we get rid of
the magic, having a file like this would still make using ERB with
other file types a lot easier.

if exists("b:current_syntax")
runtime! syntax/c.vim
unlet b:current_syntax
runtime! syntax/erb.vim
let b:current_syntax = "erbc"

That's all you would need in an erbc.vim to get ERB C highlighting.

Anyways, this is just wild brainstorming at this point.

-------------- next part --------------
" Vim syntax file
" Language:		ERB
" Maintainer:		Tim Pope <rails at tpope.info>
" Info:			$Id$
" URL:			http://vim-ruby.rubyforge.org
" Anon CVS:		See above site
" Release Coordinator:	Doug Kearns <dougkearns at gmail.com>

" For version 5.x: Clear all syntax items
" For version 6.x: Quit when a syntax file was already loaded
if version < 600
  syntax clear
elseif exists("b:current_syntax")

if !exists("main_syntax")
  let main_syntax = 'eruby'

" Always check for this magic hearder
let s:type = matchstr(getline(1),'erb_syntax=\zs\w\+')
" Check extension if main_syntax wasn't set
if main_syntax == 'eruby' && s:type == ''
  let s:type = matchstr(substitute(expand("%:t"),'\.erb$','',''),'\.\zs\w\+$')
if s:type == 'rhtml'
  let s:type = 'html'

if version < 600
  syn include @rubyTop <sfile>:p:h/ruby.vim
  if s:type != ''
    exe "runtime! syntax/".s:type.".vim"
    unlet! b:current_syntax
  syn include @rubyTop syntax/ruby.vim

syn cluster erbRegions contains=erbOneLiner,erbBlock,erbExpression,erbComment

syn region  erbOneLiner   matchgroup=erbDelimiter start="^%%\@!"    end="$"     contains=@rubyTop	     containedin=ALLBUT, at erbRegions keepend oneline
syn region  erbBlock      matchgroup=erbDelimiter start="<%%\@!-\=" end="-\=%>" contains=@rubyTop	     containedin=ALLBUT, at erbRegions
syn region  erbExpression matchgroup=erbDelimiter start="<%="       end="-\=%>" contains=@rubyTop	     containedin=ALLBUT, at erbRegions
syn region  erbComment    matchgroup=erbDelimiter start="<%#"       end="-\=%>" contains=rubyTodo, at Spell containedin=ALLBUT, at erbRegions keepend

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_erb_syntax_inits")
  if version < 508
    let did_ruby_syntax_inits = 1
    command -nargs=+ HiLink hi link <args>
    command -nargs=+ HiLink hi def link <args>

  HiLink erbDelimiter		Delimiter
  HiLink erbComment		Comment

  delcommand HiLink
let b:current_syntax = "erb"

if main_syntax == 'eruby'
  unlet main_syntax

" vim: nowrap sw=2 sts=2 ts=8 ff=unix:

More information about the vim-ruby-devel mailing list