vintage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,110 @@
1
+ module Vintage
2
+ # A module of various helpers, many of them copied or based on Rails helpers.
3
+ module Helpers
4
+ # Borrowed from Rails, a regex to pick out things to link in text
5
+ AUTO_LINK_RE = %r{
6
+ ( # leading text
7
+ <\w+.*?>| # leading HTML tag, or
8
+ [^=!:'"/]| # leading punctuation, or
9
+ ^ # beginning of line
10
+ )
11
+ (
12
+ (?:https?://)| # protocol spec, or
13
+ (?:www\.) # www.*
14
+ )
15
+ (
16
+ [-\w]+ # subdomain or domain
17
+ (?:\.[-\w]+)* # remaining subdomains or domain
18
+ (?::\d+)? # port
19
+ (?:/(?:(?:[~\w\+@%-]|(?:[,.;:][^\s$]))+)?)* # path
20
+ (?:\?[\w\+@%&=.;-]+)? # query string
21
+ (?:\#[\w\-]*)? # trailing anchor
22
+ )
23
+ ([[:punct:]]|\s|<|$) # trailing text
24
+ }x unless const_defined?(:AUTO_LINK_RE)
25
+
26
+ # Redirect to a URL or other action using a 301 status code
27
+ # and +Location+ header.
28
+ def redirect_to(url)
29
+ response.code = 301
30
+ response.headers['Location'] = url
31
+ end
32
+
33
+ # Link to a given +url+ with the given +text+.
34
+ def link_to(text, url)
35
+ "<a href='#{url}' title='#{text}'>#{text}</a>"
36
+ end
37
+
38
+ # Auto link URLs and e-mail addresses in +text+.
39
+ def auto_link(text)
40
+ auto_link_email_addresses(auto_link_urls(text))
41
+ end
42
+
43
+ # Auto link e-mail addresses in +text+.
44
+ def auto_link_email_addresses(text)
45
+ body = text.dup
46
+ text.gsub(/([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)/) do
47
+ text = $1
48
+
49
+ if body.match(/<a\b[^>]*>(.*)(#{Regexp.escape(text)})(.*)<\/a>/)
50
+ text
51
+ else
52
+ %{<a href="mailto:#{text}">#{text}</a>}
53
+ end
54
+ end
55
+ end
56
+
57
+ # Auto link URLs in +text.
58
+ def auto_link_urls(text)
59
+ text.gsub(AUTO_LINK_RE) do
60
+ all, a, b, c, d = $&, $1, $2, $3, $4
61
+
62
+ if a =~ /<a\s/i # don't replace URL's that are already linked
63
+ all
64
+ else
65
+ text = b + c
66
+ %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}">#{text}</a>#{d})
67
+ end
68
+ end
69
+ end
70
+
71
+ # Grab an excerpt from +text+, starting at +start+ and stopping at +stop+.
72
+ # The +padding+ argument lets you define what should be on eithe side of the
73
+ # excerpt.
74
+ def excerpt(text, start = 0, stop = 20, padding = "...")
75
+ return "" if text.nil?
76
+
77
+ (padding if start > 0).to_s + text[start..(start + stop)] + padding
78
+ end
79
+
80
+ # Highlight an array of +phrases+ in +text+. The +highlighter+ argument lets you set
81
+ # how to highlight the text.
82
+ def highlight(text, phrases, highlighter = '<strong class="highlight">\1</strong>')
83
+ if text.blank? || phrases.blank?
84
+ text
85
+ else
86
+ match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')
87
+ text.gsub(/(#{match})/i, highlighter)
88
+ end
89
+ end
90
+
91
+ # Truncate +text+ to the length specified in +length+ with +ending+
92
+ # as the text appearing on the end.
93
+ def truncate(text, length = 20, ending = "...")
94
+ return "" if text.nil?
95
+
96
+ text[0..length] + ending
97
+ end
98
+
99
+ # Create a button that submits to another URL.
100
+ def button_to(url, text = "Click Here")
101
+ "<form method='GET' action='#{url}'><div class='button-to'><input type='submit' value='#{text}'></div></form>"
102
+ end
103
+
104
+ # Create a +mailto:+ link for +address+. You can optionally provide
105
+ # +text+ for the link's text.
106
+ def mail_to(address, text = nil)
107
+ "<a href='mailto:#{address}'>#{text || address}</a>"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,16 @@
1
+ require 'logger'
2
+
3
+ module Vintage
4
+ # Class to handle logging to a file and to
5
+ # the console.
6
+ class Log
7
+ # Method to log something.
8
+ def self.enter(text = "", level = :debug)
9
+ @@console ||= Logger.new(STDOUT)
10
+ @@file ||= Logger.new('requests.log')
11
+
12
+ @@console.send(level, text)
13
+ @@file.send(level, text)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ %w{ erubis redcloth bluecloth markaby haml }.each do |engine|
2
+ begin
3
+ require engine
4
+ rescue LoadError
5
+ next
6
+ end
7
+ end
8
+
9
+ module Vintage
10
+ # Core rendering class.
11
+ class Renderer
12
+ # Render an ERb template. Uses Erubis if available
13
+ # and if not, falls back to standard ERb.
14
+ def self.erb(template, request)
15
+ if Erubis
16
+ Erubis::Eruby.new(template).evaluate(request)
17
+ else
18
+ ERB.new(template).result(request.send(:binding))
19
+ end
20
+ end
21
+
22
+ # Render a HAML template.
23
+ def self.haml(template, request)
24
+ if Haml
25
+ Haml::Engine.new(template).render(request)
26
+ else
27
+ raise "You don't have Haml installed!"
28
+ end
29
+ end
30
+
31
+ # Render a Textile template using RedCloth.
32
+ def self.textile(template, request)
33
+ if RedCloth
34
+ RedCloth.new(template).to_html
35
+ else
36
+ raise "You don't have RedCloth installed!"
37
+ end
38
+ end
39
+
40
+ # Render a Markdown template using BlueCloth, or
41
+ # if that isn't available, using RedCloth.
42
+ def self.markdown(template, request)
43
+ if BlueCloth
44
+ BlueCloth.new(template).to_html
45
+ elsif RedCloth
46
+ RedCloth.new(template).to_html
47
+ else
48
+ raise "You don't have BlueCloth or RedCloth installed!"
49
+ end
50
+ end
51
+
52
+ # Render a Markaby template.
53
+ def self.mab(template, request)
54
+ if Markaby
55
+ Markaby::Builder.new.instance_eval(template).to_s
56
+ else
57
+ raise "You don't have Markaby installed!"
58
+ end
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ # Class that encapsulates information about the request.
2
+ class Request
3
+ attr_accessor :remote_ip, :method, :params, :query_string, :uri, :referer, :user_agent
4
+
5
+ def initialize(request, param_hash)
6
+ self.remote_ip = request.params['REMOTE_ADDR']
7
+ self.referer = request.params['HTTP_REFERER']
8
+ self.uri = request.params['REQUEST_URI']
9
+ self.method = request.params['REQUEST_METHOD']
10
+ self.user_agent = request.params['HTTP_USER_AGENT']
11
+ self.query_string = request.params['QUERY_STRING']
12
+ self.params = param_hash
13
+ end
14
+ end
15
+
16
+ # Class that encapsulates the response's headers and
17
+ # response code.
18
+ class Response
19
+ attr_accessor :headers, :code
20
+
21
+ def initialize(headers)
22
+ self.headers = headers
23
+ self.code = 200
24
+ end
25
+ end
26
+
27
+ # A class that creates a context for template
28
+ # rendering. Helpers are mixed in here to give
29
+ # templates access to them.
30
+ class RequestContext
31
+ attr_accessor :request, :response
32
+
33
+ def initialize(incoming_request, params, headers)
34
+ self.request = Request.new(incoming_request, params)
35
+ self.response = Response.new(headers)
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+ require 'mongrel'
3
+
4
+ require 'erubis'
5
+
6
+ require 'vintage/version'
7
+ require 'vintage/log'
8
+ require 'vintage/helpers'
9
+
10
+ begin
11
+ Dir.entries("helpers/").select{|entry| entry =~ /(.*).rb$/}.each do |helper_file|
12
+ Vintage::Helpers.module_eval(File.open(helper_file).read)
13
+ end
14
+ rescue
15
+ # No helpers
16
+ end
17
+
18
+ module Vintage
19
+ # Launches the Mongrel handler.
20
+ class Server
21
+ def self.run(options)
22
+ Log.enter "- vintage version #{VERSION::STRING}"
23
+ Log.enter "\t starting server on port #{options[:port]}"
24
+ Log.enter
25
+
26
+ h = Mongrel::HttpServer.new("0.0.0.0", options[:port])
27
+ h.register(options[:mount], Handler.new(options))
28
+ h.run.join
29
+ rescue Interrupt
30
+ Log.enter
31
+ Log.enter "- interrupt signal caught"
32
+ Log.enter "\tshutting server down"
33
+ Log.enter
34
+ exit 0
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module Vintage #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/lib/vintage.rb ADDED
@@ -0,0 +1,8 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'vintage/handler'
4
+ require 'vintage/server'
5
+
6
+ module Vintage
7
+
8
+ end
data/log/debug.log ADDED
File without changes
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.join(File.dirname(__FILE__), '..')
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)