Class: MrPoole::Helper
- Inherits:
-
Object
- Object
- MrPoole::Helper
- Defined in:
- lib/mr_poole/helper.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #bad_path(path) ⇒ Object
- #creation_options_usage ⇒ Object
- #draft_usage ⇒ Object
-
#ensure_jekyll_dir ⇒ Object
Check for a _posts directory in current directory.
- #ensure_open_struct(opts) ⇒ Object
-
#gen_usage ⇒ Object
Print a usage message and exit.
- #get_date_stamp ⇒ Object
-
#get_layout(layout_path) ⇒ Object
Get a layout as a string.
-
#get_slug_for(title) ⇒ Object
Given a post title (mixed case, spaces, etc.), generates a slug for This clobbers any non-ASCII text (TODO don’t do that).
- #get_time_stamp ⇒ Object
-
#initialize ⇒ Helper
constructor
A new instance of Helper.
- #open_in_editor(path) ⇒ Object
- #post_usage ⇒ Object
- #publish_usage ⇒ Object
- #restore_orig_directory ⇒ Object
- #unpublish_usage ⇒ Object
- #version_statement ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/mr_poole/helper.rb', line 7 def config @config end |
Instance Method Details
#bad_path(path) ⇒ Object
88 89 90 91 |
# File 'lib/mr_poole/helper.rb', line 88 def bad_path(path) puts "Error: could not open #{path}" exit end |
#creation_options_usage ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/mr_poole/helper.rb', line 146 def puts '' puts 'Options:' puts ' -s, --slug Define a custom slug for post, used for generated file name' puts ' -t, --title Define a title for post This option may be omitted provided' puts ' that TITLE is given as the last argument to poole' puts ' -l, --layout Path to a custom layout file to use' end |
#draft_usage ⇒ Object
139 140 141 142 143 144 |
# File 'lib/mr_poole/helper.rb', line 139 def draft_usage puts 'Usage:' puts ' poole draft [OPTION] [ARG] TITLE' exit end |
#ensure_jekyll_dir ⇒ Object
Check for a _posts directory in current directory. If there’s not one, check for a _config.yml and look for a custom src directory. If we don’t find one, puke an error message and die. If we do, return the name of the directory
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mr_poole/helper.rb', line 17 def ensure_jekyll_dir @orig_dir = Dir.pwd start_path = Pathname.new(@orig_dir) ok = File.exists?('./_posts') new_path = nil # if it doesn't exist, check for a custom source dir in _config.yml if !ok check_custom_src_dir! ok = File.exists?('./_posts') new_path = Pathname.new(Dir.pwd) end if ok return (new_path ? new_path.relative_path_from(start_path) : '.') else puts 'ERROR: Cannot locate _posts directory. Double check to make sure' puts ' that you are in a jekyll directory.' exit end end |
#ensure_open_struct(opts) ⇒ Object
44 45 46 |
# File 'lib/mr_poole/helper.rb', line 44 def ensure_open_struct(opts) return opts.instance_of?(Hash) ? OpenStruct.new(opts) : opts end |
#gen_usage ⇒ Object
Print a usage message and exit
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/mr_poole/helper.rb', line 120 def gen_usage puts 'Usage:' puts ' poole [ACTION] [ARG]' puts '' puts 'Actions:' puts ' draft Create a new draft in _drafts with title SLUG' puts ' post Create a new timestamped post in _posts with title SLUG' puts ' publish Publish the draft with SLUG, timestamping appropriately' puts ' unpublish Move a post to _drafts, untimestamping appropriately' exit end |
#get_date_stamp ⇒ Object
79 80 81 82 |
# File 'lib/mr_poole/helper.rb', line 79 def get_date_stamp format = @config.time_format || '%Y-%m-%d' Time.now.strftime(format) end |
#get_layout(layout_path) ⇒ Object
Get a layout as a string. If layout_path is non-nil, will open that file and read it, otherwise will return a default one, and a file extension to use
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mr_poole/helper.rb', line 51 def get_layout(layout_path) if layout_path.nil? contents = "---\n" contents << "title:\n" contents << "layout: post\n" contents << "date:\n" contents << "---\n" ext = nil else begin contents = File.open(layout_path, "r").read() ext = layout_path.match(/\.(.*?)$/)[1] rescue Errno::ENOENT bad_path(layout_path) end end return contents, ext end |
#get_slug_for(title) ⇒ Object
Given a post title (mixed case, spaces, etc.), generates a slug for This clobbers any non-ASCII text (TODO don’t do that)
74 75 76 77 |
# File 'lib/mr_poole/helper.rb', line 74 def get_slug_for(title) word_sep = @config.word_separator || '_' title.downcase.gsub(/[^a-z0-9_\s-]/, '').gsub(/\s+/, word_sep) end |
#get_time_stamp ⇒ Object
84 85 86 |
# File 'lib/mr_poole/helper.rb', line 84 def get_time_stamp Time.now.strftime("%H:%M %Z") end |
#open_in_editor(path) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/mr_poole/helper.rb', line 93 def open_in_editor(path) # don't do anything if the user hasn't explicitly enabled the feature return unless @config.auto_open if editor = ENV['EDITOR'] `#{editor} #{path}` else puts "You have enabled the auto_open feature in your config.yml," puts "but have no editor configured." puts '' puts "Please set your $EDITOR environment variable to the " puts "editor that poole should use to open new posts/drafts." puts '' end end |
#post_usage ⇒ Object
132 133 134 135 136 137 |
# File 'lib/mr_poole/helper.rb', line 132 def post_usage puts 'Usage:' puts ' poole post [OPTION] [ARG] TITLE' exit end |
#publish_usage ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/mr_poole/helper.rb', line 155 def publish_usage puts 'Usage:' puts ' poole publish [OPTIONS] PATH_TO_DRAFT' puts '' puts 'Options:' puts ' -d, --keep-draft Do not delete the draft post' puts ' -t, --keep-timestamp Do not update the draft timestamp' exit end |
#restore_orig_directory ⇒ Object
40 41 42 |
# File 'lib/mr_poole/helper.rb', line 40 def restore_orig_directory Dir.chdir(@orig_dir) end |
#unpublish_usage ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/mr_poole/helper.rb', line 165 def unpublish_usage puts 'Usage:' puts ' poole unpublish PATH_TO_POST' puts '' puts 'Options:' puts ' -p, --keep-post Do not delete the existing post' puts ' -t, --keep-timestamp Do not update the existing timestamp' exit end |
#version_statement ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/mr_poole/helper.rb', line 109 def version_statement puts '' puts "This is Mr. Poole, version #{MrPoole::VERSION}, running on ruby version #{RUBY_VERSION}" puts '' puts 'Copyright 2014, Michael McClimon' puts 'https://github.com/mmcclimon/mr_poole' puts '' exit end |