wax_tasks 1.0.1 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of wax_tasks might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7aaaa017dde02724ff7e1c5099365ba0711a4da6be73f3ba2a648b737c7d7cd1
4
- data.tar.gz: 11c56f4a34b598d17912eba5c94b9f2c77a2b6ee7628dcdf591743e0213c324f
3
+ metadata.gz: d8c25dc1366583fde027108991c23bb9ea21f20dd88d8341d1bcfeaafe3ec48c
4
+ data.tar.gz: 8f5e15e6278ca7879da49ba0c5d60dfbbc064fa572ce3593b92ffc5b706f0094
5
5
  SHA512:
6
- metadata.gz: db11be3906fb85b4d8d454818441ad29893eed2de6c27c54231dcf31013449ff4126e9aea77797ed727497463e6211f92655c3410dd192347116c3e08970e2ff
7
- data.tar.gz: a851be1a81ecd5edaaec560d701b25975edca9943c6a9b7d7b0e2ab916cde986c2e3904594af2e77da7ffcd7973c8bac0ee415217eb73691e0466808dd247501
6
+ metadata.gz: cdd850cd456911bc6f4630f8158866430fe930812b744a489ada634cb2fd0b8be317ccac862c574ea028026e4424043a05a0bdf8374d9dadab77f198ebfcad4c
7
+ data.tar.gz: 882d43181056533afe818d353915c0d4544908d747dc79b375ea9b068e70d05fea27648b89f3b22426b6bea868f9a7144e8709a346ab574fe2f0586766b81546
data/spec/setup.rb CHANGED
File without changes
data/spec/spec_helper.rb CHANGED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wax_tasks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marii Nyrop
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2021-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: progress_bar
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '12.3'
47
+ version: '13.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '12.3'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: safe_yaml
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -70,20 +70,14 @@ dependencies:
70
70
  name: wax_iiif
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: 0.1.2
76
- - - "<"
73
+ - - "~>"
77
74
  - !ruby/object:Gem::Version
78
75
  version: '0.2'
79
76
  type: :runtime
80
77
  prerelease: false
81
78
  version_requirements: !ruby/object:Gem::Requirement
82
79
  requirements:
83
- - - ">="
84
- - !ruby/object:Gem::Version
85
- version: 0.1.2
86
- - - "<"
80
+ - - "~>"
87
81
  - !ruby/object:Gem::Version
88
82
  version: '0.2'
89
83
  - !ruby/object:Gem::Dependency
@@ -100,30 +94,13 @@ dependencies:
100
94
  - - "~>"
101
95
  - !ruby/object:Gem::Version
102
96
  version: '3'
103
- description: Rake tasks for minimal exhibition sites with Jekyll Wax.
97
+ description: Rake tasks for minimal exhibition sites with Minicomp/Wax.
104
98
  email:
105
- - m.nyrop@columbia.edu
99
+ - marii@nyu.edu
106
100
  executables: []
107
101
  extensions: []
108
102
  extra_rdoc_files: []
109
103
  files:
110
- - Gemfile
111
- - lib/tasks/derivatives_iiif.rake
112
- - lib/tasks/derivatives_simple.rake
113
- - lib/tasks/pages.rake
114
- - lib/tasks/search.rake
115
- - lib/wax_tasks.rb
116
- - lib/wax_tasks/asset.rb
117
- - lib/wax_tasks/collection.rb
118
- - lib/wax_tasks/collection/images.rb
119
- - lib/wax_tasks/collection/metadata.rb
120
- - lib/wax_tasks/config.rb
121
- - lib/wax_tasks/error.rb
122
- - lib/wax_tasks/index.rb
123
- - lib/wax_tasks/item.rb
124
- - lib/wax_tasks/record.rb
125
- - lib/wax_tasks/site.rb
126
- - lib/wax_tasks/utils.rb
127
104
  - spec/setup.rb
128
105
  - spec/spec_helper.rb
129
106
  homepage: https://github.com/minicomp/wax_tasks
@@ -131,7 +108,7 @@ licenses:
131
108
  - MIT
132
109
  metadata:
133
110
  yard.run: yri
134
- post_install_message:
111
+ post_install_message:
135
112
  rdoc_options: []
136
113
  require_paths:
137
114
  - lib
@@ -148,11 +125,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
125
  requirements:
149
126
  - imagemagick
150
127
  - ghostscript
151
- rubyforge_project:
152
- rubygems_version: 2.7.6
153
- signing_key:
128
+ rubygems_version: 3.1.4
129
+ signing_key:
154
130
  specification_version: 4
155
- summary: Rake tasks for minimal exhibition sites with Jekyll Wax.
131
+ summary: Rake tasks for minimal exhibition sites with Minicomp/Wax.
156
132
  test_files:
157
133
  - spec/spec_helper.rb
158
134
  - spec/setup.rb
data/Gemfile DELETED
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
- gemspec
5
-
6
- # dev/test utilities
7
- gem 'rubocop', require: false
8
- gem 'simplecov', require: false
9
- gem 'yard', require: false
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wax_tasks'
4
-
5
- namespace :wax do
6
- namespace :derivatives do
7
- desc 'generate iiif derivatives from local image files'
8
- task :iiif do
9
- args = ARGV.drop(1).each { |a| task a.to_sym }
10
- raise WaxTasks::Error::MissingArguments, Rainbow("You must specify a collection after 'wax:derivatives:iiif'").magenta if args.empty?
11
-
12
- site = WaxTasks::Site.new
13
- args.each { |a| site.generate_derivatives(a, 'iiif') }
14
- end
15
- end
16
-
17
- # alias wax:iiif to wax:derivatives:iiif for backwards compatibility
18
- task :iiif do
19
- t = Rake::Task['wax:derivatives:iiif']
20
- desc t.full_comment if t.full_comment
21
- arguments = ARGV.drop(1).each { |a| task a.to_sym }
22
- t.invoke(*arguments)
23
- end
24
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wax_tasks'
4
-
5
- namespace :wax do
6
- namespace :derivatives do
7
- desc 'generate iiif derivatives from local image files'
8
- task :simple do
9
- args = ARGV.drop(1).each { |a| task a.to_sym }
10
- raise WaxTasks::Error::MissingArguments, Rainbow("You must specify a collection after 'wax:derivatives:simple'").magenta if args.empty?
11
-
12
- site = WaxTasks::Site.new
13
- args.each { |a| site.generate_derivatives(a, 'simple') }
14
- end
15
- end
16
- end
data/lib/tasks/pages.rake DELETED
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wax_tasks'
4
-
5
- namespace :wax do
6
- desc 'generate collection md pages from yaml or csv data source'
7
- task :pages do
8
- args = ARGV.drop(1).each { |a| task a.to_sym }
9
- raise WaxTasks::Error::MissingArguments, Rainbow('You must specify a collection after wax:pages').magenta if args.empty?
10
-
11
- site = WaxTasks::Site.new
12
- args.each { |a| site.generate_pages a }
13
- end
14
-
15
- # alias :pagemaster to wax:pages for backwards compatibility
16
- task :pagemaster do
17
- t = Rake::Task['wax:pages']
18
- desc t.full_comment if t.full_comment
19
- args = ARGV.drop(1).each { |a| task a.to_sym }
20
- t.invoke(*args)
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'wax_tasks'
4
-
5
- namespace :wax do
6
- desc 'build lunr search index (with default UI if UI=true)'
7
- task :search do
8
- args = ARGV.drop(1).each { |a| task a.to_sym }
9
- raise WaxTasks::Error::MissingArguments, Rainbow('You must specify a collection after wax:search').magenta if args.empty?
10
-
11
- site = WaxTasks::Site.new
12
- args.each { |a| site.generate_static_search a }
13
- end
14
-
15
- # alias lunr to search for backwards compatibility
16
- task :lunr do
17
- t = Rake::Task['wax:search']
18
- desc t.full_comment if t.full_comment
19
- args = ARGV.drop(1).each { |a| task a.to_sym }
20
- t.invoke(*args)
21
- end
22
- end
data/lib/wax_tasks.rb DELETED
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # rubygems
4
- require 'rubygems'
5
-
6
- # stdlib
7
- require 'csv'
8
- require 'fileutils'
9
- require 'json'
10
-
11
- # 3rd party
12
- require 'rainbow'
13
- require 'safe_yaml'
14
-
15
- # relative
16
- require_relative 'wax_tasks/asset'
17
- require_relative 'wax_tasks/collection'
18
- require_relative 'wax_tasks/config'
19
- require_relative 'wax_tasks/error'
20
- require_relative 'wax_tasks/index'
21
- require_relative 'wax_tasks/item'
22
- require_relative 'wax_tasks/record'
23
- require_relative 'wax_tasks/site'
24
- require_relative 'wax_tasks/utils'
25
-
26
- #
27
- module WaxTasks
28
- DEFAULT_CONFIG_FILE = './_config.yml'
29
- #
30
- #
31
- def self.config_from_file(file = nil)
32
- Utils.validate_yaml(file || DEFAULT_CONFIG_FILE)
33
- rescue StandardError => e
34
- raise WaxTasks::Error::InvalidConfig, "Cannot open config file '#{file}'.\n #{e}"
35
- end
36
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- module WaxTasks
5
- Derivative = Struct.new(:path, :label, :img)
6
- attr_reader :id, :path
7
-
8
- #
9
- class Asset
10
- def initialize(path, pid, variants)
11
- @path = path
12
- @pid = pid
13
- @id = asset_id
14
- @variants = variants
15
- end
16
-
17
- #
18
- #
19
- def asset_id
20
- id = File.basename(@path, '.*')
21
- id.prepend "#{@pid}_" unless id == @pid
22
- id
23
- end
24
-
25
- #
26
- #
27
- def simple_derivatives
28
- @variants.map do |label, width|
29
- img = MiniMagick::Image.open(@path)
30
- raise WaxTasks::Error::InvalidConfig, "Requested variant width '#{width}' is larger than original image width." if width > img.width
31
-
32
- img.resize width
33
- img.format 'jpg'
34
- Derivative.new("#{@id}/#{label}.jpg", label, img)
35
- end
36
- end
37
-
38
- #
39
- #
40
- def to_iiif_image_record(is_only, index, base_opts)
41
- opts = base_opts.clone
42
- opts[:is_primary] = index.zero?
43
- opts[:section_label] = "Page #{index + 1}" unless is_only
44
- opts[:path] = @path
45
- opts[:manifest_id] = @pid
46
- opts[:id] = @id
47
-
48
- WaxIiif::ImageRecord.new(opts)
49
- end
50
- end
51
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'collection/images'
4
- require_relative 'collection/metadata'
5
-
6
- module WaxTasks
7
- #
8
- class Collection
9
- attr_reader :name, :config, :ext, :search_fields,
10
- :page_source, :metadata_source, :imagedata_source
11
-
12
- include Collection::Metadata
13
- include Collection::Images
14
-
15
- #
16
- #
17
- def initialize(name, config, source, collections_dir, ext)
18
- @name = name
19
- @config = config
20
- @page_extension = ext
21
- @site_source = source
22
- @page_source = Utils.safe_join source, collections_dir, "_#{name}"
23
- @metadata_source = Utils.safe_join source, '_data', config.dig('metadata', 'source')
24
- @imagedata_source = Utils.safe_join source, '_data', config.dig('images', 'source')
25
- @search_fields = %w[pid label thumbnail permalink collection]
26
- @image_variants = image_variants
27
- end
28
- end
29
- end
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mini_magick'
4
- require 'progress_bar'
5
- require 'wax_iiif'
6
-
7
- #
8
- module WaxTasks
9
- #
10
- class Collection
11
- #
12
- module Images
13
- #
14
- #
15
- def image_variants
16
- default_variants = { 'thumbnail' => 250, 'full' => 1140 }
17
- custom_variants = @config.dig('images', 'variants') || {}
18
- default_variants.merge custom_variants
19
- end
20
-
21
- #
22
- #
23
- def items_from_imagedata
24
- raise Error::MissingSource, "Cannot find image data source '#{@imagedata_source}'" unless Dir.exist? @imagedata_source
25
-
26
- pre_process_pdfs
27
- records = records_from_metadata
28
- Dir.glob(Utils.safe_join(@imagedata_source, '*')).map do |path|
29
- item = WaxTasks::Item.new(path, @image_variants)
30
- next unless item.valid?
31
-
32
- item.record = records.find { |r| r.pid == item.pid }
33
- item.iiif_config = @config.dig 'images', 'iiif'
34
- warn Rainbow("\nWarning:\nCould not find record in #{@metadata_source} for image item #{path}.\n").orange if item.record.nil?
35
- item
36
- end.compact
37
- end
38
-
39
- #
40
- #
41
- def pre_process_pdfs
42
- Dir.glob(Utils.safe_join(@imagedata_source, '*.pdf')).each do |path|
43
- target_dir = path.gsub '.pdf', ''
44
- next unless Dir.glob("#{target_dir}/*").empty?
45
-
46
- puts Rainbow("\nPreprocessing #{path} into image files. This may take a minute.\n").cyan
47
- opts = { output_dir: File.dirname(target_dir) }
48
- WaxIiif::Utilities::PdfSplitter.split(path, opts)
49
- end
50
- end
51
-
52
- #
53
- #
54
- def write_simple_derivatives(dir)
55
- puts Rainbow("Generating simple image derivatives for collection '#{@name}'\nThis might take awhile.").cyan
56
-
57
- bar = ProgressBar.new(items_from_imagedata.length)
58
- items_from_imagedata.map do |item|
59
- item.simple_derivatives.each do |d|
60
- path = "#{dir}/#{d.path}"
61
- FileUtils.mkdir_p File.dirname(path)
62
- next if File.exist? path
63
-
64
- d.img.write path
65
- item.record.set d.label, path if item.record?
66
- end
67
- bar.increment!
68
- bar.write
69
- item
70
- end.flat_map(&:record)
71
- end
72
-
73
- #
74
- #
75
- def iiif_builder(dir)
76
- build_opts = {
77
- base_url: "{{ '/' | absolute_url }}#{dir}",
78
- output_dir: dir,
79
- collection_label: @name
80
- }
81
- WaxIiif::Builder.new(build_opts)
82
- end
83
-
84
- #
85
- #
86
- def add_font_matter_to_json_files(dir)
87
- Dir.glob("#{dir}/**/*.json").each do |f|
88
- Utils.add_yaml_front_matter_to_file f
89
- end
90
- end
91
-
92
- #
93
- #
94
- def add_iiif_results_to_records(records, manifests)
95
- records.map do |record|
96
- next nil if record.nil?
97
-
98
- manifest = manifests.find { |m| m.base_id == record.pid }
99
- next record if manifest.nil?
100
-
101
- json = JSON.parse manifest.to_json
102
- @image_variants.each do |k, _v|
103
- value = json.dig k
104
- record.set k, "/#{Utils.content_clean(value)}" unless value.nil?
105
- end
106
-
107
- record.set 'manifest', "/#{Utils.content_clean(manifest.id)}"
108
- record
109
- end.compact
110
- end
111
-
112
- #
113
- #
114
- def write_iiif_derivatives(dir)
115
- items = items_from_imagedata
116
- iiif_data = items.map(&:iiif_image_records).flatten
117
- builder = iiif_builder(dir)
118
-
119
- builder.load iiif_data
120
-
121
- puts Rainbow("Generating IIIF derivatives for collection '#{@name}'\nThis might take awhile.").cyan
122
- builder.process_data
123
-
124
- add_font_matter_to_json_files dir
125
- add_iiif_results_to_records items.map(&:record), builder.manifests
126
- end
127
- end
128
- end
129
- end
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- module WaxTasks
5
- #
6
- class Collection
7
- #
8
- module Metadata
9
- #
10
- #
11
- def search_fields=(fields)
12
- @search_fields.concat(fields).flatten.compact.uniq
13
- end
14
-
15
- #
16
- #
17
- def records_from_pages
18
- paths = Dir.glob("#{@page_source}/*.{md, markdown}")
19
- warn Rainbow("There are no pages in #{@page_source} to index.").orange if paths.empty?
20
-
21
- paths.map do |path|
22
- begin
23
- content = WaxTasks::Utils.content_clean File.read(path)
24
- Record.new(SafeYAML.load_file(path)).tap do |r|
25
- r.set 'content', content
26
- r.set 'permalink', "/#{@name}/#{r.pid}#{@ext}" unless r.permalink?
27
- end
28
- rescue StandardError => e
29
- raise Error::PageLoad, "Cannot load page #{path}\n#{e}"
30
- end
31
- end
32
- end
33
-
34
- #
35
- #
36
- def records_from_metadata
37
- raise Error::MissingSource, "Cannot find metadata source '#{@metadata_source}'" unless File.exist? @metadata_source
38
-
39
- metadata = Utils.ingest @metadata_source
40
- metadata.each_with_index.map do |meta, i|
41
- Record.new(meta).tap do |r|
42
- r.set 'order', Utils.padded_int(i, metadata.length) unless r.order?
43
- r.set 'layout', @config['layout'] if @config.key? 'layout'
44
- r.set 'collection', @name
45
- end
46
- end
47
- end
48
-
49
- #
50
- #
51
- def update_metadata(update)
52
- records = consolidate_records records_from_metadata, update
53
- reformatted = case File.extname @metadata_source
54
- when '.csv'
55
- csv_string records
56
- when '.json'
57
- json_string records
58
- when /\.ya?ml/
59
- yaml_string records
60
- end
61
- File.open(@metadata_source, 'w') { |f| f.puts reformatted }
62
- end
63
-
64
- #
65
- #
66
- def consolidate_records(original, new)
67
- lost_record_pids = original.map(&:pid) - new.map(&:pid)
68
- lost_record_pids.each do |pid|
69
- new << original.find { |r| r.pid == pid }
70
- end
71
- new.sort_by(&:order)
72
- end
73
-
74
- #
75
- #
76
- def csv_string(records)
77
- keys = records.flat_map(&:keys).uniq
78
- CSV.generate do |csv|
79
- csv << keys
80
- records.each do |r|
81
- csv << keys.map { |k| r.hash.fetch(k, '') }
82
- end
83
- end
84
- end
85
-
86
- #
87
- #
88
- def json_string(records)
89
- hashes = records.map(&:hash)
90
- JSON.pretty_generate hashes
91
- end
92
-
93
- #
94
- #
95
- def yaml_string(records)
96
- hashes = records.map(&:hash)
97
- hashes.to_yaml
98
- end
99
- end
100
- end
101
- end
@@ -1,75 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- #
5
- class Config
6
- attr_reader :collections
7
-
8
- def initialize(config)
9
- @config = config
10
- @collections = process_collections
11
- end
12
-
13
- #
14
- #
15
- def source
16
- @config.dig 'source'
17
- end
18
-
19
- #
20
- #
21
- def collections_dir
22
- @config.dig 'collections_dir'
23
- end
24
-
25
- #
26
- # Contructs permalink extension from site `permalink` variable
27
- #
28
- # @return [String] the end of the permalink, either '/' or '.html'
29
- def ext
30
- case @config.dig 'permalink'
31
- when 'pretty' || '/'
32
- '/'
33
- else
34
- '.html'
35
- end
36
- end
37
-
38
- #
39
- #
40
- def process_collections
41
- if @config.key? 'collections'
42
- @config['collections'].map do |k, v|
43
- WaxTasks::Collection.new(k, v, source, collections_dir, ext)
44
- end
45
- else
46
- []
47
- end
48
- end
49
-
50
- #
51
- #
52
- def search(name)
53
- search_config = @config.dig 'search', name
54
- raise WaxTasks::Error::InvalidConfig if search_config.nil?
55
- raise WaxTasks::Error::InvalidConfig unless search_config.dig('collections').is_a? Hash
56
-
57
- search_config['collections'] = search_config['collections'].map do |k, v|
58
- fields = v.fetch('fields', [])
59
- fields << 'content' if v.fetch('content', false)
60
- find_collection(k).tap { |c| c.search_fields = fields }
61
- end
62
-
63
- search_config
64
- end
65
-
66
- #
67
- #
68
- def find_collection(name)
69
- collection = @collections.find { |c| c.name == name }
70
- raise WaxTasks::Error::InvalidCollection, "Cannot find requested collection '#{name}'" if collection.nil?
71
-
72
- collection
73
- end
74
- end
75
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- # Custom WaxTasks Errors module
5
- module Error
6
- # Custom WaxTasks Error class with magenta console output
7
- class WaxTasksError < StandardError
8
- def initialize(msg = '')
9
- super(Rainbow(msg).magenta)
10
- end
11
- end
12
-
13
- # Custom Error:
14
- # Rake task expects arguments, found none
15
- class MissingArguments < WaxTasksError; end
16
-
17
- # Custom Error:
18
- # Site config cannot be found / parsed for the task at hand
19
- class InvalidConfig < WaxTasksError; end
20
-
21
- # Custom Error:
22
- # Collection specified cannot be found / parsed in site config
23
- class InvalidCollection < WaxTasksError; end
24
-
25
- # Custom Error:
26
- # Collection data source type is not allowed or is an invalid file
27
- class InvalidSource < WaxTasksError; end
28
-
29
- # Custom Error:
30
- # Data source file could not be found
31
- class MissingSource < WaxTasksError; end
32
-
33
- # Custom Error:
34
- # Could not load collection page(s)
35
- class PageLoad < WaxTasksError; end
36
-
37
- # Custom Error:
38
- # Collection item does not have a required pid value
39
- class MissingPid < WaxTasksError; end
40
-
41
- # Custom Error:
42
- # Collection item has a non-unique pud value
43
- class NonUniquePid < WaxTasksError; end
44
-
45
- # Custom Error:
46
- # Collection page item could not be generated
47
- class PageFailure < WaxTasksError; end
48
-
49
- # Custom Error:
50
- # CSV file failed to lint + could not be loaded
51
- class InvalidCSV < WaxTasksError; end
52
-
53
- # Custom Error:
54
- # JSON file failed to lint + could not be loaded
55
- class InvalidJSON < WaxTasksError; end
56
-
57
- # Custom Error:
58
- # YAML file failed to lint + could not be loaded
59
- class InvalidYAML < WaxTasksError; end
60
-
61
- # Custom Error:
62
- # Search index in site config has no valid collections to index
63
- class NoSearchCollections < WaxTasksError; end
64
- end
65
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- #
5
- class Index
6
- attr_reader :path, :collections
7
-
8
- def initialize(config)
9
- @config = config
10
- @collections = config.dig 'collections'
11
- @path = config.dig 'index'
12
-
13
- raise WaxTasks::Error::NoSearchCollections if @collections.nil?
14
- raise WaxTasks::Error::InvalidConfig if @path.nil?
15
-
16
- @records = records
17
- end
18
-
19
- #
20
- #
21
- def records
22
- lunr_id = 0
23
- @collections.flat_map do |collection|
24
- collection.records_from_pages.each.flat_map do |r|
25
- r.keep_only collection.search_fields
26
- r.set 'lunr_id', lunr_id
27
- r.lunr_normalize_values
28
- lunr_id += 1
29
- r
30
- end
31
- end
32
- end
33
-
34
- #
35
- #
36
- def write_to(dir)
37
- file_path = WaxTasks::Utils.safe_join Dir.pwd, dir, path
38
- FileUtils.mkdir_p File.dirname(file_path)
39
- File.open(file_path, 'w') do |f|
40
- f.puts "---\nlayout: none\n---\n"
41
- f.puts JSON.pretty_generate(@records.map(&:hash))
42
- end
43
- end
44
- end
45
- end
@@ -1,116 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- #
5
- class Item
6
- attr_accessor :record, :iiif_config
7
- attr_reader :pid
8
-
9
- #
10
- #
11
- #
12
- def initialize(path, variants)
13
- @path = path
14
- @variants = variants
15
- @type = type
16
- @pid = File.basename @path, '.*'
17
- @assets = assets
18
- end
19
-
20
- #
21
- #
22
- def accepted_image_formats
23
- %w[.png .jpg .jpeg .tiff]
24
- end
25
-
26
- #
27
- #
28
- def type
29
- Dir.exist?(@path) ? 'dir' : File.extname(@path)
30
- end
31
-
32
- #
33
- #
34
- def valid?
35
- accepted_image_formats.include?(@type) || @type == 'dir'
36
- end
37
-
38
- #
39
- #
40
- def record?
41
- @record.is_a? Record
42
- end
43
-
44
- #
45
- #
46
- def assets
47
- if accepted_image_formats.include? @type
48
- [Asset.new(@path, @pid, @variants)]
49
- elsif @type == 'dir'
50
- paths = Dir.glob("#{@path}/*{#{accepted_image_formats.join(',')}}")
51
- paths.map { |p| Asset.new(p, @pid, @variants) }
52
- else
53
- []
54
- end
55
- end
56
-
57
- #
58
- #
59
- def simple_derivatives
60
- @assets.map(&:simple_derivatives).flatten
61
- end
62
-
63
- #
64
- #
65
- def logo
66
- logo_uri = @iiif_config&.dig 'logo'
67
- "{{ '#{logo_uri}' | absolute_url }}" if logo_uri
68
- end
69
-
70
- def label
71
- label_key = @iiif_config&.dig 'label'
72
- if @record && label_key
73
- @record.hash.dig label_key
74
- else
75
- @pid
76
- end
77
- end
78
-
79
- #
80
- #
81
- def description
82
- description_key = @iiif_config&.dig 'description'
83
- @record.hash.dig description_key if description_key && @record
84
- end
85
-
86
- #
87
- #
88
- def attribution
89
- attribution_key = @iiif_config.dig 'attribution'
90
- @record.hash.dig attribution_key if attribution_key && @record
91
- end
92
-
93
- #
94
- #
95
- def iiif_image_records
96
- opts = base_opts.clone
97
- is_only = @assets.length == 1
98
-
99
- @assets.map.with_index do |asset, i|
100
- asset.to_iiif_image_record(is_only, i, opts)
101
- end
102
- end
103
-
104
- #
105
- #
106
- def base_opts
107
- opts = { label: label }
108
- return opts unless @iiif_config
109
-
110
- opts[:logo] = logo if logo
111
- opts[:description] = description.to_s if description
112
- opts[:attribution] = attribution.to_s if attribution
113
- opts
114
- end
115
- end
116
- end
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- #
5
- class Record
6
- attr_reader :pid, :hash, :order
7
-
8
- def initialize(hash)
9
- @hash = hash
10
- @pid = @hash.dig 'pid'
11
- @order = @hash.dig 'order'
12
- end
13
-
14
- #
15
- #
16
- def lunr_normalize_values
17
- @hash.transform_values { |v| Utils.lunr_normalize v }
18
- end
19
-
20
- #
21
- #
22
- def keys
23
- @hash.keys
24
- end
25
-
26
- #
27
- #
28
- def set(key, value)
29
- @hash[key] = value
30
- end
31
-
32
- #
33
- #
34
- def permalink?
35
- @hash.key? 'permalink'
36
- end
37
-
38
- #
39
- #
40
- def order?
41
- @order.is_a? String
42
- end
43
-
44
- #
45
- #
46
- def keep_only(fields)
47
- @hash.select! { |k, _v| fields.include? k }
48
- end
49
-
50
- #
51
- #
52
- def write_to_page(dir)
53
- raise Error::MissingPid if @pid.nil?
54
-
55
- path = "#{dir}/#{Utils.slug(@pid)}.md"
56
- if File.exist? path
57
- 0
58
- else
59
- FileUtils.mkdir_p File.dirname(path)
60
- File.open(path, 'w') { |f| f.puts "#{@hash.to_yaml}---" }
61
- 1
62
- end
63
- end
64
- end
65
- end
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- module WaxTasks
5
- #
6
- class Site
7
- attr_reader :config
8
- IMAGE_DERIVATIVE_DIRECTORY = 'img/derivatives'
9
-
10
- #
11
- #
12
- def initialize(config = nil)
13
- @config = WaxTasks::Config.new(config || WaxTasks.config_from_file)
14
- end
15
-
16
- #
17
- #
18
- def collections
19
- @config.collections
20
- end
21
-
22
- #
23
- #
24
- def generate_pages(name)
25
- result = 0
26
- collection = @config.find_collection name
27
- raise WaxTasks::Error::InvalidCollection if collection.nil?
28
-
29
- collection.records_from_metadata.each do |record|
30
- result += record.write_to_page(collection.page_source)
31
- end
32
-
33
- puts Rainbow("#{result} pages were generated to #{collection.page_source}.").cyan
34
- puts Rainbow("\nDone ✔").green
35
- end
36
-
37
- #
38
- #
39
- def generate_static_search(name)
40
- search_config = @config.search name
41
- index = WaxTasks::Index.new(search_config)
42
-
43
- puts Rainbow("Generating #{name} search index to #{index.path}").cyan
44
- index.write_to @config.source
45
-
46
- puts Rainbow("\nDone ✔").green
47
- end
48
-
49
- #
50
- #
51
- def generate_derivatives(name, type)
52
- collection = @config.find_collection name
53
- raise WaxTasks::Error::InvalidCollection if collection.nil?
54
- raise WaxTasks::Error::InvalidConfig unless %w[iiif simple].include? type
55
-
56
- output_dir = Utils.safe_join @config.source, IMAGE_DERIVATIVE_DIRECTORY, type
57
- records = case type
58
- when 'iiif'
59
- collection.write_iiif_derivatives output_dir
60
- when 'simple'
61
- collection.write_simple_derivatives output_dir
62
- end
63
-
64
- collection.update_metadata records
65
- puts Rainbow("\nDone ✔").green
66
- end
67
- end
68
- end
@@ -1,151 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module WaxTasks
4
- # Utility helper methods
5
- module Utils
6
- #
7
- #
8
- def self.ingest(source)
9
- metadata = case File.extname source
10
- when '.csv'
11
- WaxTasks::Utils.validate_csv source
12
- when '.json'
13
- WaxTasks::Utils.validate_json source
14
- when /\.ya?ml/
15
- WaxTasks::Utils.validate_yaml source
16
- else
17
- raise Error::InvalidSource, "Can't load #{File.extname source} files. Culprit: #{source}"
18
- end
19
-
20
- WaxTasks::Utils.assert_pids metadata
21
- WaxTasks::Utils.assert_unique metadata
22
- end
23
-
24
- # Checks and asserts presence of `pid` value for each item
25
- #
26
- # @param data [Array] array of hashes each representing a collection item
27
- # @return [Array] same data unless a an item is missing the key `pid`
28
- # @raise WaxTasks::Error::MissingPid
29
- def self.assert_pids(data)
30
- data.each_with_index { |d, i| raise Error::MissingPid, "Collection is missing pid for item #{i}." unless d.key? 'pid' }
31
- data
32
- end
33
-
34
- # Checks and asserts uniqueness of `pid` value for each item
35
- #
36
- # @param data [Array] array of hashes each representing a collection item
37
- # @return [Array] same data unless an item has non-unique value for `pid`
38
- # @raise WaxTasks::Error::NonUniquePid
39
- def self.assert_unique(data)
40
- pids = data.map { |d| d['pid'] }
41
- not_unique = pids.select { |p| pids.count(p) > 1 }.uniq! || []
42
- raise Error::NonUniquePid, "#{@name} has the following nonunique pids:\n#{not_unique}" unless not_unique.empty?
43
-
44
- data
45
- end
46
-
47
- # Checks that a CSV file is valid
48
- #
49
- # @param source [String] path to CSV file
50
- # @return [Array] validated CSV data as an Array of Hashes
51
- # @raise WaxTasks::Error::InvalidCSV
52
- def self.validate_csv(source)
53
- CSV.read(source, headers: true).map(&:to_hash)
54
- rescue StandardError => e
55
- raise Error::InvalidCSV, " #{e}"
56
- end
57
-
58
- # Checks that a JSON file is valid
59
- #
60
- # @param source [String] path to JSON file
61
- # @return [Array] validated JSON data as an Array of Hashes
62
- # @raise WaxTasks::Error::InvalidJSON
63
- def self.validate_json(source)
64
- file = File.read source
65
- JSON.parse file
66
- rescue StandardError => e
67
- raise Error::InvalidJSON, " #{e}"
68
- end
69
-
70
- # Checks that a YAML file is valid
71
- #
72
- # @param source [String] path to YAML file
73
- # @return [Array] validated YAML data as an Array of Hashes
74
- # @raise WaxTasks::Error::InvalidYAML
75
- def self.validate_yaml(source)
76
- SafeYAML.load_file source
77
- rescue StandardError => e
78
- raise WaxTasks::Error::InvalidYAML, " #{e}"
79
- end
80
-
81
- # Removes YAML front matter from a string
82
- # @return [String]
83
- def self.remove_yaml(str)
84
- str.to_s.gsub!(/\A---(.|\n)*?---/, '')
85
- end
86
-
87
- # Scrubs yaml, liquid, html, and etc from content strings
88
- # @return [String]
89
- def self.content_clean(str)
90
- str.gsub!(/\A---(.|\n)*?---/, '') # remove yaml front matter
91
- str.gsub!(/{%(.*)%}/, '') # remove functional liquid
92
- str.gsub!(/{{.*}}/, '') # remove referential liquid
93
- str.gsub!(%r{<\/?[^>]*>}, '') # remove html
94
- str.gsub!('\\n', '') # remove newlines
95
- str.gsub!(/\s+/, ' ') # remove extra space
96
- str.tr!('"', "'") # replace double quotes with single
97
- str
98
- end
99
-
100
- # Normalizes accent marks/diacritics for Lunr indexing
101
- # @return [String]
102
- def self.remove_diacritics(str)
103
- to_replace = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž'
104
- replaced_by = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz'
105
- str.to_s.tr to_replace, replaced_by
106
- end
107
-
108
- # Converts string to snake case and swaps out special chars
109
- # @return [String]
110
- def self.slug(str)
111
- Utils.remove_diacritics(str).to_s.downcase.tr(' ', '_').gsub(/[^\w-]/, '')
112
- end
113
-
114
- #
115
- #
116
- def self.safe_join(*args)
117
- File.join(args.compact)
118
- end
119
-
120
- # Constructs the order variable for each page (if the collection
121
- # needs to preserve the order of items from the file)
122
- #
123
- # @return [Integer] the order if the item padded with '0's for sorting
124
- def self.padded_int(idx, max_idx)
125
- idx.to_s.rjust(Math.log10(max_idx).to_i + 1, '0')
126
- end
127
-
128
- def self.lunr_normalize(val)
129
- case val
130
- when String || Integer
131
- WaxTasks::Utils.remove_diacritics val.to_s
132
- when Array
133
- return val if val.first.is_a? Hash
134
- WaxTasks::Utils.remove_diacritics val.join(', ')
135
- else
136
- val
137
- end
138
- end
139
-
140
- def self.add_yaml_front_matter_to_file(file)
141
- front_matter = "---\nlayout: none\n---\n"
142
- filestring = File.read file
143
- return if filestring.start_with? front_matter
144
-
145
- File.open(file, 'w') do |f|
146
- f.puts front_matter
147
- f.puts filestring
148
- end
149
- end
150
- end
151
- end