0
require 'serve/version'
0
-require 'webrick/extensions'
0
+require 'serve/handlers/file_type_handler'
0
+require 'serve/handlers/textile_handler'
0
+require 'serve/handlers/markdown_handler'
0
+require 'serve/handlers/haml_handler'
0
+require 'serve/handlers/sass_handler'
0
+require 'serve/handlers/email_handler'
0
+require 'serve/handlers/redirect_handler'
0
- class FileTypeHandler < WEBrick::HTTPServlet::AbstractServlet #:nodoc:
0
- def self.extension(extension)
0
- WEBrick::HTTPServlet::FileHandler.add_handler(extension, self)
0
- def initialize(server, name)
0
- @script_filename = name
0
- data = open(@script_filename){|io| io.read }
0
- res['content-type'] = content_type
0
- res.body = parse(data)
0
- rescue StandardError => ex
0
- rescue Exception => ex
0
- raise WEBrick::HTTPStatus::InternalServerError, ex.message
0
- class TextileHandler < FileTypeHandler #:nodoc:
0
- "<html><body>#{ RedCloth.new(string).to_html }</body></html>"
0
- class MarkdownHandler < FileTypeHandler #:nodoc:
0
- "<html><body>#{ BlueCloth.new(string).to_html }</body></html>"
0
- class HamlHandler < FileTypeHandler #:nodoc:
0
- engine = Haml::Engine.new(string,
0
- :filename => @script_filename
0
- layout = find_layout(@script_filename)
0
- lines = IO.read(layout)
0
- context = Context.new(Dir.pwd, @script_filename, engine.options.dup)
0
- context.content = engine.render(context) do |*args|
0
- context.get_content_for(*args)
0
- layout_engine = Haml::Engine.new(lines, engine.options.dup)
0
- layout_engine.render(context) do |*args|
0
- context.get_content_for(*args)
0
- def find_layout(filename)
0
- path = filename[root.size..-1]
0
- path = File.dirname(path)
0
- l = File.join(root, path, '_layout.haml')
0
- layout = l if File.file?(l)
0
- end until layout or path == "/"
0
- attr_accessor :content
0
- def initialize(root, script_filename, engine_options)
0
- @root, @script_filename, @engine_options = root, script_filename, engine_options
0
- def content_for(symbol, &block)
0
- set_content_for(symbol, capture_haml(&block))
0
- def content_for?(symbol)
0
- !(get_content_for(symbol)).nil?
0
- def get_content_for(symbol = :content)
0
- if symbol.to_s.intern == :content
0
- instance_variable_get("@content_for_#{symbol}") || instance_variable_get("@#{symbol}")
0
- def set_content_for(symbol, value)
0
- instance_variable_set("@content_for_#{symbol}", value)
0
- partial = options.delete(:partial)
0
- template = options.delete(:template)
0
- render_partial(partial)
0
- render_template(template)
0
- raise "render options not supported #{options.inspect}"
0
- def render_partial(partial)
0
- render_template(partial, :partial => true)
0
- def render_template(template, options={})
0
- path = File.dirname(@script_filename)
0
- template = template[1..-1]
0
- filename = template_filename(File.join(path, template), :partial => options.delete(:partial))
0
- if File.file?(filename)
0
- lines = IO.read(filename)
0
- engine = Haml::Engine.new(lines, @engine_options)
0
- engine.render(self) do |*args|
0
- get_content_for(*args)
0
- raise "File does not exist #{filename.inspect}"
0
- def template_filename(name, options)
0
- path = File.dirname(name)
0
- template = File.basename(name)
0
- template = "_" + template if options.delete(:partial)
0
- template += ".haml" unless name =~ /\.haml$/
0
- File.join(path, template)
0
- class SassHandler < FileTypeHandler #:nodoc:
0
- engine = Sass::Engine.new(string,
0
- :filename => @script_filename
0
- class EmailHandler < FileTypeHandler #:nodoc:
0
- title = $1 + " #{title}" if string =~ /^Subject:\s*(\S.*?)$/im
0
- head, body = string.split("\n\n", 2)
0
- output << "<html><head><title>#{title}</title></head>"
0
- output << '<body style="font-family: Arial; line-height: 1.2em; font-size: 90%; margin: 0; padding: 0">'
0
- output << '<div id="head" style="background-color: #E9F2FA; padding: 1em">'
0
- key, value = line.split(":", 2).map { |a| a.strip }
0
- output << "<div><strong>#{key}:</strong> #{value}</div>"
0
- output << '</div><pre id="body" style="font-size: 110%; padding: 1em">'
0
- output << '</pre></body></html>'
0
- class RedirectHandler < FileTypeHandler #:nodoc:
0
- res['location'] = data.strip
0
- raise WEBrick::HTTPStatus::Found
0
class Server < WEBrick::HTTPServer #:nodoc:
0
\ No newline at end of file
Comments
No one has commented yet.