From 38b3f0c272a752ba2928119f75cdb22606e1cca0 Mon Sep 17 00:00:00 2001 From: Thelonius Kort Date: Thu, 26 Jan 2023 21:48:50 +0100 Subject: [PATCH] Add hyphenation and a generalized render_public_content function --- lib/outlook/hyphenation.ex | 31 +++++++++++++++++++ lib/outlook/internal_tree.ex | 18 +++++++++-- .../live/translation_live/form_component.ex | 8 +++-- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 lib/outlook/hyphenation.ex diff --git a/lib/outlook/hyphenation.ex b/lib/outlook/hyphenation.ex new file mode 100644 index 0000000..98bd333 --- /dev/null +++ b/lib/outlook/hyphenation.ex @@ -0,0 +1,31 @@ +defmodule Outlook.Hyphenation do + + def hyphenate(html, lang) do + form = get_multipart_form( + [ + {"api-token", System.get_env("HYPH_API_TOKEN")}, + {"hyph[lang]", String.downcase(lang)}, + {"hyph[text]", html}, + ] + ) + + response_raw = HTTPoison.request!( + :post, + System.get_env("HYPH_URL"), + form, + get_multipart_headers() + ) + response_raw.body + end + + defp get_multipart_form fields do + {:multipart, fields} + end + + defp get_multipart_headers() do + [ + "Content-Type": "multipart/form-data", + "Transfer-Encoding": "chunked", + ] + end +end diff --git a/lib/outlook/internal_tree.ex b/lib/outlook/internal_tree.ex index 9c83e0e..1fc92ec 100644 --- a/lib/outlook/internal_tree.ex +++ b/lib/outlook/internal_tree.ex @@ -2,6 +2,7 @@ defmodule Outlook.InternalTree do alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree,Translation} alias Outlook.HtmlPreparations.HtmlPreparation + alias Outlook.{Hyphenation, Translations} def render_html(tree) do tree @@ -55,11 +56,24 @@ defmodule Outlook.InternalTree do Translation.render_translation(tree, translation) end - def legacy_export(tree, translation) do - content= Translation.render_translation(tree, translation) + def render_public_content(tree, translation, language) do + Translation.render_translation(tree, translation) + |> Html.render_doc() + |> Hyphenation.hyphenate(language) + end + + def legacy_export(translation_id) do + translation = Translations.get_translation!(translation_id) + content= Translation.render_translation(translation.article.content, translation.content) |> garnish(%{tunits: %{class: "ttrans", "data-ttrans-status": fn n -> Map.get(n, :status) end}}) |> Html.render_doc + |> Hyphenation.hyphenate(translation.language) IO.puts "writing export.html to #{File.cwd!}" File.write("export.html", content) end + + def get_tunit_ids(tree) do + InternalTree.collect_tunit_ids(tree) + # |> List.flatten() + end end diff --git a/lib/outlook_web/live/translation_live/form_component.ex b/lib/outlook_web/live/translation_live/form_component.ex index a654b05..911ec57 100644 --- a/lib/outlook_web/live/translation_live/form_component.ex +++ b/lib/outlook_web/live/translation_live/form_component.ex @@ -197,8 +197,12 @@ defmodule OutlookWeb.TranslationLive.FormComponent do defp publish(translation_params, %{"publish" => "true"}, socket) do translation_params |> Map.put("public_content", - InternalTree.render_translation(socket.assigns.translation.article.content, translation_params["content"]) - |> Html.render_doc()) + InternalTree.render_public_content( + socket.assigns.translation.article.content, + socket.assigns.translation_content, + socket.assigns.translation.language + ) + ) end defp publish(translation_params, %{"publish" => "false"}, _) do translation_params