Skip to content

Commit

Permalink
Fix order of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusalonso committed Jul 12, 2024
1 parent eeebc50 commit 77a266f
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 31 deletions.
2 changes: 1 addition & 1 deletion blog/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</li>
<%= for post <- posts do %>
<li>
<a class="post-link" href="/posts/?post=<%= post.filename %>">
<a href="/posts/?post=<%= post.filename %>" class="post-link" >
<h2 class="post-title"><%= post.title %></h2>
</a>
<div class="post-meta">
Expand Down
2 changes: 1 addition & 1 deletion blog/post.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<section class="content">
<div class="post-container">
<a href="/" class="back-link">Back</a>
<a class="post-link" href="#">
<a href="#" class="post-link" >
<h2 class="post-title"><%= post.title %></h2>
</a>
<div class="post-meta">
Expand Down
11 changes: 4 additions & 7 deletions lib/simple_blog/rewrite_html/back_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ defmodule SimpleBlog.RewriteHTML.BackLink do
iex> link = ~s(<a href="/" class="back-link">Back</a>)
iex> SimpleBlog.RewriteHTML.BackLink.rewrite(link)
~s(<a href="../../../../index.html">Back</a>)
~s(<a href="../../../../index.html" class="back-link">Back</a>)
"""
def rewrite(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
Floki.find_and_update(document, "a.back-link", fn element ->
{"a", [{"href", "/"} | attrs]} = element
{"a", [{"href", "../../../../index.html"} | attrs]}
end)
|> Floki.raw_html()
end
Expand Down
10 changes: 3 additions & 7 deletions lib/simple_blog/rewrite_html/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ defmodule SimpleBlog.RewriteHTML.Image do
def rewrite(html, path) do
{:ok, document} = Floki.parse_document(html)

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

{"img", [{"src", src}, {"alt", alt}, {"class", _class}, {"title", _t}]} ->
{"img", [{"src", String.replace(src, "/", path, global: false)}, {"alt", alt}]}
Floki.find_and_update(document, "img", fn element ->
{"img", [{"src", src} | attrs]} = element
{"img", [{"src", String.replace(src, "/", path, global: false)} | attrs]}
end)
|> Floki.raw_html()
end
Expand Down
24 changes: 16 additions & 8 deletions lib/simple_blog/rewrite_html/posts_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ defmodule SimpleBlog.RewriteHTML.PostsLink do
def rewrite(html) do
{:ok, document} = Floki.parse_document(html)

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

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

{"a", [{"href", "posts/#{year}/#{month}/#{day}/#{filename}.html"}]}
Floki.find_and_update(document, "a.post-link", fn element ->
{"a", [{"href", x} | attrs]} = element
{"a", [{"href", filename(x)} | attrs]}
end)
|> Floki.raw_html()
end

defp filename(x) do
href =
String.split(x, "?post=")
|> List.last()
|> String.split(".md")
|> List.first()

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

"posts/#{year}/#{month}/#{day}/#{filename}.html"
end
end
2 changes: 1 addition & 1 deletion test/blog/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</li>
<%= for post <- posts do %>
<li>
<a class="post-link" href="/posts/?post=<%= post.filename %>">
<a href="/posts/?post=<%= post.filename %>" class="post-link">
<h2 class="post-title"><%= post.title %></h2>
</a>
<div class="post-meta">
Expand Down
2 changes: 1 addition & 1 deletion test/blog/post.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<section class="content">
<div class="post-container">
<a href="/" class="back-link">Back</a>
<a class="post-link" href="#">
<a href="#" class="post-link">
<h2 class="post-title"><%= post.title %></h2>
</a>
<div class="post-meta">
Expand Down
2 changes: 1 addition & 1 deletion test/simple_blog/rewrite_html/back_link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule SimpleBlog.RewriteHTML.BackLinkTest do
test "rewrites link href attribute" do
link = ~s(<a href="/" class="back-link">Back</a>)
result = SimpleBlog.RewriteHTML.BackLink.rewrite(link)
assert result == ~s(<a href="../../../../index.html">Back</a>)
assert result == ~s(<a href="../../../../index.html" class="back-link">Back</a>)
end
end
end
6 changes: 4 additions & 2 deletions test/simple_blog/rewrite_html/image_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ defmodule SimpleBlog.RewriteHTML.ImageTest do

describe "rewrite" do
test "rewrites image src attribute" do
image = ~s(<img src="/images/img_girl.jpg" alt="Girl in a jacket" class="img-circle" />)
image =
~s(<img src="/images/img_girl.jpg" alt="Girl in a jacket" title="Title" class="img-circle"/>)

path = "./"
result = SimpleBlog.RewriteHTML.Image.rewrite(image, path)

assert result ==
~s(<img src="./images/img_girl.jpg" alt="Girl in a jacket" class="img-circle"/>)
~s(<img src="./images/img_girl.jpg" alt="Girl in a jacket" title="Title" class="img-circle"/>)
end
end
end
6 changes: 4 additions & 2 deletions test/simple_blog/rewrite_html/posts_link_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ defmodule SimpleBlog.RewriteHTML.PostsLinkTest do

describe "rewrite" do
test "rewrites link to post" do
link = ~s(<a class="post-link" href="/posts/?post=2024-05-22-hello-world.md">My post</a>)
link = ~s(<a href="/posts/?post=2024-05-22-hello-world.md" class="post-link">My post</a>)
result = SimpleBlog.RewriteHTML.PostsLink.rewrite(link)
assert result == ~s(<a href="posts/2024/05/22/hello-world.html">My post</a>)

assert result ==
~s(<a href="posts/2024/05/22/hello-world.html" class="post-link">My post</a>)
end
end
end

0 comments on commit 77a266f

Please sign in to comment.