wax_tasks 1.0.0.pre.beta → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +0,0 @@
1
- require 'json'
2
- require 'wax_tasks'
3
-
4
- namespace :wax do
5
- desc 'write a simple package.json for monitoring js dependencies'
6
- task :jspackage do
7
- task_runner = WaxTasks::TaskRunner.new
8
- package = task_runner.js_package
9
- unless package.empty?
10
- src_dir = task_runner.site[:source_dir]
11
- path = WaxTasks::Utils.root_path(src_dir, 'package.json')
12
-
13
- puts "Writing javascript dependencies to #{path}".cyan
14
- File.open(path, 'w') { |f| f.write(JSON.pretty_generate(package)) }
15
- end
16
- end
17
- end
data/lib/tasks/lunr.rake DELETED
@@ -1,9 +0,0 @@
1
- require 'wax_tasks'
2
-
3
- namespace :wax do
4
- desc 'build lunr search index (with default UI if UI=true)'
5
- task :lunr do
6
- task_runner = WaxTasks::TaskRunner.new
7
- task_runner.lunr(generate_ui: !!ENV['UI'])
8
- end
9
- end
@@ -1,11 +0,0 @@
1
- require 'wax_tasks'
2
-
3
- namespace :wax do
4
- desc 'generate collection md pages from yaml or csv data source'
5
- task :pagemaster do
6
- arguments = ARGV.drop(1).each { |a| task a.to_sym }
7
- raise WaxTasks::Error::MissingArguments, 'You must specify a collection after wax:pagemaster' if arguments.empty?
8
- task_runner = WaxTasks::TaskRunner.new
9
- task_runner.pagemaster(arguments)
10
- end
11
- end
data/lib/tasks/push.rake DELETED
@@ -1,12 +0,0 @@
1
- require 'wax_tasks'
2
-
3
- namespace :wax do
4
- desc 'push compiled Jekyll site to git branch BRANCH'
5
- task :push do
6
- arguments = ARGV.drop(1).each { |a| task a.to_sym }
7
- raise WaxTasks::Error::MissingArguments, 'You must specify a branch after wax:push' if arguments.empty?
8
-
9
- task_runner = WaxTasks::TaskRunner.new
10
- task_runner.push_branch(arguments.first)
11
- end
12
- end
data/lib/tasks/test.rake DELETED
@@ -1,18 +0,0 @@
1
- require 'html-proofer'
2
-
3
- namespace :wax do
4
- desc 'run htmlproofer, rspec if .rspec file exists'
5
- task :test do
6
- opts = {
7
- check_external_hash: true,
8
- allow_hash_href: true,
9
- check_html: true,
10
- disable_external: true,
11
- empty_alt_ignore: true,
12
- only_4xx: true,
13
- verbose: true
14
- }
15
- HTMLProofer.check_directory('./_site', opts).run
16
- system('bundle exec rspec') if File.exist?('.rspec')
17
- end
18
- end
@@ -1,70 +0,0 @@
1
- require 'jekyll'
2
- require 'logger'
3
- require 'tmpdir'
4
-
5
- module WaxTasks
6
- # Parent class representing a Git Branch
7
- # that cannot be created directly. Only child classes
8
- # (LocalBranch, TravisBranch) can be initialized.
9
- #
10
- # @attr target [String] the name of the Git branch to deploy to
11
- # @attr origin [String] the current repository remote
12
- # @attr commit_msg [String] the commit message to use on push
13
- # @attr baseurl [String] the site baseurl to build with (if on gh-pages)
14
- # @attr success_msg [String] informative message to be output to console
15
- class Branch
16
- attr_reader :target, :origin, :commit_msg, :baseurl, :success
17
- private_class_method :new
18
-
19
- # This method ensures child classes can be instantiated eventhough
20
- # Branch.new cannot be.
21
- def self.inherited(*)
22
- public_class_method :new
23
- end
24
-
25
- # @param site [Hash] the site config from (TaskRunner.site)
26
- # @param target [String] the name of the Git branch to deploy to
27
- # @param time [String] message with the time of deployment
28
- def initialize(site, target)
29
- @site = site
30
- @target = target
31
- @time = Time.now.strftime('Updated at %H:%M on %Y-%m-%d')
32
- end
33
-
34
- # Rebuild the Jekyll site with branch @baseurl
35
- # @return [Nil]
36
- def rebuild
37
- if @baseurl.empty?
38
- msg = 'Building the gh-pages _site without a baseurl is not recommended'
39
- Logger.new($stdout).warn(msg.orange)
40
- end
41
- FileUtils.rm_r(SITE_DIR) if File.directory?(WaxTasks::SITE_DIR)
42
- opts = {
43
- source: @site[:source_dir] || '.',
44
- destination: WaxTasks::SITE_DIR,
45
- config: WaxTasks::DEFAULT_CONFIG,
46
- baseurl: @baseurl,
47
- verbose: true
48
- }
49
- Jekyll::Site.new(Jekyll.configuration(opts)).process
50
- end
51
-
52
- # Add, commmit, and push compiled Jekyll site to @target branch
53
- # @return [Nil]
54
- def push
55
- if @site[:env] == 'prod'
56
- rebuild if @target == 'gh-pages'
57
- raise Error::MissingSite, "Cannot find #{WaxTasks::SITE_DIR}" unless Dir.exist? WaxTasks::SITE_DIR
58
- Dir.chdir(SITE_DIR)
59
- File.open('.info', 'w') { |f| f.write(@time) }
60
- system 'git init && git add .'
61
- system "git commit -m '#{@commit_msg}'"
62
- system "git remote add origin #{@origin}"
63
- puts @success_msg.cyan
64
- system "git push origin master:refs/heads/#{@target} --force"
65
- else
66
- puts "Skipping build for branch '#{@target}' on env='test'".orange
67
- end
68
- end
69
- end
70
- end
@@ -1,86 +0,0 @@
1
- module WaxTasks
2
- # Module of helper functions for WaxTasks::IiiifCollection class
3
- module Iiif
4
- # Helpers for creating Iiif derivatives for a Collection
5
- # via `wax:derivatives:iiif` Rake task
6
- module Derivatives
7
- # @return [Hash] base WaxIiif::ImageRecord opts from item
8
- def base_opts(item)
9
- opts = { is_primary: false }
10
- opts[:description] = item.dig(description).to_s unless description.nil?
11
- opts[:attribution] = item.dig(attribution).to_s unless attribution.nil?
12
- opts[:logo] = "{{ '#{logo}' | absolute_url }}" unless logo.nil?
13
- opts
14
- end
15
-
16
- # @return [Array] Set of WaxIiif::ImageRecords
17
- def iiif_records(source_data)
18
- records = []
19
- source_data.each do |d|
20
- item = @metadata.detect { |m| m['pid'] == d[:pid] } || {}
21
- opts = base_opts(item)
22
- if d[:images].length == 1
23
- opts[:id] = d[:pid]
24
- opts[:path] = d[:images].first
25
- opts[:label] = item.fetch(label.to_s, d[:pid])
26
- opts[:is_primary] = true
27
-
28
- records << WaxIiif::ImageRecord.new(opts)
29
- else
30
- item_records = []
31
- d[:images].each do |i|
32
- img_id = File.basename(i, '.*').to_s
33
-
34
- opts[:id] = "#{d[:pid]}_#{img_id}"
35
- opts[:manifest_id] = d[:pid]
36
- opts[:path] = i
37
- opts[:label] = item.fetch(label.to_s, d[:pid])
38
- opts[:section_label] = img_id
39
-
40
- item_records << WaxIiif::ImageRecord.new(opts)
41
- end
42
- item_records.first.is_primary = true
43
- records += item_records
44
- end
45
- records.flatten
46
- end
47
- records
48
- end
49
-
50
- # Opens IIIF JSON files and prepends yaml front matter
51
- # So that liquid vars can be read by Jekyll
52
- #
53
- # @return [Nil]
54
- def add_yaml_front_matter(dir)
55
- Dir["#{dir}/**/*.json"].each do |file|
56
- front_matter = "---\nlayout: none\n---\n"
57
- filestring = File.read(file)
58
- next if filestring.start_with?(front_matter)
59
- begin
60
- json = JSON.parse(filestring)
61
- File.open(file, 'w') do |f|
62
- f.puts(front_matter)
63
- f.puts(JSON.pretty_generate(json))
64
- end
65
- rescue StandardError => e
66
- raise Error::InvalidJSON, "IIIF JSON in #{file} is invalid.\n#{e}"
67
- end
68
- end
69
- end
70
-
71
- # @return [Nil]
72
- def add_iiif_derivative_info_to_metadata(manifests)
73
- manifests.map do |m|
74
- json = JSON.parse(m.to_json)
75
- pid = m.base_id
76
- @metadata.find { |i| i['pid'] == pid }.tap do |hash|
77
- hash['manifest'] = Utils.rm_liquid(json['@id'])
78
- hash['thumbnail'] = Utils.rm_liquid(json['thumbnail'])
79
- hash['full'] = hash['thumbnail'].sub('250,/0', '1140,/0')
80
- end
81
- end
82
- overwrite_metadata
83
- end
84
- end
85
- end
86
- end
@@ -1,26 +0,0 @@
1
- module WaxTasks
2
- module Iiif
3
- # Module for handling metadata in IIIF manifests
4
- module Manifest
5
- # @return [String]
6
- def label
7
- @image_config.dig('iiif', 'label')
8
- end
9
-
10
- # @return [String]
11
- def description
12
- @image_config.dig('iiif', 'description')
13
- end
14
-
15
- # @return [String]
16
- def attribution
17
- @image_config.dig('iiif', 'attribution')
18
- end
19
-
20
- # @return [String]
21
- def logo
22
- @image_config.dig('iiif', 'logo')
23
- end
24
- end
25
- end
26
- end
@@ -1,137 +0,0 @@
1
- require 'wax_iiif'
2
- require 'mini_magick'
3
-
4
- require_relative 'iiif/derivatives'
5
- require_relative 'iiif/manifest'
6
-
7
- module WaxTasks
8
- # A Jekyll collection with image configuration + data
9
- class ImageCollection < Collection
10
- attr_reader :output_dir, :metadata, :data
11
-
12
- include WaxTasks::Iiif::Derivatives
13
- include WaxTasks::Iiif::Manifest
14
-
15
- # Creates a new IiifCollection with name @name given site config @site
16
- def initialize(name, site)
17
- super(name, site)
18
-
19
- @image_config = @config.dig('images')
20
-
21
- raise Error::InvalidCollection, "No image configuration found for collection '#{@name}'" if @image_config.nil?
22
-
23
- @data_source = self.image_source_directory
24
- @output_dir = Utils.root_path(@site[:source_dir], DEFAULT_DERIVATIVE_DIR)
25
- @data = self.image_data
26
- @metadata = ingest_file(self.metadata_source_path)
27
- @variants = WaxTasks::DEFAULT_IMAGE_VARIANTS
28
- end
29
-
30
- # Combines and described source image data including:
31
- # single image items, items from subdirectories of images,
32
- # and pdf documents.
33
- #
34
- # @return [Array] array of hashes relating item pids to image asset paths
35
- def image_data
36
- [single_image_items, multi_image_items, pdf_items].flatten.compact
37
- end
38
-
39
- # @return [String]
40
- def image_source_directory
41
- source = @image_config.dig('source')
42
- raise WaxIiif::Error::InvalidImageData, 'No image source directory specified.' if source.nil?
43
- path_to_image_source = Utils.root_path(@site[:source_dir], '_data/', source, '/')
44
- raise Error::MissingIiifSrc, "Cannot find IIIF source directory #{path_to_image_source}" unless Dir.exist?(path_to_image_source)
45
- raise Error::MissingIiifSrc, "No IIIF source data was found in #{path_to_image_source}" if Dir["#{path_to_image_source}/*"].empty?
46
- path_to_image_source
47
- end
48
-
49
- # Gets the items with 1 image asset
50
- #
51
- # @return [Array] array of hashes relating pids to image paths
52
- def single_image_items
53
- Dir["#{@data_source}*.{jpg, jpeg, tiff, png}"].map do |d|
54
- { pid: File.basename(d, '.*'), images: [d] }
55
- end
56
- end
57
-
58
- # Gets the items with multiple image assets
59
- #
60
- # @return [Array] array of hashes relating pids to image paths
61
- def multi_image_items
62
- Dir["#{@data_source}*/"].map do |d|
63
- images = Dir["#{d}*.{jpg, jpeg, tiff, png}"]
64
- { pid: File.basename(d, '.*'), images: images.sort }
65
- end
66
- end
67
-
68
- # Gets the items from pdf documents
69
- #
70
- # @return [Array] array of hashes relating pids to image paths
71
- def pdf_items
72
- Dir["#{@data_source}*.pdf"].map do |d|
73
- pid = File.basename(d, '.pdf')
74
- dir = "#{@data_source}/#{pid}"
75
- next if Dir.exist?(dir)
76
- { pid: pid, images: split_pdf(d) }
77
- end
78
- end
79
-
80
- #
81
- #
82
- # @return [Array] array of image paths generated from pdf split
83
- def split_pdf(pdf)
84
- split_opts = { output_dir: @data_source, verbose: true }
85
- WaxIiif::Utilities::PdfSplitter.split(pdf, split_opts).sort
86
- end
87
-
88
- def build_simple_derivatives
89
- simple_output_dir = "#{@output_dir}/simple"
90
- @data.each do |d|
91
- d[:images].each_with_index do |img, index|
92
- asset_id = img.gsub(@data_source, '').gsub('.jpg', '').tr('/', '_')
93
- asset_path = "#{simple_output_dir}/#{asset_id}"
94
- item_record = @metadata.find { |record| record['pid'] == d[:pid] }
95
-
96
- FileUtils.mkdir_p(asset_path)
97
-
98
- @variants.each do |label, width|
99
- variant_path = "#{asset_path}/#{label}.jpg"
100
- unless item_record.nil? || index.positive?
101
- item_record[label.to_s] = variant_path.gsub(/^./, '')
102
- end
103
- next puts "skipping #{variant_path}" if File.exist?(variant_path)
104
-
105
- image = MiniMagick::Image.open(img)
106
- image.resize(width)
107
- image.format('jpg')
108
- image.write(variant_path)
109
- end
110
- end
111
- end
112
- self.overwrite_metadata
113
- end
114
-
115
- # Creates a WaxIiif::Builder object,
116
- # builds the IIIF derivatives and json, and
117
- # makes them usable for Jekyll/Wax
118
- #
119
- # @return [Nil]
120
- def build_iiif_derivatives
121
- iiif_output_dir = "#{@output_dir}/iiif"
122
- jekyll_prefix = "{{ '' | absolute_url }}/#{DEFAULT_DERIVATIVE_DIR}/iiif"
123
- build_opts = {
124
- base_url: jekyll_prefix,
125
- output_dir: iiif_output_dir,
126
- variants: DEFAULT_IMAGE_VARIANTS,
127
- verbose: true,
128
- collection_label: @name
129
- }
130
- builder = WaxIiif::Builder.new(build_opts)
131
- builder.load(iiif_records(@data))
132
- builder.process_data
133
- add_iiif_derivative_info_to_metadata(builder.manifests)
134
- add_yaml_front_matter(iiif_output_dir)
135
- end
136
- end
137
- end
@@ -1,21 +0,0 @@
1
- require 'time'
2
-
3
- module WaxTasks
4
- # Branch object for `$ wax:push` task when run on local machine
5
- # using local credentials
6
- #
7
- # @attr origin [String] the current repository remote
8
- # @attr commit_msg [String] the commit message to use on push
9
- # @attr baseurl [String] the site baseurl to build with (if on gh-pages)
10
- # @attr success_msg [String] informative message to be output to console
11
- class LocalBranch < Branch
12
- def initialize(site, target)
13
- super(site, target)
14
-
15
- @origin = `git config --get remote.origin.url`.strip
16
- @commit_msg = "Updated via local task at #{Time.now.utc}"
17
- @baseurl = "/#{@origin.split('/').last.gsub('.git', '')}"
18
- @success_msg = "Deploying to #{@target} branch from local task."
19
- end
20
- end
21
- end
@@ -1,82 +0,0 @@
1
- require_relative 'page_set'
2
-
3
- module WaxTasks
4
- module Lunr
5
- # A Lunr::Index document that combines data from all collections
6
- # in site config that have `lunr_index` parameters.
7
- #
8
- # @attr collections [Array] a list of LunrCollection objects
9
- # @attr fields [Array] shared list of fields to index among LunrCollections
10
- class Index
11
- attr_reader :collections, :index_path, :fields
12
-
13
- # Creates a new LunrIndex object
14
- def initialize(site, index_path, collections)
15
- raise Error::NoLunrCollections, 'No collections were configured to index' if collections.nil?
16
- raise Error::NoLunrCollections, 'No path was given for index file' if index_path.nil?
17
-
18
- @collections = collections.keys.map! do |c|
19
- Lunr::PageSet.new(c, collections[c], site)
20
- end
21
-
22
- @index_path = index_path
23
- @fields = self.total_fields
24
- end
25
-
26
- # @return [Array] shared list of fields to index among indexed collections
27
- def total_fields
28
- @collections.map { |c| c.data.map(&:keys) }.flatten.uniq
29
- end
30
-
31
- # @return [String] writes index as pretty JSON with YAML front-matter
32
- def to_s
33
- data = @collections.map(&:data).flatten
34
- data.each_with_index.map { |m, id| m['lunr_index'] = id }
35
- "---\nlayout: none\n---\n#{JSON.pretty_generate(data)}"
36
- end
37
-
38
- # Creates a default LunrUI / JS file for displaying the Index
39
- #
40
- # @return [String]
41
- def default_ui
42
- <<~HEREDOC
43
- ---
44
- layout: none
45
- ---
46
- $.getJSON("{{ '#{@index_path}' | absolute_url }}", function(index_json) {
47
- window.index = new elasticlunr.Index;
48
- window.store = index_json;
49
- index.saveDocument(false);
50
- index.setRef('lunr_index');
51
- #{@fields.map { |f| "index.addField('#{f}');" }.join("\n\t")}
52
- // add docs
53
- for (i in store){
54
- index.addDoc(store[i]);
55
- }
56
- $('input#search').on('keyup', function() {
57
- var results_div = $('#results');
58
- var query = $(this).val();
59
- var results = index.search(query, { boolean: 'AND', expand: true });
60
- results_div.empty();
61
- for (var r in results) {
62
- var ref = results[r].ref;
63
- var item = store[ref];
64
- var pid = item.pid;
65
- var label = item.label;
66
- var meta = `#{@fields.except(%w[pid label]).take(3).map { |f| "${item.#{f}}" }.join(' | ')}`;
67
- if ('thumbnail' in item) {
68
- var thumb = `<img class='sq-thumb-sm' src='{{ "" | absolute_url }}${item.thumbnail}'/>&nbsp;&nbsp;&nbsp;`
69
- }
70
- else {
71
- var thumb = '';
72
- }
73
- var result = `<div class="result"><a href="${item.link}">${thumb}<p><span class="title">${item.label}</span><br>${meta}</p></a></p></div>`;
74
- results_div.append(result);
75
- }
76
- });
77
- });
78
- HEREDOC
79
- end
80
- end
81
- end
82
- end