Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add initial generate command to generate posts #567

Closed
wants to merge 3 commits into from

5 participants

@tombell

This is the beginning of implementing support for #17. I currently have a ghetto rake task for creating empty posts ready to write, but it would be awesome to have this small feature right in Jekyll.

jekyll generate post --title 'Amazing New Jekyll Generators'

This will create a new file in _posts called _posts/{todays-date}-amazing-new-jekyll-generators.md containing the YAML header defaulting the layout to post and title to the given title.

---
layout: post
title: Amazing New Jekyll Generators
---
TODO

I hope to add --textile and --markdown options to change the file extension which is used.

I was thinking it may be best to extract the generating functionality into classes/modules in lib/ but the naming seems like it would clash with the existing generator stuff, so any other alternative names would be welcomed.

For generating a skeleton site, would it be best to generate just the directory structure, empty layout files, and minimal configuration file or include a minimalist layout to get people started?

@mojombo
Owner

Will review more closely in a bit.

@parkr
Owner

+1. Nice work, @tombell.

@calavera calavera commented on the diff
bin/jekyll
((27 lines not shown))
+ 'layout' => 'post',
+ 'title' => "#{title}"
+ }
+
+ puts "Creating new post '_posts/#{title}'..."
+
+ FileUtils.mkdir('_posts') unless File.directory?('_posts')
+ File.open("_posts/#{name}", "w") do |f|
+ f.puts header.to_yaml
+ f.puts '---'
+ f.puts 'TODO'
+ end
+
+ exit(0)
+ else
+ puts "Invalid . Run `jekyll --help` for assistance."

this prints "Invalid..." when we run jekyll generate site but site is a valid option above in:

https://github.com/mojombo/jekyll/pull/567/files#L0R167

if type.nil? || !['post', 'site'].include?(type)

it would be also nice to inform the user about the available options rather than tell her to run jekyll --help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bin/jekyll
((13 lines not shown))
+ if type.nil? || !['post', 'site'].include?(type)
+ puts "Invalid generator type option. Run `jekyll --help` for assistance."
+ exit(1)
+ else
+ type = type.downcase
+ end
+
+ case type
+ when 'post'
+ title = options['title']
+ slug = title.gsub(/[^[:alnum:]]+/, '-').downcase
+ date = Time.now.strftime('%Y-%m-%d')
+ name = "#{date}-#{slug}.md"
+ header = {
+ 'layout' => 'post',
+ 'title' => "#{title}"

this is interpolating a variable into an empty string. It should be:

'title' => title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bin/jekyll
@@ -141,6 +145,10 @@ opts = OptionParser.new do |opts|
options['url'] = url
end
+ opts.on("--title [TITLE]", "Set the post title of a generated post") do |generate_title|

[TITLE] within square brackets is an optional argument, so people can run jekyll generate post --title which would give you a nil title. The option should be without brakets:

opts.on("--title TITLE" ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
bin/jekyll
((7 lines not shown))
if ARGV.size > 0
- if ARGV[0] == 'import'
+ case ARGV[0]
+ when 'generate'
+ type = ARGV[1]
+
+ if type.nil? || !['post', 'site'].include?(type)
+ puts "Invalid generator type option. Run `jekyll --help` for assistance."
+ exit(1)
+ else

the else is not really necessary because the if statement is exiting. This can be rewritten as:

if type.nil? || !['post', 'site'].include?(type)
  ...
end

if type.casecmp('post') == 0
  title = options['title']
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@calavera calavera commented on the diff
bin/jekyll
((18 lines not shown))
+ end
+
+ case type
+ when 'post'
+ title = options['title']
+ slug = title.gsub(/[^[:alnum:]]+/, '-').downcase
+ date = Time.now.strftime('%Y-%m-%d')
+ name = "#{date}-#{slug}.md"
+ header = {
+ 'layout' => 'post',
+ 'title' => "#{title}"
+ }
+
+ puts "Creating new post '_posts/#{title}'..."
+
+ FileUtils.mkdir('_posts') unless File.directory?('_posts')

If you're creating only one directory FileUtils is not really neccesary, you can use Dir.mkdir('_posts')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@tombell tombell Various fixes for suggestions from @calavera
* if statement refactoring
* remove string interpolation
* remove 'site' option until site generation is added
7420688
@tombell tombell closed this
@parkr
Owner

What happened to this, @tombell?

@parkr parkr referenced this pull request
Closed

Generators #17

@Schoonology

Is this still happening? Something like this would be super useful.

@parkr
Owner

Not sure but I'd love to have this feature. Want to take a swing at it, @Schoonology?

@Schoonology

Sure. Since jekyll 1.0 has a new command, do we want the design to be similar, albeit jekyll new post? Or would we rather (as I would) a separate command for this, say jekyll add [title] or jekyll post [title] or even jekyll write [title]?

@parkr
Owner

I like the idea of extending the new command but I don't think it's wise practically. I'd prefer a common command for making new posts and pages - something like jekyll generate, but I'm not married to generate at all. @mattr-, what do you think?

@parkr
Owner

Oh and it should be able to create drafts, posts and pages!

@Schoonology

Do we want it to be interactive (e.g. gets for title), or take all commands from options (e.g. write [title])?

@Schoonology

I'm leaning toward interactive, as I think it'll both be the most obvious to the user and the least fragile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 31, 2012
  1. @tombell
Commits on Jul 20, 2012
  1. @tombell
  2. @tombell

    Various fixes for suggestions from @calavera

    tombell authored
    * if statement refactoring
    * remove string interpolation
    * remove 'site' option until site generation is added
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 4 deletions.
  1. +43 −4 bin/jekyll
View
47 bin/jekyll
@@ -2,6 +2,10 @@
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
+require 'fileutils'
+require 'yaml'
+require 'time'
+
help = <<HELP
Jekyll is a blog-aware, static site generator.
@@ -10,6 +14,7 @@ Basic Command Line Usage:
jekyll <path to write generated site> # . -> <path>
jekyll <path to source> <path to write generated site> # <path> -> <path>
jekyll import <importer name> <options> # imports posts using named import script
+ jekyll generate post <options> # generates a new empty post
Configuration is read from '<source>/_config.yml' but can be overriden
using the following options:
@@ -48,7 +53,6 @@ opts = OptionParser.new do |opts|
opts.on("--site [SITE NAME]", "Site to import from") do |import_site|
options['site'] = import_site
end
-
opts.on("--[no-]safe", "Safe mode (default unsafe)") do |safe|
options['safe'] = safe
@@ -141,6 +145,10 @@ opts = OptionParser.new do |opts|
options['url'] = url
end
+ opts.on("--title TITLE", "Set the post title of a generated post") do |generate_title|
+ options['title'] = generate_title
+ end
+
opts.on("--version", "Display current version") do
puts "Jekyll " + Jekyll::VERSION
exit 0
@@ -150,10 +158,41 @@ end
# Read command line options into `options` hash
opts.parse!
-
-# Check for import stuff
+# Check for import or generator stuff
if ARGV.size > 0
- if ARGV[0] == 'import'
+ case ARGV[0]
+ when 'generate'
+ type = ARGV[1]
+
+ if type.nil? || !['post'].include?(type)
+ puts "Invalid generator type option. Run `jekyll --help` for assistance."
+ exit(1)
+ end
+
+ if type.casecamp('post') == 0
+ title = options['title']
+ slug = title.gsub(/[^[:alnum:]]+/, '-').downcase
+ date = Time.now.strftime('%Y-%m-%d')
+ name = "#{date}-#{slug}.md"
+ header = {
+ 'layout' => 'post',
+ 'title' => title
+ }
+
+ puts "Creating new post '_posts/#{title}'..."
+
+ FileUtils.mkdir('_posts') unless File.directory?('_posts')

If you're creating only one directory FileUtils is not really neccesary, you can use Dir.mkdir('_posts')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ File.open("_posts/#{name}", "w") do |f|
+ f.puts header.to_yaml
+ f.puts '---'
+ f.puts 'TODO'
+ end
+
+ exit(0)
+ else
+ puts "Invalid . Run `jekyll --help` for assistance."

this prints "Invalid..." when we run jekyll generate site but site is a valid option above in:

https://github.com/mojombo/jekyll/pull/567/files#L0R167

if type.nil? || !['post', 'site'].include?(type)

it would be also nice to inform the user about the available options rather than tell her to run jekyll --help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ when 'import'
migrator = ARGV[1]
if migrator.nil?
Something went wrong with that request. Please try again.