Browse Source

Add :atomize_keys config option that allows disabling to_atom on user input

It's not a good idea to turn arbitrary HTML classes into atoms:
https://engineering.klarna.com/monitoring-erlang-atoms-c1d6a741328e
pull/5/head
Greg V 1 year ago
parent
commit
dca67c6e95
2 changed files with 18 additions and 12 deletions
  1. +9
    -6
      lib/implied.ex
  2. +9
    -6
      lib/items.ex

+ 9
- 6
lib/implied.ex View File

@@ -6,7 +6,8 @@ defmodule Microformats2.Items.ImpliedProperties do
end

defp implied_url_property(entry, root, doc_url, doc) do
if entry[:properties][:url] == nil do
url_key = if Application.get_env(:microformats2, :atomize_keys, true), do: :url, else: "url"
if entry[:properties][url_key] == nil do
val = implied_url_attrval(root)

url =
@@ -20,7 +21,7 @@ defmodule Microformats2.Items.ImpliedProperties do
if Microformats2.blank?(url) do
entry
else
Map.put(entry, :properties, Map.put(entry[:properties], :url, [Microformats2.abs_uri(url, doc_url, doc)]))
Map.put(entry, :properties, Map.put(entry[:properties], url_key, [Microformats2.abs_uri(url, doc_url, doc)]))
end
else
entry
@@ -28,7 +29,8 @@ defmodule Microformats2.Items.ImpliedProperties do
end

defp implied_photo_property(entry, root) do
if entry[:properties][:photo] == nil do
photo_key = if Application.get_env(:microformats2, :atomize_keys, true), do: :photo, else: "photo"
if entry[:properties][photo_key] == nil do
val = implied_photo_attrval(root)

url =
@@ -42,7 +44,7 @@ defmodule Microformats2.Items.ImpliedProperties do
if Microformats2.blank?(url) do
entry
else
Map.put(entry, :properties, Map.put(entry[:properties], :photo, [url]))
Map.put(entry, :properties, Map.put(entry[:properties], photo_key, [url]))
end
else
entry
@@ -50,7 +52,8 @@ defmodule Microformats2.Items.ImpliedProperties do
end

defp implied_name_property(entry, root = {elem, _, _}) do
if entry[:properties][:name] == nil do
name_key = if Application.get_env(:microformats2, :atomize_keys, true), do: :name, else: "name"
if entry[:properties][name_key] == nil do
nam =
cond do
elem == "img" or elem == "area" ->
@@ -70,7 +73,7 @@ defmodule Microformats2.Items.ImpliedProperties do
end
|> Microformats2.stripped_or_nil()

Map.put(entry, :properties, Map.put(entry[:properties], :name, [nam]))
Map.put(entry, :properties, Map.put(entry[:properties], name_key, [nam]))
else
entry
end


+ 9
- 6
lib/items.ex View File

@@ -132,14 +132,16 @@ defmodule Microformats2.Items do
defp parse_prop(_, _, _, _), do: nil

defp get_value(class, p) do
name_key = if Application.get_env(:microformats2, :atomize_keys, true), do: :name, else: "name"
url_key = if Application.get_env(:microformats2, :atomize_keys, true), do: :url, else: "url"
cond do
Microformats2.is_a?(class, "p") and p[:properties][:name] != nil ->
List.first(p[:properties][:name])
Microformats2.is_a?(class, "p") and p[:properties][name_key] != nil ->
List.first(p[:properties][name_key])

Microformats2.is_a?(class, "u") and p[:properties][:url] != nil ->
List.first(p[:properties][:url])
Microformats2.is_a?(class, "u") and p[:properties][url_key] != nil ->
List.first(p[:properties][url_key])

# and p[:properties][:url] != nil ->
# and p[:properties][url_key] != nil ->
Microformats2.is_a?(class, "e") ->
# TODO handle
nil
@@ -160,7 +162,8 @@ defmodule Microformats2.Items do
parse_prop(class, child, doc, url)
end

key = strip_prefix(class) |> to_key |> String.to_atom()
key = strip_prefix(class) |> to_key
key = if Application.get_env(:microformats2, :atomize_keys, true), do: String.to_atom(key), else: key
val = if acc[key] != nil, do: acc[key], else: []
Map.put(acc, key, val ++ [prop])
end)


Loading…
Cancel
Save