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,57 +0,0 @@
1
- module WaxTasks
2
- module Lunr
3
- # Class representing a directory of markdown pages
4
- # to be indexed by a Lunr::Index
5
- class PageSet
6
- attr_reader :data
7
-
8
- def initialize(name, config, site)
9
- @name = name
10
- @fields = config.dig('fields')
11
-
12
- raise Error::MissingFields, "Cannot find fields to index collection #{@name}" if @fields.nil?
13
-
14
- @content = !!config.dig('content')
15
- @page_dir = Utils.root_path(site[:source_dir], site[:collections_dir], "_#{@name}")
16
- @data = self.ingest_pages
17
- end
18
-
19
- # Finds the page_dir of markdown pages for the collection and ingests
20
- # them as an array of hashes
21
- #
22
- # @return [Array] array of the loaded markdown pages loaded as hashes
23
- def ingest_pages
24
- data = []
25
- pages = Dir.glob("#{@page_dir}/*.md")
26
- puts "There are no pages in #{@page_dir} to index.".orange if pages.empty?
27
- pages.each do |p|
28
- begin
29
- data << load_page(p)
30
- rescue StandardError => e
31
- raise Error::LunrPageLoad, "Cannot load page #{p}\n#{e}"
32
- end
33
- end
34
- data
35
- end
36
-
37
- # Reads in a markdown file and converts it to a hash
38
- # with the values from @fields.
39
- # Adds the content of the file (below the YAML) if @content == true
40
- #
41
- # @param page [String] the path to a markdown page to load
42
- def load_page(page)
43
- yaml = YAML.load_file(page)
44
- hash = {
45
- 'link' => "{{'#{yaml.fetch('permalink')}' | absolute_url }}",
46
- 'collection' => @name
47
- }
48
- content = WaxTasks::Utils.html_strip(File.read(page))
49
- hash['content'] = WaxTasks::Utils.remove_diacritics(content) if @content
50
- fields = @fields.push('pid').push('label').uniq
51
- fields.push('thumbnail') if yaml.key?('thumbnail')
52
- fields.each { |f| hash[f] = yaml.dig(f).lunr_normalize }
53
- hash
54
- end
55
- end
56
- end
57
- end
@@ -1,60 +0,0 @@
1
- module WaxTasks
2
- # A Jekyll collection with a data source file that
3
- # can generate markdown pages from that data.
4
- #
5
- # @attr config [Hash] the collection config
6
- # @attr layout [String] Jekyll layout to be used by the generated pages
7
- # @attr ordered [Boolean] whether/not the order of items should be preserved
8
- # @attr metadata [Array] array of hashes from ingested metadata file
9
- class PagemasterCollection < Collection
10
- attr_reader :layout, :ordered, :metadata
11
-
12
- # Creates a new PagemasterCollection with name @name given site config @site
13
- def initialize(name, site)
14
- super(name, site)
15
-
16
- @config = self.config
17
- @layout = assert_layout
18
- @ordered = @config.fetch('keep_order', false)
19
- @metadata = ingest_file(self.metadata_source_path)
20
- end
21
-
22
- # Confirms + requires `layout` value in the collection @config
23
- #
24
- # @return [String] the Jekyll layout to be used by the generated pages
25
- def assert_layout
26
- raise WaxTasks::Error::MissingLayout, "Missing collection layout in _config.yml for #{@name}" unless @config.key? 'layout'
27
- @config['layout']
28
- end
29
-
30
- # Writes markdown pages from the ingested data to page_dir
31
- # with layout, permalink, and order info added (if applicable)
32
- #
33
- # @return [Array] a copy of the pages as hashes, for testing
34
- def generate_pages
35
- page_dir = self.page_dir
36
- FileUtils.mkdir_p(page_dir)
37
- pages = []
38
- @metadata.each_with_index do |item, idx|
39
- page_slug = Utils.slug(item.fetch('pid'))
40
- path = "#{page_dir}/#{page_slug}.md"
41
- item['permalink'] = "/#{@name}/#{page_slug}#{@site[:permalink]}"
42
- item['layout'] = @layout
43
- item['order'] = padded_int(idx, @metadata.length) if @ordered
44
- pages << item
45
- next "#{page_slug}.md already exits. Skipping." if File.exist?(path)
46
- File.open(path, 'w') { |f| f.write("#{item.to_yaml}---") }
47
- end
48
- puts "#{@metadata.length} pages were generated to #{page_dir} directory.".cyan
49
- pages
50
- end
51
-
52
- # Constructs the order variable for each page (if the collection
53
- # needs to preserve the order of items from the file)
54
- #
55
- # @return [Integer] the order if the item padded with '0's for sorting
56
- def padded_int(idx, max_idx)
57
- idx.to_s.rjust(Math.log10(max_idx).to_i + 1, '0')
58
- end
59
- end
60
- end
@@ -1,148 +0,0 @@
1
- module WaxTasks
2
- # Class for running the Rake tasks in ./tasks
3
- # TaskRunner is responsible for loading and parsing the site config
4
- # from `_config.yml`, which can be overridden with .override(opts)
5
- #
6
- # @attr site [Hash] main variables from site config normalized + symbolized
7
- class TaskRunner
8
- attr_reader :site
9
-
10
- # Creates a new TaskRunner with a config hash or default config file
11
- #
12
- # @param env [String] test/prod. only affects Branch module
13
- # @param config [Hash] optional hash, should mirror a parsed _config.yml
14
- # @example give a custom config
15
- # config = {
16
- # title: 'custom title',
17
- # url: 'custom.url',
18
- # collections: {...}
19
- # }
20
- # WaxTasks::TaskRunner.new(config)
21
- # @example use default config from file
22
- # WaxTasks::TaskRunner.new
23
- def initialize(config = {}, env = 'prod')
24
- @config = YAML.load_file(DEFAULT_CONFIG).symbolize_keys if config.empty?
25
- @site = {
26
- env: env,
27
- title: @config.dig(:title),
28
- url: @config.dig(:url),
29
- baseurl: @config.dig(:baseurl),
30
- repo_name: @config.dig(:repo_name),
31
- source_dir: @config.dig(:source),
32
- collections_dir: @config.dig(:collections_dir),
33
- collections: @config.dig(:collections),
34
- lunr_index: @config.dig(:lunr_index),
35
- js: @config.dig(:js),
36
- permalink: Utils.construct_permalink(@config)
37
- }
38
- rescue StandardError => e
39
- raise Error::InvalidSiteConfig, "Could not load _config.yml. => #{e}"
40
- end
41
-
42
- # Overrides a specific part of @site
43
- #
44
- # @param opts [Hash] part of the site config to be overwritten
45
- # @example override title + url
46
- # runner = WaxTasks::TaskRunner.new
47
- # runner.override({ title: 'my new title', url: 'my-new.url' })
48
- def override(opts)
49
- opts.each { |k, v| @site[k] = v }
50
- @site[:permalink] = Utils.construct_permalink(opts)
51
- self
52
- end
53
-
54
- # Given an array of command line arguments `args`,
55
- # creates a PagemasterCollection for each and generates markdown
56
- # pages from its specified data `source` file
57
- #
58
- # @param args [Array] the arguments/collection names from wax:pagemaster
59
- # @return [Nil]
60
- def pagemaster(args)
61
- args.each do |name|
62
- PagemasterCollection.new(name, @site).generate_pages
63
- end
64
- end
65
-
66
- # Creates a LunrCollection for each collection
67
- # that has lunr_index parameters in the site config
68
- # and generates a lunr-index.json file from the collection data
69
- #
70
- # @param generate_ui [Boolean] whether/not to generate a default lunr UI
71
- # @return [Nil]
72
- def lunr(generate_ui: false)
73
- @site[:lunr_index].each do |i|
74
- file = i.dig('file')
75
- ui = i.dig('ui')
76
- collections = i.dig('collections')
77
- index = Lunr::Index.new(@site, file, collections)
78
-
79
- index_path = Utils.root_path(@site[:source_dir], file)
80
- FileUtils.mkdir_p(File.dirname(index_path))
81
- File.open(index_path, 'w') { |f| f.write(index.to_s) }
82
- puts "Writing lunr search index to #{index_path}.".cyan
83
-
84
- next unless generate_ui
85
- raise Error::WaxTasksError, 'Cannot generate default UI because no path was given' if ui.nil?
86
- ui_path = Utils.root_path(@site[:source_dir], ui)
87
- puts "Writing default lunr UI to #{ui_path}.".cyan
88
- File.open(ui_path, 'w') { |f| f.write(index.default_ui) }
89
- end
90
- end
91
-
92
- # Given an array of command line arguments `args`,
93
- # creates a IiifCollection for each and generates iiif
94
- # derivative images, manifests, etc. from source image files
95
- #
96
- # @param args [Array] the arguments/collection names from wax:pagemaster
97
- # @return [Nil]
98
- def derivatives_iiif(args)
99
- args.each do |name|
100
- iiif_collection = ImageCollection.new(name, @site)
101
- iiif_collection.build_iiif_derivatives
102
- end
103
- end
104
-
105
- # @return [Nil]
106
- def derivatives_simple(args)
107
- args.each do |name|
108
- image_collection = ImageCollection.new(name, @site)
109
- image_collection.build_simple_derivatives
110
- end
111
- end
112
-
113
- # Finds the JS dependencies listed in site config and
114
- # writes them to a package.json file
115
- # in orderto easily track / monitor / update them
116
- #
117
- # @return [Nil]
118
- def js_package
119
- names = []
120
- package = {
121
- 'name' => site[:title],
122
- 'version' => @config.fetch(:version, ''),
123
- 'dependencies' => {}
124
- }
125
- site[:js].each do |dependency|
126
- name = dependency[0]
127
- names << name
128
- version = dependency[1]['version']
129
- package['dependencies'][name] = '^' + version
130
- end
131
- package
132
- end
133
-
134
- # Constructs a TravisBranch or LocalBranch object
135
- # with appropriate Git credentials and pushes
136
- # the compiled Jekyll site to the target GitHub branch
137
- #
138
- # @param target [String] the name of the Git branch to deploy to
139
- # @return [Nil]
140
- def push_branch(target)
141
- if ENV.fetch('CI', false)
142
- TravisBranch.new(self.site, target).push
143
- else
144
- LocalBranch.new(self.site, target).push
145
- end
146
- end
147
- end
148
- end
@@ -1,28 +0,0 @@
1
- require 'time'
2
-
3
- module WaxTasks
4
- # Branch object for `$ wax:push` task when run on Travis-CI VM
5
- # using encrypted Travis environment vars
6
- #
7
- # @attr repo_slug [String] the 'user/repo_name'
8
- # @attr user [String] the GitHub user making the commit/push
9
- # @attr token [String] secret git access token
10
- # @attr commit_msg [String] the commit message to use on push
11
- # @attr origin [String] the current repository remote
12
- # @attr baseurl [String] the site baseurl to build with (if on gh-pages)
13
- # @attr success_msg [String] informative message to be output to console
14
- class TravisBranch < Branch
15
- def initialize(site, target)
16
- super(site, target)
17
-
18
- @repo_slug = ENV['TRAVIS_REPO_SLUG']
19
- @user = @repo_slug.split('/').first
20
- @token = ENV['ACCESS_TOKEN']
21
-
22
- @commit_msg = "Updated via #{ENV['TRAVIS_COMMIT']} @#{Time.now.utc}"
23
- @origin = "https://#{@user}:#{@token}@github.com/#{@repo_slug}.git"
24
- @baseurl = "/#{@repo_slug.split('/').last}"
25
- @success_msg = "Deploying to #{@target} branch from Travis as #{@user}."
26
- end
27
- end
28
- end