Edit

kc3-lang/kc3/test/httpd/app/controllers

Branch :

  • doc_controller.kc3
  • 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 = Str.subst(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(List.sort(File.list(path)), path_md, dir, acc)
          end
        }
      }
    
      def show_html = fn (path_html) {
        index = doc_index("./doc/", path_html)
        menu = DocView.render_menu(index)
        title = "kc3-lang.org"
        html = File.read_all(path_html)
        page = DocView.render_show(menu, html)
        body = LayoutView.render(title, page)
        %HTTP.Response{body: body}
      }
    
      def show_md = fn (path_md) {
        index = doc_index("./doc/", path_md)
        menu = DocView.render_menu(index)
        title = "kc3-lang.org"
        md = File.read_all(path_md)
        html = Markdown.to_html_str(md)
        page = DocView.render_show(menu, html)
        body = LayoutView.render(title, page)
        %HTTP.Response{body: body}
      }
    
      def route = fn (request) {
        if (request.method == GET ||
            request.method == HEAD) 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