serum v1.0.0 Serum.HeaderParser

This module takes care of parsing headers of page (or post) source files.

Header is where all page or post metadata goes into, and has the following format:

---
key: value
...
---

where --- in the first and last line delimits the beginning and the end of the header area, and between these two lines are one or more key-value pair delimited by a colon, where key is the name of a metadata and value is the actual value of a metadata.

Link to this section Summary

Functions

Reads lines from a binary data and extracts the header into a map.

Link to this section Types

Link to this type

options()
options() :: [{atom(), value_type()}]

Link to this type

parse_result()
parse_result() :: {:ok, {map(), binary()}} | {:invalid, binary()}

Link to this type

value()
value() :: binary() | integer() | [binary()] | [integer()]

Link to this type

value_type()
value_type() :: :string | :integer | :datetime | {:list, value_type()}

Link to this section Functions

Link to this function

parse_header(data, options, required \\ [])
parse_header(binary(), options(), [atom()]) :: parse_result()

Reads lines from a binary data and extracts the header into a map.

options is a keyword list which specifies the name and type of metadata the header parser expects. So the typical options should look like this:

[key1: type1, key2: type2, ...]

See "Types" section for avilable value types.

required argument is a list of required keys (in atom). If the header parser cannot find required keys in the header area, it returns an error.

Types

Currently the HeaderParser supports following types:

  • :string - A line of string. It can contain spaces.
  • :integer - A decimal integer.
  • :datetime - Date and time. Must be specified in the format of YYYY-MM-DD hh:mm:ss. This data will be interpreted as a local time.
  • {:list, <type>} - A list of multiple values separated by commas. Every value must have the same type, either :string, :integer, or :datetime. You cannot make a list of lists.