-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a lexer for the Velocity templating language.
- Loading branch information
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" ) | ||
|
||
|