Ruby CGI scripts that I used to set up my website https://yessiest.512mb.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
2.1 KiB

#!/usr/bin/ruby
#
#Copyright 2022 Yessiest (yessiest@memeware.net)
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
# Ruby CGI module to produce a directory listing of filterable content
# Variables
ROOT_PATH="/www/"
DIR_PATH="/www/blog"
EXTENSIONS=[".md"]
TEMPLATE_PRE="template.pre.html"
TEMPLATE_POST="template.post.html"
TEMPLATE_CSS="template.css"
# Script
require 'cgi'
$cgi = CGI.new("html5")
template_css = ""
template_pre = ""
template_post = ""
if File::exists?( ROOT_PATH+TEMPLATE_CSS ) then
template_css = "<link rel = \"stylesheet\", href = \"#{TEMPLATE_CSS}\">\n"
end
if File::exists?( ROOT_PATH+TEMPLATE_PRE) then
template_pre_f = File.new( ROOT_PATH+TEMPLATE_PRE, "r")
template_pre = template_pre_f.read
template_pre_f.close
end
if File::exists?( ROOT_PATH+TEMPLATE_POST) then
template_post_f = File.new( ROOT_PATH+TEMPLATE_POST, "r")
template_post = template_post_f.read
template_post_f.close
end
def listdir(path)
$cgi.ul {
list = ""
Dir.foreach(path) do |fname|
if (not ([".",".."].include? fname)) and
File.directory?(path+"/"+fname) then
list = list+"\n"+
"<li>"+fname+": </li>"+
listdir(path+"/"+fname)
elsif fname.match(/\.\w*$/) and
EXTENSIONS.include?(fname.match(/\.\w*$/)[0]) then
list = list+"\n"+$cgi.li {
$cgi.a(href=((path+"/").gsub(ROOT_PATH,"")+fname)) {
fname
}
}+"\n"
end
end
list
}
end
content = $cgi.body {
template_pre+"\n"+
listdir(DIR_PATH)+"\n"+
template_post+"\n"
}
$cgi.out {
$cgi.html {
$cgi.head { "\n"+
$cgi.title { "File listing" }+"\n"+
template_css
} +
content
}
}