model.lua (3784B)
1 -- @module model 2 local model = {} 3 -- 4 local etlua = require 'etlua' 5 local lfs = require 'lfs' -- luafilesystem 6 local lume = require 'satelito.lib.lume.lume' 7 local dirtree = require 'satelito.dirtree' 8 local file = require 'satelito.file' 9 local markdown = require 'discount' -- lua-discount 10 local template = require 'satelito.template' 11 local inspect = require 'inspect' 12 13 function model.set(filepath) 14 local pagedata = file.get_metafile(filepath) or {} 15 local config = _G.Satelito.config 16 local contentdir = _G.Satelito.contentdir 17 local publicdir = _G.Satelito.publicdir 18 local templates = _G.Satelito.templates 19 local time_created 20 21 -- If required properties are nil 22 pagedata.title = pagedata.title or file.get_basename(filepath):match('(.+)%..*') 23 pagedata.date = pagedata.date or os.date('%Y-%m-%d', lfs.attributes(filepath).change) 24 pagedata.datetime = pagedata.datetime or os.date('%H:%M:%S', lfs.attributes(filepath).change) 25 26 -- Path properties 27 pagedata.contentdir = contentdir 28 pagedata.path = filepath 29 pagedata.relpath = file.get_relpath(filepath, contentdir) 30 pagedata.reldir = pagedata.relpath:match("(.*/)") 31 32 -- Link properties 33 pagedata.rellink = file.get_rellink(filepath, contentdir) 34 pagedata.rellinkdir = '/'.. (pagedata.reldir ~= nil and pagedata.reldir or '') 35 pagedata.permalink = file.get_permalink(filepath, contentdir, config.siteurl) 36 pagedata.exportlink = file.get_exportlink(filepath, contentdir, publicdir) 37 pagedata.dirlink = file.get_permalink(filepath, contentdir, config.siteurl):match("(.*/)") 38 39 -- Time properties 40 time_created = (pagedata.date..pagedata.datetime):gsub('%W','') 41 42 pagedata.time_created = time_created 43 pagedata.time_modification = lfs.attributes(filepath).modification 44 pagedata.time_modified_child = file.get_lastmodified(lume.array(dirtree.get(file.get_dirname(filepath)))) 45 46 -- Unique page ID for the Atom feed 47 pagedata.id = 'tag:' .. config.siteurl:match('^%w+://([^/]+)') .. ',' .. pagedata.date .. ':' .. pagedata.rellink 48 pagedata.idorder = pagedata.rellinkdir .. time_created 49 50 -- HTML content 51 if file.is_content(filepath) then 52 local that_content 53 54 -- Lua compilation (with etlua) is needed in content 55 if pagedata.lua then 56 local lua_in_content = etlua.compile(file.read(filepath)) 57 58 that_content = lua_in_content(lume.extend({}, config, pagedata)) 59 else 60 that_content = file.read(filepath) 61 end 62 63 if file.is_markdown(filepath) then 64 pagedata.content = markdown(that_content) 65 elseif file.is_html(filepath) then 66 pagedata.content = that_content 67 end 68 end 69 70 -- Summary 71 if pagedata.summary then 72 pagedata.summary = markdown(pagedata.summary) 73 end 74 75 -- List (and Feed) 76 if file.is_index(filepath) and pagedata.list ~= false then 77 pagedata.list = file.get_list(filepath, contentdir) 78 end 79 80 -- Collection 81 -- File list 82 if pagedata.collection then 83 pagedata.collection_list = file.get_collection(pagedata.collection) 84 end 85 86 -- Change the language for a specific content 87 pagedata.language = pagedata.language or config.language 88 89 -- Templates 90 pagedata.template = pagedata.template or pagedata.posttype or 'default' 91 pagedata.layout = pagedata.layout or 'layout' 92 pagedata.head = pagedata.head or 'head' 93 pagedata.navigation = pagedata.navigation or 'navigation' 94 pagedata.footer = pagedata.footer or 'footer' 95 pagedata.feed = pagedata.feed or 'feed.xml' 96 97 -- Include a partial template and compile it 98 -- @usage <%- include("test-partial") %> 99 pagedata.include = function(templatename) 100 local inc = etlua.compile(file.read(template.find(templates, templatename))) 101 102 return inc(lume.extend({}, config, pagedata)) 103 end 104 105 return lume.extend({}, config, pagedata, {templates = templates}) 106 end 107 108 return model