siteleaf 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75e7b85ae46ef7e9a4acb9f91e7cfa6aad12d461
4
- data.tar.gz: f3fad5f5f09556a6d2e850300ea5f3a5ebb75483
3
+ metadata.gz: 4625aee84a44eb9f07f7d8500060446eeec72a99
4
+ data.tar.gz: 47767d4e1d46f4e6d88ffc4ba3b69d44ced29199
5
5
  SHA512:
6
- metadata.gz: ae362f7cba5c44002055e3eb7ac4110003738a34780af46f3e2f0509d33b780e6562f968ab8716e775a79aff119ca2849741aa8b60d4ecd88d7fef47bbb63e88
7
- data.tar.gz: c19eace8a1bf47b3e4e079cbc2f021bc47e05eb26a8f3148234d75b7f37c30526ab7e774cb051a011f2516c48c9e56cce671dce34699bc340b30e4afecfd46f3
6
+ metadata.gz: ec90b144dfa9ddf7faf832edc949b82f8e2a09b7a985923f4416a9c954e867b70029e018829cd157fa3053d3fea19ca665ea8f7a5edaaf3cded4afb213207bec
7
+ data.tar.gz: 6fa276f547fc28f8394a80f6960bc148661543e74e4b2a38a075b9eeefcb11b4787bd5cb617c0ee59b7a5b7bb5e7a2ad8a81521b7019cf93ef3141bfe409371f
data/bin/siteleaf CHANGED
@@ -4,6 +4,7 @@ require 'siteleaf'
4
4
  require 'fileutils'
5
5
  require 'open-uri'
6
6
  require 'digest/md5'
7
+ require 'yaml'
7
8
 
8
9
  def help
9
10
  %Q(
@@ -46,9 +47,7 @@ def auth(re_auth = false)
46
47
  puts "\nAuthorizing..."
47
48
 
48
49
  if (auth = Siteleaf::Client.auth(email, password)) && (auth.is_a?(Hash)) && (auth.has_key?('api_key'))
49
- File.open(Siteleaf.settings_file,'w') do|file|
50
- Marshal.dump({:api_key => auth['api_key'], :api_secret => auth['api_secret']}, file)
51
- end
50
+ Siteleaf.save_settings({api_key: auth['api_key'], api_secret: auth['api_secret']})
52
51
  puts "=> Gem authorized." if re_auth
53
52
  return true
54
53
  else
@@ -106,6 +105,10 @@ end
106
105
 
107
106
  def put_theme_assets(site_id)
108
107
  theme = Siteleaf::Theme.find_by_site_id(site_id)
108
+ unless theme.id
109
+ puts "Could not retrieve theme, check your site configuration and try again.\n"
110
+ return false
111
+ end
109
112
  assets = theme.assets
110
113
  updated_count = 0
111
114
  ignore_paths = ['config.ru', '.*']
@@ -172,31 +175,30 @@ def publish(site_id, quiet = true)
172
175
  end
173
176
  end
174
177
 
175
- def export(site_id, posts_path = "posts")
176
- FileUtils.mkdir_p 'export'
178
+ def export(site_id, posts_path = "posts", dir = 'export')
177
179
  posts_path = "posts" if posts_path == ""
178
180
 
179
181
  site = Siteleaf::Site.find(site_id)
180
182
  site.posts_path = posts_path
181
183
 
182
- export_uploads(site)
183
- export_content(site)
184
- export_config(site)
184
+ export_uploads(site, dir)
185
+ export_content(site, dir)
186
+ export_config(site, dir)
185
187
  end
186
188
 
187
- def export_config(site)
189
+ def export_config(site, dir = 'export')
188
190
  filename = site.filename
189
191
  print "Exporting #{filename}..."
190
192
 
191
- filename = File.join("export", filename)
193
+ filename = File.join(dir, filename)
192
194
  open(filename, 'w:UTF-8') do |f|
193
- f.puts site.to_file
195
+ f.puts site.to_file(dir)
194
196
  end
195
197
 
196
198
  print "complete.\n"
197
199
  end
198
200
 
199
- def export_content(site)
201
+ def export_content(site, dir = 'export')
200
202
  pages = site.pages
201
203
 
202
204
  pages.each do |page|
@@ -205,10 +207,10 @@ def export_content(site)
205
207
  filename = page.filename
206
208
  print "Exporting #{filename}..."
207
209
 
208
- filename = File.join("export", filename)
210
+ filename = File.join(dir, filename)
209
211
  FileUtils.mkdir_p File.dirname(filename)
210
212
  open(filename, 'w:UTF-8') do |f|
211
- f.puts page.to_file
213
+ f.puts page.to_file(dir)
212
214
  end
213
215
 
214
216
  print "complete.\n"
@@ -218,10 +220,10 @@ def export_content(site)
218
220
  filename = post.filename(site.posts_path)
219
221
  print "Exporting #{filename}..."
220
222
 
221
- filename = File.join("export", filename)
223
+ filename = File.join(dir, filename)
222
224
  FileUtils.mkdir_p File.dirname(filename)
223
225
  open(filename, 'w:UTF-8') do |f|
224
- f.puts post.to_file
226
+ f.puts post.to_file(dir)
225
227
  end
226
228
 
227
229
  print "complete.\n"
@@ -229,20 +231,10 @@ def export_content(site)
229
231
  end
230
232
  end
231
233
 
232
- def export_uploads(site)
233
- FileUtils::mkdir_p 'export/_uploads'
234
-
234
+ def export_uploads(site, dir = 'export')
235
235
  if assets = site.assets
236
236
  assets.each do |asset|
237
- filename = File.join("export", "_uploads", asset.filename)
238
- arr_path = filename.split('/')
239
-
240
- # Make subdirectories if needed
241
- if arr_path.length > 3
242
- subdir = arr_path[0..(arr_path.length - 2)].join('/')
243
- FileUtils::mkdir_p subdir
244
- end
245
-
237
+ filename = File.join(dir, "_uploads", asset.filename)
246
238
  if !File.exist?(filename) or (asset.checksum != Digest::MD5.hexdigest(IO.binread(filename)))
247
239
  print "Exporting #{filename}..."
248
240
 
@@ -282,8 +274,8 @@ when 'n', 'new'
282
274
  if auth != false
283
275
  if (site = Siteleaf::Site.create(:title => ARGV[1], :domain => ARGV[1])) && (!site.error)
284
276
  dir = ARGV.size >= 3 ? ARGV[2] : ARGV[1]
285
- Dir.mkdir(dir) unless File.directory?(dir)
286
- Dir.chdir(dir)
277
+ FileUtils.mkdir_p dir
278
+ Dir.chdir dir
287
279
  config site
288
280
  else
289
281
  puts "Could not create site `#{ARGV[1]}`.\n"
@@ -329,7 +321,9 @@ when 'export'
329
321
  if site_id = get_site_id
330
322
  print 'Enter your main posts path (default "posts"): '
331
323
  posts_path = $stdin.gets.chomp
332
- export(site_id, posts_path)
324
+ dir = ARGV.size >= 2 ? ARGV[1] : 'export'
325
+ FileUtils.mkdir_p dir
326
+ export(site_id, posts_path, dir)
333
327
  else
334
328
  puts "Site not configured, run `siteleaf config yoursite.com`.\n"
335
329
  end
data/lib/siteleaf.rb CHANGED
@@ -30,12 +30,50 @@ module Siteleaf
30
30
  end
31
31
 
32
32
  def self.settings_file
33
- File.expand_path('~/.siteleaf')
33
+ ::File.expand_path('~/.siteleaf.yml')
34
34
  end
35
35
 
36
- def self.load_settings
37
- if File.exist?(self.settings_file)
38
- config = File.open(self.settings_file) do|file|
36
+ def self.load_settings(file = self.settings_file)
37
+ if ::File.exist?(file)
38
+ settings = ::File.open(file) { |f| YAML.load(f) }
39
+
40
+ [:api_key, :api_secret, :api_base, :api_version].each do |key|
41
+ self.send "#{key}=", settings[key.to_s] if settings.has_key?(key.to_s)
42
+ end
43
+
44
+ symbolized_settings = Hash.new
45
+ settings.each{|k,v| symbolized_settings[k.to_sym] = v}
46
+
47
+ symbolized_settings
48
+
49
+ # read legacy settings, upgrade old marshal format into yaml
50
+ elsif self.load_legacy_settings
51
+ symbolized_settings = {api_key: self.api_key, api_secret: self.api_secret}
52
+ self.save_settings(symbolized_settings)
53
+ ::File.unlink(self.legacy_settings_file)
54
+ symbolized_settings
55
+ end
56
+ rescue
57
+ nil
58
+ end
59
+
60
+ def self.save_settings(settings, file = self.settings_file)
61
+ stringified_settings = Hash.new
62
+ settings.each{|k,v| stringified_settings[k.to_s] = v}
63
+
64
+ ::File.open(file, 'w') { |f| f.write stringified_settings.to_yaml }
65
+
66
+ settings
67
+ end
68
+
69
+ # here for v1 legacy purposes
70
+ def self.legacy_settings_file
71
+ ::File.expand_path('~/.siteleaf')
72
+ end
73
+
74
+ def self.load_legacy_settings
75
+ if ::File.exist?(legacy_settings_file)
76
+ config = ::File.open(legacy_settings_file) do|file|
39
77
  Marshal.load(file)
40
78
  end
41
79
  self.api_key = config[:api_key] if config.has_key?(:api_key)
@@ -5,7 +5,8 @@ module Siteleaf
5
5
  def self.auth(email, password)
6
6
  begin
7
7
  request = HTTParty.post(Siteleaf.api_url('auth'), {
8
- :basic_auth => {:username => email, :password => password}
8
+ :basic_auth => {:username => email, :password => password},
9
+ :headers => {"User-Agent" => "Siteleaf Gem/#{Siteleaf::VERSION}"}
9
10
  })
10
11
  return request.parsed_response # parse JSON
11
12
  rescue => e
@@ -35,6 +36,7 @@ module Siteleaf
35
36
  request = HTTMultiParty.send(method, Siteleaf.api_url(path), {
36
37
  :query => params,
37
38
  :basic_auth => {:username => Siteleaf.api_key, :password => Siteleaf.api_secret},
39
+ :headers => {"User-Agent" => "Siteleaf Gem/#{Siteleaf::VERSION}"},
38
40
  :timeout => 300
39
41
  })
40
42
  if request.respond_to?('parsed_response')
data/lib/siteleaf/page.rb CHANGED
@@ -43,8 +43,11 @@ module Siteleaf
43
43
  "#{url.sub('/','')}.markdown"
44
44
  end
45
45
 
46
- def to_file
47
- assets = Dir.glob("export/_uploads/**/*").each_with_object({}) { |var, hash| hash[var.sub('export/_uploads','/assets')] = var.sub('export/_uploads','/uploads') }
46
+ def to_file(dir = 'export')
47
+ assets = Dir.glob("#{dir}/_uploads/**/*").each_with_object({}) do |var, hash|
48
+ # remap assets to _uploads
49
+ hash[var.sub("#{dir}/_uploads",'/assets')] = var.sub("#{dir}/_uploads",'/uploads')
50
+ end
48
51
  (frontmatter + "---\n\n".freeze + body.to_s).gsub(Regexp.union(assets.keys), assets)
49
52
  end
50
53
 
data/lib/siteleaf/site.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Siteleaf
2
2
  class Site < Entity
3
3
 
4
- attr_accessor :title, :domain, :timezone, :meta, :posts_path
4
+ attr_accessor :title, :domain, :timezone, :meta, :posts_path, :version
5
5
  attr_reader :id, :user_id, :created_at, :updated_at
6
6
 
7
7
  def self.find_by_domain(domain)
@@ -53,8 +53,11 @@ module Siteleaf
53
53
  "_config.yml"
54
54
  end
55
55
 
56
- def to_file
57
- assets = Dir.glob("export/_uploads/**/*").each_with_object({}) { |var, hash| hash[var.sub('export/_uploads','/assets')] = var.sub('export/_uploads','/uploads') }
56
+ def to_file(dir = 'export')
57
+ assets = Dir.glob("#{dir}/_uploads/**/*").each_with_object({}) do |var, hash|
58
+ # remap assets to _uploads
59
+ hash[var.sub("#{dir}/_uploads",'/assets')] = var.sub("#{dir}/_uploads",'/uploads')
60
+ end
58
61
  config.gsub(Regexp.union(assets.keys), assets)
59
62
  end
60
63
 
@@ -64,12 +67,11 @@ module Siteleaf
64
67
  attrs = {}
65
68
  attrs['title'] = title
66
69
  attrs['url'] = "http://#{domain}"
67
-
68
- meta.each{|m| attrs[m['key']] = m['value'].to_s.gsub("\r\n","\n")} unless meta.nil?
69
-
70
70
  attrs['timezone'] = timezone
71
71
  attrs['permalink'] = 'pretty'
72
72
 
73
+ meta.each{|m| attrs[m['key']] = m['value'].to_s.gsub("\r\n","\n")} unless meta.nil?
74
+
73
75
  # output uploads using v1 /assets path
74
76
  attrs['collections'] = {
75
77
  'uploads' => {
@@ -1,3 +1,3 @@
1
1
  module Siteleaf
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siteleaf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siteleaf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty