Skip to content

Commit

Permalink
linewrap: boolean() config
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei Matiushkin committed Aug 25, 2023
1 parent 61e04ea commit 9f52335
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ locals_without_parens = [
disclosure: 2,
paragraph: 2,
list: 2,
brace: 2
brace: 2,
linewrap: 1
]

[
Expand Down
6 changes: 6 additions & 0 deletions lib/md/parser/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ defmodule Md.Parser.DSL do
end
end
end)

defmacro linewrap(value \\ true) do
quote do
@syntax %{linewrap: unquote(value)}
end
end
end
15 changes: 12 additions & 3 deletions lib/md/parser/engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,18 @@ defmodule Md.Engine do
end

state =
state
|> listener(:linefeed)
|> push_char(unquote(lb))
case syntax()[:linewrap] do
true ->
state
|> push_path({:br, [], []})
|> listener({:tag, {" ", :br}, nil})
|> rewind_state(until: :br, inclusive: true)

_ ->
state
|> listener(:linefeed)
|> push_char(unquote(lb))
end
|> push_mode({:linefeed, 0})

do_parse(rest, state)
Expand Down
3 changes: 2 additions & 1 deletion lib/md/parser/syntax.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ defmodule Md.Parser.Syntax do
paragraph: [item()],
list: [item()],
tag: [item()],
brace: [item()]
brace: [item()],
linewrap: boolean()
}

@doc "The implementation should return settings for this particular syntax definition"
Expand Down
3 changes: 2 additions & 1 deletion lib/md/parser/syntax/default.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ defmodule Md.Parser.Syntax.Default do
{"dl", %{}},
{"dt", %{}},
{"dd", %{}}
]
],
linewrap: false
}
end
end
5 changes: 5 additions & 0 deletions test/md/dsl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ defmodule Md.DSL.Test do
assert MyDSLParser.syntax()[:comment] == [{"<!--", %{closing: "-->"}}]
assert MyDSLParser.syntax()[:brace] == [{"***", %{tag: "u"}}]
end

test "DSL is used properly" do
assert [{:p, nil, ["L1", {:br, [], []}, {"u", nil, ["L2"]}, " L3"]}] ==
elem(MyDSLParser.parse("L1\n***L2*** L3"), 1).ast
end
end
5 changes: 3 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ defmodule MyDSLParser do
use Md.Parser, syntax: @my_syntax
import Md.Parser.DSL

comment("<!--", %{closing: "-->"})
magnet("%", %{transform: Tag, terminators: [:ascii_punctuation]})
comment "<!--", %{closing: "-->"}
magnet "%", %{transform: Tag, terminators: [:ascii_punctuation]}
linewrap true
end

ExUnit.start()
Expand Down

0 comments on commit 9f52335

Please sign in to comment.