diff --git a/blog/post.html.eex b/blog/post.html.eex index 073459e..4c6bd1a 100644 --- a/blog/post.html.eex +++ b/blog/post.html.eex @@ -22,7 +22,7 @@
- Back + Back

<%= post.title %>

diff --git a/blog_test/post.html.eex b/blog_test/post.html.eex index 073459e..4c6bd1a 100644 --- a/blog_test/post.html.eex +++ b/blog_test/post.html.eex @@ -22,7 +22,7 @@
- Back + Back

<%= post.title %>

diff --git a/lib/mix/tasks/simple_blog/compile.ex b/lib/mix/tasks/simple_blog/compile.ex index cb3138d..ba13330 100644 --- a/lib/mix/tasks/simple_blog/compile.ex +++ b/lib/mix/tasks/simple_blog/compile.ex @@ -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 @@ -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") @@ -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(), - ~s() - ) - |> String.replace( - ~s(), - ~s() - ) - |> String.replace( - ~s(), - ~s() - ) - end + defp rewrite_stylesheets(html, path) do + {:ok, document} = Floki.parse_document(html) - defp rewrite_stylesheets_post(html) do - html - |> String.replace( - ~s(), - ~s() - ) - |> String.replace( - ~s(), - ~s() - ) - |> String.replace( - ~s(), - ~s() - ) - 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(avatar), - ~s(avatar) - ) + other -> + other + end) + |> Floki.raw_html() end - defp rewrite_images_post(html) do - html - |> String.replace( - ~s(avatar), - ~s(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(Back), - ~s(Back) - ) + 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() + + <> = href - <> = href + {"a", [{"href", "posts/#{year}/#{month}/#{day}/#{filename}.html"}]} - ~s() - else - line - end + other -> + other + end) + |> Floki.raw_html() end defp write_html_posts(root_directory, output_directory, posts) do @@ -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) diff --git a/mix.exs b/mix.exs index f1ab2a6..d73c2e0 100644 --- a/mix.exs +++ b/mix.exs @@ -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"} ] diff --git a/mix.lock b/mix.lock index 21a2e6f..6eec37e 100644 --- a/mix.lock +++ b/mix.lock @@ -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"},