Module: Sinatra::Editable

Defined in:
lib/sinatra/editable.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sinatra/editable.rb', line 20

def self.registered(app)
  app.helpers Editable::Helpers

  app.set :editable_route,     '/editable'
  app.set :editable_dir,       'editable'
  app.set :editable_templater, :html

  app.get "#{app.editable_route}/*" do
    path = "#{settings.root}/#{settings.editable_dir}/#{params[:splat].shift}.#{settings.editable_templater}"
    if File.exist?(path)
      File.read(path)
    else
      'new editable item..'
    end
  end

  app.put "#{app.editable_route}/*" do
    case settings.editable_templater
    when :textile
      new_template = params[:content]
      new_html = RedCloth.new(new_template).to_html
    when :html
      new_html = params[:content]
      new_template = nil
    else
      raise "Bad templater option"
    end
   
    splat = params[:splat].shift.split('/')
    file_basename = splat.pop.gsub(/\/$/,'')
            
    dir = "#{settings.root}/#{settings.editable_dir}/#{splat.join('/')}" 

    html_path = "#{dir}/#{file_basename}.html"
    template_path = "#{dir}/#{file_basename}.#{settings.editable_templater}"
    
    FileUtils.mkdir_p dir unless File.exist?(dir)
 
    if new_template
       File.open(template_path,'w') do |f|
        f.print(new_template)
        f.close
      end
    end
    
    File.open(html_path, 'w') do |f|
      f.print(new_html)
      f.close
    end

    new_html
  end
end