Compare commits

...

7 Commits

9 changed files with 95 additions and 20 deletions

View File

@ -45,3 +45,25 @@
.article a { .article a {
@apply text-cyan-900; @apply text-cyan-900;
} }
.article ul {
@apply pl-6 list-disc my-2;
}
.article ol {
@apply pl-8 list-decimal my-2;
}
.article li {
@apply text-justify;
}
.article img {
max-width: 100%;
height: auto;
}
.article img + div, a + div {
font-size: smaller;
margin-bottom: 0.6ex;
}

View File

@ -14,7 +14,7 @@ defmodule Outlook.Articles.InternalTree do
def cast(_), do: :error def cast(_), do: :error
def load(tree) when is_binary(tree) do def load(tree) when is_binary(tree) do
{:ok, Jason.decode!(tree, keys: :atoms!) |> from_json} {:ok, Jason.decode!(tree, keys: :atoms) |> from_json}
end end
def dump(tree) when is_list(tree) do def dump(tree) when is_list(tree) do

View File

@ -43,7 +43,7 @@ defmodule Outlook.HtmlPreparations.HtmlPreparation do
} | floki_to_internal(rest) ] } | floki_to_internal(rest) ]
end end
def floki_to_internal([ ]), do: ( [ ] ) def floki_to_internal([]), do: []
def set_sibling_with([ %{type: :element} = node | rest ]) do def set_sibling_with([ %{type: :element} = node | rest ]) do
@ -61,7 +61,7 @@ defmodule Outlook.HtmlPreparations.HtmlPreparation do
[ %InternalNode{ node | eph: %{sibling_with: sib_with} } | set_sibling_with(rest) ] [ %InternalNode{ node | eph: %{sibling_with: sib_with} } | set_sibling_with(rest) ]
end end
def set_sibling_with([ ]), do: ( [ ] ) def set_sibling_with([]), do: []
def strip_whitespace_textnodes [ %{type: :text} = node | rest] do def strip_whitespace_textnodes [ %{type: :text} = node | rest] do

View File

@ -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

View File

@ -2,6 +2,7 @@ defmodule Outlook.InternalTree do
alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree,Translation} alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree,Translation}
alias Outlook.HtmlPreparations.HtmlPreparation alias Outlook.HtmlPreparations.HtmlPreparation
alias Outlook.{Hyphenation, Translations}
def render_html(tree) do def render_html(tree) do
tree tree
@ -55,9 +56,24 @@ defmodule Outlook.InternalTree do
Translation.render_translation(tree, translation) Translation.render_translation(tree, translation)
end end
def legacy_export(tree, translation) do def render_public_content(tree, translation, language) do
Translation.render_translation(tree, translation) 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}}) |> garnish(%{tunits: %{class: "ttrans", "data-ttrans-status": fn n -> Map.get(n, :status) end}})
|> Html.render_doc |> 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
end end

View File

@ -13,7 +13,7 @@ defmodule Outlook.Translations.TranslationUnitsMap do
def load(serialized_map) when is_map(serialized_map) do def load(serialized_map) when is_map(serialized_map) do
tumap = for {key, val_str} <- serialized_map do tumap = for {key, val_str} <- serialized_map do
val_map = Jason.decode!(val_str, keys: :atoms!) val_map = Jason.decode!(val_str, keys: :atoms)
val_tu = struct(TranslationUnit, %{val_map | status: String.to_atom(val_map.status)}) val_tu = struct(TranslationUnit, %{val_map | status: String.to_atom(val_map.status)})
{key, val_tu} {key, val_tu}
end end

View File

@ -58,7 +58,7 @@ defmodule Outlook.Translators.Deepl do
%{status: "done"} -> %{status: "done"} ->
response response
%{status: status} -> %{status: status} ->
steps = response.seconds_remaining * 5 steps = Map.get(response, :seconds_remaining, 1) * 5
for n <- 0..steps do for n <- 0..steps do
send(pid, {:progress, %{progress: 100 * n / steps, status: status}}) send(pid, {:progress, %{progress: 100 * n / steps, status: status}})
Process.sleep 200 Process.sleep 200

View File

@ -164,7 +164,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "Translation updated successfully") |> put_flash(:info, "Translation updated successfully")
|> continue_edit(params)} |> continue_edit(:edit, params)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
@ -173,30 +173,36 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
defp save_translation(socket, :new, %{"translation" => translation_params} = params) do defp save_translation(socket, :new, %{"translation" => translation_params} = params) do
case Translations.create_translation(translation_params) do case Translations.create_translation(translation_params) do
{:ok, _translation} -> {:ok, translation} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "Translation created successfully") |> put_flash(:info, "Translation created successfully")
|> continue_edit(params)} |> continue_edit(:new, Map.put(params,"id", translation.id))}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)} {:noreply, assign(socket, changeset: changeset)}
end end
end end
defp continue_edit(socket, %{"continue_edit" => "true"}) do defp continue_edit(socket, :edit, %{"continue_edit" => "true"}) do
socket socket
|> assign(:action, :edit)
end end
defp continue_edit(socket, %{"continue_edit" => "false"}) do defp continue_edit(socket, :new, %{"continue_edit" => "true"} = params) do
socket |> push_patch(to: ~p(/translations/#{params["id"]}/edit))
end
defp continue_edit(socket, _, %{"continue_edit" => "false"}) do
socket |> push_navigate(to: socket.assigns.navigate) socket |> push_navigate(to: socket.assigns.navigate)
end end
defp publish(translation_params, %{"publish" => "true"}, socket) do defp publish(translation_params, %{"publish" => "true"}, socket) do
translation_params translation_params
|> Map.put("public_content", |> Map.put("public_content",
InternalTree.render_translation(socket.assigns.translation.article.content, translation_params["content"]) InternalTree.render_public_content(
|> Html.render_doc()) socket.assigns.translation.article.content,
socket.assigns.translation_content,
socket.assigns.translation.language
)
)
end end
defp publish(translation_params, %{"publish" => "false"}, _) do defp publish(translation_params, %{"publish" => "false"}, _) do
translation_params translation_params

View File

@ -24,7 +24,7 @@ defmodule OutlookWeb.TranslationLive.NewEdit do
end end
@impl true @impl true
def mount(%{"article_id" => article_id} = _params, _session, socket) when socket.assigns.live_action == :new do def handle_params(%{"article_id" => article_id} = _params, _session, socket) when socket.assigns.live_action == :new do
socket = socket socket = socket
|> assign_new(:translation, fn -> |> assign_new(:translation, fn ->
%Translation{ %Translation{
@ -33,15 +33,15 @@ defmodule OutlookWeb.TranslationLive.NewEdit do
} }
end) end)
|> common_assigns() |> common_assigns()
{:ok, assign_new(socket, :translation_content, fn -> {:noreply, assign_new(socket, :translation_content, fn ->
Basic.internal_tree_to_tunit_map(socket.assigns.translation.article.content) end)} Basic.internal_tree_to_tunit_map(socket.assigns.translation.article.content) end)}
end end
def mount(%{"id" => id} = _params, _session, socket) when socket.assigns.live_action == :edit do def handle_params(%{"id" => id} = _params, _session, socket) when socket.assigns.live_action == :edit do
socket = socket socket = socket
|> assign_new(:translation, fn -> Translations.get_translation!(id) end) |> assign(:translation, Translations.get_translation!(id))
|> common_assigns() |> common_assigns()
{:ok, assign_new(socket, :translation_content, fn -> socket.assigns.translation.content end)} {:noreply, assign(socket, :translation_content, socket.assigns.translation.content)}
end end
defp get_article(article_id) do defp get_article(article_id) do