Browse Source

Some improvements to resource template.

scossu 3 weeks ago
parent
commit
7b5a0ac2df
4 changed files with 73 additions and 12 deletions
  1. 36 5
      src/html_generator.lua
  2. 1 0
      src/model.lua
  3. 1 1
      templates/index.html
  4. 35 6
      templates/res.html

+ 36 - 5
src/html_generator.lua

@@ -8,6 +8,7 @@ local triple = require "lsup.triple"
 local graph = require "lsup.graph"
 
 local pkar = require "pocket_archive"
+local model = require "pocket_archive.model"
 
 
 -- Compile all templates once.
@@ -32,12 +33,40 @@ M.generate_resource = function(gr_uri)
     local gr = graph.get(gr_uri, pkar.store)
     local dmd = {}
     local rel = {}
-
-    for trp in gr:lookup(gr_uri) do
-        -- TODO transform data here according to model.
-        dmd[trp.p.data] = trp.o
+    local children = {}
+
+    local res_type
+    local type_attr = gr:attr(
+            gr_uri, term.new_iriref("pas:contentType", pkar.nsm))
+    res_type = type_attr[1].data
+    local mconf = model.models[res_type]
+
+    local attrs = gr:connections(gr_uri, term.LINK_OUTBOUND)
+    for p, ots in pairs(attrs) do
+        if ((mconf.attributes or {})[p.data] or {}).type == "rel" then
+            -- Relationship.
+            rel[p.data] = {}
+            for o in pairs(ots) do table.insert(dmd[p.data], o.data) end
+        elseif p.data == "pas:firstChild" then
+            -- Children.
+            for o in pairs(ots) do table.insert(children, o.data) end
+        elseif p.data == "pas:next" then
+            -- Sibling.
+            for o in pairs(ots) do ls_next = o.data break end
+        else
+            -- Descriptive metadata.
+            dmd[p.data] = {}
+            -- TODO differentiate term types
+            for o in pairs(ots) do table.insert(dmd[p.data], o.data) end
+        end
     end
-    pp.dump(rsrc_data)
+    table.sort(dmd)
+    table.sort(rel)
+    table.sort(children)
+    print("DMD:")
+    pp.dump(dmd)
+    print("REL:")
+    pp.dump(rel)
 
     out_html = res_tpl({
         title = pkar.config.site.title or "Pocket Archive",
@@ -45,6 +74,8 @@ M.generate_resource = function(gr_uri)
         pkar = pkar,
         dmd = dmd,
         rel = rel,
+        children = children,
+        ls_next = ls_next,
     })
 
     local res_id = gr_uri.data:gsub(pkar.nsm:get_ns("par"), "")

+ 1 - 0
src/model.lua

@@ -67,6 +67,7 @@ M.parse_model = function(mod_id)
 end
 
 
+-- Create a cache for model configurations.
 local models_mt = {
     ["__index"] = function (t, model)
         t[model] = M.parse_model(model)

+ 1 - 1
templates/index.html

@@ -13,7 +13,7 @@
                 <ul>
                 <% for uri, data in pairs(obj_idx) do %>
                     <li class="obj_link">
-                        <a href="<%= uri:gsub(pkar.nsm:get_ns("par"), "res/") %>.html">
+                        <a href="<%= uri:gsub(pkar.nsm:get_ns('par'), 'res/') %>.html">
                             <%= data.title.data %>
                             <%if data.title.lang then %>
                                 <span class="langtag"><%= data.title.lang %></span>

+ 35 - 6
templates/res.html

@@ -1,22 +1,51 @@
 <!DOCTYPE html>
 <html>
     <head>
-        <title><%= title %>&emsp;:&emsp;<%= data["dc:title"].data %></title>
+        <title>
+            <%= title %>&emsp;❁&emsp;<%= ((dmd["dc:title"] or {})[1] or {}).data or uri.data %>
+        </title>
     </head>
     <body>
         <header>
-            <h1><%= title %>&emsp;❁&emsp;<%= data["dc:title"].data %></h1>
+            <h1>
+                <%= ((dmd["dc:title"] or {})[1] or {}).data or uri.data %>
+            </h1>
         </header>
         <main>
-            <section id="res_md">
+            <section id="res_dmd">
                 <h2>Metadata</h2>
                 <dl class="res_md">
-                <% for p, o in pairs(dmd) do %>
-                <dt><%= p %></dt>
-                <dd><%= o.data %></dd>
+                <% for p, ol in pairs(dmd) do %>
+                    <dt><%= p %></dt>
+                    <% for _, o in ipairs(ol) do %>
+                      <dd><%= o %></dd>
+                    <% end %>
                 <% end %>
                 </dl>
             </section>
+            <% if #rel then %>
+            <section id="res_rel">
+                <h2>Relationships</h2>
+                <dl class="res_md">
+                <% for p, ol in pairs(rel) do %>
+                    <dt><%= p.data %></dt>
+                    <% for _, o in ipairs(ol) do %>
+                        <dd><a href="res/<%= o.data %>.html"><%= o.data %></a></dd>
+                    <% end %>
+                <% end %>
+                </dl>
+            </section>
+            <% end %>
+            <% if #children then %>
+            <section id="res_children">
+                <h2>Children</h2>
+                <ul>
+                <% for _, child in ipairs(children) do %>
+                    <li><a href = "<%= child %>"><%= child %></li>
+                <% end %>
+                </ul>
+            </section>
+            <% end %>
         </main>
         <footer></footer>
     </body>