Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chowdown cmd utility #42

Open
wants to merge 13 commits into
base: gh-pages
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
32 changes: 32 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

DESTINATION=${1:-/Volumes/share/chowdown}
BASEURL=${2:-http://chowdown.local}
BRANCH=$(git branch --show-current)

if ! [ -d "$DESTINATION" ]; then
echo "Destination does not exist: $DESTINATION"
exit 1
fi

echo "Building site ($BRANCH) to $DESTINATION"
echo "URL will be $BASEURL"

# echo "Go ahead with build?"
# select yn in "Yes" "No"; do
# case $yn in
# Yes ) echo "Ok."; break;;
# No ) echo "Exiting."; exit;;
# esac
# done

echo "Incremental or full?"
select choice in "Incremental" "Full" "Quit"; do
case $choice in
Incremental ) INCREMENTAL="--incremental"; break;;
Full ) INCREMENTAL=""; break;;
Quit ) echo "Exiting."; exit;;
esac
done

jekyll build -d $DESTINATION -b $BASEURL --trace $INCREMENTAL
142 changes: 142 additions & 0 deletions bin/chowdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env ruby

require 'pathname'
VERSION = Pathname(__FILE__).join('../../VERSION').read

class Options
attr_reader :action, :target, :name

def initialize(action, target, name)
@action = action
@target = target
@name = name
end

def self.parse(opts)
available_actions = { new: 'build', delete: 'destroy' }
available_targets = REGISTRY.keys

action = available_actions.fetch(opts.shift.to_sym)
target = opts.shift.to_sym
target = available_targets.include?(target) ? target : nil
name = opts.shift || 'Example'
self.new(action, target, name)
rescue => e
self.show_usage
end

def self.show_usage
puts "Chowdown v#{VERSION}"
puts "Usage:"
puts " #{Pathname(__FILE__).basename} ACTION PAGE NAME"
puts " ACTION is one of: new, delete"
puts " PAGE is one of: #{REGISTRY.keys.join(', ')}"
puts " NAME is the title of the page you are creating."
puts " eg:\n #{Pathname(__FILE__).basename} new recipe \"Grandma's Cookies\""
end
end

class Chowdown
def self.execute(opts)
return if opts.nil?
@target = opts.target
# puts opts.inspect
puts "Chowdown v#{VERSION}"
template
.new(opts.name)
.send(opts.action)
end

def self.template
Template.lookup(@target)
end
end

class Template
@@template = 'template.template'

def initialize(title)
@title = title
end

def targetpath
@targetpath ||= Pathname('.')
end

def filename
Pathname(
@title.gsub(/[[:space:]]/, '-')
.gsub(/[^[:alnum:]-]/, '')
.downcase + '.md'
)
end

def file_exists?
target_file.exist?
end

def target_file
targetpath.join(filename)
end

def template_path
Pathname('./bin').join(@@template)
end

def build
new_contents = template_path.read
new_contents['{TITLE}'] = @title
new_contents['{IMAGENAME}'] = filename.sub_ext('.jpg').to_s

if target_file.exist?
puts "That one already exists, try another name."
puts "Tried to create: #{target_file}"
else
target_file.open('w+') {|file| file.write(new_contents) }

puts "Created: #{target_file}\n Use the same naming structure for any images.\n eg: 'images/#{filename.basename.sub_ext('.jpg')}'"
end
end

def destroy
if target_file.exist?
target_file.delete
puts "Deleting: #{target_file}"
else
puts "Can't delete: #{target_file}\nIt does not exist."
end
end

def method_missing(m, *args, &block)
puts ['* method:', m, args]
Options.show_usage
end

def self.register(name, klass)
REGISTRY[name] = klass
end
def self.lookup(name)
REGISTRY[name.to_s.to_sym]
end
end

# NullTemplate
class NullTemplate < Template; end

REGISTRY = Hash.new(NullTemplate)

# Recipe template
class Recipe < Template
@@template = 'recipe.template'

def targetpath
@targetpath ||= Pathname('_recipes')
end
end
Template.register(:recipe, Recipe)



# Load the options and execute
options = Options.parse(ARGV)
Chowdown.execute(options)
43 changes: 43 additions & 0 deletions bin/recipe.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
layout: recipe
title: {TITLE}
image: {IMAGENAME}
# imagecredit:
# courses: [appetiser, entree, breakfast, snack, lunch, tiffin, dinner, supper]
# cuisines: [Mediterranean, Irish, British, Mexican, French]
# diets: [Diabetic, GlutenFree, Halal, Hindu, Kosher, LowCalorie, LowFat, LowLactose, LowSalt, Vegan, Vegetarian]
# yield: 4 slices
published: true

ingredients:
- Sugar and spice
- Slugs, snails
- Puppy dog tails

directions:
- Run **bin/chowdown** command to create new **recipes**.
- Edit this file to reflect your lovely new recipe.
- Copy your images to the **images** folder. If you don't have any yet just comment out the image line above.

---
This is an example recipe which should contain the full gamut of options
available. This text is for the freeform description of your recipe.

Most things conform to the [Recipe schema](https://schema.org/Recipe). Here are some useful shortcuts:

### Courses

Refer to the [type of meal](https://schema.org/recipeCategory).


### Cuisines

Which [cuisine](https://schema.org/recipeCuisine) does this recipe belong to?

### Diets

Diets is a restricted vocabulary and can only be from the [options listed here](https://schema.org/RestrictedDiet#enumbers), choose all that are appropriate.

### Tags

Can be anything else you think is relevant to your recipe. You might tag all the ones that you make in the liquidiser with **`blender`** or perhaps all your chicken wing recipes should be tagged with **`barbecue`**.