From e6cc63c2c14fc6c30edfc42e1ee3b5c8f550c6fc Mon Sep 17 00:00:00 2001 From: Astoria Date: Sun, 23 Oct 2022 14:26:46 -0500 Subject: [PATCH] Sorting by date instead of alphabetically --- v5.rb | 22 ++++++++++++++++++++++ views/archiveFolderIndex.erb | 28 ++++++++++++++++------------ views/blogpageERB.erb | 4 +--- views/blogpageMD.erb | 5 ++--- views/folderIndex.erb | 29 +++++++++++++++++------------ views/folderpage.erb | 1 + views/homeIndex.erb | 30 +++++++++++++++++------------- 7 files changed, 76 insertions(+), 43 deletions(-) mode change 100644 => 100755 v5.rb diff --git a/v5.rb b/v5.rb old mode 100644 new mode 100755 index ea20901..b9689e2 --- a/v5.rb +++ b/v5.rb @@ -1,6 +1,28 @@ #! /usr/bin/ruby3.0 require 'sinatra' require 'erb' +require 'date' + +class Page + String @path + @date + def initialize(path, date) + @path = path + @date = date + end + def setpath(path) + @path = path + end + def setdate(date) + @date = date + end + def getpath + return @path + end + def getdate + return @date + end +end get '/' do erb :greetpage diff --git a/views/archiveFolderIndex.erb b/views/archiveFolderIndex.erb index 0720e11..74f073c 100644 --- a/views/archiveFolderIndex.erb +++ b/views/archiveFolderIndex.erb @@ -1,18 +1,22 @@ <% contents = Dir.children("./" + folder).grep(/#{searchquery}/) %> +<% contentsArray = Array.new %> <% contents.each do |content| %> - <% if content.end_with?(".erb") %> - <% file = content %> - <% if File.exists?("./" + folder + "/" + file + ".meta") %> - <%= erb :blogpageERB, locals: {file: "./" + folder + "/" + content, meta: "./" + folder + "/" + file + ".meta"} %> + <% if content.end_with?(".erb") || content.end_with?(".md") %> + <% if File.exists?("./" + folder + "/" + content + ".meta") %> + <% newPage = Page.new(content, DateTime.parse(File.read("./" + folder + "/" + content + ".meta"), "%Y.%m.%d")) %> <% else %> - <%= erb :blogpageERB, locals: {file: "./" + folder+ "/" + content} %> - <% end %> - <% elsif content.end_with?(".md") %> - <% file = content %> - <% if File.exists?("./" + folder + "/" + file + ".meta") %> - <%= erb :blogpageMD, locals: {file: "./" + folder + "/" + content, meta: "./" + folder + "/" + file + ".meta"} %> - <% else %> - <%= erb :blogpageMD, locals: {file: "./" + folder + "/" + content} %> + <% newPage = Page.new(content, DateTime.parse("1970.01.01", "%Y.%m.%d")) %> <% end %> + <% contentsArray.push(newPage) %> + <% end %> +<% end %> + +<% contentsArray.sort_by! { |page| page.getdate } %> + +<% contentsArray.reverse.each do |content| %> + <% if content.getpath.end_with?(".erb") %> + <%= erb :blogpageERB, locals: {file: "./" + folder + "/" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> + <% elsif content.getpath.end_with?(".md") %> + <%= erb :blogpageMD, locals: {file: "./" + folder + "/" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> <% end %> <% end %> \ No newline at end of file diff --git a/views/blogpageERB.erb b/views/blogpageERB.erb index 16f96ee..2591af3 100644 --- a/views/blogpageERB.erb +++ b/views/blogpageERB.erb @@ -1,11 +1,9 @@
<%= ERB.new(File.read(file)).result(binding) %> - <% if defined?(meta) %> - <%= File.read(meta) %> + <%= date %> - <% end %> Article Page
\ No newline at end of file diff --git a/views/blogpageMD.erb b/views/blogpageMD.erb index e942baa..480eb67 100644 --- a/views/blogpageMD.erb +++ b/views/blogpageMD.erb @@ -1,11 +1,10 @@
<%= markdown(File.read(file)) %> - <% if defined?(meta) %> + - <%= File.read(meta) %> + <%= date %> - <% end %> Article Page
\ No newline at end of file diff --git a/views/folderIndex.erb b/views/folderIndex.erb index 0efc803..44b6f93 100644 --- a/views/folderIndex.erb +++ b/views/folderIndex.erb @@ -1,18 +1,23 @@ +<% contentsArray = Array.new %> + <% contents = Dir.children("." + request.path_info)%> <% contents.each do |content| %> - <% if content.end_with?(".erb") %> - <% file = content %> - <% if File.exists?("." + request.path_info + "/" + file + ".meta") %> - <%= erb :blogpageERB, locals: {file: "." + request.path_info + "/" + content, meta: "." + request.path_info + "/" + file + ".meta"} %> + <% if content.end_with?(".erb") || content.end_with?(".md") %> + <% if File.exists?("." + request.path_info + "/" + content + ".meta") %> + <% newPage = Page.new(content, DateTime.parse(File.read("." + request.path_info + "/" + content + ".meta"), "%Y.%m.%d")) %> <% else %> - <%= erb :blogpageERB, locals: {file: "." + request.path_info + "/" + content} %> - <% end %> - <% elsif content.end_with?(".md") %> - <% file = content %> - <% if File.exists?("." + request.path_info + "/" + file + ".meta") %> - <%= erb :blogpageMD, locals: {file: "." + request.path_info + "/" + content, meta: "." + request.path_info + "/" + file + ".meta"} %> - <% else %> - <%= erb :blogpageMD, locals: {file: "." + request.path_info + "/" + content} %> + <% newPage = Page.new(content, DateTime.parse("1970.01.01", "%Y.%m.%d")) %> <% end %> + <% contentsArray.push(newPage) %> + <% end %> +<% end %> + +<% contentsArray.sort_by! { |page| page.getdate } %> + +<% contentsArray.reverse.each do |content| %> + <% if content.getpath.end_with?(".erb") %> + <%= erb :blogpageERB, locals: {file: "." + request.path_info + "/" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> + <% elsif content.getpath.end_with?(".md") %> + <%= erb :blogpageMD, locals: {file: "." + request.path_info + "/" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> <% end %> <% end %> \ No newline at end of file diff --git a/views/folderpage.erb b/views/folderpage.erb index f3766b4..0a54234 100644 --- a/views/folderpage.erb +++ b/views/folderpage.erb @@ -25,6 +25,7 @@ <%= erb :folderIndex %> <%= erb :folderArchive, locals: {folder: request.path_info, title: title} %> <% else %> + <%= erb :topbar %> <%= erb :noFile %> <% end %> <% else %> diff --git a/views/homeIndex.erb b/views/homeIndex.erb index 8c27a83..d09d6cf 100644 --- a/views/homeIndex.erb +++ b/views/homeIndex.erb @@ -1,23 +1,27 @@ <% foldersOfInterest = ["media", "technology", "news"] %> +<% contentsArray = Array.new %> <% foldersOfInterest.each do |folder| %> <% if Dir.exists? folder %> - <% contents =Dir.children("./" + folder) %> + <% contents = Dir.children("./" + folder)%> <% contents.each do |content| %> - <% if content.end_with?(".erb") %> - <% file = content %> - <% if File.exists?("./" + folder + "/" + file + ".meta") %> - <%= erb :blogpageERB, locals: {file: "./" + folder+ "/" + content, meta: "./" + folder + "/" + file + ".meta"} %> - <% else %> - <%= erb :blogpageERB, locals: {file: "./" + folder + "/" + content} %> - <% end %> - <% elsif content.end_with?(".md") %> - <% file = content %> - <% if File.exists?("./" + folder+ "/" + file + ".meta") %> - <%= erb :blogpageMD, locals: {file: "./" + folder + "/" + content, meta: "./" + folder + "/" + file + ".meta"} %> + <% if content.end_with?(".erb") || content.end_with?(".md") %> + <% if File.exists?("./" + folder + "/" + content + ".meta") %> + <% newPage = Page.new(folder + "/" + content, DateTime.parse(File.read("./" + folder + "/" + content + ".meta"), "%Y.%m.%d")) %> <% else %> - <%= erb :blogpageMD, locals: {file: "./" + folder + "/" + content} %> + <% newPage = Page.new(folder + "/" + content, DateTime.parse("1970.01.01", "%Y.%m.%d")) %> <% end %> + <% contentsArray.push(newPage) %> <% end %> <% end %> <% end %> +<% end %> + +<% contentsArray.sort_by! { |page| page.getdate } %> + +<% contentsArray.reverse.each do |content| %> + <% if content.getpath.end_with?(".erb") %> + <%= erb :blogpageERB, locals: {file: "./" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> + <% elsif content.getpath.end_with?(".md") %> + <%= erb :blogpageMD, locals: {file: "./" + content.getpath, date: content.getdate.strftime("%Y.%m.%d")} %> + <% end %> <% end %> \ No newline at end of file