Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/bbcode/generator.ex b/lib/bbcode/generator.ex
index cf1c7fe..1bef0e5 100644
--- a/lib/bbcode/generator.ex
+++ b/lib/bbcode/generator.ex
@@ -1,45 +1,53 @@
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 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(text_node) when is_binary(text_node),
do: {:ok, text_node}
defp reduce_subtree(tree), do: {:error, "unknown input #{inspect(tree)}"}
def to_html(tree) do
with {:ok, new_tree} <-
Enum.reduce_while(tree, {: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
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 26, 7:55 PM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40471
Default Alt Text
(2 KB)

Event Timeline