diff --git a/lib/outlook/internal_tree.ex b/lib/outlook/internal_tree.ex index a223fb0..45d9874 100644 --- a/lib/outlook/internal_tree.ex +++ b/lib/outlook/internal_tree.ex @@ -1,6 +1,6 @@ defmodule Outlook.InternalTree do - alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree} + alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree,Translation} alias Outlook.HtmlPreparations.HtmlPreparation def render_html(tree) do @@ -35,4 +35,8 @@ defmodule Outlook.InternalTree do ) InternalTree.garnish(tree, options) end + + def render_translation(tree, translation) do + Translation.render_translation(tree, translation) + end end diff --git a/lib/outlook/internal_tree/translation.ex b/lib/outlook/internal_tree/translation.ex new file mode 100644 index 0000000..e2c01c1 --- /dev/null +++ b/lib/outlook/internal_tree/translation.ex @@ -0,0 +1,35 @@ +defmodule Outlook.InternalTree.Translation do + + alias Outlook.InternalTree.{InternalNode,TranslationUnit} + + def render_translation([%TranslationUnit{} = tunit | rest], translation) do + [ %TranslationUnit{tunit | + eph: add_title_attribute(tunit.eph, tunit.content), + content: Map.get(translation, tunit.nid, %{content: "ERROR!!! Missing Tunit in translation!"}) + |> Map.get(:content) + } | render_translation(rest, translation) ] + end + + def render_translation([%InternalNode{type: :element} = node | rest], translation) do + [ %InternalNode{node | + content: render_translation(node.content, translation) + } | render_translation(rest, translation) ] + end + + def render_translation([node | rest], translation) do + [ node | render_translation(rest, translation) ] + end + + def render_translation([], _), do: [] + + + defp add_title_attribute(eph, html_string) do + attributes = Map.get(eph, :attributes, %{}) + |> Map.put(:title, + html_string + |> Floki.parse_fragment!() + |> Floki.text() + ) + Map.put(eph, :attributes, attributes) + end +end diff --git a/lib/outlook_web/live/translation_live/show.ex b/lib/outlook_web/live/translation_live/show.ex index 6564e54..e1e2ec6 100644 --- a/lib/outlook_web/live/translation_live/show.ex +++ b/lib/outlook_web/live/translation_live/show.ex @@ -1,7 +1,7 @@ defmodule OutlookWeb.TranslationLive.Show do use OutlookWeb, :live_view - alias Outlook.Translations + alias Outlook.{Translations,InternalTree} @impl true def mount(_params, _session, socket) do diff --git a/lib/outlook_web/live/translation_live/show.html.heex b/lib/outlook_web/live/translation_live/show.html.heex index 4efb2ab..838d0ee 100644 --- a/lib/outlook_web/live/translation_live/show.html.heex +++ b/lib/outlook_web/live/translation_live/show.html.heex @@ -18,4 +18,8 @@ <:item title="Unauthorized"><%= @translation.unauthorized %> +
+ <.render_doc tree={InternalTree.render_translation(@translation.article.content, @translation.content)} /> +
+ <.back navigate={~p"/translations"}>Back to translations