Hash :
ae614143
Author :
Thomas de Grivel
Date :
2025-09-03T12:11:09
fix test/httpd dump and optimize doc_index and sitemap
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
## kc3
## Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted the above
## copyright notice and this permission paragraph are included in all
## copies and substantial portions of this software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
require EKC3
require File
defmodule PageController do
def page_index = fn {
(path) { page_index(path, path, []) }
([], dir, acc) { List.reverse(acc) }
([file | rest], dir, acc) {
if (Str.starts_with?(file, ".") ||
Str.starts_with?(file, "index.") ||
(Str.rindex_character(file, '.') > 0 &&
! Str.ends_with?(file, ".md") &&
! Str.ends_with?(file, ".html"))) do
page_index(rest, dir, acc)
else
path = dir + file
url = Str.slice(path, 1, -1)
if File.is_directory?(path) do
items = page_index(List.sort(File.list(path)), path + "/", acc)
page_index(rest, dir, items)
else
end_ = Str.rindex_character(name, '.')
if (end_ > 0) do
name = Str.slice(name, 0, end_)
end_ = Str.rindex_character(name, '.')
if (end_ > 0) do
name = Str.slice(name, 0, end_)
end
end
end_ = Str.rindex_character(url, '.')
if (end_ > 0) do
url = Str.slice(url, 0, end_)
end_ = Str.rindex_character(url, '.')
if (end_ > 0) do
url = Str.slice(url, 0, end_)
end
end
page_index(rest, dir, [url | acc])
end
end
}
(path, dir, acc) {
if (type(path) == Str) do
page_index(List.sort(File.list(path)), dir, acc)
end
}
}
def show_html = fn (path_html, url, locale) {
title = "kc3-lang.org"
name = File.name(path_html)
slug = Str.slice(name, 0, -9)
slug = Str.subst(slug, " ", "-")
slug = Str.subst(slug, ".", "-")
html = File.read_all(path_html)
page = PageView.render_show(html)
body = LayoutView.render(slug, title, page, url)
%HTTP.Response{body: body}
}
def show_md = fn (path_md, url, locale) {
title = "kc3-lang.org"
md = File.read_all(path_md)
[title | _] = Markdown.titles(md)
slug = Str.subst(title, " ", "-")
html = Markdown.to_html_str(md)
page = PageView.render_show(html)
body = LayoutView.render(title, page, url)
%HTTP.Response{body: body}
}
def route_locale = fn (url, url_locale, locale) {
slash = if Str.ends_with?(url, "/") do "" else "/" end
path_html = "./pages#{url}#{slash}index.#{locale}.html"
if File.exists?(path_html) do
show_html(path_html, url, url_locale)
else
path_md = "./pages#{url}#{slash}index.#{locale}.md"
if File.exists?(path_md) do
show_md(path_md, url, url_locale)
else
path_md = "./pages#{request.url}.#{locale}.md"
if File.exists?(path_md) do
show_md(path_md, url, url_locale)
end
end
end
}
def route = fn (request) {
url = request.url
if (request.method == GET ||
request.method == HEAD) do
locale = if (url[0] == '/' &&
(Str.size(url) == 3 ||
url[3] == '/')) do
l = "#{url[1]}#{url[2]}"
if List.has?(Config.available_locales, l) do
url = Str.slice(url, 3, -1)
l
end
end || "en"
route_locale(url, locale, locale) ||
route_locale(url, locale, "en")
end
}
end