|
@@ -1,32 +1,99 @@
|
|
local datafile = require "datafile"
|
|
local datafile = require "datafile"
|
|
|
|
+local dir = require "pl.dir"
|
|
local etlua = require "etlua"
|
|
local etlua = require "etlua"
|
|
local pp = require "pl.pretty"
|
|
local pp = require "pl.pretty"
|
|
|
|
|
|
local term = require "lsup.term"
|
|
local term = require "lsup.term"
|
|
|
|
+local triple = require "lsup.triple"
|
|
local graph = require "lsup.graph"
|
|
local graph = require "lsup.graph"
|
|
|
|
|
|
local pkar = require "pocket_archive"
|
|
local pkar = require "pocket_archive"
|
|
|
|
|
|
|
|
|
|
-local M = {}
|
|
|
|
|
|
+-- Compile all templates once.
|
|
|
|
+-- TODO Add override for user-maintained templates.
|
|
|
|
+local fh, idx_tpl, res_tpl, err
|
|
|
|
+fh = datafile.open("templates/index.html")
|
|
|
|
+idx_tpl, err = etlua.compile(fh:read("a"))
|
|
|
|
+if not idx_tpl then error(err) end
|
|
|
|
+fh:close()
|
|
|
|
+
|
|
|
|
+fh = datafile.open("templates/res.html")
|
|
|
|
+res_tpl, err = etlua.compile(fh:read("a"))
|
|
|
|
+if not res_tpl then error(err) end
|
|
|
|
+fh:close()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+local M = {
|
|
|
|
+ res_dir = pkar.config.htmlgen.out_dir .. "/res",
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+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
|
|
|
|
+ end
|
|
|
|
+ pp.dump(rsrc_data)
|
|
|
|
+
|
|
|
|
+ out_html = res_tpl({
|
|
|
|
+ title = pkar.config.site.title or "Pocket Archive",
|
|
|
|
+ uri = gr_uri,
|
|
|
|
+ pkar = pkar,
|
|
|
|
+ dmd = dmd,
|
|
|
|
+ rel = rel,
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ local res_id = gr_uri.data:gsub(pkar.nsm:get_ns("par"), "")
|
|
|
|
+ local ofh = assert(io.open(string.format(
|
|
|
|
+ "%s/%s.html", M.res_dir, res_id), "w"))
|
|
|
|
+ return ofh:write(out_html)
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+M.generate_resources = function()
|
|
|
|
+ -- TODO parallelize
|
|
|
|
+ dir.makepath(M.res_dir)
|
|
|
|
+ local index_ts = graph.list(pkar.store)
|
|
|
|
+ for gr_uri in pairs(index_ts) do M.generate_resource(gr_uri) end
|
|
|
|
+end
|
|
|
|
+
|
|
|
|
|
|
M.generate_idx = function()
|
|
M.generate_idx = function()
|
|
local obj_idx = {}
|
|
local obj_idx = {}
|
|
local index_ts = graph.list(pkar.store)
|
|
local index_ts = graph.list(pkar.store)
|
|
for gr_uri in pairs(index_ts) do
|
|
for gr_uri in pairs(index_ts) do
|
|
local obj = {}
|
|
local obj = {}
|
|
|
|
+ local cur
|
|
gr = graph.get(gr_uri, pkar.store)
|
|
gr = graph.get(gr_uri, pkar.store)
|
|
- for trp in gr:lookup(gr_uri, term.new_iriref("dc:title")) do
|
|
|
|
- if trp.o then obj.title = trp.o end
|
|
|
|
|
|
+ -- Get all subject of type: Artifact.
|
|
|
|
+ s_ts = gr:term_set(
|
|
|
|
+ term.new_iriref("rdf:type", pkar.nsm), triple.POS_P,
|
|
|
|
+ term.new_iriref("pas:Artifact", pkar.nsm), triple.POS_O
|
|
|
|
+ )
|
|
|
|
+ for s in pairs(s_ts) do
|
|
|
|
+ for trp in gr:lookup(s, term.new_iriref("dc:title", pkar.nsm)) do
|
|
|
|
+ obj.title = trp.o
|
|
|
|
+ break -- Only one value to use
|
|
|
|
+ end
|
|
end
|
|
end
|
|
if obj.title then obj_idx[gr_uri.data] = obj end
|
|
if obj.title then obj_idx[gr_uri.data] = obj end
|
|
end
|
|
end
|
|
|
|
|
|
- local fh = datafile.open("templates/index.html")
|
|
|
|
- local idx_tpl, err = etlua.compile(fh:read("a"))
|
|
|
|
- if not idx then error(err) end
|
|
|
|
pp.dump(obj_idx)
|
|
pp.dump(obj_idx)
|
|
- print(idx_tpl({obj_idx = obj_idx, term = term}))
|
|
|
|
|
|
+ out_html = idx_tpl({
|
|
|
|
+ title = pkar.config.site.title or "Pocket Archive",
|
|
|
|
+ pkar = pkar,
|
|
|
|
+ obj_idx = obj_idx,
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ local ofh = assert(io.open(
|
|
|
|
+ pkar.config.htmlgen.out_dir .. "/index.html", "w"))
|
|
|
|
+
|
|
|
|
+ return ofh:write(out_html)
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|