class JekyllPageBoilerplate::Page

Constants

BOILERPLATES_PATH
FILE_DATE_FORMATE
READ_CONFIG_REGEX
READ_FILE_REGEX

Public Class Methods

new(boilerplate) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 14
def initialize boilerplate

  plate_path = get_boilerplate_path(boilerplate).to_s
  
  abort_unless_file_exists( plate_path )

  parsed_file = {}
  File.open(plate_path, 'r') do |file|
    parsed_file = file.read.match(READ_FILE_REGEX).named_captures
  end

  @config = get_config(parsed_file['head'])       
  @head = get_head(parsed_file['head'])
  @body = get_body(parsed_file['body'])
end

Public Instance Methods

create(title) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 30
def create title
  abort_unless_file_exists(@config['path'])
  
  set_header_entry 'title', title.gsub(/[&-]/, '&'=>'&', '-'=>' ')
  set_header_entry 'created', Time.now.to_s

  create_new_page get_new_page_filename(title)
end

Private Instance Methods

abort_if_file_exists(file_path) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 92
def abort_if_file_exists(file_path)
  if File.exist?(file_path)
    raise "#{file_path} already exists!"
  end
end
abort_unless_file_exists(file_path) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 98
def abort_unless_file_exists(file_path)
  unless File.exist?(file_path)
    raise "#{file_path} does not exist!"
  end
end
create_new_page(filename) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 41
def create_new_page filename
  new_file_path = File.join( @config['path'], filename )

  abort_if_file_exists(new_file_path)

  open(new_file_path, 'w') do |page|
    page.puts '---'
    page.puts @head.lstrip
    page.puts '---'
    page.puts @body
    page.puts ''
  end      
end
get_body(markdown) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 61
def get_body markdown
  return markdown
end
get_boilerplate_path(plate_name) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 74
def get_boilerplate_path plate_name
  return Dir.glob( 
    "#{File.join(BOILERPLATES_PATH, plate_name)}.{md,markdown,MD,MARKDOWN}" 
  ).first
end
get_config(head) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 65
def get_config head
  return YAML.load(head.match(READ_CONFIG_REGEX).to_s)['_boilerplate']
end
get_head(head) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 69
def get_head head
  return head.gsub( READ_CONFIG_REGEX, '')
end
get_new_page_filename(title) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 81
def get_new_page_filename title
  title = title.to_url
  title = "#{title}#{@config['suffix'] || '.markdown'}"
  if @config['timestamp']
    title = "#{Time.now.strftime(FILE_DATE_FORMATE)}-#{title}"
  end
  return title
end
set_header_entry(key, val) click to toggle source
# File lib/jekyll_page_boilerplate/page.rb, line 56
def set_header_entry key, val
  @head << "\n#{key}: null" unless @head.match /^#{key}:.*$/
  @head.gsub! /^#{key}:.*$/, "#{key}: #{val}"
end