class Jekyllpress::App

Public Instance Methods

new_page(title="") click to toggle source
# 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="") click to toggle source
# 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="") click to toggle source
# 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="") click to toggle source
# 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
setup() click to toggle source
# 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
version() click to toggle source
# File lib/jekyllpress.rb, line 21
def version
  say "Jekyllpress Version: #{Jekyllpress::VERSION}"
  Jekyllpress::VERSION
end

Private Instance Methods

check_templates() click to toggle source
# File lib/jekyllpress.rb, line 182
def check_templates
  raise SetupError.new SETUPERROR_MESSAGE unless jekyll_config.has_key?("templates")
  File.stat(File.join(source, template_dir))
  File.stat(File.join(source, template_dir, new_page_template))
  File.stat(File.join(source, template_dir, new_post_template))
rescue ::Errno::ENOENT => error
  raise SetupError.new SETUPERROR_MESSAGE
end
config_reset() click to toggle source
# File lib/jekyllpress.rb, line 207
def config_reset
  @jekyll_config = nil
end
destination(*paths) click to toggle source
# File lib/jekyllpress.rb, line 235
def destination(*paths)
  File.join(paths.compact.map(&:to_s))
end
index_name(default="index") click to toggle source
# File lib/jekyllpress.rb, line 227
def index_name(default="index")
  "#{default}.#{new_ext}"
end
jekyll_config(options={}) click to toggle source
# File lib/jekyllpress.rb, line 191
def jekyll_config(options={})
  @jekyll_config ||= begin
    conf = Jekyll.configuration(options)
    unless conf.has_key?("templates")
      conf.merge!({
        "templates" => {
          "template_dir" => "_templates",
          "new_post_template" => "new_post.markdown",
          "new_page_template" => "new_page.markdown"
        }
        })
    end
  end
  # @jekyll_config ||= Jekyll.configuration(options)
end
new_ext() click to toggle source
# File lib/jekyllpress.rb, line 211
def new_ext
  jekyll_config["markdown_ext"].split(',').first
end
new_page_template() click to toggle source
# File lib/jekyllpress.rb, line 247
def new_page_template
  jekyll_config["templates"]["new_page_template"]
end
new_post_template() click to toggle source
# File lib/jekyllpress.rb, line 243
def new_post_template
  jekyll_config["templates"]["new_post_template"]
end
page_dirname(title) click to toggle source
# File lib/jekyllpress.rb, line 223
def page_dirname(title)
  "#{title.to_url}"
end
post_file_name(post) click to toggle source
# File lib/jekyllpress.rb, line 251
def post_file_name(post)
  File.join(post.site.source, post.path)
end
post_filename(title, date=Date.today.iso8601) click to toggle source
# File lib/jekyllpress.rb, line 219
def post_filename(title, date=Date.today.iso8601)
  "#{date}-#{title.to_url}.#{new_ext}"
end
posts_dir() click to toggle source
# File lib/jekyllpress.rb, line 231
def posts_dir
  '_posts'
end
source() click to toggle source
# File lib/jekyllpress.rb, line 215
def source
  jekyll_config['source']
end
template_dir() click to toggle source
# File lib/jekyllpress.rb, line 239
def template_dir
  jekyll_config["templates"]["template_dir"]
end
with_config(options={}) { |jekyll_config(options)| ... } click to toggle source
# File lib/jekyllpress.rb, line 163
def with_config(options={})
  raise "no block given at #{caller[1]}" unless block_given?
  self.class.source_root(jekyll_config["source"])
  config_reset
  yield jekyll_config(options)
end
with_posts(options={}) { |post| ... } click to toggle source
# File lib/jekyllpress.rb, line 170
def with_posts(options={})
  with_config(options) do |config|
    site = Jekyll::Site.new(config)
    site.reset
    site.read
    site.posts.each do |post|
      yield post
    end
    site.reset
  end
end