# dynamic vy by Christian Neukirchen, MIT-licensed.
b = binding; Dir.mkdir("site")  rescue nil
def n(s); s.downcase.delete("^a-z0-9"); end;  def pages; @pages; end
def reload; @pages = pages = Dir["entries/*[^~]"].map { |f| File.basename f }
@rx = Regexp.new pages.map { |p|"\\b"+p.gsub(/[\W-]/,'.?')+"\\b"}.join('|'), "i"
@words = Hash[*pages.map { |e| [n(e), e] }.flatten] end; reload
Dvy = lambda { |env| p = env["PATH_INFO"][/[\w-]+$/] || "index"
reload  if env["REQUEST_METHOD"] == "POST"
pages.include? p or return 404, {"Content-Type" => "text/plain"}, ["Not found"]
[200, {"Content-Type" => "text/html"},
["<!DOCTYPE html><meta charset='UTF-8'><title>vy: #{p}</title>",
"<style>body, pre { background-color: #333; color: #eee;
font: 12px Monaco, fixed, Consolas, monospace; }
a { color: #ffcb00; text-decoration: none; }</style>\n<pre>",
File.read("entries/#{p}").gsub(/^!(.*)/) { eval $1, b }.
     gsub(@rx) { |w| %Q{<a href="#{@words[n(w)]}">#$&</a>}}, '</pre>']]}