Ver código fonte

Trim some fat.

scossu 1 semana atrás
pai
commit
93009d944f
5 arquivos alterados com 32 adições e 83 exclusões
  1. 2 42
      src/core.lua
  2. 19 17
      src/generator.lua
  3. 3 16
      src/model.lua
  4. 7 7
      src/submission.lua
  5. 1 1
      src/validator.lua

+ 2 - 42
src/core.lua

@@ -47,26 +47,7 @@ local M = {
     },
 
     -- Commonly used terms.
-    RDF_TYPE = term.new_iriref_ns("rdf:type"),
-
-    CONTENT_TYPE_P = term.new_iriref_ns("pas:contentType"),
-    DC_TITLE_P = term.new_iriref_ns("dc:title"),
-    FIRST_P = term.new_iriref_ns("pas:first"),
-    NEXT_P = term.new_iriref_ns("pas:next"),
-    PATH_P = term.new_iriref_ns("pas:sourcePath"),
-    PREF_REP_P = term.new_iriref_ns("pas:hasPreferredRepresentation"),
-    REF_P = term.new_iriref_ns("pas:ref"),
-    SUBMITTED_P = term.new_iriref_ns("dc:dateSubmitted"),
-    TN_P = term.new_iriref_ns("pas:thumbnail"),
-
-    ART_T = term.new_iriref_ns("pas:Artifact"),
-    PART_T = term.new_iriref_ns("pas:Part"),
-    FILE_T = term.new_iriref_ns("pas:File"),
-    BRICK_T = term.new_iriref_ns("pas:Brick"),
-
-    -- Common namespaces
-    PAR_NS = nsm.get_ns("par"),
-    PAS_NS = nsm.get_ns("pas"),
+    RDF_TYPE = term.new_iriref(term.RDF_TYPE),
 
     -- Methods.
 
@@ -77,27 +58,6 @@ local M = {
 
 }
 
--- Adapted from lua-núcleo
-M.escape_pattern = function(s)
-    local matches = {
-        ["^"] = "%^";
-        ["$"] = "%$";
-        ["("] = "%(";
-        [")"] = "%)";
-        ["%"] = "%%";
-        ["."] = "%.";
-        ["["] = "%[";
-        ["]"] = "%]";
-        ["*"] = "%*";
-        ["+"] = "%+";
-        ["-"] = "%-";
-        ["?"] = "%?";
-        ["\0"] = "%z";
-    }
-
-    return (s:gsub(".", matches))
-end
-
 
 --[[
   Generate pairtree directory and file path from an ID string and prefix.
@@ -115,7 +75,7 @@ end
   return: full file path, with the optional extension if provided.
 --]]
 M.gen_pairtree = function (pfx, id_str, ext, no_create)
-    local bare_id = id_str:gsub(M.PAR_NS, ""):gsub("^par:", "")
+    local bare_id = id_str:gsub(nsm.get_ns("par"), ""):gsub("^par:", "")
     local res_dir = path.join(pfx, bare_id:sub(1,2), bare_id:sub(3,4))
 
     local created, err

+ 19 - 17
src/generator.lua

@@ -91,18 +91,19 @@ end
     (`.`). If not provided, `.jpg` is used.
 --]]
 local function get_tn_url(s, ext)  -- TODO caller needs to pass correct ext
-    if repo.gr:contains(triple.new(s, pkar.RDF_TYPE, pkar.FILE_T)) then
+    if repo.gr:contains(triple.new(s, pkar.RDF_TYPE, model.id_to_uri.file))
+    then
         -- The subject is a file.
         return pkar.gen_pairtree(TN_WEB_PATH, s.data, ext or ".jpg", true)
     end
 
     -- If it's a brick, look for its ref.
     local ref
-    _, ref = next(repo.gr:attr(s, pkar.REF_P))
+    _, ref = next(repo.gr:attr(s, model.id_to_uri.ref))
     if ref then return get_tn_url(ref, ext) end
 
     local pref_rep
-    _, pref_rep = next(repo.gr:attr(s, pkar.PREF_REP_P))
+    _, pref_rep = next(repo.gr:attr(s, model.id_to_uri.pref_rep))
     if pref_rep then return get_tn_url(pref_rep, ext) end
 
     -- Recurse through all first children until one with a thumbnail, or a
@@ -112,8 +113,8 @@ local function get_tn_url(s, ext)  -- TODO caller needs to pass correct ext
     _, t = next(repo.gr:attr(s, model.id_to_uri["pref_rep"]))
     if not t then
         -- If not found, look for reference of first child.
-        _, t = next(repo.gr:attr(s, pkar.FIRST_P))
-        if t then _, t = next(repo.gr:attr(t, pkar.REF_P)) end
+        _, t = next(repo.gr:attr(s, model.id_to_uri.first))
+        if t then _, t = next(repo.gr:attr(t, model.id_to_uri.ref)) end
     end
     if t then return get_tn_url(t, ext) end
 end
@@ -170,7 +171,7 @@ local function generate_dres(s, mconf)
 
                 while child_s do
                     -- Loop trough all next nodes for each first child.
-                    local _, ref = next(repo.gr:attr(child_s, pkar.REF_P))
+                    local _, ref = next(repo.gr:attr(child_s, model.id_to_uri.ref))
                     --dbg()
                     table.insert(ll, {
                         href = pkar.gen_pairtree("/res", ref.data, ".html", true),
@@ -178,7 +179,8 @@ local function generate_dres(s, mconf)
                         tn = get_tn_url(ref),
                     })
                     -- There can only be one "next"
-                    _, child_s = next(repo.gr:attr(child_s, pkar.NEXT_P))
+                    _, child_s = next(
+                            repo.gr:attr(child_s, model.id_to_uri.next))
                 end
                 table.insert(children, ll)
             end
@@ -208,7 +210,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 == pkar.DC_TITLE_P then title = attr[1] end
+            if p == model.id_to_uri.label then title = attr[1] end
             table.insert(dmd, attr)
         end
         ::skip::
@@ -227,7 +229,7 @@ local function generate_dres(s, mconf)
     logger:debug("Breadcrumbs:", pp.write(get_breadcrumbs(mconf)))
 
     local pref_rep, pref_rep_url
-    _, pref_rep = next(repo.gr:attr(s, pkar.PREF_REP_P))
+    _, pref_rep = next(repo.gr:attr(s, model.id_to_uri.pref_rep))
     if pref_rep then
         pref_rep_url = pkar.gen_pairtree("/res", pref_rep.data, ".html", true)
     end
@@ -316,7 +318,7 @@ local function generate_ores(s, mconf)
     local pres_conf = txconf.pres or {fn = "copy"}
     -- Set file name to resource ID + source extension.
     dest_fname = (
-            s.data:gsub(pkar.PAR_NS, "") ..
+            s.data:gsub(nsm.get_ns("par"), "") ..
             (pres_conf.ext or path.extension(res_path[1])))
     dest_dir = path.join(
             M.media_dir, dest_fname:sub(1, 2), dest_fname:sub(3, 4))
@@ -391,8 +393,8 @@ M.generate_search_idx = function(s, mconf)
 
     for p, ots in pairs(attrs) do
         local pname
-        if p == pkar.CONTENT_TYPE_P then goto skip end
-        if p == pkar.PATH_P then
+        if p == model.id_to_uri.content_type then goto skip end
+        if p == model.id_to_uri.source_path then
             if mconf.types.file then
                 _, fpath = next(ots)
                 rrep.fname = path.basename(fpath.data)
@@ -453,7 +455,7 @@ end
 
 M.generate_resource = function(s)
     local res_type
-    _, res_type = next(repo.gr:attr(s, pkar.CONTENT_TYPE_P))
+    _, res_type = next(repo.gr:attr(s, model.id_to_uri.content_type))
     local mconf = model.from_uri(res_type)
 
     -- Generate RDF/Turtle doc.
@@ -519,8 +521,8 @@ M.generate_homepage = function()
     for _, s in pairs(s_ts) do
         if i > (pkar.config.htmlgen.max_homepage_items or 10) then break end
         local title, submitted
-        _, title = next(repo.gr:attr(s, pkar.DC_TITLE_P))
-        _, submitted = next(repo.gr:attr(s, pkar.SUBMITTED_P))
+        _, title = next(repo.gr:attr(s, model.id_to_uri.label))
+        _, submitted = next(repo.gr:attr(s, model.id_to_uri.submitted))
 
         local obj = {
             href = pkar.gen_pairtree("/res", s.data, ".html", true),
@@ -542,8 +544,8 @@ M.generate_homepage = function()
     )
     for _, s in pairs(s_ts) do
         local title, submitted
-        _, title = next(repo.gr:attr(s, pkar.DC_TITLE_P))
-        _, submitted = next(repo.gr:attr(s, pkar.SUBMITTED_P))
+        _, title = next(repo.gr:attr(s, model.id_to_uri.label))
+        _, submitted = next(repo.gr:attr(s, model.id_to_uri.submitted))
 
         local coll = {
             href = pkar.gen_pairtree("/res", s.data, ".html", true),

+ 3 - 16
src/model.lua

@@ -9,17 +9,15 @@ local pkar = require "pocket_archive"
 local dbg = require "debugger"
 
 
--- Escape magic characters.
-local PAS_NS_PTN = pkar.escape_ptn(pkar.PAS_NS)
-
 local M = {
     -- Parsed typedef configurations.
     types = {},
 
-    -- Term-to-URI map.
+    -- Term-to-URI map. URIs are volksdata.Term objects.
     id_to_uri = {},
 
-    -- URI-to-term map.
+    -- URI-to-term map. Keys are URI strings, not volksdata.Term objects,
+    -- because key comparison wouldn't work with them.
     uri_to_id = {},
 }
 
@@ -36,16 +34,6 @@ local MODEL_PATH = path.join(pkar.config_path, "model")
 local gen_config = dofile(path.join(MODEL_PATH, "generation.lua"))
 
 
---[[
-local function camel2snake(src)
-    return src
-        :gsub("^pas:", "")  -- Strip namespace.
-        :gsub("([^^])(%u)", "%1_%2")  -- Uppercase (except initial) to _.
-        :lower()
-end
---]]
-
-
 local function add_term(id, uri_str)
     --if not uri then error(("Term %s has not a URI!"):format(term), 2) end
     if not uri_str then return end
@@ -56,7 +44,6 @@ end
 
 
 local function parse_model(mod_id)
-    mod_id = mod_id:gsub(PAS_NS_PTN, ""):gsub("par:", "")
     local hierarchy = {}
 
     local function traverse(mod_id)

+ 7 - 7
src/submission.lua

@@ -98,9 +98,9 @@ M.generate_sip = function(src_path)
         -- Skip empty lines.
         if not has_content then goto skip end
 
-        logger:debug("Row path: ", row.source_path)
+        logger:debug("Row path: ", row.source_path or "")
         logger:debug("Parsing row:", pp.write(row))
-        if #row.source_path > 0 then
+        if row.source_path then
             i = i + 1
             logger:info(
                     ("Processing LL resource #%d at row #%d.")
@@ -178,8 +178,7 @@ M.generate_sip = function(src_path)
             goto skip
         end
         for j = i + 1, #sip do
-            if sip[j].source_path:match(
-                    "^" .. pkar.escape_pattern(v.source_path))
+            if sip[j].source_path:match("^" .. pkar.escape_ptn(v.source_path))
             then
                 local rel_path = sip[j].source_path:sub(#v.source_path + 2)
                 logger:debug("rel_path: " .. rel_path)
@@ -252,7 +251,8 @@ M.rsrc_to_graph = function(rsrc)
                 local brick_uri = term.new_iriref_ns("par:" .. M.idgen())
                 if i == 1 then
                     proxy_s = s
-                    it:add_iter(triple.new(proxy_s, pkar.FIRST_P, brick_uri))
+                    it:add_iter(triple.new(
+                            proxy_s, model.id_to_uri.first, brick_uri))
 
                     -- Add the "has member" property.
                     it:add_iter(triple.new(
@@ -260,7 +260,7 @@ M.rsrc_to_graph = function(rsrc)
                         term.new_iriref_ns(pconf.uri),
                         term.new_iriref_ns(vv)))
                 else
-                    it:add_iter(triple.new(proxy_s, pkar.NEXT_P, brick_uri))
+                    it:add_iter(triple.new(proxy_s, model.id_to_uri.next, brick_uri))
                 end
                 -- Add the reference.
                 -- Add basic triples.
@@ -273,7 +273,7 @@ M.rsrc_to_graph = function(rsrc)
                 end
                 it:add_iter(triple.new(
                     brick_uri,
-                    pkar.CONTENT_TYPE_P,
+                    model.id_to_uri.content_type,
                     term.new_iriref_ns("pas:Brick")))
                 -- Add reference.
                 it:add_iter(triple.new(

+ 1 - 1
src/validator.lua

@@ -16,7 +16,7 @@ local M = {}
 
 
 M.validate = function(gr, s)
-    _, ctype = next(gr:attr(s, pkar.CONTENT_TYPE_P))
+    _, ctype = next(gr:attr(s, model.id_to_uri.content_type))
     local rmod = model.from_uri(ctype)
     if not rmod then error("No type definition for " .. ctype.data) end