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