c2h 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/bin/c2h +203 -0
  3. metadata +73 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cae7285e2e82cffe04fdd8f1a2cd4357bd9a7e11
4
+ data.tar.gz: 531c6061d32805085e347173f72ccafaf16e6ff9
5
+ SHA512:
6
+ metadata.gz: 94996ba16336b4d43e451efa1b3ee0d1087d3006fb7355743dc186c84b0ee98bbacdf572066f857d8394c593faf467057b0f36fe048339c8f8daa03aff2b2b37
7
+ data.tar.gz: 20633feb61710c54fd45d5075a5040cb12ece627a1421f32775b7b5e1fc133fe64eb0898d85b990b185975cc81b2db92f64663c021f9255174d0dcea0f9c7012
data/bin/c2h ADDED
@@ -0,0 +1,203 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'contentful'
4
+ require 'yaml'
5
+ require 'choice'
6
+ require 'open-uri'
7
+ require 'fileutils'
8
+ require 'time'
9
+
10
+ PROGRAM_VERSION = '0.0.1'
11
+
12
+ Choice.options do
13
+
14
+ option :configfile do
15
+ short '-c'
16
+ long '--conf pad/to/configfile'
17
+ desc 'Location of your configfile'
18
+ desc '(default contentful.yaml)'
19
+ default 'contentful.yaml'
20
+ end
21
+
22
+ option :help do
23
+ long '--help'
24
+ desc 'Show this message'
25
+ end
26
+
27
+ option :verbose do
28
+ long '--verbose'
29
+ short '-v'
30
+ desc 'Give more output'
31
+ end
32
+
33
+ option :version do
34
+ long '--version'
35
+ desc 'Show version'
36
+ action do
37
+ puts "c2h Contentful 2 Hugo v#{PROGRAM_VERSION}"
38
+ exit
39
+ end
40
+ end
41
+ end
42
+
43
+ begin
44
+ #Load config file
45
+ config = YAML.load_file(Choice.choices.configfile)
46
+
47
+ # Check if the content_dir is set in configfile
48
+ raise "content_dir not set in config file" if config['content_dir']== '' || config['content_dir'] == nil
49
+
50
+ # Check if the access_token is set in configfile
51
+ raise "access_token not set in config file" if config['access_token']== '' || config['access_token'] == nil
52
+
53
+ #Content dir location
54
+ content_dir = "#{File.dirname(Choice.choices.configfile)}/#{config['content_dir']}"
55
+
56
+ # Check if content directory exists
57
+ raise "Content directory not found - #{content_dir}" if !File.directory?(content_dir)
58
+
59
+ if config['download_images'] == 'true' || config['download_images'] == true
60
+ # Check if the image_dir is set in configfile
61
+ raise "image_dir not set in config file" if config['image_dir']== '' || config['image_dir'] == nil
62
+
63
+ #Image dir location
64
+ image_dir = "#{File.dirname(Choice.choices.configfile)}/#{config['image_dir']}"
65
+
66
+ # Check if image directory exists
67
+ raise "Image directory not found - #{image_dir}" if !File.directory?(image_dir)
68
+
69
+ # Image download list (no double downloads for the same img)
70
+ downloaded_images = {}
71
+ end
72
+
73
+ # Process spaces
74
+ config["spaces"].each do |space_key, space_config|
75
+
76
+ puts "Getting #{space_key} => #{space_config['section']}" if Choice.choices.verbose
77
+
78
+ begin
79
+ # Check if section is set
80
+ raise "No section set for this space - #{space_key}" if space_config['section'] == '' || space_config['section'] == nil
81
+
82
+ # Get space
83
+ client = Contentful::Client.new(
84
+ access_token: config['access_token'],
85
+ space: space_key
86
+ )
87
+
88
+ #section content directory Location
89
+ section_content_dir = "#{content_dir}/#{space_config['section']}"
90
+
91
+ # Process entries
92
+ client.entries.each do |entry|
93
+
94
+ # Reset variables
95
+ content = ''
96
+ fields = {}
97
+ filename = ''
98
+
99
+ # Process fields
100
+ entry.fields.each do |key, value|
101
+ key = key[0,key.length] #remove ':' before keys
102
+ if space_config['filename'] != nil && space_config['filename'] != '' && key == space_config['filename']
103
+ filename = value
104
+ end
105
+ if key == space_config['content']
106
+ content = value
107
+ else
108
+ fields[key] = value
109
+ end
110
+ end
111
+
112
+ # If no filename field is found, the entry id is used
113
+ filename = entry.id if filename == ''
114
+
115
+ # Path to content-file
116
+ fullpath = "#{section_content_dir}/#{filename}.md"
117
+
118
+ if File.new(fullpath).mtime > Time.parse(entry.sys[:updatedAt].to_s)
119
+ puts " #{fullpath}: UpToDate -> skip" if Choice.choices.verbose
120
+ else
121
+
122
+ if config['download_images'] == 'true' || config['download_images'] == true
123
+ # Section image directory location
124
+ section_image_dir = "#{image_dir}/#{space_config['section']}"
125
+
126
+ # Entry image directory location
127
+ entry_image_dir = "#{section_image_dir}/#{filename}"
128
+
129
+ # Get images from content
130
+ content.scan(/!\[[^\]]*\]\(([A-Za-z0-9_\/\.\-]*\/)([A-Za-z0-9_\.\-]+)\)/).each do |url, name|
131
+
132
+ puts " #{entry_image_dir}/#{name}" if Choice.choices.verbose
133
+
134
+ # Create sub directory for section if it doesn't exist
135
+ if !File.directory?(section_image_dir)
136
+ Dir.mkdir(section_image_dir)
137
+ end
138
+
139
+ # Create sub directory for entry if it doesn't exist
140
+ if !File.directory?(entry_image_dir)
141
+ Dir.mkdir(entry_image_dir)
142
+ end
143
+
144
+ full_url = "http:#{url}#{name}"
145
+ full_path = "#{entry_image_dir}/#{name}"
146
+
147
+ # Image isn't downloaded yet
148
+ if downloaded_images[full_url] == nil
149
+ begin
150
+ # Download image & write to file
151
+ File.write(full_path, open(full_url).read)
152
+ if downloaded_images[full_url] == nil
153
+ downloaded_images[full_url] = {}
154
+ end
155
+ downloaded_images[full_url][full_path] = true;
156
+ rescue => e
157
+ puts (Choice.choices.verbose ? " #{e.message}": "#{e.message}")
158
+
159
+ downloaded_images[full_url][full_path] = false;
160
+ end
161
+ else
162
+ # The image was downloaded but to an other location
163
+ if downloaded_images[full_url][full_path] == nil
164
+ # Search already downloaded copy
165
+ downloaded_images[full_url].each do |u|
166
+ prev_full_url = u
167
+ next
168
+ end
169
+ begin
170
+ # Copy prev downloaded copy
171
+ FileUtils.cp(prev_full_url, full_url);
172
+ downloaded_images[full_url][full_path] = true;
173
+ rescue => e
174
+ puts (Choice.choices.verbose ? " #{e.message}": "#{e.message}")
175
+ downloaded_images[full_url][full_path] = false;
176
+ end
177
+ end
178
+ end
179
+ # Replace URL in content, remove static dir if present
180
+ content = content.sub("#{url}#{name}", full_path.sub(/.*static/,''))
181
+ end
182
+ end
183
+
184
+ # Create sub directory for section if it doesn't exist
185
+ if !File.directory?(section_content_dir)
186
+ Dir.mkdir(section_content_dir)
187
+ end
188
+
189
+ # Write file
190
+ File.open(fullpath, 'w') do |file|
191
+ file.write(fields.to_yaml)
192
+ file.write("---\n")
193
+ file.write(content)
194
+ end
195
+ end
196
+ end
197
+ rescue => e
198
+ puts (Choice.choices.verbose ? " #{e.message}": "#{e.message}")
199
+ end
200
+ end
201
+ rescue => e
202
+ puts e.message
203
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: c2h
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arno Nuyts
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: contentful
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: choice
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ description: A tool to create content-files for hugo from content on contentful
42
+ email: arno.nuyts@gmail.com
43
+ executables:
44
+ - c2h
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - bin/c2h
49
+ homepage: https://github.com/ArnoNuyts/contentful2hugo
50
+ licenses:
51
+ - Apache License 2.0
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.14
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Contentful 2 Hugo
73
+ test_files: []