Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Add Velocity lexer (rouge-ruby#1518)
Browse files Browse the repository at this point in the history
This commit adds a lexer for the Velocity templating language.
  • Loading branch information
pyrmont authored and mattt committed May 19, 2021
1 parent 6244b8a commit 46da39e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rouge/demos/velocity
@@ -0,0 +1,9 @@
#*
There is multi-line comment.
see this text because the Velocity Templating Engine will ignore it.
*#
<h3>List</h3>
## This is a single line comment.
#if( $allProducts )
<p>not found.</p>
#end
71 changes: 71 additions & 0 deletions lib/rouge/lexers/velocity.rb
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*- #

module Rouge
module Lexers
class Velocity < TemplateLexer
title 'Velocity'
desc 'Velocity is a Java-based template engine (velocity.apache.org)'
tag 'velocity'
filenames '*.vm', '*.velocity', '*.fhtml'
mimetypes 'text/html+velocity'

id = /[a-z_]\w*/i

state :root do
rule %r/[^{#$]+/ do
delegate parent
end

rule %r/(#)(\*.*?\*)(#)/m, Comment::Multiline
rule %r/(##)(.*?$)/, Comment::Single

rule %r/(#\{?)(#{id})(\}?)(\s?\()/m do
groups Punctuation, Name::Function, Punctuation, Punctuation
push :directive_params
end

rule %r/(#\{?)(#{id})(\}|\b)/m do
groups Punctuation, Name::Function, Punctuation
end

rule %r/\$\{?/, Punctuation, :variable
end

state :variable do
rule %r/#{id}/, Name::Variable
rule %r/\(/, Punctuation, :func_params
rule %r/(\.)(#{id})/ do
groups Punctuation, Name::Variable
end
rule %r/\}/, Punctuation, :pop!
rule(//) { pop! }
end

state :directive_params do
rule %r/(&&|\|\||==?|!=?|[-<>+*%&|^\/])|\b(eq|ne|gt|lt|ge|le|not|in)\b/, Operator
rule %r/\[/, Operator, :range_operator
rule %r/\b#{id}\b/, Name::Function
mixin :func_params
end

state :range_operator do
rule %r/[.]{2}/, Operator
mixin :func_params
rule %r/\]/, Operator, :pop!
end

state :func_params do
rule %r/\$\{?/, Punctuation, :variable
rule %r/\s+/, Text
rule %r/,/, Punctuation
rule %r/"(\\\\|\\"|[^"])*"/, Str::Double
rule %r/'(\\\\|\\'|[^'])*'/, Str::Single
rule %r/0[xX][0-9a-fA-F]+[Ll]?/, Num::Hex
rule %r/\b[0-9]+\b/, Num::Integer
rule %r/(true|false|null)\b/, Keyword::Constant
rule %r/[(\[]/, Punctuation, :push!
rule %r/[)\]}]/, Punctuation, :pop!
end
end
end
end
19 changes: 19 additions & 0 deletions spec/lexers/velocity_spec.rb
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*- #

describe Rouge::Lexers::Velocity do
let(:subject) { Rouge::Lexers::Velocity.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.velocity'
assert_guess :filename => 'foo.vm'
assert_guess :filename => 'foo.fhtml'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/html+velocity'
end
end
end
28 changes: 28 additions & 0 deletions spec/visual/samples/velocity
@@ -0,0 +1,28 @@

#*
There is multi-line comment.
see this text because the Velocity Templating Engine will ignore it.
*#
<h3>List</h3>
## This is a single line comment.
#if( $allProducts )
<p>not found.</p>
#else

<table>
<tbody>
#foreach( $product in $allProducts )
<tr id="product-$product.Id" data-url="#SURL("$!product.Slug")">
<td>$product.Title</td>
</tr>
#end
</tbody>
</table>
#end
#set( $monkey.Say = ["Not", $my, "fault", 10, false, null] ) ## ArrayList
${mudSlinger}
${customer.Address}
${purchase.getTotal(true)}
$title.set( "Homage to Catalonia" )


0 comments on commit 46da39e

Please sign in to comment.