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
t() :: %Blogit.Models.Post{html: String.t, meta: Blogit.Models.Post.Meta.t, name: String.t, raw: String.t}
year_month_count_result() :: {pos_integer, 1..12, non_neg_integer}
Link to this section Functions
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}]
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/
from_file(String.t, Blogit.RepositoryProvider.t) :: t
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.
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"]
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"]