Skip to content
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 a first support to EEX (Embedded Elixir) #874

Merged
merged 3 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rouge/demos/eex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<title><%= @title %></title>
51 changes: 51 additions & 0 deletions lib/rouge/lexers/eex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*- #

module Rouge
module Lexers
class EEX < TemplateLexer
title "EEX"
desc "Embedded Elixir"

tag 'eex'

filenames '*.eex'

def initialize(opts={})
@elixir_lexer = Elixir.new(opts)

super(opts)
end

start do
parent.reset!
@elixir_lexer.reset!
end

open = /<%%|<%=|<%#|<%/
close = /%%>|%>/

state :root do
rule %r/<%#/, Comment, :comment

rule open, Comment::Preproc, :elixir

rule %r/.+?(?=#{open})|.+/mo do
delegate parent
end
end

state :comment do
rule close, Comment, :pop!
rule %r/.+?(?=#{close})|.+/mo, Comment
end

state :elixir do
rule close, Comment::Preproc, :pop!

rule %r/.+?(?=#{close})|.+/mo do
delegate @elixir_lexer
end
end
end
end
end
14 changes: 14 additions & 0 deletions spec/lexers/eex_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*- #

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

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.eex'
assert_guess :filename => 'foo.html.eex'
end
end
end
45 changes: 45 additions & 0 deletions spec/visual/samples/eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h3>List</h3>
<%= if Enum.empty?(@list) do %>
<p>not found.</p>
<% else %>
<table>
<tbody>
<%= for {item, i} <- Enum.with_index(list) do %>
<tr bgcolor="<%= if Integer.mod(i, 2) == 0, do: "#FFCCCC", else: "#CCCCFF" %>">
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
<td><%= item %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>

<%# comment %>

<% header_tag = 'h1' %>

<<%= header_tag %>>
it's a header!
</<%= header_tag %>>

<%#
two line
comment
%>

<script>
var foo = 1;
var bar = <%= bar %>;
var baz = {
a: 1,
b: 2
};
</script>
<style>
.foo {
line-height: <%= @line_height %>
}
</style>
<p>
Some another text
</p>
thumbsup 0