Hash :
dc7b65d8
Author :
Thomas de Grivel
Date :
2024-09-07T19:57:29
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
defmodule DocController do
require EKC3
require File
require List
require Str
def doc_index = fn {
(path, path_md) { doc_index(path, path_md, path, []) }
([], path_md, dir, acc) { List.reverse(acc) }
([file | rest], path_md, 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
doc_index(rest, path_md, dir, acc)
else
path = dir + file
name = ^ file
url = Str.slice(path, 1, -1)
if File.is_directory?(path) do
items = doc_index(List.sort(File.list(path)), path_md,
path + "/", [])
item = %{type: :dir,
url: url,
name: name,
items: items}
doc_index(rest, path_md, dir, [item | acc])
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
item = %{type: :file,
url: url,
name: name,
items: []}
doc_index(rest, path_md, dir, [item | acc])
end
end
}
(path, path_md, dir, acc) {
if (type(path) == Str) do
doc_index(File.list(path), path_md, dir, acc)
end
}
}
def show_html = fn (path_html) {
index = doc_index("./doc/", path_html)
menu = EKC3.render_file("app/templates/doc/menu.html.ekc3")
title = "kc3-lang.org"
html = File.read(path_html)
page = EKC3.render_file("app/templates/doc/show.html.ekc3")
body = EKC3.render_file("app/templates/layout.html.ekc3")
%HTTP.Response{body: body}
}
def show_md = fn (path_md) {
index = doc_index("./doc/", path_md)
menu = EKC3.render_file("app/templates/doc/menu.html.ekc3")
title = "kc3-lang.org"
md = File.read(path_md)
html = Markdown.to_html_str(md)
page = EKC3.render_file("app/templates/doc/show.html.ekc3")
body = EKC3.render_file("app/templates/layout.html.ekc3")
%HTTP.Response{body: body}
}
def route = fn (request) {
if (request.method == :get) do
locale = "en"
path_html = ".#{request.url}/index.#{locale}.html"
if File.exists?(path_html) do
show_html(path_html)
else
path_md = ".#{request.url}/index.#{locale}.md"
if File.exists?(path_md) do
show_md(path_md)
else
path_md = ".#{request.url}.#{locale}.md"
if File.exists?(path_md) do
show_md(path_md)
else
HTTPd.error_404_page(request)
end
end
end
else
HTTPd.error_404_page(request)
end
}
end