wax_tasks 0.0.40 → 0.0.41

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e575498bcfd76bd5bd11705d9d6cac510a422eb
4
- data.tar.gz: 725bae639d3bbb4ed57bdb0d79e9fb8147c85324
3
+ metadata.gz: afb4a9e989cd10f473e2fead7740ce6e8f450d4a
4
+ data.tar.gz: 13d1d213f15697986aefc80396cf82a29b1aedc9
5
5
  SHA512:
6
- metadata.gz: 4c120c938aaf9778d1061749c2f9f38e0899ba4b56dd20ae97c08a34b3a15a77766f481b91efa43b6612295210368260613f9b79be2d863a7d90f35bc9accb16
7
- data.tar.gz: 3f71b6b7e531c01f2d8d22aa2ef03d0c053f96a8e5dd9938e352226c0325e75a8c5e2b7fbf58cdf7d4ec3c76d588cd0ac5ad122d90de4e1d766f938d2adbcd35
6
+ metadata.gz: 2068c29aec041abea14be1384ae22104972da1d9f74d052e6ba2e206f86d786af2e4bcc2f03e2192f2cb170a9f4e6229d2fa7ee7f7f77920ba1c255c9120180b
7
+ data.tar.gz: 7a1245c635cb173acd1a69f1673bdf66af77ead311ca17a49922fa3870c79fce00f77c3335f7c222e201385dfaec8517d988c54787cf6750963547ef8340938a
@@ -0,0 +1,23 @@
1
+ require 'wax_tasks'
2
+ require 'json'
3
+
4
+ namespace :wax do
5
+ desc 'write a simple package.json'
6
+ task :jspackage => :config do
7
+ package = {
8
+ 'name' => $config['title'],
9
+ 'version' => '1.0.0',
10
+ 'description' => $config['description'],
11
+ 'dependencies' => {}
12
+ }
13
+ names = []
14
+ $config['js'].each do |dependency|
15
+ name = dependency[0]
16
+ names << name
17
+ version = dependency[1]['version']
18
+ package['dependencies'][name] = '^' + version
19
+ end
20
+ File.open('package.json', 'w') { |file| file.write(package.to_json) }
21
+ puts "Writing #{names} to simple package.json.".cyan
22
+ end
23
+ end
data/lib/tasks/lunr.rake CHANGED
@@ -3,12 +3,24 @@ require 'wax_tasks'
3
3
  namespace :wax do
4
4
  desc 'build lunr search index'
5
5
  task :lunr => :config do
6
- collections = $config['collections']
7
- lunr_language = $config['lunr_language']
8
- index = Index.new(collections, lunr_language)
9
-
10
6
  Dir.mkdir('js') unless File.exist?('js')
11
- File.open('js/lunr-index.js', 'w') { |file| file.write(index.output) }
12
- puts "Writing lunr index to js/lunr-index.js".cyan
7
+ yaml = "---\nlayout: none\n---\n"
8
+ lunr = Lunr.new($config)
9
+
10
+ # write index to json file
11
+ idx = yaml + lunr.index
12
+ idx_path = 'js/lunr-index.json'
13
+ File.open(idx_path, 'w') { |file| file.write(idx) }
14
+ puts "Writing lunr index to #{idx_path}".cyan
15
+
16
+ # write index.ui to js file
17
+ ui = yaml + lunr.ui
18
+ ui_path = 'js/lunr-ui.js'
19
+ if File.exist?(ui_path)
20
+ puts "Lunr UI already exists at #{ui_path}. Skipping".cyan
21
+ else
22
+ File.open(ui_path, 'w') { |file| file.write(ui) }
23
+ puts "Writing lunr index to #{ui_path}".cyan
24
+ end
13
25
  end
14
26
  end
@@ -23,5 +23,10 @@ def rm_diacritics(str)
23
23
  end
24
24
 
25
25
  def slug(str)
26
- str.downcase.tr(' ', '_').gsub(/[^\w-]/, '')
26
+ str.downcase.tr(' ', '_').gsub(/[^:\w-]/, '')
27
+ end
28
+
29
+ def thing2string(thing)
30
+ thing = thing.join(" ") if thing.is_a?(Array)
31
+ thing.to_s
27
32
  end
@@ -0,0 +1,69 @@
1
+ include FileUtils
2
+
3
+ # for generating Lunr Index and Lunr UI files
4
+ class Lunr
5
+ def initialize(config)
6
+ @lunr_collections = collections_to_index(config['collections'])
7
+ @total_fields = total_fields(@lunr_collections)
8
+ @lunr_language = config['lunr_language']
9
+ @collections_dir = config['collections_dir'].nil? ? '' : config.fetch('collections_dir') + '/'
10
+ end
11
+
12
+ def collections_to_index(collections)
13
+ collections.find_all { |c| c[1].key?('lunr_index') && c[1]['lunr_index'].key?('fields') }
14
+ end
15
+
16
+ def total_fields(collections)
17
+ total_fields = ['pid']
18
+ collections.each do |c|
19
+ total_fields = total_fields.concat(c[1]['lunr_index']['fields'])
20
+ total_fields << 'content' if c[1]['lunr_index']['content']
21
+ end
22
+ total_fields.uniq
23
+ end
24
+
25
+ def index
26
+ full_index = []
27
+ count = 0
28
+ # catch
29
+ abort("There are no valid collections to index.".magenta) if @lunr_collections.nil?
30
+ # index each lunr_collection
31
+ @lunr_collections.each do |c|
32
+ c_dir = @collections_dir + '_' + c[0]
33
+ c_fields = c[1]['lunr_index']['fields'].uniq
34
+ c_pages = Dir.glob(c_dir + '/*.md')
35
+ # catch
36
+ abort "There are no markdown pages in directory '#{c_dir}'".magenta if c_pages.empty?
37
+ abort "There are no fields specified for #{c[0]}.".magenta if c_fields.empty?
38
+ puts "Loading #{c_pages.length} pages from #{c_dir}"
39
+ # index each page in collection
40
+ c_pages.each do |page|
41
+ yaml = YAML.load_file(page)
42
+ hash = {
43
+ 'lunr_id' => count,
44
+ 'link' => "{{'" + yaml.fetch('permalink') + "' | relative_url }}"
45
+ }
46
+ c_fields.each { |f| hash[f] = rm_diacritics(thing2string(yaml[f])) }
47
+ hash['content'] = rm_diacritics(clean(File.read(page))) if c[1]['lunr_index']['content']
48
+ count += 1
49
+ full_index << hash
50
+ end
51
+ end
52
+ JSON.pretty_generate(full_index)
53
+ end
54
+
55
+ def ui
56
+ # set up index
57
+ ui_string = "$.getJSON(\"{{ site.baseurl }}/js/lunr-index.json\", function(index_json) {\nwindow.index = new elasticlunr.Index;\nwindow.store = index_json;\nindex.saveDocument(false);\nindex.setRef('lunr_id');"
58
+ # add fields to index
59
+ @total_fields.each { |field| ui_string += "\nindex.addField('#{field}');" }
60
+ # add docs
61
+ ui_string += "\n// add docs\nfor (i in store){index.addDoc(store[i]);}"
62
+ # gui
63
+ ui_string += "\n$('input#search').on('keyup', function() {\nvar results_div = $('#results');\nvar query = $(this).val();\nvar results = index.search(query, { boolean: 'AND', expand: true });\nresults_div.empty();\nif (results.length > 10) {\nresults_div.prepend(\"<p><small>Displaying 10 of \" + results.length + \" results.</small></p>\");\n}\nfor (var r in results.slice(0, 9)) {\nvar ref = results[r].ref;\nvar item = store[ref];"
64
+ # add fields as display vars
65
+ @total_fields.each { |field| ui_string += "var #{field} = item.#{field};\n" }
66
+ ui_string += "var result = '<div class=\"result\"><b><a href=\"' + item.link + '\">' + title + '</a></b></p></div>';\nresults_div.append(result);\n}\n});\n});"
67
+ ui_string
68
+ end
69
+ end
data/lib/wax_tasks.rb CHANGED
@@ -4,4 +4,4 @@ Bundler.require(:default)
4
4
 
5
5
  require 'wax_tasks/helpers'
6
6
  require 'wax_tasks/collection'
7
- require 'wax_tasks/index'
7
+ require 'wax_tasks/lunr'
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: 0.0.40
4
+ version: 0.0.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marii Nyrop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-09 00:00:00.000000000 Z
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -147,6 +147,7 @@ files:
147
147
  - lib/tasks/config.rake
148
148
  - lib/tasks/ghbranch.rake
149
149
  - lib/tasks/iiif.rake
150
+ - lib/tasks/jspackage.rake
150
151
  - lib/tasks/lunr.rake
151
152
  - lib/tasks/pagemaster.rake
152
153
  - lib/tasks/s3branch.rake
@@ -154,7 +155,7 @@ files:
154
155
  - lib/wax_tasks.rb
155
156
  - lib/wax_tasks/collection.rb
156
157
  - lib/wax_tasks/helpers.rb
157
- - lib/wax_tasks/index.rb
158
+ - lib/wax_tasks/lunr.rb
158
159
  - spec/spec_helper.rb
159
160
  homepage: https://github.com/mnyrop/wax_tasks
160
161
  licenses:
@@ -176,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
177
  version: '0'
177
178
  requirements: []
178
179
  rubyforge_project:
179
- rubygems_version: 2.6.9
180
+ rubygems_version: 2.6.10
180
181
  signing_key:
181
182
  specification_version: 4
182
183
  summary: Rake tasks for minimal exhibitions.
@@ -1,67 +0,0 @@
1
- include FileUtils
2
-
3
- # initializes a lunr index to be output to js/lunr-index.js
4
- class Index
5
- attr_reader :output
6
-
7
- def initialize(collections, lunr_language)
8
- @collections = collections
9
- @lunr_language = lunr_language
10
- @cdir = $config['collections_dir'].nil? ? '' : $config.fetch('collections_dir') + '/'
11
- @lunr_collections = []
12
- @total_fields = []
13
- @output = ''
14
-
15
- pre_process
16
- add_docs
17
- end
18
-
19
- def pre_process
20
- @output += "---\nlayout: none\n---\nvar index = new elasticlunr.Index;\nindex.setRef('lunr_id');\nindex.saveDocument(false);"
21
- @output += "\nindex.pipeline.remove(elasticlunr.trimmer);" if @lunr_language
22
- @collections.each do |c|
23
- if c[1].key?('lunr_index') && c[1]['lunr_index'].key?('fields')
24
- @total_fields.concat c[1]['lunr_index']['fields']
25
- @total_fields << 'content' if c[1]['lunr_index']['content']
26
- @lunr_collections << c
27
- end
28
- end
29
- @total_fields.uniq!
30
- abort("Fields are not properly configured.".magenta) if @total_fields.empty?
31
- @total_fields.each { |f| @output += "\nindex.addField(" + "'" + f + "'" + "); " }
32
- end
33
-
34
- def add_docs
35
- count = 0
36
- store_string = "\nvar store = ["
37
-
38
- abort("There are no valid collections to index.".magenta) if @collections.nil?
39
- @lunr_collections.each do |c|
40
- dir = @cdir + '_' + c[0]
41
- fields = c[1]['lunr_index']['fields'].uniq
42
- pages = Dir.glob(dir + '/*.md')
43
-
44
- abort "There are no markdown pages in directory '#{dir}'".magenta if pages.empty?
45
- abort "There are no fields specified for #{c[0]}.".magenta if fields.empty?
46
-
47
- puts "Loading #{pages.length} pages from #{dir}"
48
- pages.each do |page|
49
- begin
50
- yaml = YAML.load_file(page)
51
- hash = {
52
- 'lunr_id' => count,
53
- 'link' => "{{'" + yaml.fetch('permalink') + "' | relative_url }}"
54
- }
55
- fields.each { |f| hash[f] = rm_diacritics(yaml[f].to_s) }
56
- hash['content'] = rm_diacritics(clean(File.read(page))) if c[1]['lunr_index']['content']
57
- @output += "\nindex.addDoc(" + hash.to_json + "); "
58
- store_string += "\n" + hash.to_json + ", "
59
- count += 1
60
- rescue StandardError
61
- abort "Cannot load data from markdown pages in #{dir}.".magenta
62
- end
63
- end
64
- end
65
- @output += store_string.chomp(', ') + '];'
66
- end
67
- end