Preface
The library contains various functions that can be used in site.nix and templates.
This file is generated with a nix expression.
Library functions are grouped in namespaces, but are also all included in the top-level scope to be easily accessible.
So for example, lib.utils.chunksOf can be shortened to lib.chunksOf.
1. conf
2. data
The data namespace contains functions to fetch and manipulate data.
groupBy
Example
groupBy [
{ type = "fruit"; name = "apple"; }
{ type = "fruit"; name = "pear"; }
{ type = "vegetable"; name = "lettuce"; }
]
(s: s.type)
[ {
fruit = [ {
name = "apple";
type = "fruit";
} {
name = "pear";
type = "fruit";
} ];
} {
vegetable = [ {
name = "lettuce";
type = "vegetable";
} ];
} ]
loadDir
mkTaxonomyData
Example
mkTaxonomyData {
data = [
{ tags = [ "foo" "bar" ]; path = "/a.html"; }
{ tags = [ "foo" ]; path = "/b.html"; }
{ category = [ "baz" ]; path = "/c.html"; }
];
taxonomies = [ "tags" "category" ];
}
[ {
category = [ {
baz = [ {
category = [ "baz" ];
path = "/c.html";
} ];
} ];
} {
tags = [ {
foo = [ {
path = "/b.html";
tags = [ "foo" ];
} {
path = "/a.html";
tags = [ "foo" "bar" ];
} ];
} {
bar = [ {
path = "/a.html";
tags = [ "foo" "bar" ];
} ];
} ];
} ]
3. generation
localesToPageList
Arguments (Attribute Set)
Example
pagelist = localesToPageList {
inherit locales;
default = locale: {
layout = locale.env.templates.layout;
};
};
localesToPageList {
locales = {
eng = rec {
code = "eng";
prefix = "/${code}";
pages = {
foo = { path = "/foo.html"; };
bar = [ { path = "/bar-1.html"; } { path = "/bar-2.html"; } ];
};
};
fre = rec {
code = "fre";
prefix = "/${code}";
pages = {
foo = { path = prefix + "/foo.html"; };
bar = [ { path = prefix + "/bar-1.html"; } { path = prefix + "/bar-2.html"; } ];
};
};
};
default = locale: {
baz = "${locale.code}-baz";
};
}
[ {
baz = "eng-baz";
path = "/foo.html";
} {
baz = "eng-baz";
path = "/bar-1.html";
} {
baz = "eng-baz";
path = "/bar-2.html";
} {
baz = "fre-baz";
path = "/fre/foo.html";
} {
baz = "fre-baz";
path = "/fre/bar-1.html";
} {
baz = "fre-baz";
path = "/fre/bar-2.html";
} ]
mkSite
Arguments (Attribute Set)
files
A list of static files directories to copy in the site.
Type: [ Path ].
Optional, defaults to [ ].
genPageFn
Function to generate a page source from a page attribute set.
Type: Page → String.
Optional, defaults to {
_type = "literalExpression";
text = "lib.generation.generatePage";
}.
pagePathFn
Function to generate a page from a page attribute set.
Type: Page → String.
Optional, defaults to {
_type = "literalExpression";
text = "page: page.path";
}.
postGen
A set of command to execute after generating the site.
Type: String.
Optional, defaults to "".
pagesToList
Arguments (Attribute Set)
Example
pagelist = pagestolist {
inherit pages;
default.layout = templates.layout;
};
pagesToList {
pages = {
foo = { path = "/foo.html"; };
bar = [ { path = "/bar-1.html"; } { path = "/bar-2.html"; } ];
};
default = {
baz = "baz";
};
}
[ {
baz = "baz";
path = "/foo.html";
} {
baz = "baz";
path = "/bar-1.html";
} {
baz = "baz";
path = "/bar-2.html";
} ]
4. pages
mkMultipages
Arguments (Attribute Set)
basePath
String used by pathFn to generate the page path. Used in pageFn default, ignored if pageFn is set.
Type: String.
Optional, defaults to null.
mkPageList
Arguments (Attribute Set)
multipageFn
Function to generate extra attributes of mutipages.
Type: Int → Data → Attrs.
Optional, defaults to {
_type = "literalExpression";
text = "index: data: {
path = mkSplitPagePath { pre = \"${pathPrefix}${data.fileData.basename}\"; inherit index; };
}
";
}.
mkPages
mkSplit
Arguments (Attribute Set)
Return
List of pages. Each page has:
-
items: List of the page data items. -
pages: List of splitted pages.
Example
pages.archives = mkSplit {
basePath = "/archives";
itemsPerPage = 10;
data = pages.posts;
template = templates.archives;
};
mkSplit {
data = map (x: { id = x; }) (range 1 4);
itemsPerPage = 2;
basePath = "/test";
}
[ {
index = 1;
items = [ {
id = 1;
} {
id = 2;
} ];
pages = {
_type = "literalExpression";
text = "[ ... ]";
};
path = "/test.html";
} {
index = 2;
items = [ {
id = 3;
} {
id = 4;
} ];
pages = {
_type = "literalExpression";
text = "[ ... ]";
};
path = "/test-2.html";
} ]
|
Any extra arguments will be forwarded to every generated page set. |
mkSplitCustom
Arguments (Attribute Set)
pageFn
A function to apply to each data set, takes the index of the page and a data set and return a page set.
Must set itemsNb, the number of item to have on the page, and path to generate valid pages.
Type: Int → Data → Page.
Example: {
_type = "literalExpression";
text = "index: data: {
itemsNb = if index == 1 then 3 else 5;
path = if index == 1 then \"/index.html\" else \"/archive-${toString index}.html\";
}
";
}.
Return
List of pages. Each page has:
-
items: List of the page data items. -
pages: List of splitted pages.
Example
mkSplitCustom {
data = map (x: { id = x; }) (range 1 4);
pageFn = (index: data: {
itemsNb = if index == 1 then 3 else 5;
path = if index == 1 then "/index.html" else "/archive-${toString index}.html";
});
}
[ {
index = 1;
items = [ {
id = 1;
} ];
pages = {
_type = "literalExpression";
text = "[ ... ]";
};
path = "/index.html";
} {
index = 2;
items = [ {
id = 2;
} {
id = 3;
} ];
pages = {
_type = "literalExpression";
text = "[ ... ]";
};
path = "/archive-2.html";
} {
index = 3;
items = [ {
id = 4;
} ];
pages = {
_type = "literalExpression";
text = "[ ... ]";
};
path = "/archive-3.html";
} ]
mkTaxonomyPages
Arguments (Attribute Set)
taxonomyPageFn
Function to add extra attributes to the taxonomy page set.
Type: (String → Page).
Optional, defaults to {
_type = "literalExpression";
text = "taxonomy: {}";
}.
5. proplist
The proplist namespace contains functions to manipulate property lists, list of attribute set with only one attribute.
Property lists are used in the taxonomy data structure.
Example:
[ { type = "fruit"; } { name = "Apple"; } ]
6. template
documentedTemplate
Arguments (Attribute Set)
arguments
Template arguments documentation. Attrs if the arguments are an attribute set, List for standard arguments.
Type: Null | Attrs | List.
Optional, defaults to null.
normalTemplate
Example
let template = normalTemplate "A simple string.";
page = { data = "Page data."; };
in template page
{
content = "A simple string.";
data = "Page data.";
}
let template = normalTemplate { content = "Page content."; };
page = { data = "Page data."; };
in template page
{
content = "Page content.";
data = "Page data.";
}
let template = normalTemplate (page: "Page data: ${page.data}");
page = { data = "Page data."; };
in template page
{
content = "Page data: Page data.";
data = "Page data.";
}
let template = normalTemplate (page: { title = "foo"; "Page data: ${page.data}"; });
page = { data = "Page data."; };
in template page
{
content = "Page data: Page data.";
data = "Page data.";
title = "foo";
}
parseDate
Return
A date attribute set, with the following attributes:
-
YYYY: The year in 4 digit format (2012). -
YY: The year in 2 digit format (12). -
Y: Alias toYYYY. -
y: Alias toYY. -
MM: The month in 2 digit format (12, 01). -
M: The month number (12 ,1). -
m: Alias toMM. -
m-: Alias toM. -
B: Month in text format (December, January). -
b: Month in short text format (Dec, Jan). -
DD: Day of the month in 2 digit format (01, 31). -
D: Day of the month (1, 31). -
d-: Alias toD. -
hh: The hour in 2 digit format (08, 12). -
h: The hour in 1 digit format (8, 12). -
mm: The minuts in 2 digit format (05, 55). -
ss: The seconds in 2 digit format (05, 55). -
time: The time in themm:hh:ssformat (12:00:00). -
date.num: The date in theYYYY-MM-DDformat (2012-12-21). -
date.lit: The date in theD B YYYYformat (21 December 2012). -
T: The date and time combined in theYYYY-MM-DDThh:mm:ssZformat (2012-12-21T12:00:00Z).
processBlocks
Example
processBlocks [ {
content = "Block A";
extraJS = "js/a.js";
} {
content = "Block B";
extraJS = "js/b.js";
extraCSS = "css/b.css";
} {
content = "Block C";
extraCSS = "css/c.css";
} {
content = "Block D";
extraJS = [ "js/d.js" "js/d-1.js" ];
extraCSS = [ "css/d.css" "css/d-1.css" ];
} ]
{
content = "Block A
Block B
Block C
Block D";
extraCSS = [ "css/b.css" "css/c.css" "css/d.css" "css/d-1.css" ];
extraJS = [ "js/a.js" "js/b.js" "js/d.js" "js/d-1.js" ];
}
7. themes
load
Arguments (Attribute Set)
decls
A declaration set to merge into to themes configuration.
Type: Attrs.
Optional, defaults to [ ].
Return
A theme data attribute set containing:
-
conf: Themes configuration merged withextraConf. -
lib: The merged themes library. -
files: List of static files folder. -
templates: The merged themes template set. -
themes: List of themes attribute sets. -
decls: Themes declaration set. -
docs: Themes documentation set. -
env: Generated environment attribute set,extraEnvmerged withlib,confandtemplates.
loadData
Return
A theme data attribute set containing:
-
lib: Theme library set. -
meta: Theme meta information set. -
path: Path of the theme. -
decls: Theme declaration set, only if the theme defines a configuration interface. -
docs: Theme documentation set, only if the theme defines a configuration interface. -
exampleSrc: Theme example site source, only if the theme provides an example site. -
templates: Theme templates set, only if the theme provides templates. -
files: Theme static files path, only if the theme provides static files.
8. utils
This namespace contains generic functions.
documentedFunction
Description
Create a documented function. A documented function is used to automatically generate documentation and tests.
Arguments (Attribute Set)
arguments
Function arguments documentation. Attrs if the arguments are an attribute set, List for standard arguments.
Type: Null | Attrs | List.
Optional, defaults to null.
find
Arguments (Standard)
Example
find { uid = "bar"; } [
{ uid = "foo"; }
{ uid = "bar"; content = "hello!"; }
{ uid = "baz"; }
]
{
content = "hello!";
uid = "bar";
}
find { number = (x: x > 3); color = "blue"; } [
{ number = 1; color = "blue"; }
{ number = 4; color = "red"; }
{ number = 6; color = "blue"; }
]
{
color = "blue";
number = 6;
}