main.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. local main = {}
  2. local sailor = require "sailor"
  3. local markdown = require "markdown"
  4. local function read_md(path)
  5. local f = io.open (sailor.path.."/"..path..".md", "rb")
  6. if f then
  7. local src = f:read("*all")
  8. f:close()
  9. return markdown(src)
  10. end
  11. return false
  12. end
  13. local function is_directory(path)
  14. local attr = lfs.attributes(path)
  15. return attr and attr.mode == "directory"
  16. end
  17. local function get_categories()
  18. local lfs = require "lfs"
  19. local categories = {}
  20. for file in lfs.dir(sailor.path..'/posts') do
  21. if is_directory(sailor.path..'/posts/'..file) and file ~= '.' and file ~= '..' then
  22. categories[#categories+1] = file
  23. end
  24. end
  25. return categories
  26. end
  27. local function get_category_total(name)
  28. local lfs = require "lfs"
  29. local total = 0
  30. for file in lfs.dir(sailor.path..'/posts/'..name) do
  31. total = total + 1
  32. end
  33. return total - 2
  34. end
  35. function main.index(page)
  36. local posts = assert(require "posts.posts_meta")
  37. local total = #posts
  38. local posts_per_page = sailor.conf.custom and sailor.conf.custom.posts_per_page or 3
  39. local page_counter = tonumber(page.GET.p) or 1
  40. local max_page = page_counter + 1
  41. local categories = get_categories()
  42. local filter
  43. if page.GET.c then
  44. filter = function(p)
  45. if not p or not p.category then return false end
  46. return p.category == page.GET.c
  47. end
  48. total = get_category_total(page.GET.c)
  49. end
  50. local min = page_counter * posts_per_page - posts_per_page + 1
  51. if posts_per_page > total then posts_per_page = total end
  52. local page_posts = {}
  53. while #page_posts < posts_per_page do
  54. if not posts[min] then
  55. break
  56. end
  57. if not filter or filter(posts[min]) then
  58. local category = posts[min].category or 'general'
  59. posts[min].body = read_md('posts/'..category..'/'..posts[min].short_url)
  60. if posts[min].body then
  61. posts[min].url = sailor.conf.app_url..posts[min].category.."/"..posts[min].short_url
  62. page_posts[#page_posts+1] = posts[min]
  63. end
  64. end
  65. min = min + 1
  66. end
  67. -- Disable see older pagination
  68. if not posts[min] then
  69. max_page = page_counter
  70. elseif filter then
  71. max_page = page_counter
  72. while min < #posts do
  73. if filter(posts[min]) then
  74. max_page = page_counter + 1
  75. break
  76. end
  77. min = min + 1
  78. end
  79. end
  80. page:render('index',{posts = page_posts, categories = categories, page_counter=page_counter, max_page = max_page})
  81. end
  82. function main.post(page)
  83. if not page.GET.t or not page.GET.c then return 404 end
  84. local posts = require "posts.posts_meta"
  85. local categories = get_categories()
  86. local post
  87. for _,p in ipairs(posts) do
  88. if p.short_url == page.GET.t and p.category == page.GET.c then
  89. post = p
  90. post.body = read_md('posts/'..post.category..'/'..post.short_url)
  91. if not post.body then error('Error opening post markdown') end
  92. end
  93. end
  94. if not post then return 404 end
  95. post.url = sailor.conf.app_url..post.category.."/"..post.short_url
  96. page.title = page.title.. ' | '..post.page_title
  97. page:render('post',{post = post, categories = categories})
  98. end
  99. function main.about(page)
  100. local categories = get_categories()
  101. page:render('about',{categories = categories})
  102. end
  103. function main.archive(page)
  104. local posts = require "posts.posts_meta"
  105. local categories = get_categories()
  106. for _,p in pairs(posts) do
  107. p.url = sailor.conf.app_url..p.category.."/"..p.short_url
  108. p.category = p.category:gsub("^%l", string.upper)
  109. end
  110. page:render('archive',{posts = posts, categories = categories})
  111. end
  112. return main