ferron

[ARCHIVED] Ferron is a fast and simple static website generator made with Lua.
Log | Files | Refs | Submodules | README | LICENSE

content.lua (3302B)


      1 -- content.lua
      2 
      3 -- @TODO It would be nice to have at the Ferron root a symlink to the
      4 -- last created content.
      5 local content = {}
      6 local contentmeta = {
      7   __call = function(self, key, vars)
      8     print(key)
      9   end
     10 }
     11 
     12 -- Required Packages
     13 local site = require 'ferron.site'
     14 local fileutils = require 'ferron.utilities.file-utils'
     15 local slugify = require 'ferron.utilities.lua-slugify.slugify'
     16 local tb = require 'ferron.utilities.table-utils'
     17 local tp = require 'ferron.utilities.template-utils'
     18 
     19 function content.getarchetypes(dir)
     20   local archetypes = {}
     21   local archetypesdir = dir or site.archetypes
     22 
     23   if fileutils.isDirectory(archetypesdir) then
     24     for archetype, attr in fileutils.getdirtree(archetypesdir) do
     25       if attr.mode ~= 'directory'
     26         and archetype:match('^.+(%..+)$') == '.json'
     27       then
     28         archetypes[#archetypes+1] = archetype:match('^.+/(.+)$'):match('(.+)%..*')
     29       end
     30     end
     31 
     32     return archetypes
     33   end
     34 end
     35 
     36 function content.setcontent()
     37   local archetypes = content.getarchetypes(site.archetypes)
     38   local whicharchetype = nil
     39   local whichtitle = nil
     40 
     41   repeat
     42     io.write('Which archetypes do you need to complete your request? \n')
     43 
     44     for k, v in ipairs(archetypes) do
     45       io.write(k .. ') ' .. v .. '\n')
     46     end
     47 
     48     io.write('Please enter the number... \n')
     49     io.flush()
     50 
     51     whicharchetype=io.read()
     52   until (tb.haskey(archetypes, tonumber(whicharchetype))) == true
     53 
     54   repeat
     55     io.write('Please write the title of your new content... \n')
     56     io.flush()
     57 
     58     whichtitle=io.read()
     59   until (type(tostring(whichtitle))) == 'string'
     60 
     61   return content.makecontent(archetypes[tonumber(whicharchetype)], tostring(whichtitle))
     62 end
     63 
     64 function content.makecontent(contenttype, contenttitle)
     65   local archetypetype = contenttype or arg[1]
     66   local title = contenttitle or arg[2]
     67 
     68   if tb.hasvalue(content.getarchetypes(), archetypetype) and type(title) == 'string' then
     69     local archetype = {}
     70     local contentpath = site.content .. '/' .. archetypetype .. '/' .. os.date('%Y') ..'/'..os.date('%m') .. '/'
     71     local archetypepath = site.archetypes .. '/'
     72 
     73     archetype.title = title
     74     archetype.filename = slugify(archetype.title)
     75     archetype.date = os.date('%Y-%m-%d')
     76     archetype.datetime = os.date('%H:%M:%S')
     77     archetype.template = archetypetype
     78 
     79     if not fileutils.isDirectory(contentpath) then
     80       fileutils.mkdir(contentpath)
     81     end
     82 
     83     -- Build the markdown file
     84     fileutils.pushfilecontent(
     85       contentpath .. archetype.filename .. '.md',
     86       tp.rendermustache(
     87         fileutils.pullfilecontent(archetypepath .. archetypetype .. '.md' ),
     88         archetype
     89       )
     90     )
     91 
     92     -- Build the JSON file
     93     fileutils.pushfilecontent(
     94       contentpath .. archetype.filename .. '.json',
     95       tp.rendermustache(
     96         fileutils.pullfilecontent(archetypepath .. archetypetype .. '.json'),
     97         archetype
     98       )
     99     )
    100 
    101     if fileutils.isFile(contentpath .. archetype.filename .. '.md')
    102       and fileutils.isFile(contentpath .. archetype.filename .. '.json')
    103     then
    104       print('¤¤ The files `' .. contentpath .. '{' .. archetype.filename .. '.md,' .. archetype.filename .. '.json}` have been created. ¤¤')
    105     else
    106       print('! Error, something went wrong !')
    107     end
    108   end
    109 end
    110 
    111 return setmetatable(content, contentmeta)