58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/jekyll-airtable-import.rb', line 58
def generate(site)
@site = site
@log_name = "Airtable:"
return unless site.config['airtable']
@site = site
if ENV['AIRTABLE_API_KEY']
api_key = ENV['AIRTABLE_API_KEY']
else
Jekyll.logger.warn @log_name, "No API key found. Make sure your key is available as AIRTABLE_API_KEY in the local environment => Ruby ENV hash."
return
end
@client = Airtable::Client.new(api_key)
@app_id = nil
@table_id = nil
@view_id = nil
site.config['airtable'].each do |name, conf|
conf ||= Hash.new
if conf['app']
@app_id = conf['app'][0..3] == 'ENV_' ? ENV[conf['app'][4..-1]] : conf['app']
end
unless @app_id
Jekyll.logger.warn @log_name, "No app ID for Airtable import of #{name}"
next
end
if conf['table']
@table_id = conf['table'][0..3] == 'ENV_' ? ENV[conf['table'][4..-1]] : conf['table']
end
unless @table_id
Jekyll.logger.warn @log_name, "No table ID for Airtable import of #{name}"
next
end
if conf['view']
@view_id = conf['view'][0..3] == 'ENV_' ? ENV[conf['view'][4..-1]] : conf['view']
end
Jekyll.logger.debug @log_name, "Importing #{name} from https://airtable.com/#{@app_id}/#{@table_id}/#{conf['view']}"
@table = @client.table(@app_id, @table_id)
@records = @table.all(:view => @view_id,:fields => conf['fields'])
Jekyll.logger.debug @log_name, "Found #{@records.length} records to import for #{name}"
data = @records.map { |record| record.attributes }
parsed_data = parse_airtable_data(data)
if conf['collection']
if conf['collection'].is_a?(Hash)
collection_conf = conf['collection']
end
collection_conf ||= Hash.new
content_field = collection_conf['content'] || 'content'
slug_fields = [collection_conf['slug'] || "slug", "slug", "title","name"]
layout = collection_conf['layout'] || name.singularize
if site.collections[name]
new_collection = site.collections[name]
else
new_collection = Jekyll::Collection.new(site, name)
end
parsed_data.each_with_index do |item, index|
content = item[conf['collection']['content'] || 'content'] if conf['collection'].is_a?(Hash)
for slug_field in slug_fields
if item[slug_field] and !item[slug_field].is_a?(Array)
slug = Jekyll::Utils.slugify(item[slug_field])
break
end
end
slug ||= Jekyll::Utils.slugify("#{name}#{index}")
path = File.join(site.source, "_#{name}", "#{slug}.md")
doc = Jekyll::Document.new(path, collection: new_collection, site: site)
item.merge!({
'layout' => layout,
'slug' => slug,
'airtable_id' => item['id']
})
doc.merge_data!(item.except('id'))
doc.content = content
new_collection.docs << doc
end
site.collections[name] = new_collection
else
site.data[name] = data
if conf['combine'] and site.collections.key?(name)
site.collections[name].docs.each do |document|
site.data[name].append(document)
end
end
end
end
end
|