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