blogit v1.2.3 Blogit.Models.Post View Source

A module holding the representation of a post in a blog and helper functions for it.

The Blogit.Models.Post struct 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 struct.

Usually a post is created by invoking the Blogit.Models.Post.from_file/2 function. This function takes a module implementing the Blogit.RepositoryProvider behaviour for access to a repository, a file path and a Blogit.RepositoryProvider struct. It uses them to read the file and generate the Bogit.Models.Post struct.

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

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

Link to this section Summary

Functions

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

Returns a map of maps. For every supported language, returned by invoking Blogit.Settings.languages/0, the result of a call to this function will have a key, representing it. The value for every such key will be a map with keys the names of the posts created from parsing the files in the directory representing the language in the given list of paths and values the post structs created.

Creates a Blogit.Models.Post struct from a file stored in a repository.

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

Link to this section Types

Link to this type

from_file_result()

View Source
from_file_result() :: t() | :source_file_not_found
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 that month and that year.

The tuples 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(String.t()) => %{optional(atom()) => t()}
}

Returns a map of maps. For every supported language, returned by invoking Blogit.Settings.languages/0, the result of a call to this function will have a key, representing it. The value for every such key will be a map with keys the names of the posts created from parsing the files in the directory representing the language in the given list of paths and values the post structs created.

Uses from_file/2 to parse the files and create the Blogit.Models.Post structs.

Skips all the non-markdown files as well as the ones that mark the post content as not published or does not exist in the given repository.

Link to this function

from_file(file_path, repository, language)

View Source

Creates a Blogit.Models.Post struct from a file stored in a repository.

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

The given file_path should be located in the given repository.

The result struct will have meta created by invoking Blogit.Models.Post.Meta.from_file/5 with the given file_path, repository and language, the name of the post and the markdown data read from the file.

If the source file located at the given file_path doesn't exist, the atom :source_file_not_found is returned as a result.

If the meta data of the post to be created has published: false, post.html won't be generated and will be nil.

Link to this function

names_from_files(files)

View Source
names_from_files([String.t()]) :: [String.t()]

Retrieves unique 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"])
...> |> Enum.map(&(elem(&1, 1)))
[:somefile, :another]

iex> Blogit.Models.Post.names_from_files(["one/two/name.md"])
...> |> Enum.map(&(elem(&1, 1)))
[:one_two_name]