Parcourir la source

Complete RDF generator.

scossu il y a 5 jours
Parent
commit
bc3b06cab8
3 fichiers modifiés avec 15 ajouts et 25 suppressions
  1. 13 25
      src/html_generator.lua
  2. 1 0
      templates/dres.html
  3. 1 0
      templates/ores.html

+ 13 - 25
src/html_generator.lua

@@ -20,20 +20,6 @@ local NT = {}
 -- Default store graph to search all triples.
 local gr = graph.new(pkar.store, term.DEFAULT_CTX)
 
--- Some commonly used terms.
-local dc_title_p = term.new_iriref_ns("dc:title")
-local dc_created_p = term.new_iriref_ns("dc:created")
-local tn_p = term.new_iriref_ns("pas:thumbnail")
-local first_p = term.new_iriref_ns("pas:first")
-local next_p = term.new_iriref_ns("pas:next")
-local path_p = term.new_iriref_ns("pas:path")
-local content_type_p = term.new_iriref_ns("pas:contentType")
-local file_t = term.new_iriref_ns("pas:File")
-
--- Namespaces.
-local par_ns = nsm.get_ns("par")
---local pas_ns = namespace.get_ns("pas")
-
 -- HTML templates. Compile them only once.
 -- TODO Add override for user-maintained templates.
 local fh, idx_tpl, dres_tpl, ores_tpl
@@ -78,9 +64,9 @@ end
 
 
 local function get_tn_url(s)
-    if gr:attr(s, pkar.RDF_TYPE)[file_t.hash] then
+    if gr:attr(s, pkar.RDF_TYPE)[pkar.FILE_T.hash] then
         -- The subject is a file.
-        tn_fname = (s.data:gsub(par_ns, "") .. ".jpg")  -- FIXME do not hardcode.
+        tn_fname = (s.data:gsub(pkar.PAR_NS, "") .. ".jpg")  -- FIXME do not hardcode.
         return plpath.join(
                 M.media_dir, tn_fname:sub(1, 2), tn_fname:sub(3, 4), tn_fname)
     end
@@ -88,7 +74,7 @@ local function get_tn_url(s)
     -- Recurse through all first children until one with a thumbnail, or a
     -- leaf without children, is found.
     local first_child
-    _, first_child = next(gr:attr(s, first_p))
+    _, first_child = next(gr:attr(s, pkar.FIRST_P))
     if first_child then return get_tn_url(first_child) end
 end
 
@@ -117,7 +103,7 @@ local function generate_dres(s, mconf)
                 logger:debug("local child_s: ", child_s.data)
                 local ll = {}
                 local label
-                _, label = next(gr:attr(child_s, dc_title_p))
+                _, label = next(gr:attr(child_s, pkar.DC_TITLE_P))
                 while child_s do
                     -- Loop trough all next nodes for each first child.
                     table.insert(ll, {
@@ -127,7 +113,7 @@ local function generate_dres(s, mconf)
                         tn = get_tn_url(child_s):gsub(M.media_dir, "/media/tn"),
                     })
                     -- There can only be one "next"
-                    _, child_s = next(gr:attr(child_s, next_p))
+                    _, child_s = next(gr:attr(child_s, pkar.NEXT_P))
                 end
                 table.insert(children, ll)
             end
@@ -140,7 +126,7 @@ local function generate_dres(s, mconf)
             -- TODO differentiate term types
             for _, o in pairs(ots) do table.insert(attr, o.data) end
             table.sort(attr)
-            if p == dc_title_p then title = attr[1] end
+            if p == pkar.DC_TITLE_P then title = attr[1] end
             table.insert(dmd, attr)
         end
         ::skip::
@@ -165,6 +151,7 @@ local function generate_dres(s, mconf)
         children = children,
         ls_next = ls_next,
         breadcrumbs = get_breadcrumbs(mconf),
+        rdf_href = pkar.gen_pairtree("/res", s.data, ".ttl", true),
     })
 
     local res_path = pkar.gen_pairtree(M.res_dir, s.data, ".html")
@@ -217,7 +204,7 @@ local function generate_ores(s, mconf)
     local txconf = (mconf.transformers or NT).deliverable or {fn = "copy"}
     -- Set file name to resource ID + source extension.
     dest_fname = (
-            s.data:gsub(par_ns, "") ..
+            s.data:gsub(pkar.PAR_NS, "") ..
             (txconf.ext or plpath.extension(res_path[1])))
     dest_dir = plpath.join(
             M.media_dir, dest_fname:sub(1, 2), dest_fname:sub(3, 4))
@@ -257,6 +244,7 @@ local function generate_ores(s, mconf)
         breadcrumbs = get_breadcrumbs(mconf),
         deliverable = deliverable,
         thumbnail = tn,
+        rdf_href = pkar.gen_pairtree("/res", s.data, ".ttl", true),
     })
 
     local res_path = pkar.gen_pairtree(M.res_dir, s.data, ".html")
@@ -270,7 +258,7 @@ end
 
 M.generate_resource = function(s)
     local res_type
-    _, res_type = next(gr:attr(s, content_type_p))
+    _, res_type = next(gr:attr(s, pkar.CONTENT_TYPE_P))
     local mconf = model.models[res_type.data]
 
     -- Generate RDF/Turtle doc.
@@ -279,7 +267,7 @@ M.generate_resource = function(s)
     logger:debug("Serializing graph: ", s.data)
     local res_path = pkar.gen_pairtree(M.res_dir, s.data, ".ttl")
     local ofh = assert(io.open(res_path, "w"))
-    ofh:write(res_gr:encode("nt"))
+    ofh:write(res_gr:encode("ttl"))
     ofh:close()
 
     -- Generate HTML doc.
@@ -307,8 +295,8 @@ M.generate_idx = function()
     )
     for _, s in pairs(s_ts) do
         local title, created
-        _, title = next(gr:attr(s, dc_title_p))
-        _, created = next(gr:attr(s, dc_created_p))
+        _, title = next(gr:attr(s, pkar.DC_TITLE_P))
+        _, created = next(gr:attr(s, pkar.DC_CREATED_P))
 
         local obj = {
             href = pkar.gen_pairtree("/res", s.data, ".html", true),

+ 1 - 0
templates/dres.html

@@ -23,6 +23,7 @@
                     </a>
                 <% end %>
                 </p>
+                <a href="<%= rdf_href %>">RDF document</a>
             </section>
             <section id="res_dmd">
                 <h2>Metadata</h2>

+ 1 - 0
templates/ores.html

@@ -23,6 +23,7 @@
                     </a>
                 <% end %>
                 </p>
+                <a href="<%= rdf_href %>">RDF document</a>
             </section>
             <section class="res_md" id="res_techmd">
                 <h2>Metadata</h2>