list.lua (4051B)
1 -- @module list 2 local list = {} 3 -- 4 local dirtree = require 'satelito.dirtree' 5 local file = require 'satelito.file' 6 local model = require 'satelito.model' 7 local lume = require 'satelito.lib.lume.lume' 8 9 -- Pagination 10 function list.set_pagination(pagelist, len) 11 local slicedlist = {} 12 local i = 1 13 14 while(i < #pagelist) do 15 local j 16 17 if i == 1 then 18 j = 1 19 i = i + (len-1) 20 else 21 j = i + 1 22 i = i + len 23 end 24 25 slicedlist[#slicedlist+1] = lume.slice(pagelist, j, i) 26 end 27 28 return slicedlist 29 end 30 31 -- Get children contents 32 function list.get_children(children_list, sitedata, asc) 33 local children = {} 34 local relpaths = {} 35 local contentdir = _G.Satelito.contentdir 36 37 if children_list then 38 for i = 1, #sitedata do 39 relpaths[#relpaths+1] = sitedata[i].relpath 40 end 41 42 for i = 1, #children_list do 43 local is_exists = lume.find(relpaths, children_list[i]) 44 45 if is_exists then 46 children[#children+1] = sitedata[is_exists] 47 else 48 children[#children+1] = model.set(contentdir..children_list[i]) 49 end 50 end 51 52 -- Sorting ASC if list have asc set to true 53 if asc then 54 table.sort(children, function(a, b) return tonumber(a.time_created) < tonumber(b.time_created) end) 55 else 56 table.sort(children, function(a, b) return tonumber(a.time_created) > tonumber(b.time_created) end) 57 end 58 end 59 60 return children 61 end 62 63 -- Archives 64 function list.get_archives(contentdir) 65 local archives_table = {} 66 67 for filepath in dirtree.get(contentdir) do 68 if file.is_markdown(filepath) 69 or file.is_html(filepath) 70 and file.get_metafile(filepath) 71 then 72 local metafile = file.get_metafile(filepath) 73 74 if metafile.date ~= '0000-00-00' then 75 local year = string.sub(metafile.date, 1, 4) 76 local month = string.sub(metafile.date, 6, 7) 77 78 if archives_table[year] then 79 archives_table[year][month] = {} 80 else 81 archives_table[year] = { 82 [month] = {} 83 } 84 end 85 end 86 end 87 end 88 89 for filepath in dirtree.get(contentdir) do 90 if file.is_markdown(filepath) 91 or file.is_html(filepath) 92 and file.get_metafile(filepath) 93 then 94 local metafile = file.get_metafile(filepath) 95 96 if metafile.date ~= '0000-00-00' then 97 local year = string.sub(metafile.date, 1, 4) 98 local month = string.sub(metafile.date, 6, 7) 99 local day = string.sub(metafile.date, 9, 10) 100 local archive_info = { 101 day = day, 102 rellink = file.get_rellink(filepath, contentdir), 103 title = metafile.title 104 } 105 106 archives_table[year][month][#archives_table[year][month]+1] = archive_info 107 end 108 end 109 end 110 111 return archives_table 112 end 113 114 -- Tags 115 function list.get_tags(contentdir) 116 local tags_table = {} 117 118 -- Insert all the keywords as key of an empty subtable 119 for filepath in dirtree.get(contentdir) do 120 if file.is_markdown(filepath) 121 or file.is_html(filepath) 122 and file.get_metafile(filepath) 123 then 124 local metafile = file.get_metafile(filepath) 125 126 if metafile.keywords ~= nil then 127 for _, keyword in pairs(file.get_metafile(filepath).keywords) do 128 if keyword ~= '' then 129 tags_table[keyword] = {} 130 end 131 end 132 end 133 end 134 end 135 136 -- After insert relative links in each keyword's table 137 for filepath in dirtree.get(contentdir) do 138 if file.is_markdown(filepath) 139 or file.is_html(filepath) 140 and file.get_metafile(filepath) 141 then 142 local metafile = file.get_metafile(filepath) 143 144 if metafile.keywords ~= nil then 145 for _, keyword in pairs(file.get_metafile(filepath).keywords) do 146 if tags_table[keyword] then 147 local tags_info = { 148 title = metafile.title, 149 rellink = file.get_rellink(filepath, contentdir) 150 } 151 152 tags_table[keyword][#tags_table[keyword]+1] = tags_info 153 end 154 end 155 end 156 end 157 end 158 159 return tags_table 160 end 161 162 return list