长生不老药/凤凰通过元组枚举以更换路径



我正在用floki解析一些HTML。并收到以下元组:

{"html", [{"lang", "en"}],
 [{"head", [],
   [{"title", [], ["My App"]},
    {"link", [{"rel", "stylesheet"}, {"href", "/css/app.css"}], []}]},
  {"body", [],
   [{"main", [{"id", "main_container"}, {"role", "main"}], []},
    {"script", [{"src", "/js/app.js"}], [""]},
    {"iframe",
     [{"src", "/phoenix/live_reload/frame"}, {"style", "display: none;"}],
     []}]}]}

是否可以列举所有元素,对于那些具有hrefsrc的元素,可以为它们添加完整的路径吗?例如,在这种情况下,将它们替换为:http://localhost/css/app.csshttp://localhost/js/app.js

这是您可以使用递归函数进行的一种方法。

defmodule HTML do
  def use_full_path({el, attrs, children}) do
    {el, update_attrs(attrs), Enum.map(children, &use_full_path/1)}
  end
  def use_full_path(string) do
    string
  end

  defp update_attrs(attrs) do
    Enum.map(attrs, fn {key, val} ->
      if key in ["href", "src"] do
        {key, "http://localhost" <> val}
      else
        {key, val}
      end
    end)
  end
end
tree = {"html", [{"lang", "en"}],
 [{"head", [],
   [{"title", [], ["My App"]},
    {"link", [{"rel", "stylesheet"}, {"href", "/css/app.css"}], []}]},
  {"body", [],
   [{"main", [{"id", "main_container"}, {"role", "main"}], []},
    {"script", [{"src", "/js/app.js"}], [""]},
    {"iframe",
     [{"src", "/phoenix/live_reload/frame"}, {"style", "display: none;"}],
     []}]}]}
HTML.use_full_path(tree) |> IO.inspect

最新更新