Skip to content

Instantly share code, notes, and snippets.

@manuel-rubio
Created May 11, 2022 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuel-rubio/dab3b530a944ed7c02c8a86d9855e5f6 to your computer and use it in GitHub Desktop.
Save manuel-rubio/dab3b530a944ed7c02c8a86d9855e5f6 to your computer and use it in GitHub Desktop.
Categories extension for lambdapad
@doc """
A set of transformations to help us work with categories.
This extension requires the following configuration entries:
[categories.desarrollo]
class = "cat-1"
link = "category/desarrollo"
[categories.historias]
class = "cat-2"
link = "category/historias"
There are two different transformations in this file:
- categories: it generates a map where the key is the category and the content is the number of posts in addition of the configuration options. You can use this in pages or widgets with transform_config.
- category class: sets the category_class key based on the configuration and what's defined in the category field for each post. You can use this in pages or widgets with transforma_item.
"""
transform "categories" do
set on: :config
set run: fn(config, posts) ->
num_posts = Enum.reduce(posts, %{}, & Map.update(&2, &1["category"], 1, fn i -> i + 1 end))
categories =
for {name, data} <- config["categories"], into: %{} do
{name, Map.put(data, "num_posts", num_posts[name])}
end
|> Enum.sort_by(fn {_, data} -> data["class"] || "000" end)
Map.put(config, "_categories", categories)
end
end
transform "category class" do
set on: :item
set run: fn(post, config) ->
category_name = post["category"]
class = config["categories"][category_name]["class"] || ""
Map.put(post, "category_class", class)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment