-
-
Save electron0zero/bfb8a087eb2e2ff20175b9978984963a to your computer and use it in GitHub Desktop.
My Jekyll Rake tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| # Rake tasks for Jekyll | |
| require 'rake/clean' | |
| require 'stringex' | |
| DEFAULT_TEXT = "Add first paragraph here \n\n\<\!--more--\>\n\nAdd more content here" | |
| PAGES_DIR = 'pages' | |
| POSTS_DIR = '_posts' | |
| DRAFTS_DIR = '_drafts' | |
| BUILD_DIR = '_site' | |
| CLEAN.include BUILD_DIR | |
| desc 'Build the site' | |
| task :build do | |
| sh 'jekyll', 'build' | |
| end | |
| desc 'Start web server to preview site' | |
| task :preview do | |
| sh 'jekyll', 'serve', '--watch', '--drafts', '--port', ENV.fetch('PORT', '4000') | |
| end | |
| # Hacking Rake for pretty args | |
| # exit at the end of task so rake can not execute our args as task | |
| # https://stackoverflow.com/a/36929059 | |
| desc 'Create a new draft, usage: rake draft "Title of draft"' | |
| task :draft do | |
| _, title = ARGV | |
| title = title || 'New Draft' | |
| filepath = File.join(DRAFTS_DIR, "#{title.to_url}.md") | |
| build_file(filepath, title) | |
| exit | |
| end | |
| desc 'Create a new post, usage: rake post "Title of post"' | |
| task :post do | |
| _, title = ARGV | |
| title = title || 'New Post' | |
| timestamp = Time.now.strftime('%Y-%m-%d') | |
| filepath = File.join(POSTS_DIR, "#{timestamp}-#{title.to_url}.md") | |
| build_file(filepath, title, timestamp: timestamp) | |
| exit | |
| end | |
| desc 'Create a new page, usage: rake page "Title of page"' | |
| task :page do | |
| _, title = ARGV | |
| title = title || 'New Page' | |
| filepath = File.join(PAGES_DIR, "#{title.to_url}.md") | |
| build_file(filepath, title, page: true) | |
| exit | |
| end | |
| task default: :preview | |
| def build_file(filepath, title, page: false, timestamp: false) | |
| open(filepath, 'w') do |f| | |
| f << "---\n" | |
| f << "layout: post\n" unless page | |
| f << "layout: page\n" if page | |
| f << "title: \"#{title}\"\n" | |
| f << "permalink: \"/#{title.to_url}\"\n" if page | |
| f << "date: #{timestamp || Time.now.strftime('%Y-%m-%d')}\n" unless page | |
| f << "comments: false\n" unless page | |
| f << "archive: false\n" unless page | |
| f << "categories: New-Post WIP\n" unless page | |
| f << "---\n" | |
| f << "\n" | |
| f << "#{DEFAULT_TEXT}\n" | |
| end | |
| puts "Created: #{filepath}" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment