Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iwamot committed Jul 6, 2008
0 parents commit 6175199
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
memo/*
1 change: 1 addition & 0 deletions README
@@ -0,0 +1 @@
Please wait.
8 changes: 8 additions & 0 deletions config/config.yml
@@ -0,0 +1,8 @@
uri_templates:
top: http://pinto.jp/
index: http://pinto.jp/index{lang}
signup_openid: http://pinto.jp/signup_forms/openid{lang}
signup_auth: http://pinto.jp/signup_forms/auth{lang}{query}
signup_account: http://pinto.jp/signup_forms/account{lang}{query}
users: http://pinto.jp/users{lang}
user: http://pinto.jp/users/{username}{lang}
13 changes: 13 additions & 0 deletions lib/pinto/config.rb
@@ -0,0 +1,13 @@
# lib/pinto/config.rb

require 'yaml'

module Pinto
class Config
def self.load(key = nil)
config = YAML.load_file('../config/config.yml')
return config[key] unless key.nil?
return config
end
end
end
24 changes: 24 additions & 0 deletions lib/pinto/controller/index.rb
@@ -0,0 +1,24 @@
# lib/pinto/controller/top.rb

require 'pinto/view'

module Pinto
module Controller
class Top
def self.run(request)
param = {
:greeting => '"<こんにちはRuby>"',
:color => 'red',
:address => '<address>id:IwamotoTakashi</address>'
}
response_body = Pinto::View.render('top', param)

return [
200,
{'Content-Type' => 'text/html; charset=UTF-8'},
[response_body]
]
end
end
end
end
21 changes: 21 additions & 0 deletions lib/pinto/controller/signup_account.rb
@@ -0,0 +1,21 @@
# lib/pinto/controller/signup_account.rb

require 'pinto/encoding'
require 'pinto/view'

module Pinto
class Controller
class SignupAccount
def self.run(request)
param = {:str => request.params}
response_body = Pinto::View.render('top', param)

[
200,
{'Content-Type' => 'text/html; charset=UTF-8'},
[response_body]
]
end
end
end
end
19 changes: 19 additions & 0 deletions lib/pinto/controller/top.rb
@@ -0,0 +1,19 @@
# lib/pinto/controller/top.rb

require 'pinto/view'

module Pinto
module Controller
class Top
def self.run(request)
response_body = Pinto::View.render('top')

return [
300,
{'Content-Type' => 'application/xhtml+xml; charset=UTF-8'},
[response_body]
]
end
end
end
end
33 changes: 33 additions & 0 deletions lib/pinto/dispatcher.rb
@@ -0,0 +1,33 @@
# lib/pinto/dispatcher.rb

require 'addressable/uri'
require 'pinto/config'
require 'pinto/pathname'
require 'pinto/uri/extract_processor'
require 'rack'

module Pinto
class Dispatcher
def call(env)
uri_templates = Pinto::Config.load('uri_templates')
request = Rack::Request.new(env)
controller, request[:uri_map] = extract(uri_templates, request.url)
run_controller(controller, request)
end

def extract(uri_templates, request_uri)
uri = Addressable::URI.parse(request_uri)
uri_templates.each do |controller, template|
param = uri.extract_mapping(template, Pinto::URI::ExtractProcessor)
return [controller, param] unless param.nil?
end
['not_found', {}]
end

def run_controller(controller, request)
path = Pathname.new("pinto/controller/#{controller}")
require path
path.get_class.run(request)
end
end
end
22 changes: 22 additions & 0 deletions lib/pinto/encoding/utf8.rb
@@ -0,0 +1,22 @@
# lib/pinto/encoding/utf8.rb

module Pinto
module Encoding
class UTF8
def self.valid?(value)
valid_utf8 = /^(?:
[\x00-\x7F] # U+0000 - U+007F
|[\xC2-\xDF][\x80-\xBF] # U+0080 - U+07FF
|\xE0[\xA0-\xBF][\x80-\xBF] # U+0800 - U+0FFF
|[\xE1-\xEC][\x80-\xBF]{2} # U+1000 - U+CFFF
|\xED[\x80-\x9F][\x80-\xBF] # U+D000 - U+D7FF
|\xEF[\x80-\xBF][\x80-\xBD] # U+E000 - U+FFFD
|\xF0[\x90-\xBF][\x80-\xBF]{2} # U+10000 - U+3FFFF
|[\xF1-\xF3][\x80-\xBF]{3} # U+40000 - U+FFFFF
|\xF4[\x80-\x8F][\x80-\xBF]{2} # U+100000 - U+10FFFF
)*$/x
return !value.to_s.match(valid_utf8).nil?
end
end
end
end
16 changes: 16 additions & 0 deletions lib/pinto/pathname.rb
@@ -0,0 +1,16 @@
# lib/pinto/pathname.rb

require 'pathname'
require 'pinto/string'

class Pathname
def get_class
self.to_classname.split('::').inject(Object) do |parent, child|
parent.const_get(child)
end
end

def to_classname
self.to_str.split('/').map {|part| part.camelize}.join('::')
end
end
7 changes: 7 additions & 0 deletions lib/pinto/string.rb
@@ -0,0 +1,7 @@
# lib/pinto/string.rb

class String
def camelize
self.split('_').map{|word| word.capitalize}.join
end
end
10 changes: 10 additions & 0 deletions lib/pinto/uri/expand_processor.rb
@@ -0,0 +1,10 @@
module Pinto
class URI
class ExpandProcessor
def self.transform(name, value)
return '.' + value if name == 'lang'
return value
end
end
end
end
18 changes: 18 additions & 0 deletions lib/pinto/uri/extract_processor.rb
@@ -0,0 +1,18 @@
module Pinto
class URI
class ExtractProcessor
def self.match(name)
return '\.ja|\.en|' if name == 'lang'
return '\?.+|' if name == 'query'
return '[0-9a-zA-Z_\-]+' if name == 'username'
return '.*'
end

def self.restore(name, value)
return value.gsub(/^\./, '') if name == 'lang'
return value.gsub(/^\?/, '') if name == 'query'
return value
end
end
end
end
12 changes: 12 additions & 0 deletions lib/pinto/view.rb
@@ -0,0 +1,12 @@
# lib/pinto/view.rb

require 'pinto/view/xhtml'

module Pinto
class View
def self.render(view, param = {})
template = File.read("../view/#{view}.erb")
return Pinto::View::XHTML.new(template).evaluate(param)
end
end
end
51 changes: 51 additions & 0 deletions lib/pinto/view/helper/html.rb
@@ -0,0 +1,51 @@
# lib/pinto/view/helper/html.rb

require 'pinto/encoding/utf8'

module Pinto
class View
class Helper
module HTML
ESCAPE_TABLE = {
'&' => '&amp;',
'<' => '&lt;',
'>' => '&gt;',
'"' => '&quot;'
}

def escape_for_value(value)
escape_chars = ['&', '<', '>']
return eliminate(value, escape_chars)
end

def escape_for_quote(value)
escape_chars = ['&', '<', '>', '"']
return eliminate(value, escape_chars)
end

def escape_for_tag(value)
return eliminate(value)
end

alias h escape_for_value
alias in_quote escape_for_quote
alias has_tag escape_for_tag

def eliminate(value, escape_chars = nil)
str = value.to_s
raise ArgumentError unless Pinto::Encoding::UTF8.valid? str

control_codes = /
[\x00-\x08]|\x0B|\x0C|[\x0E-\x1F] # C0 control codes
|\x7F # DEL
|\xC2[\x80-\x9F] # C1 control codes
/ux
str.gsub!(control_codes, '')

return str unless escape_chars.is_a? Array
return str.gsub(/[#{escape_chars.join}]/) {|s| ESCAPE_TABLE[s]}
end
end
end
end
end
20 changes: 20 additions & 0 deletions lib/pinto/view/helper/uri.rb
@@ -0,0 +1,20 @@
# lib/pinto/view/helper/uri.rb

require 'addressable/uri'
require 'pinto/config'
require 'pinto/uri/expand_processor'

module Pinto
class View
class Helper
module URI
def uri(controller, param)
uri_templates = Pinto::Config.load('uri_templates')
return Addressable::URI.expand_template(uri_templates[controller],
param,
Pinto::URI::ExpandProcessor)
end
end
end
end
end
22 changes: 22 additions & 0 deletions lib/pinto/view/xhtml.rb
@@ -0,0 +1,22 @@
# lib/pinto/view/xhtml.rb

require 'erubis'
require 'pinto/view/helper/html'
require 'pinto/view/helper/uri'

module Pinto
class View
class XHTML < Erubis::EscapedEruby
def escaped_expr(code)
return "h(#{code})"
end

def evaluate(param)
context = Erubis::Context.new(param)
context.extend Pinto::View::Helper::HTML
context.extend Pinto::View::Helper::URI
super context
end
end
end
end
5 changes: 5 additions & 0 deletions script/server.ru
@@ -0,0 +1,5 @@
# script/server.ru

$LOAD_PATH << '../lib'
require 'pinto/dispatcher'
run Pinto::Dispatcher.new
15 changes: 15 additions & 0 deletions view/index.erb
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<title>Pinto</title>
<base href="http://pinto.jp/" />
</head>
<body>
<h1>Pinto</h1>
<ul>
<li><a href="/signup_forms/openid"><%= _('Sign Up') %></a></li>
</ul>
</body>
</html>
15 changes: 15 additions & 0 deletions view/top.erb
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<title>Pinto</title>
</head>
<body>
<h1>Pinto</h1>
<ul>
<li><a href="<%== uri('index', 'lang' => 'en') %>">English version</a></li>
<li><a href="<%== uri('index', 'lang' => 'ja') %>">Japanese version</a></li>
</ul>
</body>
</html>

0 comments on commit 6175199

Please sign in to comment.