Yessiest
2 years ago
3 changed files with 275 additions and 1 deletions
-
44README.md
-
84listing.rb
-
148markdown.rb
@ -1,3 +1,45 @@ |
|||||
# website.rb |
# website.rb |
||||
|
|
||||
Ruby CGI scripts that I used to set up my website |
|
||||
|
Ruby CGI scripts that I used to set up my website |
||||
|
|
||||
|
## What is included |
||||
|
|
||||
|
- listing.rb - generates a recursve listing of files at your preferred location |
||||
|
- markdown.rb - processes markdown (partially) and converts it to HTML |
||||
|
|
||||
|
## How to apply this to your site |
||||
|
|
||||
|
- Drop all .rb files into your web directory root |
||||
|
- Edit markdown.rb and point ROOT\_PATH to the full web directory root path |
||||
|
- Edit listing.rb and point ROOT\_PATH to the full web directory root path and DIR\_PATH to the directory where markdown files are located (they have to be in adirectory under root!) |
||||
|
- If you're using lighttpd: |
||||
|
- Install `mod_cgi` in your config file and add "listing.rb" as an index |
||||
|
``` |
||||
|
server.modules += ("mod_cgi") |
||||
|
index-file.names += ("listing.rb") |
||||
|
cgi.assign = ( |
||||
|
".rb" => "/usr/bin/ruby" |
||||
|
) |
||||
|
``` |
||||
|
- If you're using apache: |
||||
|
- lmao idk |
||||
|
- just add CGI module, make it run ruby files and make listing.rb an index |
||||
|
- If you're using NGINX: |
||||
|
- Install lighttpd and configure it the same way as above, but use a custom port for the server |
||||
|
- Add `listing.rb` as an index file to your preferred location |
||||
|
- Add a reverse proxy clause in NGINX config to proxy all .md and .rb requests to the lighttpd server |
||||
|
```nginx |
||||
|
... |
||||
|
|
||||
|
# pass ruby files to lighttpd |
||||
|
location ~ \.rb { |
||||
|
proxy_pass http://127.0.0.1:6969; |
||||
|
} |
||||
|
|
||||
|
# pass .md files as query parameters to markdown.rb |
||||
|
location ~ \.md { |
||||
|
proxy_pass http://127.0.0.1:6969/markdown.rb?docfile=$uri; |
||||
|
} |
||||
|
|
||||
|
... |
||||
|
``` |
@ -0,0 +1,84 @@ |
|||||
|
#!/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="/var/www/" |
||||
|
DIR_PATH="/var/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 = cgi.link( rel = "stylesheet", href = TEMPLATE_CSS) |
||||
|
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 |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,148 @@ |
|||||
|
#!/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 human-readable HTML from markdown pages |
||||
|
# Variables |
||||
|
ROOT_PATH="/var/www/" |
||||
|
TAB_MULTIPLIER=10 |
||||
|
TAB_UNIT="px" |
||||
|
QUOTE_TAB=4 |
||||
|
TEMPLATE_PRE="template.pre.html" |
||||
|
TEMPLATE_POST="template.post.html" |
||||
|
TEMPLATE_CSS="template.css" |
||||
|
# Script |
||||
|
require 'cgi' |
||||
|
|
||||
|
cgi = CGI.new("html5") |
||||
|
|
||||
|
def ident(ident) |
||||
|
return "<span style=\"margin-left: #{(ident*TAB_MULTIPLIER).to_s+TAB_UNIT}\">" |
||||
|
end |
||||
|
|
||||
|
def _parse_list(text,mode=["ul",/ *-/]) |
||||
|
new_text = "" |
||||
|
list_started = false |
||||
|
text.each_line do |line| |
||||
|
starts = line.start_with?(mode[1]) |
||||
|
l_ident = line.match(/ */)[0].length |
||||
|
if (not list_started) and (starts) then |
||||
|
list_started = true |
||||
|
new_text=new_text+"<#{mode[0]}>\n<li>#{ident l_ident}\n"+line.gsub(mode[1],"")+"</span></li>\n" |
||||
|
elsif (list_started) and (not starts) then |
||||
|
list_started = false |
||||
|
new_text=new_text+"</#{mode[0]}>\n"+line |
||||
|
elsif (list_started) and (starts) then |
||||
|
new_text = new_text+"<li>#{ident l_ident}\n"+line.gsub(mode[1],"")+"</span></li>\n" |
||||
|
else |
||||
|
new_text = new_text+line |
||||
|
end |
||||
|
end |
||||
|
return new_text |
||||
|
end |
||||
|
|
||||
|
template_css = "" |
||||
|
template_pre = "" |
||||
|
template_post = "" |
||||
|
if File::exists?( ROOT_PATH+TEMPLATE_CSS ) then |
||||
|
template_css = cgi.link( rel = "stylesheet", href = TEMPLATE_CSS) |
||||
|
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 |
||||
|
|
||||
|
doc = "--- |
||||
|
# Error 404: Document not available |
||||
|
|
||||
|
Document you requested is not available |
||||
|
---" |
||||
|
# Absolutely containing the script to the ROOT_PATH and making it only read .md |
||||
|
if cgi["docfile"].end_with?(".md") and |
||||
|
(File.absolute_path(ROOT_PATH+cgi["docfile"]).start_with?(ROOT_PATH)) and |
||||
|
File::exists?( ROOT_PATH+cgi["docfile"] ) then |
||||
|
docfile = File.new(ROOT_PATH+cgi["docfile"],"r") |
||||
|
doc = docfile.read |
||||
|
docfile.close |
||||
|
end |
||||
|
|
||||
|
content = cgi.body { |
||||
|
doc = "\n"+CGI.escapeHTML(doc) |
||||
|
|
||||
|
# Extra |
||||
|
# - Horizontal rule |
||||
|
doc = doc.gsub(/-{3,}/,"<hr />") |
||||
|
# - Paragraph separators |
||||
|
doc = doc.gsub(/(?<=\n\n)(.*?)(?=\n\n)/m,"<p>\n\\1\n</p>") |
||||
|
|
||||
|
# Parse lists |
||||
|
# - Unordered lists |
||||
|
doc = _parse_list(doc) |
||||
|
# - Ordered lists |
||||
|
doc = _parse_list(doc,mode = ["ol",/ *\d*\./]) |
||||
|
|
||||
|
# Bounded markup |
||||
|
# - Bold/Italics |
||||
|
doc = doc.gsub(/(?<!\\)\*{3}(.*?)(?<!\\)\*{3}/m,"<i><b>\\1</b></i>") |
||||
|
doc = doc.gsub(/(?<!\\)\*{2}(.*?)(?<!\\)\*{2}/m,"<b>\\1</b>") |
||||
|
doc = doc.gsub(/(?<!\\)\*{1}(.*?)(?<!\\)\*{1}/m,"<i>\\1</i>") |
||||
|
# - Underline |
||||
|
doc = doc.gsub(/(?<!\\)_(.*?)_(?<!\\)/m,"<span style=\"text-decoration:underline;\">\\1</span>") |
||||
|
# - Strikethrough |
||||
|
doc = doc.gsub(/(?<!\\)~(.*?)~(?<!\\)/m,"<strike>\\1</strike>") |
||||
|
|
||||
|
# Unbounded (line) markup |
||||
|
# - Headers |
||||
|
doc = doc.gsub(/(?<=\n)\#{4} ?(.*)/,"<h4>\\1</h4>") |
||||
|
doc = doc.gsub(/(?<=\n)\#{3} ?(.*)/,"<h3>\\1</h3>") |
||||
|
doc = doc.gsub(/(?<=\n)\#{2} ?(.*)/,"<h2>\\1</h2>") |
||||
|
doc = doc.gsub(/(?<=\n)\# ?(.*)/,"<h1>\\1</h1>") |
||||
|
|
||||
|
# - Quote |
||||
|
doc = doc.gsub(/(?<=\n)>(.*)/,"#{ident QUOTE_TAB}<span class=\"quote\">\\1</span></span>") |
||||
|
|
||||
|
# Links |
||||
|
# - Images |
||||
|
doc = doc.gsub(/!\[(.*?)\]\((.*?)\)/,"<img src=\"\\2\" alt=\"\\1\" />") |
||||
|
# - Hyperlinks |
||||
|
doc = doc.gsub(/\[(.*?)\]\((.*?)\)/,"<a href=\"\\2\">\\1</a>") |
||||
|
|
||||
|
# Code blocks |
||||
|
# - Full block |
||||
|
doc = doc.gsub(/(?<!\\)`{3}(.*?)(?<!\\)`{3}/m,"<pre><code class=\"codeblock\">\n\\1</pre></code>") |
||||
|
# - Inline block |
||||
|
doc = doc.gsub(/(?<!\\)`{2}(.*?)(?<!\\)`{2}/m,"<code class=\"codeinline\">\\1</code>") |
||||
|
|
||||
|
template_pre+"\n"+ |
||||
|
doc+"\n"+ |
||||
|
template_post+"\n" |
||||
|
} |
||||
|
|
||||
|
cgi.out { |
||||
|
cgi.html { |
||||
|
cgi.head { "\n"+ |
||||
|
cgi.title { cgi["docfile"].gsub(/.md$/,"") }+"\n"+ |
||||
|
template_css |
||||
|
} + |
||||
|
content |
||||
|
} |
||||
|
} |
||||
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue