Packages
reactor
0.15.4
1.0.2
1.0.1
1.0.0
0.17.0
0.16.0
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
An asynchronous, graph-based execution engine
Current section
Files
Jump to
Current section
Files
lib/reactor/mermaid/utils.ex
defmodule Reactor.Mermaid.Utils do
@moduledoc """
Utilities for generating Mermaid.
"""
@safe_id ~r/\A[a-zA-Z0-9\._-]+\Z/
@doc "Escape markdown as needed"
def md_escape(nil), do: ""
def md_escape(md) do
md
|> String.replace("\\", "\\\\")
|> String.replace("\"", """)
|> String.replace("`", "`")
|> String.replace("*", "\\*")
|> String.replace("_", "\\_")
|> String.replace("{", "\\{")
|> String.replace("}", "\\}")
|> String.replace("[", "\\[")
|> String.replace("]", "\\]")
|> String.replace("<", "\\<")
|> String.replace(">", "\\>")
|> String.replace("(", "\\(")
|> String.replace(")", "\\)")
|> String.replace("#", "\\#")
|> String.replace("+", "\\+")
|> String.replace("-", "\\-")
|> String.replace(".", "\\.")
|> String.replace("!", "\\!")
|> String.replace("|", "\\|")
end
@doc "Generate a mermaid ID for a term"
@spec mermaid_id(any, String.Chars.t()) :: String.t()
def mermaid_id(name, prefix) when is_atom(name) do
name =
name
|> to_string()
|> deelixirify()
if Regex.match?(@safe_id, name) do
"#{prefix}_#{name}"
else
"#{prefix}_#{:erlang.phash2(name)}"
end
end
def mermaid_id(name, prefix) when is_binary(name) do
if Regex.match?(@safe_id, name) do
"#{prefix}_#{name}"
else
"#{prefix}_#{:erlang.phash2(name)}"
end
end
def mermaid_id(name, prefix), do: "#{prefix}_#{:erlang.phash2(name)}"
@doc "Generate a name which can be used within a Mermaid node"
def name(name) when is_binary(name) do
if String.printable?(name) do
name
else
inspect(name)
end
end
def name(name) when is_atom(name) do
name
|> to_string()
|> deelixirify()
end
def name(name), do: inspect(name)
defp deelixirify(name) do
if String.starts_with?(name, "Elixir.") do
String.replace_leading(name, "Elixir.", "")
else
name
end
end
@doc "Convert the direction atom into a mermaid direction"
@spec direction(atom) :: String.t()
def direction(:top_to_bottom), do: "TD"
def direction(:bottom_to_top), do: "BT"
def direction(:left_to_right), do: "LR"
def direction(:right_to_left), do: "RL"
end