Class: Jekyllpress::App

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/jekyllpress.rb

Instance Method Summary collapse

Instance Method Details

#new_page(title = "") ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/jekyllpress.rb', line 115

def new_page(title="")
  check_templates
  @title = title.to_s
  @title = ask("Page title: ") if @title.empty?
  @layout = options["layout"]
  location = options.fetch("location", nil)
  raise "location can not be an absolute path: #{location}" if location[0] == ?/

  with_config do |config|
    @filename = destination(source, location, page_dirname(@title), index_name)

    template(File.join(template_dir, new_page_template), @filename)

    [__method__, @title, @filename, location, @layout]
  end
end

#new_post(title = "") ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jekyllpress.rb', line 59

def new_post(title="")
  check_templates
  @title = title.to_s
  @title = ask("Title for your post: ") if @title.empty?

  @date = options.fetch("date", Date.today.iso8601)
  @time = options.fetch("time", Time.now.strftime("%H:%M"))
  @categories = options.fetch("categories", [])
  @tags = options.fetch("tags", [])
  @layout = options[:layout]
  @url = options[:url]
  @template = options.fetch("template") { new_post_template }
  @location = options.fetch("location", posts_dir) || posts_dir

  with_config do |config|
    check_templates
    @filename = destination(source, @location, post_filename(@title, @date))
    template(File.join(template_dir, @template), @filename)
    [__method__, @title, @date, @time, @filename, @categories, @tags, @layout, @url, @template, @location]
  end
end

#np(title = "") ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jekyllpress.rb', line 90

def np(title="")
  check_templates
  @title = title.to_s
  @title = ask("Title for your post: ") if @title.empty?

  @date = options.fetch("date", Date.today.iso8601)
  @time = options.fetch("time", Time.now.strftime("%H:%M"))
  @categories = options.fetch("categories", [])
  @tags = options.fetch("tags", [])
  @layout = options[:layout]
  @url = options[:url]
  @template = options.fetch("template") { new_post_template }
  @location = options.fetch("location", posts_dir) || posts_dir

  with_config do |config|
    check_templates
    @filename = destination(source, @location, post_filename(@title, @date))
    template(File.join(template_dir, @template), @filename)
    [__method__, @title, @date, @time, @filename, @categories, @tags, @layout, @url, @template, @location]
  end
end

#redirect(old_dir = "") ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jekyllpress.rb', line 135

def redirect(old_dir="")
  @old_dir = old_dir.to_s
  @old_dir = ask("Old directory to use in redirect: ") if @old_dir.empty?
  permalink_template = options.fetch("permalink_template") { Jekyll::Configuration::DEFAULTS["permalink"] }
  force_redirect = options.fetch("force") { false }
  processed_posts = []
  with_posts({"permalink" => permalink_template}) do |post|
    if post.data.has_key?("redirect_from") && !force_redirect
      say "skipping #{post.name} - redirect_from detected"
      next
    end

    time_now = Time.now
    insert_text = %Q{# BEGIN: redirect added by jekyllpress on #{time_now}
redirect_from:
  - #{File.join('', old_dir, post.url)}
# END:   redirect added by jekyllpress on #{time_now}
}
    insert_into_file(post_file_name(post), insert_text, :after => %r{\A---\n})

    processed_posts << {name: post.name, file: post_file_name(post)}
  end

  [__method__, @old_dir, processed_posts]
end

#setupObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jekyllpress.rb', line 27

def setup()
  with_config do |config|
    empty_directory(File.join(source, template_dir))
    create_file(File.join(source, template_dir, new_post_template),
      '---
       layout: <%= @layout %>
       title: "<%= @title %>"
       date: <%= @date %>T<%= @time %>
       categories: <%= Array(@categories) %>
       tags: <%= Array(@tags) %>
       source: "<%= @url %>"
       ---
      '.gsub(/^\s*/,''))
    create_file(File.join(source, template_dir, new_page_template),
      '---
       layout: <%= @layout %>
       title: "<%= @title %>"
       ---
      '.gsub(/^\s*/,''))
    [__method__, source, template_dir, new_post_template, new_page_template]
  end
end

#versionObject



21
22
23
24
# File 'lib/jekyllpress.rb', line 21

def version
  say "Jekyllpress Version: #{Jekyllpress::VERSION}"
  Jekyllpress::VERSION
end