Brix.Gen (Brix v0.1.1)

Copy Markdown View Source

Shared helpers for Brix generator mix tasks.

Summary

Functions

Finds a template by name. Normalizes hyphens to underscores. Returns {:ok, template} or :error.

Finds the latest version directory in a page dir, by timestamp. Returns {version_name, version_dir} or nil.

Scans a directory for {NN}-* files/dirs and returns the next position number. Returns 1 if the directory is empty or doesn't exist.

Normalizes a slug: strips leading /, lowercases, validates characters, then prepends /.

Zero-pads a number to 2 digits.

Resolves the page directory for a given slug. Slug / maps to index/, others strip leading /.

Parses --content-dir from args, defaulting to priv/content. Returns {content_dir, parsed_opts, remaining_args}.

Resolves which version to use: explicit --version option, or latest. Returns {version_name, version_dir} or exits with error.

Generates placeholder field values from a template definition. Required strings get "TODO", optional get "", booleans get false, etc. Skips :sections type fields (those become .children/ directories).

Returns the field names in a template that have type :sections.

Converts a template name to a kebab-case filename segment. "reviews_section""reviews-section"

Returns a map of template name → %SectionTemplate{}.

Titleizes a slug segment: "my-page""My Page".

Generates YAML from an ordered list of {key, value} pairs. Handles strings, numbers, booleans, nil, empty maps, and one level of nested maps (for fields: blocks).

Runs Brix.Validator.validate/1, prints issues, returns :ok or :error.

Functions

find_template(templates_map, name)

Finds a template by name. Normalizes hyphens to underscores. Returns {:ok, template} or :error.

latest_version(page_dir)

Finds the latest version directory in a page dir, by timestamp. Returns {version_name, version_dir} or nil.

next_position(dir)

Scans a directory for {NN}-* files/dirs and returns the next position number. Returns 1 if the directory is empty or doesn't exist.

normalize_slug(slug)

Normalizes a slug: strips leading /, lowercases, validates characters, then prepends /.

pad_position(n)

Zero-pads a number to 2 digits.

page_dir(content_dir, slug)

Resolves the page directory for a given slug. Slug / maps to index/, others strip leading /.

parse_content_dir(args, extra_switches \\ [])

Parses --content-dir from args, defaulting to priv/content. Returns {content_dir, parsed_opts, remaining_args}.

extra_switches are merged with the base [content_dir: :string].

resolve_version(page_dir, opts)

Resolves which version to use: explicit --version option, or latest. Returns {version_name, version_dir} or exits with error.

scaffold_fields(section_template)

Generates placeholder field values from a template definition. Required strings get "TODO", optional get "", booleans get false, etc. Skips :sections type fields (those become .children/ directories).

sections_fields(section_template)

Returns the field names in a template that have type :sections.

template_to_filename(name)

Converts a template name to a kebab-case filename segment. "reviews_section""reviews-section"

templates_by_name(content_dir)

Returns a map of template name → %SectionTemplate{}.

titleize(slug)

Titleizes a slug segment: "my-page""My Page".

to_yaml(pairs)

Generates YAML from an ordered list of {key, value} pairs. Handles strings, numbers, booleans, nil, empty maps, and one level of nested maps (for fields: blocks).

validate_and_report(content_dir)

Runs Brix.Validator.validate/1, prints issues, returns :ok or :error.