Compare commits
No commits in common. "f807d5ca8d393adf3abce58a017acfed30eb55c8" and "4312d31268bf7fd3c75dbd4b4fd1156dfdceb68d" have entirely different histories.
f807d5ca8d
...
4312d31268
|
@ -1,19 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
|
|
||||||
require 'hyde'
|
|
||||||
|
|
||||||
app = Hyde::Server.new do
|
|
||||||
root ENV["PWD"]
|
|
||||||
index ["index.html"]
|
|
||||||
post "/" do
|
|
||||||
formdata = form if form?
|
|
||||||
puts formdata.pretty_inspect
|
|
||||||
erubi(file("index.rhtml"), { formdata: formdata }).run
|
|
||||||
end
|
|
||||||
get "/" do
|
|
||||||
erubi(file("index.rhtml")).run
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
run app
|
|
|
@ -1,35 +0,0 @@
|
||||||
<!DOCTYPE>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>Form upload test</title>
|
|
||||||
<style>
|
|
||||||
.form { display: flex; flex-direction: column }
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Form upload test</h1>
|
|
||||||
<hr/>
|
|
||||||
<p> Please enter the following details: <p>
|
|
||||||
<form method="post"
|
|
||||||
class="form"
|
|
||||||
enctype="multipart/form-data">
|
|
||||||
<input type="text" name="form_name" placeholder="Name" required>
|
|
||||||
<input type="text" name="form_company" placeholder="Company" required>
|
|
||||||
<input type="text" name="form_phone" placeholder="Phone" required>
|
|
||||||
<textarea name="comment" placeholder="Comment"></textarea>
|
|
||||||
<input id="form_files" type="file" name="form_files[]" multiple>
|
|
||||||
<label for="form_files">Attach file</label>
|
|
||||||
<input type="submit" value="Send form">
|
|
||||||
</form>
|
|
||||||
<% if (defined? formdata) and formdata %>
|
|
||||||
<hr>
|
|
||||||
<ul>
|
|
||||||
<% formdata.each do |key, part| %>
|
|
||||||
<li><%= key %>: <%= part.is_a?(String) ? part : "File: #{part.map do |x| x.filename end.join(',')}" %></li>
|
|
||||||
<% end %>
|
|
||||||
</ul>
|
|
||||||
<% end %>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
../../lib
|
|
|
@ -1 +0,0 @@
|
||||||
Example of handling forms in Hyde
|
|
|
@ -6,7 +6,7 @@ module Hyde
|
||||||
module ProbeConstructors
|
module ProbeConstructors
|
||||||
# Create a new erb template
|
# Create a new erb template
|
||||||
# @see {Hyde::Template#new}
|
# @see {Hyde::Template#new}
|
||||||
def erb(input, vars = {})
|
def erb(input, vars = nil)
|
||||||
Hyde::Templates::ERB.new(input,
|
Hyde::Templates::ERB.new(input,
|
||||||
vars,
|
vars,
|
||||||
parent: @origin)
|
parent: @origin)
|
||||||
|
@ -16,7 +16,7 @@ module Hyde
|
||||||
# @see {Hyde::Template#new}
|
# @see {Hyde::Template#new}
|
||||||
# @param freeze [Boolean] whether to use frozen string literal
|
# @param freeze [Boolean] whether to use frozen string literal
|
||||||
# @param capture [Boolean] whether to enable output capturing
|
# @param capture [Boolean] whether to enable output capturing
|
||||||
def erubi(input, vars = {}, freeze: true, capture: false)
|
def erubi(input, vars = nil, freeze: true, capture: false)
|
||||||
Hyde::Templates::Erubi.new(input,
|
Hyde::Templates::Erubi.new(input,
|
||||||
vars,
|
vars,
|
||||||
parent: @origin,
|
parent: @origin,
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require_relative 'header'
|
|
||||||
|
|
||||||
module Hyde
|
|
||||||
# Utility class for handling cookies
|
|
||||||
class Cookie
|
|
||||||
# @param data [String] raw cookie data
|
|
||||||
def initialize(key, value, params = {})
|
|
||||||
@key = key
|
|
||||||
@value = value
|
|
||||||
@params = params
|
|
||||||
end
|
|
||||||
|
|
||||||
# Convert cookie to "Set-Cookie: " string representation.
|
|
||||||
# @return [String]
|
|
||||||
def to_s
|
|
||||||
Hyde::Util.make_value(to_short, @params)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Convert cookie to "Cookie: " string representation (no params)
|
|
||||||
def to_short
|
|
||||||
"#{key.to_s.strip}=#{value.to_s.strip}"
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_accessor :key, :value
|
|
||||||
attr_reader :params
|
|
||||||
|
|
||||||
# Create cookie from a "Set-Cookie: " format
|
|
||||||
# @param data [String] value part of "Set-Cookie: " header
|
|
||||||
# @return [Cookie]
|
|
||||||
def self.from_setcookie_string(data)
|
|
||||||
kvpair, params = Hyde::Util.parse_value(data)
|
|
||||||
key, value = kvpair.split("=").map(&:strip)
|
|
||||||
Cookie.new(key, value, params)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Create cookie(s) from a "Cookie: " format
|
|
||||||
# @param data [String] value part of "Cookie: " header
|
|
||||||
# @return [Array(Cookie)]
|
|
||||||
def self.from_cookie_string(data)
|
|
||||||
data.split(";").map do |cookiestr|
|
|
||||||
Cookie.new(*cookiestr.split("=").map(&:strip))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -12,12 +12,7 @@ module Hyde
|
||||||
parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
|
parts = input.split(sep).map { |x| URI.decode_uri_component(x).strip }
|
||||||
base = parts.shift
|
base = parts.shift
|
||||||
opts = parts.map do |raw|
|
opts = parts.map do |raw|
|
||||||
key, value = raw.match(/
|
key, value = raw.match(/^([^=]*)(?:=(.*)|)\Z/).to_a[1..]
|
||||||
\A # beginning of string
|
|
||||||
([!-~&&[^=;,]]+) # key
|
|
||||||
(?:=([\s!-~&&[^;,]]*)|) # optional value
|
|
||||||
\Z # end of sting
|
|
||||||
/x).to_a[1..]
|
|
||||||
[key, ((value&.match?(/^".*"$/) ? value[1..-2] : value) or true)]
|
[key, ((value&.match?(/^".*"$/) ? value[1..-2] : value) or true)]
|
||||||
end.to_h
|
end.to_h
|
||||||
[base, opts]
|
[base, opts]
|
||||||
|
@ -29,7 +24,7 @@ module Hyde
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def self.make_value(input, opts, sep = ";")
|
def self.make_value(input, opts, sep = ";")
|
||||||
unless input.match?(/^[\w!#$%&'*+-.^_`|~]*=?[^[:cntrl:]\\",;]+$/)
|
unless input.match?(/^[\w!#$%&'*+-.^_`|~]*=?[^[:cntrl:]\\",;]+$/)
|
||||||
raise StandardError, "input format is invalid"
|
raise StandardError, "input contains invalid characters"
|
||||||
end
|
end
|
||||||
|
|
||||||
output = input
|
output = input
|
||||||
|
|
Loading…
Reference in New Issue