Skip to content

Commit

Permalink
Merge pull request #10 from viniciusalonso/floki
Browse files Browse the repository at this point in the history
Use Floki to parse html documents
  • Loading branch information
viniciusalonso authored Oct 30, 2023
2 parents ba2e3af + ed0ecaf commit 0344871
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 70 deletions.
2 changes: 1 addition & 1 deletion blog/post.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</section>
<section class="content">
<div class="post-container">
<a href="/">Back</a>
<a href="/" class="back-link">Back</a>
<a class="post-link" href="#">
<h2 class="post-title"><%= post.title %></h2>
</a>
Expand Down
2 changes: 1 addition & 1 deletion blog_test/post.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</section>
<section class="content">
<div class="post-container">
<a href="/">Back</a>
<a href="/" class="back-link">Back</a>
<a class="post-link" href="#">
<h2 class="post-title"><%= post.title %></h2>
</a>
Expand Down
120 changes: 53 additions & 67 deletions lib/mix/tasks/simple_blog/compile.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Mix.Tasks.SimpleBlog.Compile do
use Mix.Task
require Logger
require Floki

@moduledoc """
Command responsible for transpile markdown into html
Expand All @@ -26,16 +27,13 @@ defmodule Mix.Tasks.SimpleBlog.Compile do
index_html =
File.read(root_directory <> "/index.html.eex")
|> SimpleBlog.Converter.Page.exx_to_html(posts)
|> rewrite_stylesheets()
|> rewrite_images()
|> rewrite_stylesheets("./")
|> rewrite_images("./")
|> rewrite_post_links()

File.mkdir(output_directory)
{:ok, file} = File.open(output_directory <> "/index.html", [:write])

index_html
|> String.split("\n")
|> Enum.each(fn line -> IO.binwrite(file, rewrite_post_links(line) <> "\n") end)

IO.binwrite(file, index_html)
File.close(file)

File.cp_r(root_directory <> "/css", output_directory <> "/css")
Expand All @@ -44,73 +42,61 @@ defmodule Mix.Tasks.SimpleBlog.Compile do
write_html_posts(root_directory, output_directory, posts)
end

defp rewrite_stylesheets(html) do
html
|> String.replace(
~s(<link rel="stylesheet" href="/css/_solarized-light.css">),
~s(<link rel="stylesheet" href="./css/_solarized-light.css">)
)
|> String.replace(
~s(<link rel="stylesheet" href="/css/plain.css">),
~s(<link rel="stylesheet" href="./css/plain.css">)
)
|> String.replace(
~s(<link rel="stylesheet" href="/css/style.css">),
~s(<link rel="stylesheet" href="./css/style.css">)
)
end
defp rewrite_stylesheets(html, path) do
{:ok, document} = Floki.parse_document(html)

defp rewrite_stylesheets_post(html) do
html
|> String.replace(
~s(<link rel="stylesheet" href="/css/_solarized-light.css">),
~s(<link rel="stylesheet" href="../../../../css/_solarized-light.css">)
)
|> String.replace(
~s(<link rel="stylesheet" href="/css/plain.css">),
~s(<link rel="stylesheet" href="../../../../css/plain.css">)
)
|> String.replace(
~s(<link rel="stylesheet" href="/css/style.css">),
~s(<link rel="stylesheet" href="../../../../css/style.css">)
)
end
Floki.find_and_update(document, "link", fn
{"link", [{"href", href}]} ->
{"link", [{"href", String.replace(href, "/", path)}]}

defp rewrite_images(html) do
html
|> String.replace(
~s(<img src="/images/avatar.png" alt="avatar" class="avatar">),
~s(<img src="./images/avatar.png" alt="avatar" class="avatar">)
)
other ->
other
end)
|> Floki.raw_html()
end

defp rewrite_images_post(html) do
html
|> String.replace(
~s(<img src="/images/avatar.png" alt="avatar" class="avatar">),
~s(<img src="../../../../images/avatar.png" alt="avatar" class="avatar">)
)
defp rewrite_images(html, path) do
{:ok, document} = Floki.parse_document(html)

Floki.find_and_update(document, "img", fn
{"img", [{"src", src}]} ->
{"img", [{"src", String.replace(src, "/", path)}]}

other ->
other
end)
|> Floki.raw_html()
end

defp rewrite_back_link_post(html) do
html
|> String.replace(
~s(<a href="/">Back</a>),
~s(<a href="../../../../index.html">Back</a>)
)
defp rewrite_back_link(html) do
{:ok, document} = Floki.parse_document(html)

Floki.find_and_update(document, "a.back-link", fn
{"a", [{"href", "/"}, {"class", "back-link"}]} ->
{"a", [{"href", "../../../../index.html"}]}

other ->
other
end)
|> Floki.raw_html()
end

defp rewrite_post_links(line) do
if String.contains?(line, "post-link") do
href = String.split(line, "?post=") |> List.last() |> String.split(".md") |> List.first()
defp rewrite_post_links(html) do
{:ok, document} = Floki.parse_document(html)

Floki.find_and_update(document, "a.post-link", fn
{"a", [{"class", _}, {"href", href}]} ->
href = String.split(href, "?post=") |> List.last() |> String.split(".md") |> List.first()

<<year::binary-size(4), _, month::binary-size(2), _, day::binary-size(2), _,
filename::binary>> = href

<<year::binary-size(4), _, month::binary-size(2), _, day::binary-size(2), _,
filename::binary>> = href
{"a", [{"href", "posts/#{year}/#{month}/#{day}/#{filename}.html"}]}

~s(<a class="post-link" href="posts/#{year}/#{month}/#{day}/#{filename}.html">)
else
line
end
other ->
other
end)
|> Floki.raw_html()
end

defp write_html_posts(root_directory, output_directory, posts) do
Expand Down Expand Up @@ -141,9 +127,9 @@ defmodule Mix.Tasks.SimpleBlog.Compile do
result =
File.read(root_directory <> "/post.html.eex")
|> SimpleBlog.Converter.Page.exx_to_html(post)
|> rewrite_stylesheets_post()
|> rewrite_images_post()
|> rewrite_back_link_post()
|> rewrite_stylesheets("../../../../")
|> rewrite_images("../../../../")
|> rewrite_back_link()

{:ok, file} = File.open(dir <> filename, [:write])
IO.binwrite(file, result)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule SimpleBlog.MixProject do
[
{:plug_cowboy, "~> 2.0"},
{:earmark, "~> 1.4"},
{:ex_doc, "~> 0.27", only: :dev, runtime: false}
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
{:floki, "~> 0.35.0"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"earmark": {:hex, :earmark, "1.4.46", "8c7287bd3137e99d26ae4643e5b7ef2129a260e3dcf41f251750cb4563c8fb81", [:mix], [], "hexpm", "798d86db3d79964e759ddc0c077d5eb254968ed426399fbf5a62de2b5ff8910a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.37", "2ad73550e27c8946648b06905a57e4d454e4d7229c2dafa72a0348c99d8be5f7", [:mix], [], "hexpm", "6b19783f2802f039806f375610faa22da130b8edc21209d0bff47918bb48360e"},
"ex_doc": {:hex, :ex_doc, "0.30.9", "d691453495c47434c0f2052b08dd91cc32bc4e1a218f86884563448ee2502dd2", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "d7aaaf21e95dc5cddabf89063327e96867d00013963eadf2c6ad135506a8bc10"},
"floki": {:hex, :floki, "0.35.2", "87f8c75ed8654b9635b311774308b2760b47e9a579dabf2e4d5f1e1d42c39e0b", [:mix], [], "hexpm", "6b05289a8e9eac475f644f09c2e4ba7e19201fd002b89c28c1293e7bd16773d9"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
Expand Down

0 comments on commit 0344871

Please sign in to comment.