Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace The Ruby Racer with Mini Racer #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions lib/middleman-search/search-index-resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,42 @@ def render(opts={}, locs={})

def build_index
# Build js context
context = V8::Context.new
context.load(lunr_resource('lunr.js'))

libs = []
libs << lunr_resource('lunr.js')
if @language != 'en' # English is the default
context.load(lunr_resource("lunr.stemmer.support.js"))
context.load(lunr_resource("lunr.#{@language}.js"))
lunr_lang = context.eval("lunr.#{@language}")
libs << lunr_resource("lunr.stemmer.support.js")
libs << lunr_resource("lunr.#{@language}.js")
end

context.eval('lunr.Index.prototype.indexJson = function () {return JSON.stringify(this.toJSON());}')
source = libs.map { |lib| File.read(lib, mode: "rb:UTF-8") }
source << "lunr.Index.prototype.indexJson = function () {return JSON.stringify(this.toJSON());};"

# Register pipeline functions
pipeline = context.eval('lunr.Pipeline')
@pipeline.each do |name, function|
context[name] = context.eval("(#{function})")
pipeline.registerFunction(context[name], name)
source << "lunr.Pipeline.registerFunction((#{function}), '#{name}');"
end

# Build lunr based on config
lunr = context.eval('lunr')
lunr_conf = proc do |this|
source << "lunr.middlemanSearchIndex = lunr(function () {"

# Use autogenerated id field as reference
this.ref('id')
# Use autogenerated id field as reference
source << "this.ref('id');"

# Add functions to pipeline (just registering them isn't enough)
@pipeline.each do |name, function|
this.pipeline.add(context[name])
end
# Add functions to pipeline (just registering them isn't enough)
@pipeline.each do |name, function|
source << "this.pipeline.add(lunr.Pipeline.registeredFunctions.#{name});"
end

# Define fields with boost
this.use(lunr_lang) if @language != 'en'
@fields.each do |field, opts|
next if opts[:index] == false
this.field(field, {:boost => opts[:boost]})
end
# Use language if set
source << "this.use(lunr.#{@language});" if @language != 'en'

# Define fields with boost
@fields.each do |field, opts|
next if opts[:index] == false
source << "this.field('#{field}', { boost: #{opts[:boost]}});"
end

# Get lunr index
index = lunr.call(lunr_conf)
source << "});"


# Ref to resource map
store = Hash.new
Expand All @@ -95,7 +91,8 @@ def build_index

@callback.call(to_index, to_store, resource) if @callback

index.add(to_index.merge(id: id))
source << "lunr.middlemanSearchIndex.add(#{to_index.merge(id: id).to_json});"

store[id] = to_store
end
rescue => ex
Expand All @@ -104,7 +101,9 @@ def build_index
end

# Generate JSON output
"{\"index\": #{index.indexJson()}, \"docs\": #{store.to_json}}"
context = ExecJS.compile(source.join("\n"))
json = context.eval('lunr.middlemanSearchIndex.indexJson()')
"{\"index\": #{json}, \"docs\": #{store.to_json}}"
end

def binary?
Expand Down
4 changes: 2 additions & 2 deletions middleman-search.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "middleman-core", [">= 3.2"]
spec.add_dependency "therubyracer", ["~> 0.12.2"]
spec.add_dependency "mini_racer", ["~> 0.2.6"]
spec.add_dependency "nokogiri", ["~> 1.6"]

spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "bundler", ">= 1.5"
spec.add_development_dependency "rake"
end