Add importing html and save it to Article
Additionally defines a wizard logic which is partially unused yet.
This commit is contained in:
@ -2,11 +2,12 @@ defmodule Outlook.Articles.Article do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Outlook.Articles.InternalTree
|
||||
alias Outlook.Authors.Author
|
||||
alias Outlook.Translations.Translation
|
||||
|
||||
schema "articles" do
|
||||
field :content, :string
|
||||
field :content, InternalTree
|
||||
field :date, :utc_datetime
|
||||
field :language, :string
|
||||
field :title, :string
|
||||
@ -20,7 +21,7 @@ defmodule Outlook.Articles.Article do
|
||||
@doc false
|
||||
def changeset(article, attrs) do
|
||||
article
|
||||
|> cast(attrs, [:title, :content, :url, :language, :date])
|
||||
|> validate_required([:title, :content, :url, :language, :date])
|
||||
|> cast(attrs, [:title, :content, :url, :language, :date, :author_id])
|
||||
|> validate_required([:title, :content, :url, :language, :date, :author_id])
|
||||
end
|
||||
end
|
||||
|
||||
52
lib/outlook/articles/internal_tree.ex
Normal file
52
lib/outlook/articles/internal_tree.ex
Normal file
@ -0,0 +1,52 @@
|
||||
defmodule Outlook.Articles.InternalTree do
|
||||
use Ecto.Type
|
||||
|
||||
alias Outlook.InternalTree.InternalNode
|
||||
alias Outlook.InternalTree.TranslationUnit
|
||||
|
||||
def type, do: :string
|
||||
|
||||
def cast(tree) when is_list(tree) do
|
||||
{:ok, tree}
|
||||
end
|
||||
|
||||
def cast(_), do: :error
|
||||
|
||||
def load(tree) when is_binary(tree) do
|
||||
{:ok, Jason.decode!(tree, keys: :atoms!) |> from_json}
|
||||
end
|
||||
|
||||
def dump(tree) when is_list(tree), do: {:ok, Jason.encode!(tree)}
|
||||
def dump(_), do: :error
|
||||
|
||||
|
||||
defp from_json([%{status: _} = node | rest]) do
|
||||
[ %TranslationUnit{
|
||||
status: String.to_atom(node.status),
|
||||
uuid: node.uuid,
|
||||
content: node.content
|
||||
} | from_json(rest) ]
|
||||
end
|
||||
|
||||
defp from_json([%{type: "element"} = node | rest]) do
|
||||
[ %InternalNode{
|
||||
name: node.name,
|
||||
attributes: node.attributes,
|
||||
type: String.to_atom(node.type),
|
||||
uuid: node.uuid,
|
||||
content: from_json(node.content)
|
||||
} | from_json(rest) ]
|
||||
end
|
||||
|
||||
defp from_json([%{type: _} = node | rest]) do
|
||||
[ %InternalNode{
|
||||
name: node.name,
|
||||
attributes: node.attributes,
|
||||
type: String.to_atom(node.type),
|
||||
uuid: node.uuid,
|
||||
content: node.content
|
||||
} | from_json(rest) ]
|
||||
end
|
||||
|
||||
defp from_json([]), do: []
|
||||
end
|
||||
Reference in New Issue
Block a user