-
Notifications
You must be signed in to change notification settings - Fork 57
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
Separate interpolation from verbatim text #130
Merged
doomspork
merged 7 commits into
slime-lang:master
from
little-bobby-tables:separate-interpolation
Jun 9, 2017
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
829df5b
add phoenix html integration test
little-bobby-tables 3818de1
separate dynamic eex in the grammar
little-bobby-tables f7d8ca4
handle interpolation in inline html
little-bobby-tables 924a9af
embedded engines
little-bobby-tables 8ba02d7
eliminate dead code
little-bobby-tables 1e381ba
fix embedded engine rendering, raise an error if the engine is unknown
little-bobby-tables 7fd6482
remove an obsolete TODO in grammar
little-bobby-tables File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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
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 |
---|---|---|
|
@@ -17,6 +17,8 @@ defmodule Slime.Parser.Transform do | |
alias Slime.Parser.Nodes.InlineHTMLNode | ||
alias Slime.Parser.Nodes.DoctypeNode | ||
|
||
alias Slime.TemplateSyntaxError | ||
|
||
@default_tag Application.get_env(:slime, :default_tag, "div") | ||
@sort_attrs Application.get_env(:slime, :sort_attrs, true) | ||
@merge_attrs Application.get_env(:slime, :merge_attrs, %{"class" => " "}) | ||
|
@@ -25,9 +27,6 @@ defmodule Slime.Parser.Transform do | |
"#" => %{attr: "id"} | ||
}) | ||
|
||
# TODO: separate dynamic elixir blocks by parser | ||
@quote_outside_interpolation_regex ~r/(^|\G)(?:\\.|[^#]|#(?!\{)|(?<pn>#\{(?:[^"}]++|"(?:\\.|[^"#]|#(?!\{)|(?&pn))*")*\}))*?\K"/u | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RIP |
||
|
||
@type ast :: term | ||
@type index :: {{:line, non_neg_integer}, {:column, non_neg_integer}} | ||
|
||
|
@@ -99,6 +98,13 @@ defmodule Slime.Parser.Transform do | |
content: TextBlock.render_without_indentation(text)} | ||
end | ||
|
||
def transform(:text_item, input, _index) do | ||
case input do | ||
{:dynamic, [_, expression, _]} -> {:eex, to_string(expression)} | ||
{:static, text} -> to_string(text) | ||
end | ||
end | ||
|
||
def transform(:html_comment, input, _index) do | ||
indent = indent_size(input[:indent]) | ||
decl_indent = indent + String.length(input[:type]) | ||
|
@@ -138,40 +144,30 @@ defmodule Slime.Parser.Transform do | |
end | ||
end | ||
|
||
def transform(:text_block_line, input, _index) do | ||
[space, line] = input | ||
indent = indent_size(space) | ||
case line do | ||
{:simple, content} -> {indent, to_string(content), false} | ||
{:dynamic, content} -> {indent, to_string(content), true} | ||
end | ||
end | ||
|
||
def transform(:embedded_engine, [engine, _, lines], _index) do | ||
lines = case lines do | ||
{:empty, _} -> "" | ||
_ -> List.flatten(lines[:lines]) | ||
end | ||
case EmbeddedEngine.render_with_engine(engine, lines) do | ||
{tag, content} -> %HTMLNode{name: tag, | ||
attributes: (content[:attributes] || []), | ||
children: content[:children]} | ||
content -> content | ||
def transform(:embedded_engine, [engine, _, content], index) do | ||
case EmbeddedEngine.parse(engine, content[:lines]) do | ||
{:ok, {tag, content}} -> | ||
%HTMLNode{name: tag, | ||
attributes: (content[:attributes] || []), | ||
children: content[:children]} | ||
{:ok, content} -> content | ||
{:error, message} -> | ||
{{:line, line_number}, {:column, column}} = index | ||
raise TemplateSyntaxError, message: message, | ||
line: "", line_number: line_number, column: column | ||
end | ||
end | ||
|
||
def transform(:embedded_engine_lines, input, _index) do | ||
[line, rest] = input | ||
lines = Enum.map(rest, fn ([_, lines]) -> lines end) | ||
[line | lines] | ||
def transform(:embedded_engine_lines, [first_line, rest], _index) do | ||
[first_line | Enum.map(rest, fn ([_, lines]) -> lines end)] | ||
end | ||
|
||
def transform(:embedded_engine_line, input, _index) do | ||
to_string(input) | ||
def transform(:indented_text_line, [space, content], _index) do | ||
{indent_size(space), content} | ||
end | ||
|
||
def transform(:inline_html, [_, content, children], _index) do | ||
%InlineHTMLNode{content: [content], children: children} | ||
%InlineHTMLNode{content: content, children: children} | ||
end | ||
|
||
def transform(:code, input, _index) do | ||
|
@@ -202,14 +198,6 @@ defmodule Slime.Parser.Transform do | |
def transform(:code_line, input, _index), do: to_string(input) | ||
def transform(:code_line_with_break, input, _index), do: to_string(input) | ||
|
||
def transform(:text_content, input, _index) do | ||
case input do | ||
{:dynamic, content} -> | ||
%EExNode{content: content |> to_string |> wrap_in_quotes, output: true} | ||
{:simple, content} -> content | ||
end | ||
end | ||
|
||
def transform(:dynamic_content, input, _index) do | ||
content = input |> Enum.at(3) |> to_string | ||
%EExNode{content: content, output: true} | ||
|
@@ -275,30 +263,18 @@ defmodule Slime.Parser.Transform do | |
end | ||
end | ||
|
||
def transform(:text, input, _index), do: to_string(input) | ||
def transform(:tag_name, input, _index), do: to_string(input) | ||
def transform(:attribute_name, input, _index), do: to_string(input) | ||
def transform(:crlf, input, _index), do: to_string(input) | ||
def transform(_symdol, input, _index), do: input | ||
|
||
def remove_empty_lines(lines) do | ||
Enum.filter(lines, fn | ||
({0, ""}) -> false | ||
(_) -> true | ||
end) | ||
end | ||
|
||
def expand_tag_shortcut(tag) do | ||
case Map.fetch(@shortcut, tag) do | ||
:error -> {tag, []} | ||
{:ok, spec} -> expand_shortcut(spec, tag) | ||
end | ||
end | ||
|
||
def wrap_in_quotes(content) do | ||
~s("#{String.replace(content, @quote_outside_interpolation_regex, ~S(\\"))}") | ||
end | ||
|
||
defp expand_attr_shortcut(type, value) do | ||
spec = Map.fetch!(@shortcut, type) | ||
expand_shortcut(spec, value) | ||
|
Oops, something went wrong.
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.
Better to keep this check, it will be easier to add descriptive error in case of invalid engine name