John Guidry avatar John Guidry committed ebe1c07

Add article creation task to rake.

Comments (0)

Files changed (1)

-require 'nanoc3/tasks'
+require 'nanoc3/tasks'
+require 'fileutils'
+
+# Basics nicked from https://github.com/unthinkingly/unthinkingly-blog
+namespace :create do
+  desc "Creates a new article"
+  task :article do
+    if !ENV['title']
+      $stderr.puts "\t[error] Missing title argument.\n\tusage: rake create:article title='article title'"
+      exit 1
+    end
+
+    title = ENV['title']
+    path, filename, full_path = calc_path(title)
+
+    if File.exists?(full_path)
+      $stderr.puts "\t[error] Exists #{full_path}"
+      exit 1
+    end
+
+    template = <<TEMPLATE
+---
+kind: article
+created_at: #{Time.now.strftime('%e %b %Y %H:%m')}
+title: "#{title}"
+---
+
+TEMPLATE
+
+    FileUtils.mkdir_p(path) if !File.exists?(path)
+    File.open(full_path, 'w') { |f| f.write(template) }
+    $stdout.puts "\t[ok] Edit #{full_path}"
+  end
+
+  def calc_path(title)
+    year, month, day = Time.now.to_s.split(' ')[0].split('-', 3)
+    path = "content/posts/" + year + "/" + month + "/"
+    filename = to_slug(title) + ".haml"
+    [path, filename, path + filename]
+  end
+
+  def to_slug(title)
+    # strip the string
+    title = title.strip
+  
+    # blow away apostrophes
+    title.gsub! /['`]/,""
+    
+    # @ --> at, and & --> and
+    title.gsub! /\s*@\s*/, " at "
+    title.gsub! /\s*&\s*/, " and "
+
+    # replace all non alphanumeric, underscore or periods with underscore
+    title.gsub! /\s*[^A-Za-z0-9\.\-]\s*/, '_'  
+    
+    # convert double underscores to single
+    title.gsub! /_+/,"_"
+    
+    # strip off leading/trailing underscore
+    title.gsub! /\A[_\.]+|[_\.]+\z/,""
+    
+    title
+  end
+end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.