An IndieWeb engine for a self-hostable website. https://koype.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.4KB

  1. # Koype: a IndieWeb-focused, single-tenant website engine for people.
  2. #
  3. # Copyright © 2019 Jacky Alciné <jacky.is@black.af>
  4. #
  5. # This file belongs to the Koype project.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. defmodule Koype.Web.BaseView do
  20. def get_current_template(assigns)
  21. def get_current_template(%{view_template: template}), do: template
  22. def get_current_template(%{"template_name" => template}), do: template
  23. def get_current_module(%{"template_view_module" => module}), do: module
  24. def get_current_module(%{view_module: module}), do: module
  25. def base_title() do
  26. Koype.Profile.displayed_name() || "koype"
  27. end
  28. def header_tags(tag, assigns) do
  29. module = get_current_module(assigns)
  30. Phoenix.HTML.raw(
  31. do_compile_tags(Koype.Web.LayoutView, tag, assigns) <> do_compile_tags(module, tag, assigns)
  32. )
  33. end
  34. defp do_expand_attrs_to_string(tag, attrs)
  35. defp do_expand_attrs_to_string(tag, attrs) when is_tuple(attrs) do
  36. do_expand_attrs_to_string(tag, Map.new([attrs]))
  37. end
  38. defp do_expand_attrs_to_string(tag, attrs) when is_map(attrs) do
  39. attrs_str = Enum.map(attrs, fn {key, value} -> "#{key}=\"#{value}\" " end)
  40. "<#{tag} #{attrs_str}/>\n"
  41. end
  42. defp do_expand_attrs_to_string(tag, attrs_list) when is_list(attrs_list) do
  43. Enum.map(attrs_list, fn attrs -> do_expand_attrs_to_string(tag, attrs) end)
  44. end
  45. defp do_compile_tags(module, tag, assigns) do
  46. template = get_current_template(assigns)
  47. tag_entries = module.tags(tag, template, assigns)
  48. module_tag_data =
  49. Enum.map(tag_entries, fn tag_entry -> module.tag({tag, tag_entry}, template, assigns) end)
  50. |> Enum.reject(&is_nil/1)
  51. |> Enum.reject(&Enum.empty?/1)
  52. Enum.join(
  53. Enum.map(module_tag_data, fn attrs -> do_expand_attrs_to_string(tag, attrs) end)
  54. |> Enum.reject(&is_nil/1)
  55. )
  56. end
  57. end