zerofetcher 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63caf9975ea01eab3859437a18b8a96d4e1a727a
4
+ data.tar.gz: b0c5a693bfece4869310e2136161d6650f2d0a89
5
+ SHA512:
6
+ metadata.gz: 2a4e70f381da85e72e5283ba8de770d4e00282dd268f45118d514fea1531c423d89d2a832d4a6f9a16191735d3d598a9cb98abc58aa41a3eee7502d6135e0d59
7
+ data.tar.gz: 6d13f4ad3a73cd252ff2f14f159cd16fe0414743e44468454f9d399e09aeee9fdbe5a9f9d74233ce612d47b8d9b437de225a54ab9e3c1597532182f906856bc2
data/bin/zerofetcher ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'zerofetcher'
4
+ ZeroFetcher.run
@@ -0,0 +1,147 @@
1
+
2
+
3
+ class JekyllFile
4
+ @file_path
5
+ @file_exists = false
6
+ @file_contents
7
+ @yaml_section = ''
8
+ @yaml_parsed = {}
9
+ @content_section = ''
10
+ @file_data
11
+ @file_name
12
+ @ignore_keys = []
13
+ @content_keys = ['content']
14
+ @file_type
15
+
16
+ def initialize(path, data, file_type)
17
+ @content_keys = ['content']
18
+
19
+ @file_data = data;
20
+ @file_type = file_type
21
+
22
+ case @file_type
23
+ when 'page'
24
+ @file_name = data['url'] + '.md';
25
+ else
26
+ @file_name = data['slug'] + '.md';
27
+ end
28
+ @file_path = path + '/' + @file_name;
29
+
30
+ @yaml_parsed = {}
31
+ if Pathname.new(@file_path).file?
32
+ parseFile
33
+ else
34
+ if 'page' == @file_type
35
+ @yaml_parsed = {
36
+ 'permalink' => '/' + @file_data['url'] + '/'
37
+ }
38
+
39
+ @content_section = '{% include pages/' + @file_data['url'] + '.md %}'
40
+ end
41
+ end
42
+
43
+ # set layout
44
+ if !@yaml_parsed.key?('layout')
45
+ @yaml_parsed['layout'] = getLayout
46
+ end
47
+
48
+ # do some file type specific stuff
49
+ case @file_type
50
+ when 'page'
51
+ # set page title
52
+ title = @file_data['name']
53
+ if @file_data['meta_title']
54
+ title = @file_data['meta_title']
55
+ end
56
+
57
+ @yaml_parsed['title'] = title
58
+ else
59
+ # add file data
60
+ @file_data.each do |key, value|
61
+ if !@content_keys.include?(key)
62
+ #if !@ignore_keys.include?(key) && !@content_keys.include?(key)
63
+ #if !@ignore_keys.has_key?(key) && !@content_keys.has_key?(key)
64
+ @yaml_parsed[key] = value
65
+ end
66
+ end
67
+
68
+ # add content
69
+ @content_keys.each do |key|
70
+ if @file_data.key?(key)
71
+ @content_section = @file_data[key]
72
+ end
73
+ end
74
+ end
75
+
76
+ end if
77
+
78
+ def getLayout
79
+ case @file_type
80
+ when 'page'
81
+ if 'index' == @file_data['url']
82
+ layout = 'home'
83
+ else
84
+ layout = 'page'
85
+ end
86
+ else
87
+ layout = 'post'
88
+ end
89
+
90
+ return layout
91
+ end
92
+
93
+ def getFileName
94
+ return @file_name
95
+ end
96
+
97
+ def parseFile
98
+ file_contents = ZeroFetcher.readFile(@file_path)
99
+
100
+ file_contents.gsub!(/\r\n?/, "\n")
101
+
102
+ area = false
103
+ @yaml_section = ''
104
+ @content_section = ''
105
+
106
+ file_contents.each_line do |line|
107
+ #puts 'LINE AREA '+area.to_s+' '+line
108
+ sline = line
109
+
110
+ if '---' == sline.strip
111
+ case area
112
+ when false
113
+ area = 'yaml'
114
+ when 'yaml'
115
+ area = 'content'
116
+ end
117
+ else
118
+ case area
119
+ when 'yaml'
120
+ @yaml_section += line
121
+ when 'content'
122
+ @content_section += line
123
+ end
124
+ end
125
+ end
126
+
127
+ #@yaml_parsed = YAML.parse(@yaml_section)
128
+ @yaml_parsed = YAML.load(@yaml_section)
129
+ end
130
+
131
+ def getFileName()
132
+ return @file_path;
133
+ end
134
+
135
+ def saveContentFile(file_path, contents)
136
+ File.write(file_path, contents)
137
+ end
138
+
139
+ def savePageFile
140
+ #file_contents = '---A'+"\n"
141
+ #file_contents += YAML.generate(@yaml_parsed)+"\n"
142
+ file_contents = @yaml_parsed.to_yaml
143
+ file_contents += '---'+"\n"
144
+ file_contents += @content_section
145
+ File.write(@file_path, file_contents)
146
+ end
147
+ end
@@ -0,0 +1,31 @@
1
+
2
+ class JekyllPost
3
+ @file_path
4
+ @file_exists = false
5
+ @file_contents
6
+ @yaml_section = ''
7
+ @yaml_parsed
8
+ @content_section = ''
9
+ @post
10
+
11
+ def initialize(path, post)
12
+ @post = post
13
+ @file_path = path + '/' + @post['date']+'-'+@post['slug']+'.md'
14
+
15
+ @yaml_parsed = {
16
+ 'layout' => 'post',
17
+ 'title' => post['title'],
18
+ 'date' => post['date'],
19
+ 'categories' => post['categories'],
20
+ }
21
+
22
+ @content_section = post['content']
23
+ end
24
+
25
+ def savePageFile
26
+ file_contents = @yaml_parsed.to_yaml
27
+ file_contents += '---'+"\n"
28
+ file_contents += @content_section
29
+ File.write(@file_path, file_contents)
30
+ end
31
+ end
@@ -0,0 +1,241 @@
1
+ # Libs
2
+ require 'yaml'
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'fileutils'
6
+ require 'pathname'
7
+ require 'open-uri'
8
+
9
+ #app_path = File.dirname(__FILE__)
10
+ #require './code/functions.rb'
11
+ require 'code/JekyllFile.rb'
12
+ require 'code/JekyllPost.rb'
13
+
14
+ class ZeroFetcher
15
+ def self.run
16
+ # Paths
17
+ app_path = File.dirname(__FILE__)
18
+ jekyll_path = Dir.pwd
19
+
20
+ # App Requires
21
+ FileUtils::mkdir_p jekyll_path+'/_data'
22
+ FileUtils::mkdir_p jekyll_path+'/_posts'
23
+ FileUtils::mkdir_p jekyll_path+'/assets'
24
+ FileUtils::mkdir_p jekyll_path+'/assets/images'
25
+ FileUtils::mkdir_p jekyll_path+'/assets/files'
26
+ FileUtils::mkdir_p jekyll_path+'/_includes'
27
+ FileUtils::mkdir_p jekyll_path+'/_includes/pages'
28
+ FileUtils::mkdir_p jekyll_path+'/_includes/content_blocks'
29
+
30
+ if !Pathname.new(jekyll_path+"/_config.yml").file?
31
+ abort('_config.yml not found')
32
+ end
33
+
34
+ config = YAML.load_file(jekyll_path+"/_config.yml")
35
+
36
+ if !config.has_key?('fetcher')
37
+ abort('No Fetcher Info Found')
38
+ end
39
+
40
+ end_point = config['fetcher']['end_point']
41
+
42
+ data = self.loadDataFromApi(end_point, config['fetcher']['api_key'], config['fetcher']['site_id'])
43
+
44
+ # Site Info
45
+ if data.key?("site")
46
+ File.write(jekyll_path+'/_data/site.json', data['site'].to_json)
47
+ end
48
+
49
+ # Pages
50
+ if data.key?("pages")
51
+ pages_folder = jekyll_path+'/_includes/pages'
52
+
53
+ puts 'Pages - ' + data['pages'].length.to_s
54
+
55
+ # array to store hash info on all pages
56
+ pages = Array.new
57
+
58
+ # Loop through each page
59
+ data['pages'].each do |page|
60
+ puts page['name']
61
+ jpage = JekyllFile.new(jekyll_path, page, 'page')
62
+
63
+ jpage.saveContentFile(pages_folder+'/'+page['url']+'.md', page['contents'])
64
+ jpage.savePageFile
65
+ puts jpage.getFileName
66
+
67
+ # Add to pages array
68
+ pages.push({
69
+ 'name' => page['name'],
70
+ 'slug' => page['slug'],
71
+ 'url' => page['url'],
72
+ 'meta_title' => (page['meta_title']) ? page['meta_title'] : page['name'],
73
+ 'meta_description' => page['meta_description'],
74
+ 'short_description' => page['short_description'],
75
+ })
76
+ end
77
+
78
+ File.write(jekyll_path+'/_data/pages.json', pages.to_json)
79
+ end
80
+
81
+ # Variables
82
+ if data.key?("variables")
83
+ File.write(jekyll_path+'/_data/vars.json', data['variables'].to_json)
84
+ end
85
+
86
+ # Content Blocks
87
+ if data.key?("content_blocks")
88
+ puts 'Content Blocks - ' + data['content_blocks'].length.to_s
89
+
90
+ cb_folder = jekyll_path+'/_includes/content_blocks'
91
+
92
+ data['content_blocks'].each do |key, content_block|
93
+ File.write(cb_folder+'/'+key+'.md' , content_block.to_s)
94
+ end
95
+ end
96
+
97
+
98
+ # Posts
99
+ existing_post_files = Dir.glob(File.join(jekyll_path+'/_posts', "*"))
100
+
101
+ # Remove all post files
102
+ existing_post_files.each do |file|
103
+ FileUtils.rm(file)
104
+ end
105
+
106
+ # Post files
107
+ if data.key?("posts")
108
+ puts "Posts - " + data['posts'].length.to_s
109
+ data['posts'].each do |post|
110
+ jpost = JekyllPost.new(jekyll_path+'/_posts', post)
111
+ jpost.savePageFile
112
+ end
113
+ end
114
+
115
+
116
+ # Galleries
117
+ if data.key?("galleries")
118
+ FileUtils::mkdir_p jekyll_path+'/assets/images/galleries'
119
+
120
+ puts "Galleries - " + data['galleries'].length.to_s
121
+
122
+ # Save Json Data
123
+ File.write(jekyll_path+'/_data/galleries.json', data['galleries'].to_json)
124
+
125
+ # Save Photos
126
+ data['galleries'].each do |gallery|
127
+ FileUtils::mkdir_p jekyll_path+'/assets/images/galleries/' + gallery['id']
128
+
129
+ puts " - " + gallery['name'] + ' has ' + gallery['photos'].length.to_s + ' photos'
130
+
131
+ gallery['photos'].each do |photo|
132
+ self.getAndSaveFile(end_point + '/media/images/galleries/photos/' + gallery['id'] + '/' + photo['image'], jekyll_path+'/assets/images/galleries/' + gallery['id'] + '/' + photo['image'])
133
+ end
134
+ end
135
+ end
136
+
137
+ # Calendar
138
+ if data.key?("calendar")
139
+ FileUtils::mkdir_p jekyll_path+'/assets/images/calendar'
140
+ FileUtils::mkdir_p jekyll_path+'/assets/files/calendar'
141
+
142
+ puts "Calendar - " + data['calendar'].length.to_s
143
+
144
+ # Save Json Data
145
+ File.write(jekyll_path+'/_data/calendar.json', data['calendar'].to_json)
146
+
147
+ # Save Photos / Files
148
+ if data['calendar'].key?("events")
149
+ data['calendar']['events'].each do |event|
150
+ if event['image']
151
+ self.getAndSaveFile(end_point + '/media/images/calendar/' + event['image'], jekyll_path+'/assets/images/calendar/' + event['image'])
152
+ end
153
+
154
+ if event['file']
155
+ self.getAndSaveFile(end_point + '/media/files/calendar/' + event['file'], jekyll_path+'/assets/files/calendar/' + event['file'])
156
+ end
157
+ end
158
+ end
159
+ end
160
+
161
+
162
+ # Custom Collections
163
+ if data.key?("collections")
164
+ # Create empty collection hash
165
+ collection_config = {};
166
+
167
+ data['collections'].each do |collection_type,collection_data|
168
+ puts "Collection - " + collection_type + ' - ' + collection_data.length.to_s
169
+
170
+ # Add to collection hash
171
+ collection_config[ collection_type ] = ['output' => true];
172
+
173
+ # Save Json Data
174
+ File.write(jekyll_path+'/_data/'+collection_type+'.json', collection_data.to_json)
175
+
176
+ # Directory Check
177
+ collection_path = jekyll_path+'/_' + collection_type
178
+ FileUtils::mkdir_p collection_path
179
+
180
+ # Create files saved array
181
+ files_saved = Array.new
182
+
183
+ # Loop through the collection data
184
+ collection_data.each do |row|
185
+ jfile = JekyllFile.new(collection_path, row, 'collection')
186
+ jfile.savePageFile
187
+
188
+ files_saved.push(jfile.getFileName)
189
+ end
190
+
191
+ # Clean up unused collection files
192
+ self.cleanUpFolder(collection_path, files_saved)
193
+ end
194
+
195
+ # Add Collections to _config.tml
196
+ config['collections'] = collection_config
197
+ File.write(jekyll_path+"/_config.yml", config.to_yaml)
198
+ end
199
+ end
200
+
201
+
202
+ def self.readFile(file)
203
+ file = File.open(file, "r")
204
+ data = file.read
205
+ file.close
206
+ return data
207
+ end
208
+
209
+ def self.loadDataFromApi(end_point, api_key, site_id)
210
+ uri = URI(end_point+'/api/Sites/allcontent')
211
+ params = { :key => api_key, :site_id => site_id }
212
+ uri.query = URI.encode_www_form(params)
213
+
214
+ res = Net::HTTP.get_response(uri)
215
+
216
+ return JSON.parse(res.body)['data']
217
+ end
218
+
219
+ def self.getAndSaveFile(source, dest)
220
+ if !Pathname.new(dest).file?
221
+ uri = URI.encode(source)
222
+
223
+ open(dest, 'wb') do |file|
224
+ file << open(uri).read
225
+ end
226
+ end
227
+ end
228
+
229
+ def self.cleanUpFolder(path, files)
230
+ existing_files = Dir.glob(File.join(path, "*"))
231
+
232
+ existing_files.each do |file|
233
+ if !files.include?( file )
234
+ FileUtils.rm(file)
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+
241
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zerofetcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Brian Liccardo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Fetches content from zero
14
+ email: brian@d3corp.com
15
+ executables:
16
+ - zerofetcher
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/zerofetcher
21
+ - lib/code/JekyllFile.rb
22
+ - lib/code/JekyllPost.rb
23
+ - lib/zerofetcher.rb
24
+ homepage: http://d3corp.com
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Zero Fetcher
48
+ test_files: []