aboutsummaryrefslogtreecommitdiff
path: root/examples/site/build.lua
blob: 4d0939aa53a975eaad46f4e61a26c12dc266dc5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- External scripts are resolved relative to the HTML file that includes them :)

local function find_by_class(node, wanted)
  if type(node) ~= "table" then return nil end
  local classes = node.attributes and node.attributes.class or ""
  if (" " .. classes .. " "):find(" " .. wanted .. " ", 1, true) then
    return node
  end
  for _, child in ipairs(node.children or {}) do
    local match = find_by_class(child, wanted)
    if match then return match end
  end
end

local features = {
  "inline Lua",
  "external Lua",
  "DOM updates",
  "directory walking",
  "logging",
  "mixed content"
}

local feature_list = assert(find_by_class(page, "feature-list"))
for _, feature in ipairs(features) do
  table.insert(feature_list.children, html.element("li", nil, { feature }))
end

local entries = {}
for entry in fs.walk(".") do
  table.insert(entries, entry)
end
table.sort(entries, function(left, right) return left.path < right.path end)

local summary = assert(find_by_class(page, "file-summary"))
summary.text = tostring(#entries) .. " entries"

local file_list = assert(find_by_class(page, "file-list"))
for _, entry in ipairs(entries) do
  table.insert(file_list.children, html.element("li", nil, {
    html.element("code", nil, { entry.path }),
    " (", entry.type, ")"
  }))
end

local build_summary = assert(find_by_class(page, "build-summary"))
build_summary.text = "Built with " .. site.name .. " " .. site.version

local mixed = assert(find_by_class(page, "mixed-content"))
mixed.children = {
  { kind = "comment", text = "generated by Lua" },
  "Text with ",
  html.element("strong", nil, { "an element" }),
  "."
}

table.insert(page.head.children, 1, {
  kind = "processing_instruction",
  target = "yapssg",
  text = "generated"
})

page.attributes["data-entry-count"] = tostring(#entries)
log.info("generated", #features, "feature items")
log.debug("walked", #entries, "entries")