Commit 0202e9ce368f704782c06ab4d67777be0c544a90

Thomas de Grivel 2024-09-14T00:02:57

views with preloaded templates

diff --git a/ekc3/ekc3.c b/ekc3/ekc3.c
index a755b5e..6955818 100644
--- a/ekc3/ekc3.c
+++ b/ekc3/ekc3.c
@@ -11,6 +11,7 @@
  * THIS SOFTWARE.
  */
 #include <libkc3/kc3.h>
+#include <unistd.h>
 #include "ekc3.h"
 #include "html.h"
 
@@ -400,6 +401,30 @@ s_str * ekc3_inspect_block (const s_block *block, s_str *dest)
   return dest;
 }
 
+s_tag * ekc3_load (const s_str *path, s_tag *dest)
+{
+  s_buf buf;
+  s32 fd = -1;
+  s_tag tmp = {0};
+  if (! buf_init_alloc(&buf, BUF_SIZE))
+    return NULL;
+  if (! file_open_r(path, &fd))
+    goto clean;
+  if (! buf_fd_open_r(&buf, fd))
+    goto clean;
+  if (! ekc3_buf_parse(&buf, &tmp.data.list))
+    goto clean;
+  tmp.type = TAG_LIST;
+  buf_clean(&buf);
+  *dest = tmp;
+  return dest;
+ clean:
+  if (fd > 0)
+    close(fd);
+  buf_clean(&buf);
+  return NULL;
+}
+
 sw ekc3_render (s_buf *buf, const p_ekc3 *ekc3)
 {
   const s_list *l;
diff --git a/lib/kc3/0.1/ekc3.kc3 b/lib/kc3/0.1/ekc3.kc3
index 097cc19..cb39be2 100644
--- a/lib/kc3/0.1/ekc3.kc3
+++ b/lib/kc3/0.1/ekc3.kc3
@@ -2,6 +2,10 @@ defmodule EKC3 do
 
   dlopen(__DIR__ + "ekc3.so")
 
+  def load = cfn Tag "ekc3_load" (Str, Result)
+
+  def render = cfn Str "ekc3_render_to_str" (List, Result)
+
   def render_file = cfn Str "ekc3_render_file_to_str" (Str, Result)
 
 end
diff --git a/test/httpd/app/controllers/doc_controller.kc3 b/test/httpd/app/controllers/doc_controller.kc3
index b976cbc..2cf86d7 100644
--- a/test/httpd/app/controllers/doc_controller.kc3
+++ b/test/httpd/app/controllers/doc_controller.kc3
@@ -71,12 +71,12 @@ defmodule DocController do
 
   def show_md = fn (path_md) {
     index = doc_index("./doc/", path_md)
-    menu = EKC3.render_file("app/templates/doc/menu.html.ekc3")
+    menu = DocView.render_menu(index)
     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")
+    page = DocView.render_show(menu, html)
+    body = LayoutView.render(title, page)
     %HTTP.Response{body: body}
   }
 
diff --git a/test/httpd/app/templates/doc/menu.html.ekc3 b/test/httpd/app/templates/doc/menu.html.ekc3
index fe5161f..e23b5ee 100644
--- a/test/httpd/app/templates/doc/menu.html.ekc3
+++ b/test/httpd/app/templates/doc/menu.html.ekc3
@@ -4,9 +4,7 @@
   <li>
     <a href="#{URL.escape(file.url)}">#{HTML.escape(file.name)}</a>
     #{if (file.type == :dir) do
-        let %{index: file.items} do
-          EKC3.render_file("app/templates/doc/menu.html.ekc3")
-        end
+        DocView.render_menu(file.items)
       else
         ""
       end}
diff --git a/test/httpd/app/views/doc_view.kc3 b/test/httpd/app/views/doc_view.kc3
index c01ed1f..fbf163d 100644
--- a/test/httpd/app/views/doc_view.kc3
+++ b/test/httpd/app/views/doc_view.kc3
@@ -1,11 +1,15 @@
 defmodule DocView do
 
+  def menu_template = EKC3.load("app/templates/doc/menu.html.ekc3")
+
+  def show_template = EKC3.load("app/templates/doc/show.html.ekc3")
+
   def render_menu = fn (index) {
-    EKC3.render_file("app/templates/doc/menu.html.ekc3")
+    EKC3.render(menu_template)
   }
 
   def render_show = fn (menu, html) {
-    EKC3.render_file("app/templates/doc/show.html.ekc3")
+    EKC3.render(show_template)
   }
 
 end
diff --git a/test/httpd/app/views/layout_view.kc3 b/test/httpd/app/views/layout_view.kc3
index aa85042..e3043d1 100644
--- a/test/httpd/app/views/layout_view.kc3
+++ b/test/httpd/app/views/layout_view.kc3
@@ -1,7 +1,9 @@
 defmodule LayoutView do
 
+  def template = EKC3.load("app/templates/layout.html.ekc3")
+
   def render = fn (title, page) {
-    EKC3.render_file("app/templates/layout.html.ekc3")
+    EKC3.render(template)
   }
 
 end