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.

132 lines
4.0KB

  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.Template.Renderer do
  20. require Logger
  21. def render_partial(theme_name, file_path, template_data) do
  22. Logger.debug("Attempting to render template partial.",
  23. file_path: file_path,
  24. theme_name: theme_name
  25. )
  26. {time, value} =
  27. :timer.tc(fn ->
  28. try do
  29. with(
  30. %Liquid.Template{} = template <- fetch_template(theme_name, file_path <> ".liquid"),
  31. {:ok, rendered_template, _data} when is_binary(rendered_template) <-
  32. Liquid.Template.render(
  33. template,
  34. template_data
  35. )
  36. ) do
  37. {:ok, rendered_template}
  38. else
  39. {:error, _} = error ->
  40. Logger.error("Encountered an error when grabbing template information.",
  41. error: inspect(error |> elem(1))
  42. )
  43. error
  44. end
  45. rescue
  46. error ->
  47. Logger.error(Exception.format(:error, error, __STACKTRACE__))
  48. reraise(Koype.Template.Error, file: file_path, raw: error)
  49. end
  50. end)
  51. Logger.debug("Template compilation completed.",
  52. build_time: time,
  53. file_path: file_path,
  54. theme_name: theme_name
  55. )
  56. value
  57. end
  58. # TODO: Allow each individual page to define to render its layout.
  59. @spec render_page(binary(), binary(), map()) :: {:ok, binary()} | {:error, any()}
  60. def render_page(theme_name, file_path, template_data) when is_binary(theme_name) do
  61. Koype.Template.Tags.register()
  62. Koype.Template.Filters.register()
  63. Logger.debug("Beginning render of page.",
  64. theme_name: theme_name,
  65. file_path: file_path
  66. )
  67. {time, value} =
  68. :timer.tc(fn ->
  69. data = Koype.Template.Decorator.decorate(template_data, theme: theme_name)
  70. theme_components = ["_header", file_path, "_footer"]
  71. theme_components
  72. |> Enum.map(fn template_name ->
  73. render_partial(
  74. theme_name,
  75. template_name <> ".html",
  76. data
  77. )
  78. end)
  79. |> Enum.reduce_while({:ok, ""}, fn
  80. {:ok, template}, {:ok, acc} when is_binary(template) ->
  81. {:cont, {:ok, Enum.join([acc, template])}}
  82. {:error, _} = error, _ ->
  83. {:halt, error}
  84. error, _ ->
  85. {:halt, {:error, reason: :unknown_issue, raw: error}}
  86. end)
  87. end)
  88. Logger.debug("Completed render of page.",
  89. theme_name: theme_name,
  90. file_path: file_path,
  91. time_ms: time
  92. )
  93. value
  94. end
  95. @spec fetch_template(binary(), binary()) :: {:ok, binary()} | {:error, keyword()}
  96. def fetch_template(theme_name, file_path) do
  97. theme_root = Koype.Theme.template_dir(theme_name)
  98. theme_file_path = Path.join(theme_root, file_path)
  99. case File.read(theme_file_path) do
  100. {:ok, text} ->
  101. try do
  102. Liquid.Template.parse(text)
  103. rescue
  104. e in Liquid.SyntaxError ->
  105. {:error, error: :fetch_template_failed, reason: e, path: theme_file_path}
  106. e ->
  107. {:error, :error, :unexpected_error, reason: e, path: theme_file_path}
  108. end
  109. {:error, err} ->
  110. {:error,
  111. error: :fetch_template_failed, reason: :file.format_error(err), path: theme_file_path}
  112. end
  113. end
  114. end