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.



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

def create_new_post(params)      
  post_title = params['post']['title']
    = (Time.now).strftime("%Y-%m-%d")

  content    = yaml_data(post_title).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

#default_yamlObject



34
35
36
37
38
39
40
41
42
# File 'lib/pagoda/helper.rb', line 34

def default_yaml
  defaults_file = File.join(jekyll_site.source, '_default.yml')
  defaults  = {}
  if File.exists? defaults_file
    defaults = YAML::load(File.read(defaults_file))
  end

  defaults
end

#jekyll_post(post_file) ⇒ Object

Jekyll instance of post file



45
46
47
# File 'lib/pagoda/helper.rb', line 45

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pagoda/app.rb', line 54

def merge_config(yaml, params)
  if params['post'].has_key? 'yaml'
    params['post']['yaml'].each do |key, value|
      if value == 'true'
        yaml[key] = true
      elsif value == 'false'
        yaml[key] = false
      else
        yaml[key] = value
      end
    end
  end

  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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pagoda/helper.rb', line 51

def posts_template_data(post_items)
  if post_items.nil?
    return []
  end

  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.



80
81
82
83
84
85
86
87
# File 'lib/pagoda/app.rb', line 80

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



70
71
72
73
74
75
76
77
# File 'lib/pagoda/app.rb', line 70

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

#yaml_data(post_title) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/pagoda/app.rb', line 29

def yaml_data(post_title)
  defaults = { 'title' => post_title,
    'layout' => 'post',
    'published' => false }

  defaults = defaults.merge(default_yaml())

  defaults
end