Browse Source

initial commit

tags/microformats2-0.0.2
Christian Kruse 4 years ago
parent
commit
06920218ab
8 changed files with 175 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +20
    -0
      README.md
  3. +30
    -0
      config/config.exs
  4. +37
    -0
      lib/microformats2.ex
  5. +32
    -0
      mix.exs
  6. +2
    -0
      mix.lock
  7. +48
    -0
      test/microformats2_test.exs
  8. +1
    -0
      test/test_helper.exs

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
/_build
/cover
/deps
erl_crash.dump
*.ez

+ 20
- 0
README.md View File

@@ -0,0 +1,20 @@
# Microformats2

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:

1. Add microformats2 to your list of dependencies in `mix.exs`:

def deps do
[{:microformats2, "~> 0.0.1"}]
end

2. Ensure microformats2 is started before your application:

def application do
[applications: [:microformats2]]
end


+ 30
- 0
config/config.exs View File

@@ -0,0 +1,30 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.

# You can configure for your application as:
#
# config :microformats2, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:microformats2, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"

+ 37
- 0
lib/microformats2.ex View File

@@ -0,0 +1,37 @@
defmodule Microformats2 do
def parse(content) when is_bitstring(content) do
doc = Floki.parse(content)
rels = parse_rels(doc)

%{items: [], rels: rels[:rels], rel_urls: rels[:rel_urls]}
end

defp parse_rels(doc) do
link_rels = Floki.find(doc, "[rel][href]") |>
Enum.filter(fn(element) ->
rel = Floki.attribute(element, "rel") |> List.first
href = Floki.attribute(element, "href") |> List.first
String.strip(to_string(rel)) != "" and String.strip(to_string(href)) != ""
end) |>
Enum.reduce(%{rels: %{}, rel_urls: %{}}, fn(element, acc) ->
rel = Floki.attribute(element, "rel") |> List.first |> String.split(" ", trim: true)
url = Floki.attribute(element, "href") |> List.first # TODO convert to absolute URL

n_map = Enum.reduce(rel, acc, fn(rel, map) ->
if map[:rels][rel] == nil do
Map.put(map, :rels, Map.put(map[:rels], rel, [url]))
else
Map.put(map, :rels, Map.put(map[:rels], rel, Enum.uniq(map[:rels][rel] ++ [url])))
end
end)

if n_map[:rel_urls][url] == nil do
Map.put(n_map, :rel_urls, Map.put(n_map[:rel_urls], url, rel))
else
Map.put(n_map, :rel_urls, Map.put(n_map[:rel_urls], url, Enum.uniq(n_map[:rel_urls][url] ++ rel)))
end
end)

link_rels
end
end

+ 32
- 0
mix.exs View File

@@ -0,0 +1,32 @@
defmodule Microformats2.Mixfile do
use Mix.Project

def project do
[app: :microformats2,
version: "0.0.1",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger]]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[{:floki, "~> 0.7"}]
end
end

+ 2
- 0
mix.lock View File

@@ -0,0 +1,2 @@
%{"floki": {:hex, :floki, "0.7.1"},
"mochiweb": {:hex, :mochiweb, "2.12.2"}}

+ 48
- 0
test/microformats2_test.exs View File

@@ -0,0 +1,48 @@
defmodule Microformats2Test do
use ExUnit.Case
doctest Microformats2

test "parse successfully parses rels" do
assert(Microformats2.parse("<a rel=\"me\" href=\"http://blub\">blub</a>") ==
%{items: [],
rel_urls: %{"http://blub" => ["me"]},
rels: %{"me" => ["http://blub"]}})
end

test "parse successfully parses multiple rels" do
assert(Microformats2.parse("""
<a rel=\"me\" href=\"http://blub\">blub</a>
<a rel=\"me\" href=\"http://blah\">blub</a>
""") ==
%{items: [],
rel_urls: %{"http://blub" => ["me"],
"http://blah" => ["me"]},
rels: %{"me" => ["http://blub", "http://blah"]}})
end

test "parse only saves one URL" do
assert(Microformats2.parse("""
<a rel=\"me\" href=\"http://blub\">blub</a>
<a rel=\"me\" href=\"http://blub\">blub</a>
""") ==
%{items: [],
rel_urls: %{"http://blub" => ["me"]},
rels: %{"me" => ["http://blub"]}})
end

test "parse saves all rels" do
assert(Microformats2.parse("""
<a rel=\"me\" href=\"http://blub\">blub</a>
<a rel=\"moo\" href=\"http://blub\">blub</a>
""") ==
%{items: [],
rel_urls: %{"http://blub" => ["me", "moo"]},
rels: %{"me" => ["http://blub"],
"moo" => ["http://blub"]}})
end

test "parse generates an absolute URL" do
assert false, "TODO"
end

end

+ 1
- 0
test/test_helper.exs View File

@@ -0,0 +1 @@
ExUnit.start()

Loading…
Cancel
Save