jekyll_post_generator 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ NOTES.markdown
6
+ .rvmrc
7
+ .pomo
8
+ _posts/*
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ source :rubygems
data/LICENSE.markdown ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Aziz Light
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,50 @@
1
+ Jekyll Post Generator
2
+ =====================
3
+
4
+ This is a gem that is used to generate Jekyll post files with the
5
+ appropriate name and YAML Front Matter.
6
+
7
+ Usage
8
+ -----
9
+
10
+ You need to be in the same directory as the `_posts` directory, or in
11
+ the `_posts` directory itself to be able to use the generator. A `jp`
12
+ script is provided with the gem. When used without args, it will prompt
13
+ the user for a title.
14
+
15
+ Alternatively, you can either provide default options via a `.jprc` file
16
+ in your home directory, or pass the options directly via the command
17
+ line. For more info on that, type `jp --help`
18
+
19
+ Compatibility
20
+ -------------
21
+
22
+ Jepeto require Ruby 1.9+ and will be tested exclusively with Ruby
23
+ 1.9.2+. MiniTest will be used instead of Test::Unit and until the
24
+ official version of Chronic is compatible with Ruby 1.9, the
25
+ `aaronh-chonic` gem will be used instead.
26
+
27
+ License
28
+ -------
29
+
30
+ The MIT License, also available in the `LICENSE.markdown` file.
31
+
32
+ Copyright (c) 2011 Aziz Light
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining a copy
35
+ of this software and associated documentation files (the "Software"), to deal
36
+ in the Software without restriction, including without limitation the rights
37
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38
+ copies of the Software, and to permit persons to whom the Software is
39
+ furnished to do so, subject to the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be included in
42
+ all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
50
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rake/testtask"
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList["test/test_*.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/jp ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/jepeto"
4
+ include Jepeto
5
+
6
+ unless Dir.exists?(Jepeto::POST_DIRECTORY)
7
+ if Dir.getwd.chomp("/").rpartition("/").last == Jepeto::POST_DIRECTORY
8
+ Dir.chdir("..")
9
+ end
10
+ end
11
+
12
+ # -Option-Parsing---------------------------------------------------------------
13
+ title = ""
14
+ ARGV.each do |arg|
15
+ unless arg.match(/^-{1,2}[a-zA-Z][a-z]*/)
16
+ title << " "
17
+ title << arg
18
+ end
19
+ end
20
+
21
+ options = OptionsParser.parse!(title)
22
+
23
+ # Jepete REALLY needs a title
24
+ if options.empty?
25
+ if ARGV.empty?
26
+ puts "Please enter a title"
27
+ title = $stdin.gets.chomp
28
+ options[:title] = title unless title.empty?
29
+ else
30
+ options[:title] = ARGV.join(" ")
31
+ end
32
+ end
33
+ # ------------------------------------------------------------------------------
34
+
35
+ begin
36
+ post = Jepeto::JekyllPost.new(options)
37
+ file = post.save!
38
+ rescue ArgumentError => error
39
+ puts error.message
40
+ exit
41
+ rescue RuntimeError => error
42
+ puts error.message
43
+ exit
44
+ rescue NameError => error
45
+ puts error.message
46
+ end
47
+
48
+ unless file.nil?
49
+ puts "Post successfully created! Full path: \n"
50
+ puts file
51
+ end
data/jepeto.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jepeto/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jekyll_post_generator"
7
+ s.version = Jepeto::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Aziz Light"]
10
+ s.email = ["aziiz.light+gem@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Jekyll Post Generator}
13
+ s.description = %q{Generate jekyll posts painlessly}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,12 @@
1
+ module Jepeto
2
+ module JekyllPostHelper
3
+ def slugalize(string)
4
+ string.gsub!(/[^\x00-\x7F]+/, '') # Remove non-ASCII (e.g. diacritics).
5
+ string.gsub!(/[^a-z0-9\-_\+]+/i, '-') # Turn non-slug chars into the separator.
6
+ string.gsub!(/-{2,}/, '-') # No more than one of the separator in a row.
7
+ string.gsub!(/^-|-$/, '') # Remove leading/trailing separator.
8
+ string.downcase!
9
+ string
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,146 @@
1
+ require_relative 'helpers/jekyll_post_helper'
2
+
3
+ module Jepeto
4
+
5
+ # Don't edit this constant hash!
6
+ # This can be overridden by the DEFAULT_OPTIONS hash
7
+ # or by options passed in by the user.
8
+ HARDCODED_DEFAULT_OPTIONS = {
9
+ date: Date.today.to_s,
10
+ extension: 'markdown',
11
+ published: false,
12
+ layout: 'default'
13
+ }
14
+
15
+ # This array should contain all the file extensions supported by jekyll
16
+ VALID_FILE_EXTENSIONS = [
17
+ 'markdown', 'mdown', 'md',
18
+ 'textile'
19
+ ]
20
+
21
+ POST_DIRECTORY = "_posts"
22
+
23
+ class JekyllPost
24
+ include Jepeto::JekyllPostHelper
25
+
26
+ attr_reader :options
27
+
28
+ def initialize(options)
29
+ define_instance_variables!
30
+ @options = check_options(options)
31
+ end
32
+
33
+ # Automagically create custom accessor methods for each option
34
+ %w[title date extension published layout].each do |option|
35
+ define_method(option) do
36
+ instance_variable_get("@options").fetch(option.to_sym)
37
+ end
38
+ end
39
+
40
+ # Automagically create custom accessor methods for the other attributes
41
+ %w[slug filename yaml_front_matter].each do |attribute|
42
+ define_method(attribute) do
43
+ send "generate_#{attribute}" if instance_variable_get("@#{attribute}").nil?
44
+ end
45
+ end
46
+
47
+ def save!
48
+
49
+ Dir.chdir('..') if Dir.getwd.include?(Jepeto::POST_DIRECTORY)
50
+ post_file = File.join(Jepeto::POST_DIRECTORY, self.filename)
51
+
52
+ unless File.writable?(Jepeto::POST_DIRECTORY)
53
+ raise "The post directory is not wriatble"
54
+ exit
55
+ end
56
+
57
+ if File.exists?(post_file)
58
+ raise NameError, "A post file with the same name already exists"
59
+ exit
60
+ end
61
+
62
+ File.open(post_file, 'w') do |file|
63
+ file.puts yaml_front_matter
64
+ end
65
+
66
+ File.expand_path(post_file)
67
+ end
68
+
69
+ private
70
+
71
+ def define_instance_variables!
72
+ %w[slug filename yaml_front_matter].each do |attribute|
73
+ instance_variable_set("@#{attribute}", nil)
74
+ end
75
+ end
76
+
77
+ def check_options(options)
78
+ # If the user defined default values via the DEFAULT_OPTIONS constant
79
+ # replace all the nil values with default values.
80
+
81
+ jprc_options = ""
82
+ config_file = File.expand_path("~/.jprc")
83
+ if File.exists?(config_file)
84
+ File.open(config_file, 'r') do |file|
85
+ while line = file.gets
86
+ jprc_options << line
87
+ end
88
+ end
89
+ jprc_options = YAML.load(jprc_options)
90
+ jprc_options = jprc_options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
91
+
92
+ options = merge_options(options, jprc_options)
93
+ end
94
+
95
+ # If there are still some nil values, replace them with default values from
96
+ # the HARDCODED_DEFAULT_OPTIONS constant.
97
+ options = merge_options(options, Jepeto::HARDCODED_DEFAULT_OPTIONS)
98
+
99
+ options[:extension] = check_extension(options[:extension])
100
+
101
+ # At this point, the only value that could potentially
102
+ # be nil or empty is the title.
103
+ # That is unacceptable!!
104
+ if options[:title].nil? || options[:title].empty?
105
+ raise ArgumentError, "The post file can't be created without a title!!!"
106
+ end
107
+
108
+ options
109
+ end
110
+
111
+ def merge_options(options, default_options)
112
+ default_options.each do |option, value|
113
+ if options[option].nil?
114
+ options[option] = value
115
+ end
116
+ end
117
+
118
+ options
119
+ end
120
+
121
+ def check_extension(extension)
122
+ extension.slice!(0) if extension[0] == '.'
123
+ raise "#{extension} is not a valid extension." unless VALID_FILE_EXTENSIONS.include?(extension)
124
+
125
+ @extension = extension
126
+ end
127
+
128
+ def generate_slug
129
+ @slug = slugalize(title)
130
+ end
131
+
132
+ def generate_filename
133
+ "#{date}-#{slug}.#{extension}"
134
+ end
135
+
136
+ def generate_yaml_front_matter
137
+ @yaml_front_matter = {}
138
+ @yaml_front_matter['layout'] = layout
139
+ @yaml_front_matter['title'] = title
140
+ @yaml_front_matter['published'] = published
141
+ @yaml_front_matter = @yaml_front_matter.to_yaml
142
+ @yaml_front_matter << "---\n"
143
+ end
144
+
145
+ end
146
+ end
@@ -0,0 +1,53 @@
1
+ module Jepeto
2
+
3
+ class OptionsParser
4
+
5
+ def self.parse!(the_real_title = nil)
6
+ options = {}
7
+
8
+ option_parser = OptionParser.new do |opt|
9
+ opt.banner = "Usage: jp [title]"
10
+ opt.separator "Usage: jp [title]"
11
+ opt.separator ""
12
+ opt.separator "The title has to be passed to create a new post file."
13
+ opt.separator "If the title isn't passed as an option, the user will be"
14
+ opt.separator "prompted to enter one."
15
+ opt.separator ""
16
+ opt.separator "Options"
17
+
18
+ opt.on( "-t", "--title=TITLE", "The post title.") do |title|
19
+ options[:title] = title
20
+ end
21
+
22
+ opt.on( "-e", "--extension=EXTENSION", "The extension of the post file.") do |extension|
23
+ options[:extension] = extension
24
+ end
25
+
26
+ # TODO: Add the date option
27
+
28
+ opt.on( "--draft=DRAFT", "Whether or not to create a draft post") do |draft|
29
+ options[:draft] = true
30
+ end
31
+
32
+ opt.on( "-v", "--version", "Show version number") do
33
+ puts "jekyll_post_generator version #{Jepeto::VERSION}"
34
+ exit
35
+ end
36
+
37
+ opt.on( "-h", "--help", "Show this help message.") do
38
+ puts option_parser
39
+ exit
40
+ end
41
+
42
+ opt.separator ""
43
+ end
44
+
45
+ if options[:title].nil? && !the_real_title.nil? && !the_real_title.empty?
46
+ options[:title] = the_real_title
47
+ end
48
+
49
+ option_parser.parse!
50
+ options
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module Jepeto
2
+ VERSION = "0.1"
3
+ end
data/lib/jepeto.rb ADDED
@@ -0,0 +1,18 @@
1
+ # General requirements
2
+ require "date"
3
+ require "yaml"
4
+ require "optparse"
5
+
6
+ # Personal requirements
7
+ require_relative "./jepeto/version"
8
+ require_relative "./jepeto/jekyll_post"
9
+ require_relative "./jepeto/option_parser"
10
+
11
+ module Jepeto
12
+
13
+ DEFAULT_OPTIONS = {
14
+ layout: "post",
15
+ extension: "markdown",
16
+ published: true
17
+ }
18
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll_post_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Aziz Light
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-02-26 00:00:00.000000000 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Generate jekyll posts painlessly
16
+ email:
17
+ - aziiz.light+gem@gmail.com
18
+ executables:
19
+ - jp
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - LICENSE.markdown
26
+ - README.markdown
27
+ - Rakefile
28
+ - bin/jp
29
+ - jepeto.gemspec
30
+ - lib/jepeto.rb
31
+ - lib/jepeto/helpers/jekyll_post_helper.rb
32
+ - lib/jepeto/jekyll_post.rb
33
+ - lib/jepeto/option_parser.rb
34
+ - lib/jepeto/version.rb
35
+ has_rdoc: true
36
+ homepage: ''
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.5.2
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Jekyll Post Generator
60
+ test_files: []