Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F114291
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/bbcode/generator.ex b/lib/bbcode/generator.ex
index 9e2f9dc..b246e06 100644
--- a/lib/bbcode/generator.ex
+++ b/lib/bbcode/generator.ex
@@ -1,65 +1,77 @@
defmodule BBCode.Generator do
@moduledoc """
Generate HTML from BBCode fragments in AST form.
The BBCode syntax supported is described at [bbcode.org][bbcode].
[bbcode]: https://www.bbcode.org/reference.php
"""
defp start_tag(tagname), do: Enum.join(["<", tagname, ">"])
defp end_tag(tagname), do: Enum.join(["</", tagname, ">"])
defp simple_tag(tagname, subtree) do
{:ok, text} = reduce_subtree(subtree)
{:ok, [start_tag(tagname), text, end_tag(tagname)] |> Enum.join()}
end
defp link_tag(url), do: Enum.join(["<a href=\"", url, "\">"])
defp reduce_subtree({:b, subtree}), do: simple_tag("strong", subtree)
defp reduce_subtree({:i, subtree}), do: simple_tag("em", subtree)
defp reduce_subtree({:u, subtree}), do: simple_tag("u", subtree)
defp reduce_subtree({:s, subtree}), do: simple_tag("s", subtree)
defp reduce_subtree({:ul, subtree}), do: simple_tag("ul", subtree)
defp reduce_subtree({:ol, subtree}), do: simple_tag("ol", subtree)
defp reduce_subtree({:li, subtree}), do: simple_tag("li", subtree)
defp reduce_subtree({:code, subtree}), do: simple_tag("pre", subtree)
defp reduce_subtree({:quote, subtree}), do: simple_tag("blockquote", subtree)
defp reduce_subtree({:table, subtree}), do: simple_tag("table", subtree)
defp reduce_subtree({:tr, subtree}), do: simple_tag("tr", subtree)
defp reduce_subtree({:th, subtree}), do: simple_tag("th", subtree)
defp reduce_subtree({:td, subtree}), do: simple_tag("td", subtree)
defp reduce_subtree({:url, text}),
do: {:ok, [link_tag(text), text, end_tag("a")] |> Enum.join()}
defp reduce_subtree({:url, address, text}),
do: {:ok, [link_tag(address), text, end_tag("a")] |> Enum.join()}
+ defp reduce_subtree({:img, address}),
+ do: {:ok, "<img src=\"#{address}\">"}
+
+ defp reduce_subtree({:img, size, address}) do
+ [width, height] = String.split(size, "x")
+
+ {width, ""} = Integer.parse(width)
+ {height, ""} = Integer.parse(height)
+
+ {:ok, "<img src=\"#{address}\" width=\"#{width}\" height=\"#{height}\">"}
+ end
+
defp reduce_subtree(text_node) when is_binary(text_node),
do: {:ok, text_node}
defp reduce_subtree(children) when is_list(children) do
with {:ok, new_tree} <-
Enum.reduce_while(children, {:ok, []}, fn x, {:ok, acc} ->
with {:ok, new_tree} <- reduce_subtree(x) do
{:cont, {:ok, acc ++ [new_tree]}}
else
{:error, e} ->
{:halt, {:error, e}}
end
end) do
{:ok, Enum.join(new_tree)}
else
{:error, e} ->
{:error, e}
end
end
defp reduce_subtree(tree), do: {:error, "unknown input #{inspect(tree)}"}
def to_html(tree) when is_list(tree), do: reduce_subtree(tree)
def to_html(_), do: {:error, "not a valid tree"}
end
diff --git a/test/bbcode/generator_test.exs b/test/bbcode/generator_test.exs
index a48b428..1fcd9fc 100644
--- a/test/bbcode/generator_test.exs
+++ b/test/bbcode/generator_test.exs
@@ -1,123 +1,149 @@
defmodule BBCode.Generator.Test do
use ExUnit.Case
describe "simple tags" do
test "[b] tags are translated to <strong>" do
assert {:ok, "<strong>testing</strong>"} = BBCode.to_html("[b]testing[/b]")
end
test "[i] tags are translated to <em>" do
assert {:ok, "<em>testing</em>"} = BBCode.to_html("[i]testing[/i]")
end
test "[u] tags are translated to <u>" do
assert {:ok, "<u>testing</u>"} = BBCode.to_html("[u]testing[/u]")
end
test "[s] tags are translated to <s>" do
assert {:ok, "<s>testing</s>"} = BBCode.to_html("[s]testing[/s]")
end
test "[code] tags are translated to <pre>" do
assert {:ok, "<pre>testing</pre>"} = BBCode.to_html("[code]testing[/code]")
end
test "[quote] tags are translated to <blockquote>" do
assert {:ok, "<blockquote>testing</blockquote>"} = BBCode.to_html("[quote]testing[/quote]")
end
test "compounding simple tags works as expected" do
assert {:ok, "<strong><em>testing</em></strong>"} = BBCode.to_html("[b][i]testing[/i][/b]")
end
end
describe "lists" do
test "[ul] lists are rendered properly" do
data = """
[ul]
[*]a
[*]b
[*]c
[/ul]
"""
expected = """
<ul>
<li>a</li><li>b</li><li>c</li></ul>
"""
assert {:ok, ^expected} = BBCode.to_html(data)
end
test "[ol] lists are rendered properly" do
data = """
[ol]
[*]a
[*]b
[*]c
[/ol]
"""
expected = """
<ol>
<li>a</li><li>b</li><li>c</li></ol>
"""
assert {:ok, ^expected} = BBCode.to_html(data)
end
end
describe "tables" do
test "[table] tables are rendered properly" do
data = """
[table]
[tr]
[th]header[/th]
[/tr]
[tr]
[td]cell[/td]
[/tr]
[/table]
"""
expected = """
<table>
<tr>
<th>header</th>
</tr>
<tr>
<td>cell</td>
</tr>
</table>
"""
assert {:ok, ^expected} = BBCode.to_html(data)
end
end
describe "links" do
test "bare [url] links are rendered properly" do
data = """
[url]http://example.com[/url]
"""
expected = """
<a href="http://example.com">http://example.com</a>
"""
assert {:ok, ^expected} = BBCode.to_html(data)
end
test "named [url] links are rendered properly" do
data = """
[url=http://example.com]Example[/url]
"""
expected = """
<a href="http://example.com">Example</a>
"""
assert {:ok, ^expected} = BBCode.to_html(data)
end
end
+
+ describe "images" do
+ test "bare [img] links are rendered properly" do
+ data = """
+ [img]http://example.com/image.jpg[/img]
+ """
+
+ expected = """
+ <img src="http://example.com/image.jpg">
+ """
+
+ assert {:ok, ^expected} = BBCode.to_html(data)
+ end
+
+ test "sized [img] links are rendered properly" do
+ data = """
+ [img=32x32]http://example.com/image.jpg[/img]
+ """
+
+ expected = """
+ <img src="http://example.com/image.jpg" width="32" height="32">
+ """
+
+ assert {:ok, ^expected} = BBCode.to_html(data)
+ end
+ end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 26, 1:46 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40168
Default Alt Text
(6 KB)
Attached To
Mode
R10 bbcode
Attached
Detach File
Event Timeline
Log In to Comment