Class: Shwedagon::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/pagoda/app.rb,
lib/pagoda/config.rb,
lib/pagoda/helper.rb

Instance Method Summary collapse

Instance Method Details

#create_new_post(params) ⇒ Object

Create a new post from scratch. Return filename This would not commit the file.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pagoda/app.rb', line 22

def create_new_post(params)      
  post_title = params['post']['title']
    = (Time.now).strftime("%Y-%m-%d")
  yaml_data  = { 'title' => post_title,
    'layout' => 'post',
    'published' => false }

  content    = yaml_data.to_yaml + "---\n" + params[:post][:content]
  post_file  = ( + " " + post_title).to_url + '.md'
  file       = File.join(jekyll_site.source, *%w[_posts], post_file)
  File.open(file, 'w') { |file| file.write(content)}
  post_file
end

#jekyll_post(post_file) ⇒ Object

Jekyll instance of post file



35
36
37
# File 'lib/pagoda/helper.rb', line 35

def jekyll_post(post_file)
  Jekyll::Post.new(jekyll_site, jekyll_site.source, '', post_file)
end

#jekyll_siteObject

Jekyll site instance



6
7
8
9
10
11
12
13
14
# File 'lib/pagoda/helper.rb', line 6

def jekyll_site
  if not @site
    config  = Jekyll.configuration({'source' => settings.blog})
    @site   = Jekyll::Site.new(config)
    @site.read
  end

  @site
end

#merge_config(yaml, params) ⇒ Object

Merge existing yaml with post params



38
39
40
41
42
43
44
# File 'lib/pagoda/app.rb', line 38

def merge_config(yaml, params)
  yaml['published'] = !(params[:post].has_key? 'draft' and
    params[:post]['draft'] == 'on')
  yaml['title']     = params[:post][:title]

  yaml
end

#post_exists?(post_file) ⇒ Boolean

Shortcut for checking whether the post exists

Returns:

  • (Boolean)


25
26
27
# File 'lib/pagoda/helper.rb', line 25

def post_exists?(post_file)
  File.exists? post_path(post_file)
end

#post_path(post_file) ⇒ Object

Expanded post path of the post file



30
31
32
# File 'lib/pagoda/helper.rb', line 30

def post_path(post_file)
  File.join(jekyll_site.source, *%w[_posts], post_file)
end

#posts_template_data(post_items) ⇒ Object

Gives out a sorted list of post template data for a post or draft



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pagoda/helper.rb', line 41

def posts_template_data(post_items)
  template_data = post_items.map do |post|
    {
      :title    => post.data['title'],
      :filename => post.name,
      :date     => post.date
    }
  end

  template_data.sort! { |x,y| y[:date] <=> x[:date] }

  template_data
end

#repoObject

Grit repo instance



17
18
19
20
21
22
# File 'lib/pagoda/helper.rb', line 17

def repo
  @repo ||= Grit::Repo.new(settings.blog) 
  Dir.chdir(settings.blog)

  @repo
end

#update_post(params) ⇒ Object

Update exiting post.



56
57
58
59
60
61
62
63
# File 'lib/pagoda/app.rb', line 56

def update_post(params)
  post_file   = params[:post][:name]
  post        = jekyll_post(post_file)
  yaml_config = merge_config(post.data, params)
  write_post_contents(params[:post][:content], yaml_config, post_file)

  post_file
end

#write_post_contents(content, yaml, post_file) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/pagoda/app.rb', line 46

def write_post_contents(content, yaml, post_file)
  writeable_content  = yaml.to_yaml + "---\n" + content
  file_path          = post_path(post_file)

  if File.exists? file_path
    File.open(file_path, 'w') { |file| file.write(writeable_content)}
  end
end