Page MenuHomePhorge

No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/bbcode/parser.ex b/lib/bbcode/parser.ex
index 6845a82..4407ab1 100644
--- a/lib/bbcode/parser.ex
+++ b/lib/bbcode/parser.ex
@@ -1,201 +1,214 @@
defmodule BBCode.Parser do
import NimbleParsec
@moduledoc """
Parse BBCode into an abstract tree.
"""
tag = utf8_string([?a..?z, ?A..?Z, ?0..?9], min: 1)
text = utf8_string([not: ?[, not: ?], not: ?\r, not: ?\n], min: 1)
end_tag =
ignore(string("[/"))
|> concat(tag)
|> ignore(string("]"))
# block tags
quote_tag = string("quote")
ul_tag = string("ul")
ol_tag = string("ol")
li_tag = string("li")
code_tag = string("code")
table_tag = string("table")
tr_tag = string("tr")
th_tag = string("th")
td_tag = string("td")
# span tags
b_tag = string("b")
i_tag = string("i")
u_tag = string("u")
s_tag = string("s")
url_tag = string("url")
img_tag = string("img")
# special tags
star_tag = ignore(string("[*]"))
# newline
newline = utf8_char([?\r, ?\n])
defcombinatorp(
:block_tag,
ignore(string("["))
|> choice([quote_tag, ul_tag, ol_tag, li_tag, code_tag, table_tag, tr_tag, th_tag, td_tag])
|> ignore(string("]"))
|> ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2)))
)
defcombinatorp(
:block_stanza,
parsec(:block_tag)
|> repeat(lookahead_not(string("[/")) |> choice([parsec(:child_stanza), text]))
|> wrap()
|> concat(end_tag)
|> ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2)))
|> post_traverse(:emit_tree_node)
)
defcombinatorp(
:span_tag,
ignore(string("["))
|> choice([url_tag, img_tag, b_tag, i_tag, u_tag, s_tag])
|> ignore(string("]"))
|> ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2)))
)
defcombinatorp(
:span_tag_with_property,
ignore(string("["))
|> concat(url_tag)
|> ignore(string("="))
|> concat(text)
|> ignore(string("]"))
|> ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2)))
)
defcombinatorp(
:img_tag_with_size_property,
ignore(string("["))
|> concat(img_tag)
|> ignore(string("="))
|> integer(min: 1)
|> ignore(string("x"))
|> integer(min: 1)
|> ignore(string("]"))
|> ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2)))
)
defcombinatorp(
:span_stanza,
parsec(:span_tag)
|> repeat(lookahead_not(string("[/")) |> choice([parsec(:child_stanza), text]))
|> wrap()
|> concat(end_tag)
|> post_traverse(:emit_tree_node)
)
defcombinatorp(
:text_stanza,
text
|> wrap()
|> post_traverse(:emit_tree_node)
)
defcombinatorp(
:star_stanza,
star_tag
|> repeat(
lookahead_not(string("\n"))
|> choice([parsec(:child_stanza), text])
)
|> wrap()
|> concat(ignore(optional(utf8_string([?\n, ?\r], min: 1, max: 2))))
|> post_traverse(:emit_tree_node_star)
)
defcombinatorp(
:span_stanza_with_property,
parsec(:span_tag_with_property)
|> repeat(lookahead_not(string("[/")) |> choice([parsec(:child_stanza), text]))
|> wrap()
|> concat(end_tag)
|> post_traverse(:emit_tree_node_property)
)
defcombinatorp(
:img_stanza_with_size_property,
parsec(:img_tag_with_size_property)
|> repeat(lookahead_not(string("[/")) |> choice([parsec(:child_stanza), text]))
|> wrap()
|> concat(end_tag)
|> post_traverse(:emit_tree_node_size_property)
)
defcombinatorp(
:newline_stanza,
newline
|> post_traverse(:emit_tree_node_newline)
)
+ defcombinatorp(
+ :bracket_text_stanza,
+ string("[")
+ |> concat(text)
+ |> string("]")
+ |> wrap()
+ |> post_traverse(:emit_tree_node)
+ )
+
defcombinatorp(
:child_stanza,
choice([
parsec(:newline_stanza),
parsec(:star_stanza),
parsec(:block_stanza),
parsec(:img_stanza_with_size_property),
parsec(:span_stanza_with_property),
- parsec(:span_stanza)
+ parsec(:span_stanza),
+ parsec(:bracket_text_stanza)
])
)
defcombinatorp(
:root_stanza,
choice([parsec(:child_stanza), parsec(:text_stanza)])
)
defparsecp(
:parse_tree,
repeat(lookahead_not(string("[/")) |> parsec(:root_stanza)) |> eos()
)
defp emit_tree_node_newline(_rest, _args, context, _line, _offset),
do: {[{:br}], context}
defp emit_tree_node_star(_rest, [nodes], context, _line, _offset),
do: {[{:li, nodes}], context}
defp emit_tree_node_size_property(
_rest,
[tag, [tag, width, height, inside]],
context,
_line,
_offset
),
do: {[{String.to_atom(tag), width, height, inside}], context}
defp emit_tree_node_property(_rest, [tag, [tag, property, inside]], context, _line, _offset),
do: {[{String.to_atom(tag), property, inside}], context}
defp emit_tree_node_property(_rest, [tag, [tag, property | nodes]], context, _line, _offset),
do: {[{String.to_atom(tag), property, nodes}], context}
defp emit_tree_node(_rest, [tag, [tag, inside]], context, _line, _offset),
do: {[{String.to_atom(tag), inside}], context}
defp emit_tree_node(_rest, [tag, [tag | nodes]], context, _line, _offset),
do: {[{String.to_atom(tag), nodes}], context}
defp emit_tree_node(_rest, [[text]], context, _line, _offset),
do: {[text], context}
+ defp emit_tree_node(_rest, [["[", text, "]"]], context, _line, _offset),
+ do: {["[" <> text <> "]"], context}
+
def parse(text) do
with {:ok, nodes, _, _, _, _} <- parse_tree(text) do
{:ok, nodes}
else
{:error, e, _, _, _, _} ->
{:error, e}
end
end
end
diff --git a/test/bbcode/parser_test.exs b/test/bbcode/parser_test.exs
index 82e3683..e4bb7fa 100644
--- a/test/bbcode/parser_test.exs
+++ b/test/bbcode/parser_test.exs
@@ -1,89 +1,92 @@
defmodule BBCode.Parser.Test do
use ExUnit.Case
alias BBCode.Parser
describe "simple tags" do
test "it parses [b] tags correctly" do
assert {:ok, [b: "testing"]} = Parser.parse("[b]testing[/b]")
end
test "it parses [i] tags correctly" do
assert {:ok, [i: "testing"]} = Parser.parse("[i]testing[/i]")
end
test "it parses [u] tags correctly" do
assert {:ok, [u: "testing"]} = Parser.parse("[u]testing[/u]")
end
test "it parses [s] tags correctly" do
assert {:ok, [s: "testing"]} = Parser.parse("[s]testing[/s]")
end
test "it parses [code] tags correctly" do
assert {:ok, [code: "testing"]} = Parser.parse("[code]testing[/code]")
end
test "it parses [quote] tags correctly" do
assert {:ok, [quote: "testing"]} = Parser.parse("[quote]testing[/quote]")
end
end
describe "nested tags" do
test "it parses [ul] lists correctly" do
assert {:ok, [{:ul, [{:li, "a"}, {:li, "b"}]}]} =
Parser.parse("[ul][li]a[/li][li]b[/li][/ul]")
end
test "it parses [ol] lists correctly" do
assert {:ok, [{:ol, [{:li, "a"}, {:li, "b"}]}]} =
Parser.parse("[ol][li]a[/li][li]b[/li][/ol]")
end
end
describe "multiline" do
test "it parses a multiline [li] list" do
data = """
[ul]
[li]a[/li]
[li]b[/li]
[/ul]
"""
assert {:ok, [{:ul, [{:li, "a"}, {:li, "b"}]}]} = Parser.parse(data)
end
test "it parses a multiline [*] list" do
data = """
[ul]
[*]a
[*]b
[/ul]
"""
assert {:ok, [{:ul, [{:li, ["a"]}, {:li, ["b"]}]}]} = Parser.parse(data)
end
test "it parses a multiline [*] list with children" do
data = """
[ul]
[*][url=http://example.com]Example[/url]
[/ul]
"""
assert {:ok, [{:ul, {:li, [{:url, "http://example.com", "Example"}]}}]} = Parser.parse(data)
end
end
describe "property tags" do
test "it parses [url=] tags correctly" do
assert {:ok, [{:url, "http://example.com", "Example"}]} =
Parser.parse("[url=http://example.com]Example[/url]")
end
end
- describe "invalid input" do
- test "it fails to parse unterminated input" do
- {:error, "expected end of string"} = Parser.parse("hello [b]world")
+ describe "non-tags" do
+ test "it properly handles bracket text" do
+ data = "oh no! [swearing intensifies]"
+
+ assert {:ok, ["oh no! ", "[swearing intensifies]"]} =
+ Parser.parse(data)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 9:01 PM (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39487
Default Alt Text
(8 KB)

Event Timeline