Permalink
Browse files

allow the user to configure file naming via git config --global. norm…

…alize exts.
  • Loading branch information...
1 parent ea3a119 commit 6e2a4e5d9d9e23ae89a767380b70644bed11f575 @ahoward committed Jun 6, 2012
Showing with 29 additions and 9 deletions.
  1. +29 −9 lib/gist.rb
View
@@ -204,13 +204,12 @@ def copy(content)
# Give an array of file information and private boolean, returns
# an appropriate payload for POSTing to gist.github.com
def data(files, private_gist, description)
- i = 0
- file_data = {}
- files.each do |file|
- i = i + 1
- filename = file[:filename] ? file[:filename] : "gistfile#{i}"
- file_data[filename] = {:content => file[:input]}
- file_data[filename][:extension] = file[:extension].to_s.sub(/\A\.*/, '.')
+ file_data = Hash.new{|h,k| h[k] = {}}
+
+ files.each_with_index do |file, i|
+ filename = file[:filename] ? file[:filename] : default_filename(i)
+ file_data[filename][:content] = file[:input]
+ file_data[filename][:extension] = normalize_extension(file[:extension])
end
data = {"files" => file_data}
@@ -219,6 +218,23 @@ def data(files, private_gist, description)
data
end
+ # generates numbered filenames based on
+ #
+ # basename (git config --global 'gist.basename' 'teh_sweet_basename')
+ # extension (git config --global 'gist.extension' 'rb')
+ def default_filename(n)
+ basename = [defaults['basename'], (n if n > 0)].compact.join('-')
+ ext = normalize_extension(defaults['extension'])
+ "#{ basename }#{ ext }"
+ end
+
+ # teh hubz hate too many or not enough dots in the file extension so be
+ # careful to normalize it
+ #
+ def normalize_extension(ext)
+ ext.to_s.sub(/\A\.*/, '.')
+ end
+
# Returns a basic auth string of the user's GitHub credentials if set.
# http://github.com/guides/local-github-config
#
@@ -246,14 +262,18 @@ def auth
# Settings applicable to gist.rb are:
#
# gist.private - boolean
+ # gist.browse - boolean
# gist.extension - string
+ # gist.basename - string
def defaults
- extension = config("gist.extension") || '.txt'
+ extension = config("gist.extension") || ".txt"
+ basename = config("gist.basename") || "gistfile"
return {
"private" => config("gist.private"),
"browse" => config("gist.browse"),
- "extension" => extension
+ "extension" => extension,
+ "basename" => basename
}
end

0 comments on commit 6e2a4e5

Please sign in to comment.