-
Notifications
You must be signed in to change notification settings - Fork 728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Velocity template support. #352
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,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" ) | ||
|
||
|
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,102 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
module Rouge | ||
module Lexers | ||
class Velocity < TemplateLexer | ||
title 'Velocity' | ||
desc 'Generic `Velocity <http://velocity.apache.org/>`_ template lexer.' | ||
tag 'velocity' | ||
filenames '*.vm', '*.velocity', '*.fhtml' | ||
mimetypes 'text/html+velocity' | ||
|
||
IDENTIFIER = IDENTIFIER = /[a-zA-Z_]\w*/ | ||
|
||
def self.analyze_text(text) | ||
rv = 0.0 | ||
if text =~ /#\{?macro\}?\(.*?\).*?#\{?end\}?/ | ||
rv += 0.25 | ||
end | ||
|
||
if text =~ /#\{?if\}?\(.+?\).*?#\{?end\}?/ | ||
rv += 0.15 | ||
end | ||
|
||
if text =~ /#\{?foreach\}?\(.+?\).*?#\{?end\}?/ | ||
rv += 0.15 | ||
end | ||
|
||
if text =~ /\$\{?[a-zA-Z_]\w*(\([^)]*\))?(\.\w+(\([^)]*\))?)*\}?/ | ||
rv += 0.01 | ||
end | ||
|
||
rv | ||
end | ||
|
||
state :root do | ||
rule /[^{#$]+/, Other | ||
|
||
rule /(#)(\*.*?\*)(#)/m do | ||
groups Comment::Preproc, Comment, Comment::Preproc | ||
end | ||
|
||
rule /(##)(.*?$)/ do | ||
groups Comment::Preproc, Comment | ||
end | ||
|
||
rule /(#\{?)(#{IDENTIFIER})(\}?)(\s?\()/m do | ||
groups Punctuation, Name::Function, Punctuation, Punctuation do | ||
goto :directiveparams | ||
end | ||
end | ||
|
||
rule /(#\{?)(#{IDENTIFIER})(\}|\b)/m do | ||
groups Punctuation, Name::Function, Punctuation | ||
end | ||
|
||
rule /\$\{?/, Punctuation, :variable | ||
end | ||
|
||
state :variable do | ||
rule /#{IDENTIFIER}/, Name::Variable | ||
rule /\(/, Punctuation, :funcparams | ||
rule /(\.)(#{IDENTIFIER})/ do | ||
groups Punctuation, Name::Variable do | ||
goto :push | ||
end | ||
end | ||
rule /\}/, Punctuation, :pop! | ||
rule /./, Str::Char, :pop! | ||
end | ||
|
||
state :directiveparams do | ||
rule /(&&|\|\||==?|!=?|[-<>+*%&|^\/])|\b(eq|ne|gt|lt|ge|le|not|in)\b/, Operator | ||
rule /\[/, Operator, :rangeoperator | ||
rule /\b#{IDENTIFIER}\b/, Name::Function | ||
mixin :funcparams | ||
end | ||
|
||
state :rangeoperator do | ||
rule /\.\./, Operator | ||
mixin :funcparams | ||
rule /\]/, Operator, :pop! | ||
end | ||
|
||
state :funcparams do | ||
rule /\$\{?/, Punctuation, :variable | ||
rule /\s+/, Text | ||
rule /,/, Punctuation | ||
rule /"(\\\\|\\"|[^"])*"/, Str::Double | ||
rule /'(\\\\|\\'|[^'])*'/, Str::Single | ||
rule /0[xX][0-9a-fA-F]+[Ll]?/, Literal::Number | ||
rule /\b[0-9]+\b/, Literal::Number | ||
rule /(true|false|null)\b/, Keyword::Constant | ||
rule /\(/, Punctuation, :push | ||
rule /\)/, Punctuation, :push | ||
rule /\[/, Punctuation, :push | ||
rule /\]/, Punctuation, :push | ||
rule /\}/, 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" ) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of highlighting this as
Other
, I'd recommend delegating it toparent
, a lexer provided by theTemplateLexer
class that by default is HTML. So:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then after doing that, I suggest you look into the errors in the sample. Run
bundle exec rackup
and viewlocalhost:9292/velocity
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(to see in detail how it's being lexed, add
?debug=1
)