Skip to content

Commit

Permalink
Fix inline nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Apr 22, 2017
1 parent 746ffa3 commit ed67331
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/slime/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@ defmodule Slime.Parser do
end
end

@inline_tag_regex ~r/\A(?<short_tag>(?:[\.#]?[\w-]+)++):\W*(?<inline_tag>.*)/
@inline_tag_regex ~r/\A(?<short_tag>(?:[\.#]?[\w-]+)++):[^\w\.#]*(?<rest>.*)/

def parse_line(line) do
case strip_line(line) do
{_indentation, ""} ->
if Application.get_env(:slime, :keep_lines), do: {:prev, ""}, else: nil
{indentation, line} ->
[tag, inline_tag] =
[tag, rest] =
case Regex.run(@inline_tag_regex, line, capture: :all_but_first) do
nil -> [line, nil]
match -> match
end

parse_tag = fn (tag) -> tag |> String.first |> parse_line(tag) end
tag = parse_tag.(tag)
tag = if inline_tag do
inline_tag = parse_tag.(inline_tag)
tag = if rest do
{0, rest} = parse_line(rest)
{tag_name, attrs} = tag
{tag_name, [{:children, [inline_tag]} | attrs]}
{tag_name, Keyword.put(attrs, :children, [rest])}
else
tag
end
Expand Down
24 changes: 24 additions & 0 deletions test/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ defmodule ParserTest do
]
end

test "parses inline nesting" do
parsed = [".row: .col-lg-12: p Hello World"] |> Parser.parse_lines
assert parsed == [
{0, {"div",
children: [
{"div",
children: [
{"p",
attributes: [],
children: ["Hello World"],
spaces: %{},
close: false}
],
attributes: [class: "col-lg-12"],
spaces: %{},
close: false}
],
attributes: [class: "row"],
spaces: %{},
close: false}
}
]
end

test "parses css classes with dashes" do
{_, {"div", opts}} = ".my-css-class test"
|> Parser.parse_line
Expand Down

0 comments on commit ed67331

Please sign in to comment.