Skip to content

Commit

Permalink
allow the user to configure file naming via git config --global. norm…
Browse files Browse the repository at this point in the history
…alize exts.
  • Loading branch information
ahoward committed Jun 6, 2012
1 parent ea3a119 commit 6e2a4e5
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions lib/gist.rb
Expand Up @@ -204,13 +204,12 @@ def copy(content)
# Give an array of file information and private boolean, returns # Give an array of file information and private boolean, returns
# an appropriate payload for POSTing to gist.github.com # an appropriate payload for POSTing to gist.github.com
def data(files, private_gist, description) def data(files, private_gist, description)
i = 0 file_data = Hash.new{|h,k| h[k] = {}}
file_data = {}
files.each do |file| files.each_with_index do |file, i|
i = i + 1 filename = file[:filename] ? file[:filename] : default_filename(i)
filename = file[:filename] ? file[:filename] : "gistfile#{i}" file_data[filename][:content] = file[:input]
file_data[filename] = {:content => file[:input]} file_data[filename][:extension] = normalize_extension(file[:extension])
file_data[filename][:extension] = file[:extension].to_s.sub(/\A\.*/, '.')
end end


data = {"files" => file_data} data = {"files" => file_data}
Expand All @@ -219,6 +218,23 @@ def data(files, private_gist, description)
data data
end 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. # Returns a basic auth string of the user's GitHub credentials if set.
# http://github.com/guides/local-github-config # http://github.com/guides/local-github-config
# #
Expand Down Expand Up @@ -246,14 +262,18 @@ def auth
# Settings applicable to gist.rb are: # Settings applicable to gist.rb are:
# #
# gist.private - boolean # gist.private - boolean
# gist.browse - boolean
# gist.extension - string # gist.extension - string
# gist.basename - string
def defaults def defaults
extension = config("gist.extension") || '.txt' extension = config("gist.extension") || ".txt"
basename = config("gist.basename") || "gistfile"


return { return {
"private" => config("gist.private"), "private" => config("gist.private"),
"browse" => config("gist.browse"), "browse" => config("gist.browse"),
"extension" => extension "extension" => extension,
"basename" => basename
} }
end end


Expand Down

0 comments on commit 6e2a4e5

Please sign in to comment.