Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples of typing usage #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/simple_blog/converter/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule SimpleBlog.Converter.Page do
Module responsible for convert pages from eex to html
"""

$ type parsed() = {{:ok, string()}}
@doc """
Convert eex file to html

Expand All @@ -16,6 +17,7 @@ defmodule SimpleBlog.Converter.Page do
iex> SimpleBlog.Converter.Page.eex_to_html({:ok, "<%= post.title %>"}, post)
"post 1"
"""
$ (parsed(),[posts]) -> string() when posts: SimpleBlog.Post.t()
def eex_to_html({:ok, body}, posts) when is_list(posts) do
quoted = EEx.compile_string(body)

Expand All @@ -24,6 +26,7 @@ defmodule SimpleBlog.Converter.Page do
end
end

$ (parsed(), SimpleBlog.Post.t()) -> string()
def eex_to_html({:ok, body}, post) do
quoted = EEx.compile_string(body)

Expand Down
3 changes: 3 additions & 0 deletions lib/simple_blog/converter/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ defmodule SimpleBlog.Converter.Posts do
iex> SimpleBlog.Converter.Posts.markdown_to_html([])
[]
"""
$ list() -> list()
def markdown_to_html([]), do: []

$ [files] -> string() when files: string()
def markdown_to_html(files) when is_list(files) do
html =
for file <- files do
Expand All @@ -31,6 +33,7 @@ defmodule SimpleBlog.Converter.Posts do
html
end

$ string() -> string()
def markdown_to_html(file) do
{:ok, html_doc, []} = Earmark.as_html(file)
html_doc
Expand Down
4 changes: 4 additions & 0 deletions lib/simple_blog/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule SimpleBlog.Post do
iex> SimpleBlog.Post.generate_filename(%SimpleBlog.Post{ title: "My first blog post", date: ~D[2023-10-04]})
"2023-10-04-my-first-blog-post.md"
"""
$ SimpleBlog.Post.t() -> string()
def generate_filename(%SimpleBlog.Post{title: title, date: date}) do
normalized_title =
title
Expand All @@ -36,6 +37,7 @@ defmodule SimpleBlog.Post do
iex> SimpleBlog.Post.parse(body)
%SimpleBlog.Post{body: body, title: "Dev onboarding", date: "2023-10-25", filename: "2023-10-25-dev-onboarding.md"}
"""
$ string() -> SimpleBlog.Post.t()
def parse(body) do
[_, filename_line, title_line, date_line | _] = String.split(body, "\n")

Expand All @@ -54,6 +56,7 @@ defmodule SimpleBlog.Post do
iex> SimpleBlog.Post.generate_html_dir(%SimpleBlog.Post{date: "2023-10-04"}, "output")
"output/2023/10/04/"
"""
$ (SimpleBlog.Post.t(), string()) -> string()
def generate_html_dir(%SimpleBlog.Post{date: date}, base_dir) do
[year, month, day] = String.split(date, "-")
base_dir <> "/" <> year <> "/" <> month <> "/" <> day <> "/"
Expand All @@ -67,6 +70,7 @@ defmodule SimpleBlog.Post do
iex> SimpleBlog.Post.generate_html_filename(%SimpleBlog.Post{title: "doctests with elixir"})
"doctests-with-elixir.html"
"""
$ SimpleBlog.Post.t() -> string()
def generate_html_filename(%SimpleBlog.Post{title: title}) do
title
|> String.replace(" ", "-")
Expand Down
3 changes: 3 additions & 0 deletions lib/simple_blog/reader/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule SimpleBlog.Reader.Posts do
iex> SimpleBlog.Reader.Posts.read_from_dir("blog")
["## post title 1", "## post title 2"]
"""
$ string() -> list()
def read_from_dir(root_directory) do
posts_directory = root_directory <> "/_posts/"

Expand All @@ -28,6 +29,7 @@ defmodule SimpleBlog.Reader.Posts do
iex> SimpleBlog.Reader.Posts.read_post("blog", "2023-10-25-metaprogramming-in-ruby.md")
"### Metaprogramming in ruby"
"""
$ (string(), string()) -> string()
def read_post(root_directory, post) do
posts_directory = root_directory <> "/_posts/"
post_path = posts_directory <> post
Expand All @@ -40,6 +42,7 @@ defmodule SimpleBlog.Reader.Posts do

defp pipeline(_posts_directory, []), do: []

$ (string(), list()) -> list()
defp pipeline(posts_directory, files) when is_list(files) do
files
|> Enum.map(fn file -> full_path(file, posts_directory) end)
Expand Down
1 change: 1 addition & 0 deletions lib/simple_blog/rewrite_html/back_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule SimpleBlog.RewriteHTML.BackLink do
iex> SimpleBlog.RewriteHTML.BackLink.rewrite(link)
~s(<a href="../../../../index.html" class="back-link">Back</a>)
"""
$ string() -> string()
def rewrite(html) do
{:ok, document} = Floki.parse_document(html)

Expand Down
1 change: 1 addition & 0 deletions lib/simple_blog/rewrite_html/image.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule SimpleBlog.RewriteHTML.Image do
require Floki

$ string() -> string()
def rewrite(html, path) do
{:ok, document} = Floki.parse_document(html)

Expand Down
2 changes: 2 additions & 0 deletions lib/simple_blog/rewrite_html/posts_link.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule SimpleBlog.RewriteHTML.PostsLink do
require Floki

$ string() -> string()
def rewrite(html) do
{:ok, document} = Floki.parse_document(html)

Expand All @@ -11,6 +12,7 @@ defmodule SimpleBlog.RewriteHTML.PostsLink do
|> Floki.raw_html()
end

$ string() -> string()
defp filename(x) do
href =
String.split(x, "?post=")
Expand Down
1 change: 1 addition & 0 deletions lib/simple_blog/rewrite_html/stylesheet.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule SimpleBlog.RewriteHTML.Stylesheet do
require Floki

$ (string(), string()) -> string()
def rewrite(html, path) do
{:ok, document} = Floki.parse_document(html)

Expand Down
17 changes: 17 additions & 0 deletions lib/simple_blog/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ defmodule SimpleBlog.Server do
@doc """
Initializes server passing initial params
"""

$type opts() ::
string()
| tuple()
| atom()
| integer()
| float()
| [opts()]
| %{optional(opts()) => opts()}
| MapSet.t()

$ opts() -> opts()
def init(_options) do
Logger.info("Initializing server ...")
end

@doc """
Handle HTTP requests.
"""
# Verificar se é assim mesmo
$ (Plug.Conn.t(), opts()) -> Plug.Conn.t()
def call(%Plug.Conn{request_path: "/posts/", query_string: query_string} = conn, _opts) do
postname =
query_string
Expand All @@ -42,6 +56,7 @@ defmodule SimpleBlog.Server do
|> send_resp(200, result)
end

$ (Plug.Conn.t(), opts()) -> Plug.Conn.t()
def call(%Plug.Conn{request_path: "/"} = conn, _opts) do
posts =
"blog"
Expand All @@ -58,6 +73,7 @@ defmodule SimpleBlog.Server do
|> send_resp(200, result)
end

$ (Plug.Conn.t(), opts()) -> Plug.Conn.t()
def call(
%Plug.Conn{request_path: _request_path, req_headers: [{"accept", accept} | _]} = conn,
_opts
Expand All @@ -68,6 +84,7 @@ defmodule SimpleBlog.Server do
end
end

$ (Plug.Conn.t(), string()) -> Plug.Conn.t()
defp asset_pipeline(%Plug.Conn{request_path: request_path} = conn, content_type) do
Logger.info(request_path)

Expand Down
Loading