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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #!/usr/bin/ruby
  2. #
  3. #Copyright 2022 Yessiest (yessiest@memeware.net)
  4. #
  5. #Licensed under the Apache License, Version 2.0 (the "License");
  6. #you may not use this file except in compliance with the License.
  7. #You may obtain a copy of the License at
  8. #
  9. #http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. #Unless required by applicable law or agreed to in writing, software
  12. #distributed under the License is distributed on an "AS IS" BASIS,
  13. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. #See the License for the specific language governing permissions and
  15. #limitations under the License.
  16. # Ruby CGI module to produce a directory listing of filterable content
  17. # Variables
  18. ROOT_PATH="/www/"
  19. DIR_PATH="/www/blog"
  20. EXTENSIONS=[".md"]
  21. TEMPLATE_PRE="template.pre.html"
  22. TEMPLATE_POST="template.post.html"
  23. TEMPLATE_CSS="template.css"
  24. # Script
  25. require 'cgi'
  26. $cgi = CGI.new("html5")
  27. template_css = ""
  28. template_pre = ""
  29. template_post = ""
  30. if File::exists?( ROOT_PATH+TEMPLATE_CSS ) then
  31. template_css = "<link rel = \"stylesheet\", href = \"#{TEMPLATE_CSS}\">\n"
  32. end
  33. if File::exists?( ROOT_PATH+TEMPLATE_PRE) then
  34. template_pre_f = File.new( ROOT_PATH+TEMPLATE_PRE, "r")
  35. template_pre = template_pre_f.read
  36. template_pre_f.close
  37. end
  38. if File::exists?( ROOT_PATH+TEMPLATE_POST) then
  39. template_post_f = File.new( ROOT_PATH+TEMPLATE_POST, "r")
  40. template_post = template_post_f.read
  41. template_post_f.close
  42. end
  43. def listdir(path)
  44. $cgi.ul {
  45. list = ""
  46. Dir.foreach(path) do |fname|
  47. if (not ([".",".."].include? fname)) and
  48. File.directory?(path+"/"+fname) then
  49. list = list+"\n"+
  50. "<li>"+fname+": </li>"+
  51. listdir(path+"/"+fname)
  52. elsif fname.match(/\.\w*$/) and
  53. EXTENSIONS.include?(fname.match(/\.\w*$/)[0]) then
  54. list = list+"\n"+$cgi.li {
  55. $cgi.a(href=((path+"/").gsub(ROOT_PATH,"")+fname)) {
  56. fname
  57. }
  58. }+"\n"
  59. end
  60. end
  61. list
  62. }
  63. end
  64. content = $cgi.body {
  65. template_pre+"\n"+
  66. listdir(DIR_PATH)+"\n"+
  67. template_post+"\n"
  68. }
  69. $cgi.out {
  70. $cgi.html {
  71. $cgi.head { "\n"+
  72. $cgi.title { "File listing" }+"\n"+
  73. template_css
  74. } +
  75. content
  76. }
  77. }