blogit v1.0.0 Blogit.Models.Post View Source

Represents a post in a blog.

Contains an unique name of the post, which identifies it, the raw content of the post in markdown, html version of the post content and meta information.

The meta information is contained in a Blogit.Models.Post.Meta structure.

Usualy a post is created by invokin the Blogit.Models.Post.from_file/2 function. This function takes a provider for access to repository, a file path and a repository. It uses them to read the file and generate the Post structure.

The Blogit.Models.Post.compile_posts/2 function is able to create a list of multiple Post structures using a list of files and repository.

The module contains a set of utility methods for working with Blogit.Models.Post structures.

Link to this section Summary

Functions

Calculates a list of tuples of three elements from the given list of posts

Creates a map with keys the names of the posts created from parsing the files at the given list of paths and values the posts structures created

Creates a Post structure from a file stored in a repository

Retrieves uniqie names, which can be used as names of posts, from a list of file names

Sorts a list of Post structures by the given meta field

Link to this section Types

Link to this type t() View Source
t() :: %Blogit.Models.Post{html: String.t, meta: Blogit.Models.Post.Meta.t, name: String.t, raw: String.t}
Link to this type year_month_count_result() View Source
year_month_count_result() :: {pos_integer, 1..12, non_neg_integer}

Link to this section Functions

Link to this function collect_by_year_and_month(posts) View Source
collect_by_year_and_month([t]) :: [year_month_count_result]

Calculates a list of tuples of three elements from the given list of posts.

The first element of a tuple is a year. The second is a month number. The third is a counter - how many posts are created during the month and the year.

The tupes are sorted from the newest to the oldest, using the years and the months.

Examples

iex> alias Blogit.Models.Post.Meta
iex> posts = [
...>   %Blogit.Models.Post{
...>     name: "", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2016-04-14 22:23:12]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-04-22 14:53:45]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-02-01 07:42:56]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-04-20 12:23:12]}
...>   }
...> ]
iex> Blogit.Models.Post.collect_by_year_and_month(posts)
[{2017, 4, 2}, {2017, 2, 1}, {2016, 4, 1}]
Link to this function compile_posts(list, repository) View Source
compile_posts([String.t], Blogit.RepositoryProvider.t) :: %{optional(atom) => t}

Creates a map with keys the names of the posts created from parsing the files at the given list of paths and values the posts structures created.

Uses from_file/2 to parse the files and create the Post structures.

Skips all the non-markdown files as well as the ones located in the folders slides/ and pages/

Link to this function from_file(file_path, repository) View Source

Creates a Post structure from a file stored in a repository.

The name of the file is used as the name identificator of the post. For example the Post structure created from the file some_post.md will have post.name == "some_post".

The given file path should be located in the given repository.

Link to this function names_from_files(files) View Source
names_from_files([String.t]) :: [String.t]

Retrieves uniqie names, which can be used as names of posts, from a list of file names.

Examples

iex> Blogit.Models.Post.names_from_files(["SomeFile.md", "another.md"])
["somefile", "another"]

iex> Blogit.Models.Post.names_from_files(["one/two/name.md"])
["one_two_name"]
Link to this function sorted(posts, meta_field \\ :created_at) View Source
sorted([t], atom) :: [t]

Sorts a list of Post structures by the given meta field.

By default this field is created_at. Note that the sort is descending.

Examples

iex> alias Blogit.Models.Post.Meta
iex> posts = [
...>   %Blogit.Models.Post{
...>     name: "first", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-02-14 22:23:12]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "newest", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-04-22 14:53:45]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "very old", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-03-01 07:42:56]}
...>   },
...>   %Blogit.Models.Post{
...>     name: "last", raw: "", html: "",
...>     meta: %Meta{created_at: ~N[2017-04-20 12:23:12]}
...>   }
...> ]
iex> sorted = Blogit.Models.Post.sorted(posts)
iex> sorted |> Enum.map(fn (post) -> post.name end)
["newest", "last", "very old", "first"]